/// <summary> /// Returns an interpreted shape. /// </summary> /// <param name="obj"></param> /// <returns></returns> public ShapeF<GeoCoordinate, GeoCoordinateBox, GeoCoordinateLine> Interpret(OsmGeo obj) { switch (obj.Type) { case OsmType.Node: return new ShapeDotF<GeoCoordinate, GeoCoordinateBox, GeoCoordinateLine>( PrimitiveGeoFactory.Instance, (obj as Node).Coordinate); case OsmType.Way: if (obj.Tags.ContainsKey("area")) { return new ShapePolyGonF<GeoCoordinate, GeoCoordinateBox, GeoCoordinateLine>( PrimitiveGeoFactory.Instance, (obj as Way).GetCoordinates().ToArray<GeoCoordinate>()); } else { return new ShapePolyLineF<GeoCoordinate, GeoCoordinateBox, GeoCoordinateLine>( PrimitiveGeoFactory.Instance, (obj as Way).GetCoordinates().ToArray<GeoCoordinate>()); } case OsmType.Relation: List<ShapeF<GeoCoordinate, GeoCoordinateBox, GeoCoordinateLine>> shapes = new List<ShapeF<GeoCoordinate, GeoCoordinateBox, GeoCoordinateLine>>(); foreach (RelationMember member in (obj as Relation).Members) { shapes.Add(this.Interpret(member.Member)); } return new ShapeCombinedF<GeoCoordinate, GeoCoordinateBox, GeoCoordinateLine>( PrimitiveGeoFactory.Instance, shapes); } throw new InvalidOperationException(string.Format("Cannot interpret object {0}!", obj.ToString())); }
/// <summary> /// Returns true if the object passes through this filter. /// </summary> /// <param name="obj"></param> /// <returns></returns> public override bool Evaluate(OsmGeo obj) { string value; if (obj.Tags != null && obj.Tags.TryGetValue(_key, out value)) { return value == _value; } return false; }
/// <summary> /// Returns true if the object passed through the filter. /// </summary> /// <param name="obj"></param> /// <returns></returns> public override bool Evaluate(OsmGeo obj) { var result = false; if(obj.Tags != null) { result = obj.Tags.ContainsKey(_tag); } return result; }
/// <summary> /// Translates the given OSM objects into corresponding geometries. /// </summary> /// <param name="scene"></param> /// <param name="projection"></param> /// <param name="source"></param> /// <param name="osmGeo"></param> public virtual void Translate(Scene2D scene, IProjection projection, IDataSourceReadOnly source, OsmGeo osmGeo) { switch (osmGeo.Type) { case OsmGeoType.Node: this.Translate(scene, projection, CompleteNode.CreateFrom(osmGeo as Node)); break; case OsmGeoType.Way: this.Translate(scene, projection, CompleteWay.CreateFrom(osmGeo as Way, source)); break; case OsmGeoType.Relation: this.Translate(scene, projection, CompleteRelation.CreateFrom(osmGeo as Relation, source)); break; } }
/// <summary> /// Translates the given OSM objects into corresponding geometries. /// </summary> /// <param name="scene"></param> /// <param name="projection"></param> /// <param name="source"></param> /// <param name="osmGeo"></param> public virtual void Translate(Scene2D scene, IProjection projection, IDataSourceReadOnly source, OsmGeo osmGeo) { switch (osmGeo.Type) { case OsmGeoType.Node: var node = osmGeo as Node; if(node.Tags == null) { // make sure that a node has a tag collection by default. node.Tags= new TagsCollection(); } this.Translate(scene, projection, node); break; case OsmGeoType.Way: this.Translate(scene, projection, CompleteWay.CreateFrom(osmGeo as Way, source)); break; case OsmGeoType.Relation: this.Translate(scene, projection, CompleteRelation.CreateFrom(osmGeo as Relation, source)); break; } }
/// <summary> /// Initializes this source. /// </summary> public override void Initialize() { _connection = new SQLiteConnection(_connectionString); _connection.Open(); _current = null; _currentType = OsmGeoType.Node; _nodeReader = null; }
private void ProcessTagsIdAndName(SQLiteCommand command, OsmGeo geo, Dictionary<TagsCollectionBase, int> _uniques, int params_count, int batch_index = 0) { // name & tags id if (geo.Tags != null) { // filter the geo tags var filtered_tags = _tag_filter.Evaluate(geo); // if we actually returned some tags if (filtered_tags.Count > 0) { // we should have those filtered tags in the unique dict if (_uniques.ContainsKey(filtered_tags)) { command.Parameters[(batch_index * params_count) + params_count - 2].Value = _uniques[filtered_tags]; // we only add geo names if they are of interest via tags if (geo.Tags.ContainsKey("name")) { var name = geo.Tags["name"]; if (!string.IsNullOrEmpty(name)) { command.Parameters[(batch_index * params_count) + params_count - 1].Value = name.Truncate(500); } else { command.Parameters[(batch_index * params_count) + params_count - 1].Value = DBNull.Value; } } else { command.Parameters[(batch_index * params_count) + params_count - 1].Value = DBNull.Value; } } // if not, somethings wrong! else { throw new InvalidOperationException( geo + " passes filtered tags, but filtered tags are not in unique table."); } } // no tags of interest, name and tag id are null else { command.Parameters[(batch_index * params_count) + params_count - 2].Value = DBNull.Value; command.Parameters[(batch_index * params_count) + params_count - 1].Value = DBNull.Value; } } // no tags at all, name and tag id are null else { command.Parameters[(batch_index * params_count) + params_count - 2].Value = DBNull.Value; command.Parameters[(batch_index * params_count) + params_count - 1].Value = DBNull.Value; } }
/// <summary> /// Does the filtering. /// </summary> /// <param name="osmGeos"></param> /// <param name="toExclude"></param> /// <param name="excludeNodes"></param> /// <param name="excludeWays"></param> /// <param name="excludeRelations"></param> private List<OsmGeo> Filter(OsmGeo[] osmGeos, OsmGeo[] toExclude, bool excludeNodes, bool excludeWays, bool excludeRelations) { var excludeFilter = new OsmStreamFilterExclude(excludeNodes, excludeWays, excludeRelations); excludeFilter.RegisterSource(osmGeos); excludeFilter.RegisterSource(toExclude); return new List<OsmGeo>(excludeFilter); }
private bool DoMoveNextRelation() { if (_relation_reader == null) { var relationCommand = new SQLiteCommand("select * from relation order by id", _connection); _relation_reader = relationCommand.ExecuteReader(); if (!_relation_reader.Read()) { _relation_reader.Close(); } var relationTagCommand = new SQLiteCommand("select * from relation_tags order by relation_id", _connection); _relation_tag_reader = relationTagCommand.ExecuteReader(); if (!_relation_tag_reader.IsClosed && !_relation_tag_reader.Read()) { _relation_tag_reader.Close(); } var relationNodeCommand = new SQLiteCommand("select * from relation_members order by relation_id,sequence_id", _connection); _relation_member_reader = relationNodeCommand.ExecuteReader(); if (!_relation_member_reader.IsClosed && !_relation_member_reader.Read()) { _relation_member_reader.Close(); } } // read next relation. if (!_relation_reader.IsClosed) { // load/parse data. long id = _relation_reader.GetInt64(0); long changesetId = _relation_reader.GetInt64(1); bool visible = _relation_reader.GetInt64(2) == 1; DateTime timestamp = _relation_reader.IsDBNull(3) ? DateTime.MinValue : _relation_reader.GetDateTime(3); long version = _relation_reader.GetInt64(4); string user = _relation_reader.GetString(5); long uid = _relation_reader.GetInt64(6); var relation = new Relation { Id = id, ChangeSetId = changesetId, TimeStamp = timestamp, UserId = null, UserName = null, Version = (ulong)version, Visible = visible }; relation.UserName = user; relation.UserId = uid; if (!_relation_tag_reader.IsClosed) { long returnedId = _relation_tag_reader.GetInt64(0); while (returnedId == relation.Id.Value) { if (relation.Tags == null) { relation.Tags = new TagsCollection(); } string key = _relation_tag_reader.GetString(1); string value = _relation_tag_reader.GetString(2); relation.Tags.Add(key, value); if (!_relation_tag_reader.Read()) { _relation_tag_reader.Close(); returnedId = -1; } else { returnedId = _relation_tag_reader.GetInt64(0); } } } if (!_relation_member_reader.IsClosed) { long returnedId = _relation_member_reader.GetInt64(0); while (returnedId == relation.Id.Value) { if (relation.Members == null) { relation.Members = new List<RelationMember>(); } string memberType = _relation_member_reader.GetString(1); long memberId = _relation_member_reader.GetInt64(2); object memberRole = _relation_member_reader.GetValue(3); var member = new RelationMember(); member.MemberId = memberId; if (memberRole != DBNull.Value) { member.MemberRole = memberRole as string; } switch (memberType) { case "Node": member.MemberType = OsmGeoType.Node; break; case "Way": member.MemberType = OsmGeoType.Way; break; case "Relation": member.MemberType = OsmGeoType.Relation; break; } relation.Members.Add(member); if (!_relation_member_reader.Read()) { _relation_member_reader.Close(); returnedId = -1; } else { returnedId = _relation_member_reader.GetInt64(0); } } } // set the current variable! _current = relation; // advance the reader(s). if (!_relation_reader.Read()) { _relation_reader.Close(); } if (!_relation_tag_reader.IsClosed && !_relation_tag_reader.Read()) { _relation_tag_reader.Close(); } if (!_relation_member_reader.IsClosed && !_relation_member_reader.Read()) { _relation_member_reader.Close(); } return true; } else { _relation_reader.Close(); _relation_reader.Dispose(); _relation_reader = null; _relation_tag_reader.Close(); _relation_tag_reader.Dispose(); _relation_tag_reader = null; _current_type = OsmGeoType.Relation; return false; } }
/// <summary> /// Resets this source. /// </summary> public override void Reset() { _current = null; _current_type = OsmGeoType.Node; if (_node_reader != null) { _node_reader.Close(); _node_reader.Dispose(); _node_reader = null; } }
/// <summary> /// Creates a new change object. /// </summary> /// <param name="type"></param> /// <param name="obj"></param> public Change(ChangeType type, OsmGeo obj) { _type = type; _obj = obj; }
public void TestWriteWay() { // build source stream. var sourceWay = Way.Create(1, 1, 2); var sourceObjects = new OsmGeo[] { sourceWay }; // build pfb stream target. using (var stream = new MemoryStream()) { var target = new PBFOsmStreamTarget(stream); target.RegisterSource(sourceObjects.ToOsmStreamSource()); target.Pull(); stream.Seek(0, SeekOrigin.Begin); var resultObjects = new List<OsmGeo>(new PBFOsmStreamSource(stream)); Assert.IsNotNull(resultObjects); Assert.AreEqual(1, resultObjects.Count); Assert.AreEqual(sourceObjects[0].Id, resultObjects[0].Id); Assert.AreEqual(0, resultObjects[0].ChangeSetId); Assert.AreEqual(Utilities.FromUnixTime(0), resultObjects[0].TimeStamp); Assert.AreEqual(0, resultObjects[0].UserId); Assert.AreEqual(sourceObjects[0].UserName, resultObjects[0].UserName); Assert.AreEqual(0, resultObjects[0].Version); var resultWay = resultObjects[0] as Way; Assert.AreEqual(sourceWay.Nodes.Count, resultWay.Nodes.Count); Assert.AreEqual(sourceWay.Nodes[0], resultWay.Nodes[0]); Assert.AreEqual(sourceWay.Nodes[1], resultWay.Nodes[1]); } // build source stream. sourceWay = Way.Create(1, 1, 2); sourceWay.Tags = new TagsCollection(); sourceWay.Tags.Add("highway", "residential"); sourceObjects = new OsmGeo[] { sourceWay }; // build pfb stream target. using (var stream = new MemoryStream()) { var target = new PBFOsmStreamTarget(stream); target.RegisterSource(sourceObjects.ToOsmStreamSource()); target.Pull(); stream.Seek(0, SeekOrigin.Begin); var resultObjects = new List<OsmGeo>(new PBFOsmStreamSource(stream)); Assert.IsNotNull(resultObjects); Assert.AreEqual(1, resultObjects.Count); Assert.AreEqual(sourceObjects[0].Id, resultObjects[0].Id); Assert.AreEqual(0, resultObjects[0].ChangeSetId); Assert.AreEqual(Utilities.FromUnixTime(0), resultObjects[0].TimeStamp); Assert.AreEqual(0, resultObjects[0].UserId); Assert.AreEqual(string.Empty, resultObjects[0].UserName); Assert.AreEqual(0, resultObjects[0].Version); Assert.AreEqual(sourceObjects[0].Tags.Count, resultObjects[0].Tags.Count); Assert.IsTrue(resultObjects[0].Tags.Contains(sourceObjects[0].Tags.First<Tag>())); var resultWay = resultObjects[0] as Way; Assert.AreEqual(sourceWay.Nodes.Count, resultWay.Nodes.Count); Assert.AreEqual(sourceWay.Nodes[0], resultWay.Nodes[0]); Assert.AreEqual(sourceWay.Nodes[1], resultWay.Nodes[1]); } // build source stream. sourceWay = Way.Create(1, 1, 2); sourceWay.Tags = new TagsCollection(); sourceWay.Tags.Add("highway", "residential"); sourceWay.ChangeSetId = 1; sourceWay.TimeStamp = DateTime.Now; sourceWay.UserId = 1; sourceWay.UserName = "******"; sourceWay.Version = 3; sourceWay.Visible = true; sourceObjects = new OsmGeo[] { sourceWay }; // build pfb stream target. using (var stream = new MemoryStream()) { var target = new PBFOsmStreamTarget(stream); target.RegisterSource(sourceObjects.ToOsmStreamSource()); target.Pull(); stream.Seek(0, SeekOrigin.Begin); var resultObjects = new List<OsmGeo>(new PBFOsmStreamSource(stream)); Assert.IsNotNull(resultObjects); Assert.AreEqual(1, resultObjects.Count); Assert.AreEqual(sourceObjects[0].Id, resultObjects[0].Id); Assert.AreEqual(sourceObjects[0].ChangeSetId, resultObjects[0].ChangeSetId); Assert.AreEqual(sourceObjects[0].TimeStamp.Value.Ticks, resultObjects[0].TimeStamp.Value.Ticks, 10000000); Assert.AreEqual(sourceObjects[0].UserId, resultObjects[0].UserId); Assert.AreEqual(sourceObjects[0].UserName, resultObjects[0].UserName); Assert.AreEqual(sourceObjects[0].Version, resultObjects[0].Version); Assert.AreEqual(sourceObjects[0].Tags.Count, resultObjects[0].Tags.Count); Assert.IsTrue(resultObjects[0].Tags.Contains(sourceObjects[0].Tags.First<Tag>())); var resultWay = resultObjects[0] as Way; Assert.AreEqual(sourceWay.Nodes.Count, resultWay.Nodes.Count); Assert.AreEqual(sourceWay.Nodes[0], resultWay.Nodes[0]); Assert.AreEqual(sourceWay.Nodes[1], resultWay.Nodes[1]); } }
/// <summary> /// Creates a new MapCSS object with an osmgeo object. /// </summary> /// <param name="osmGeo"></param> public MapCSSObject(OsmGeo osmGeo) { if (osmGeo == null) throw new ArgumentNullException(); this.OsmGeo = osmGeo; }
/// <summary> /// Does the filtering. /// </summary> /// <param name="osmGeos"></param> /// <param name="filter"></param> private List<OsmGeo> Filter(OsmGeo[] osmGeos, OsmStreamFilterTagsFilter.TagsFilterDelegate filter) { OsmStreamFilterTagsFilter tagsFilter = new OsmStreamFilterTagsFilter(filter); tagsFilter.RegisterSource(osmGeos); return new List<OsmGeo>(tagsFilter); }
private bool MoveNextWay() { if (_wayReader == null) { OracleCommand way_command = new OracleCommand("select * from way order by id"); way_command.Connection = _connection; _wayReader = way_command.ExecuteReader(); if (!_wayReader.Read()) { _wayReader.Close(); } OracleCommand way_tag_command = new OracleCommand("select * from way_tags order by way_id"); way_tag_command.Connection = _connection; _wayTagReader = way_tag_command.ExecuteReader(); if (!_wayTagReader.IsClosed && !_wayTagReader.Read()) { _wayTagReader.Close(); } OracleCommand way_node_command = new OracleCommand("select * from way_nodes order by way_id,sequence_id"); way_node_command.Connection = _connection; _wayNodeReader = way_node_command.ExecuteReader(); if (!_wayNodeReader.IsClosed && !_wayNodeReader.Read()) { _wayNodeReader.Close(); } } // read next way. if (!_wayReader.IsClosed) { // load/parse data. long id = _wayReader.GetInt64(0); long changeset_id = _wayReader.GetInt64(1); DateTime timestamp = _wayReader.GetDateTime(2); bool visible = _wayReader.GetInt64(3) == 1; long version = _wayReader.GetInt64(4); string user = _wayReader.GetString(5); long uid = _wayReader.GetInt64(6); Way way = new Way(); way.Id = id; way.ChangeSetId = changeset_id; way.TimeStamp = timestamp; way.UserId = uid; way.UserName = user; way.Version = (ulong)version; way.Visible = visible; if (!_wayTagReader.IsClosed) { long returned_id = _wayTagReader.GetInt64(_wayTagReader.GetOrdinal("way_id")); while (returned_id == way.Id.Value) { if (way.Tags == null) { way.Tags = new TagsCollection(); } string key = _wayTagReader.GetString(1); string value = _wayTagReader.GetString(2); way.Tags.Add(key, value); if (!_wayTagReader.Read()) { _wayTagReader.Close(); returned_id = -1; } else { returned_id = _wayTagReader.GetInt64(0); } } } if (!_wayNodeReader.IsClosed) { long returned_id = _wayNodeReader.GetInt64(_wayNodeReader.GetOrdinal("way_id")); while (returned_id == way.Id.Value) { if (way.Nodes == null) { way.Nodes = new List<long>(); } long node_id = _wayNodeReader.GetInt64(1); way.Nodes.Add(node_id); if (!_wayNodeReader.Read()) { _wayNodeReader.Close(); returned_id = -1; } else { returned_id = _wayNodeReader.GetInt64(0); } } } // set the current variable! _current = way; // advance the reader(s). if (!_wayReader.Read()) { _wayReader.Close(); } if (!_wayTagReader.IsClosed && !_wayTagReader.Read()) { _wayTagReader.Close(); } if (!_wayNodeReader.IsClosed && !_wayNodeReader.Read()) { _wayNodeReader.Close(); } return true; } else { _wayReader.Close(); _wayReader.Dispose(); _wayReader = null; _wayTagReader.Close(); _wayTagReader.Dispose(); _wayTagReader = null; _current_type = OsmGeoType.Relation; return false; } }
private bool MoveNextNode() { if (_nodeReader == null) { OracleCommand node_command = new OracleCommand("select * from node order by id"); node_command.Connection = _connection; _nodeReader = node_command.ExecuteReader(); if (!_nodeReader.Read()) { _nodeReader.Close(); } OracleCommand node_tag_command = new OracleCommand("select * from node_tags order by node_id"); node_tag_command.Connection = _connection; _nodeTagReader = node_tag_command.ExecuteReader(); if (!_nodeTagReader.Read()) { _nodeTagReader.Close(); } } // read next node. if (!_nodeReader.IsClosed) { // load/parse data. long id = _nodeReader.GetInt64(0); long latitude_int = _nodeReader.GetInt64(1); long longitude_int = _nodeReader.GetInt64(2); long changeset_id = _nodeReader.GetInt64(3); bool visible = _nodeReader.GetInt64(4) == 1; DateTime timestamp = _nodeReader.GetDateTime(5); long tile = _nodeReader.GetInt64(6); long version = _nodeReader.GetInt64(7); string user = _nodeReader.GetString(8); long uid = _nodeReader.GetInt64(9); Node node = new Node(); node.Id = id; node.Latitude = latitude_int; node.Longitude = longitude_int; node.ChangeSetId = changeset_id; node.TimeStamp = timestamp; node.UserId = null; node.UserName = null; node.Version = (ulong)version; node.Visible = visible; node.UserName = user; node.UserId = uid; if (!_nodeTagReader.IsClosed) { long returned_id = _nodeTagReader.GetInt64(0); while (returned_id == node.Id.Value) { if (node.Tags == null) { node.Tags = new TagsCollection(); } string key = _nodeTagReader.GetString(1); string value = _nodeTagReader.GetString(2); node.Tags.Add(key, value); if (!_nodeTagReader.Read()) { _nodeTagReader.Close(); } returned_id = _nodeTagReader.GetInt64(0); } } // set the current variable! _current = node; // advance the reader(s). if (!_nodeReader.Read()) { _nodeReader.Close(); } if (!_nodeTagReader.IsClosed && !_nodeTagReader.Read()) { _nodeTagReader.Close(); } return true; } else { _nodeReader.Close(); _nodeReader.Dispose(); _nodeReader = null; _nodeTagReader.Close(); _nodeTagReader.Dispose(); _nodeTagReader = null; _current_type = OsmGeoType.Way; return false; } }
public void TestWriteMix() { var sourceNode = Node.Create(1, 1.1, 1.2); sourceNode.Tags = new TagsCollection(); sourceNode.Tags.Add("highway", "residential"); sourceNode.ChangeSetId = 1; sourceNode.TimeStamp = DateTime.Now; sourceNode.UserId = 1; sourceNode.UserName = "******"; sourceNode.Version = 3; sourceNode.Visible = true; var sourceWay = Way.Create(1, 1, 2); sourceWay.Tags = new TagsCollection(); sourceWay.Tags.Add("highway", "residential"); sourceWay.ChangeSetId = 1; sourceWay.TimeStamp = DateTime.Now; sourceWay.UserId = 1; sourceWay.UserName = "******"; sourceWay.Version = 3; sourceWay.Visible = true; var sourceRelation = Relation.Create(1, new RelationMember() { MemberId = 1, MemberRole = "fake role", MemberType = OsmGeoType.Node }, new RelationMember() { MemberId = 1, MemberRole = "fake role", MemberType = OsmGeoType.Way }); sourceRelation.Tags = new TagsCollection(); sourceRelation.Tags.Add("highway", "residential"); sourceRelation.ChangeSetId = 1; sourceRelation.TimeStamp = DateTime.Now; sourceRelation.UserId = 1; sourceRelation.UserName = "******"; sourceRelation.Version = 3; sourceRelation.Visible = true; var sourceObjects = new OsmGeo[] { sourceNode, sourceWay, sourceRelation }; // build pfb stream target. using (var stream = new MemoryStream()) { var target = new PBFOsmStreamTarget(stream); target.RegisterSource(sourceObjects.ToOsmStreamSource()); target.Pull(); stream.Seek(0, SeekOrigin.Begin); var resultObjects = new List<OsmGeo>(new PBFOsmStreamSource(stream)); Assert.IsNotNull(resultObjects); Assert.AreEqual(3, resultObjects.Count); Assert.AreEqual(sourceObjects[0].Id, resultObjects[0].Id); Assert.AreEqual(sourceObjects[0].ChangeSetId, resultObjects[0].ChangeSetId); Assert.AreEqual(sourceObjects[0].TimeStamp.Value.Ticks, resultObjects[0].TimeStamp.Value.Ticks, 10000000); Assert.AreEqual(sourceObjects[0].UserId, resultObjects[0].UserId); Assert.AreEqual(sourceObjects[0].UserName, resultObjects[0].UserName); Assert.AreEqual(sourceObjects[0].Version, resultObjects[0].Version); Assert.AreEqual(sourceObjects[0].Tags.Count, resultObjects[0].Tags.Count); Assert.IsTrue(resultObjects[0].Tags.Contains(sourceObjects[0].Tags.First<Tag>())); var resultNode = resultObjects[0] as Node; Assert.AreEqual(sourceNode.Latitude.Value, resultNode.Latitude.Value, .0001f); Assert.AreEqual(sourceNode.Longitude.Value, resultNode.Longitude.Value, .0001f); Assert.AreEqual(sourceObjects[1].Id, resultObjects[1].Id); Assert.AreEqual(sourceObjects[1].ChangeSetId, resultObjects[1].ChangeSetId); Assert.AreEqual(sourceObjects[1].TimeStamp.Value.Ticks, resultObjects[1].TimeStamp.Value.Ticks, 10000000); Assert.AreEqual(sourceObjects[1].UserId, resultObjects[1].UserId); Assert.AreEqual(sourceObjects[1].UserName, resultObjects[1].UserName); Assert.AreEqual(sourceObjects[1].Version, resultObjects[1].Version); Assert.AreEqual(sourceObjects[1].Tags.Count, resultObjects[1].Tags.Count); Assert.IsTrue(resultObjects[1].Tags.Contains(sourceObjects[1].Tags.First<Tag>())); var resultWay = resultObjects[1] as Way; Assert.AreEqual(sourceWay.Nodes.Count, resultWay.Nodes.Count); Assert.AreEqual(sourceWay.Nodes[0], resultWay.Nodes[0]); Assert.AreEqual(sourceWay.Nodes[1], resultWay.Nodes[1]); Assert.AreEqual(sourceObjects[2].Id, resultObjects[2].Id); Assert.AreEqual(sourceObjects[2].ChangeSetId, resultObjects[2].ChangeSetId); Assert.AreEqual(sourceObjects[2].TimeStamp.Value.Ticks, resultObjects[2].TimeStamp.Value.Ticks, 10000000); Assert.AreEqual(sourceObjects[2].UserId, resultObjects[2].UserId); Assert.AreEqual(sourceObjects[2].UserName, resultObjects[2].UserName); Assert.AreEqual(sourceObjects[2].Version, resultObjects[2].Version); Assert.AreEqual(sourceObjects[2].Tags.Count, resultObjects[2].Tags.Count); Assert.IsTrue(resultObjects[2].Tags.Contains(sourceObjects[2].Tags.First<Tag>())); var resultRelation = resultObjects[2] as Relation; Assert.AreEqual(sourceRelation.Members.Count, resultRelation.Members.Count); Assert.AreEqual(sourceRelation.Members[0].MemberId, resultRelation.Members[0].MemberId); Assert.AreEqual(sourceRelation.Members[0].MemberRole, resultRelation.Members[0].MemberRole); Assert.AreEqual(sourceRelation.Members[0].MemberType, resultRelation.Members[0].MemberType); Assert.AreEqual(sourceRelation.Members[1].MemberId, resultRelation.Members[1].MemberId); Assert.AreEqual(sourceRelation.Members[1].MemberRole, resultRelation.Members[1].MemberRole); Assert.AreEqual(sourceRelation.Members[1].MemberType, resultRelation.Members[1].MemberType); } }
/// <summary> /// Adds a new osmgeo object. /// </summary> /// <param name="osmGeo"></param> public void Add(OsmGeo osmGeo) { if (osmGeo is Node) { this.AddNode(osmGeo as Node); } else if (osmGeo is Way) { this.AddWay(osmGeo as Way); } else if (osmGeo is Relation) { this.AddRelation(osmGeo as Relation); } }
/// <summary> /// Moves to the next object. /// </summary> /// <returns></returns> private bool DoMoveNext(bool ignoreNodes, bool ignoreWays, bool ignoreRelations) { if (this.Source.MoveNext(ignoreNodes, ignoreWays, ignoreRelations)) { _current = this.Source.Current(); // give the opportunity to filter tags. if (_current.Tags != null && _current.Tags.Count > 0) { // only filter tags when there are tags to be filtered. _filter.Invoke(_current.Tags); } return true; } return false; }
public void TestWriteRelation() { // build source stream. var sourceRelation = Relation.Create(1, new RelationMember() { MemberId = 1, MemberRole = "fake role", MemberType = OsmGeoType.Node }, new RelationMember() { MemberId = 1, MemberRole = "fake role", MemberType = OsmGeoType.Way }); var sourceObjects = new OsmGeo[] { sourceRelation }; var source = sourceObjects.ToOsmStreamSource(); // build pfb stream target. using (var stream = new MemoryStream()) { var target = new PBFOsmStreamTarget(stream); target.RegisterSource(source); target.Pull(); stream.Seek(0, SeekOrigin.Begin); var resultObjects = new List<OsmGeo>(new PBFOsmStreamSource(stream)); Assert.IsNotNull(resultObjects); Assert.AreEqual(1, resultObjects.Count); Assert.AreEqual(sourceObjects[0].Id, resultObjects[0].Id); Assert.AreEqual(0, resultObjects[0].ChangeSetId); Assert.AreEqual(Utilities.FromUnixTime(0), resultObjects[0].TimeStamp); Assert.AreEqual(0, resultObjects[0].UserId); Assert.AreEqual(string.Empty, resultObjects[0].UserName); Assert.AreEqual(0, resultObjects[0].Version); var resultRelation = resultObjects[0] as Relation; Assert.AreEqual(sourceRelation.Members.Count, resultRelation.Members.Count); Assert.AreEqual(sourceRelation.Members[0].MemberId, resultRelation.Members[0].MemberId); Assert.AreEqual(sourceRelation.Members[0].MemberRole, resultRelation.Members[0].MemberRole); Assert.AreEqual(sourceRelation.Members[0].MemberType, resultRelation.Members[0].MemberType); Assert.AreEqual(sourceRelation.Members[1].MemberId, resultRelation.Members[1].MemberId); Assert.AreEqual(sourceRelation.Members[1].MemberRole, resultRelation.Members[1].MemberRole); Assert.AreEqual(sourceRelation.Members[1].MemberType, resultRelation.Members[1].MemberType); } // build source stream. sourceRelation = Relation.Create(1, new RelationMember() { MemberId = 1, MemberRole = "fake role", MemberType = OsmGeoType.Node }, new RelationMember() { MemberId = 1, MemberRole = "fake role", MemberType = OsmGeoType.Way }); sourceRelation.Tags = new TagsCollection(); sourceRelation.Tags.Add("highway", "residential"); sourceObjects = new OsmGeo[] { sourceRelation }; // build pfb stream target. using (var stream = new MemoryStream()) { var target = new PBFOsmStreamTarget(stream); target.RegisterSource(sourceObjects.ToOsmStreamSource()); target.Pull(); stream.Seek(0, SeekOrigin.Begin); var resultObjects = new List<OsmGeo>(new PBFOsmStreamSource(stream)); Assert.IsNotNull(resultObjects); Assert.AreEqual(1, resultObjects.Count); Assert.AreEqual(sourceObjects[0].Id, resultObjects[0].Id); Assert.AreEqual(0, resultObjects[0].ChangeSetId); Assert.AreEqual(Utilities.FromUnixTime(0), resultObjects[0].TimeStamp); Assert.AreEqual(0, resultObjects[0].UserId); Assert.AreEqual(string.Empty, resultObjects[0].UserName); Assert.AreEqual(0, resultObjects[0].Version); Assert.AreEqual(sourceObjects[0].Tags.Count, resultObjects[0].Tags.Count); Assert.IsTrue(resultObjects[0].Tags.Contains(sourceObjects[0].Tags.First<Tag>())); var resultRelation = resultObjects[0] as Relation; Assert.AreEqual(sourceRelation.Members.Count, resultRelation.Members.Count); Assert.AreEqual(sourceRelation.Members[0].MemberId, resultRelation.Members[0].MemberId); Assert.AreEqual(sourceRelation.Members[0].MemberRole, resultRelation.Members[0].MemberRole); Assert.AreEqual(sourceRelation.Members[0].MemberType, resultRelation.Members[0].MemberType); Assert.AreEqual(sourceRelation.Members[1].MemberId, resultRelation.Members[1].MemberId); Assert.AreEqual(sourceRelation.Members[1].MemberRole, resultRelation.Members[1].MemberRole); Assert.AreEqual(sourceRelation.Members[1].MemberType, resultRelation.Members[1].MemberType); } // build source stream. sourceRelation = Relation.Create(1, new RelationMember() { MemberId = 1, MemberRole = "fake role", MemberType = OsmGeoType.Node }, new RelationMember() { MemberId = 1, MemberRole = "fake role", MemberType = OsmGeoType.Way }); sourceRelation.Tags = new TagsCollection(); sourceRelation.Tags.Add("highway", "residential"); sourceRelation.ChangeSetId = 1; sourceRelation.TimeStamp = DateTime.Now; sourceRelation.UserId = 1; sourceRelation.UserName = "******"; sourceRelation.Version = 3; sourceRelation.Visible = true; sourceObjects = new OsmGeo[] { sourceRelation }; // build pfb stream target. using (var stream = new MemoryStream()) { var target = new PBFOsmStreamTarget(stream); target.RegisterSource(sourceObjects.ToOsmStreamSource()); target.Pull(); stream.Seek(0, SeekOrigin.Begin); var resultObjects = new List<OsmGeo>(new PBFOsmStreamSource(stream)); Assert.IsNotNull(resultObjects); Assert.AreEqual(1, resultObjects.Count); Assert.AreEqual(sourceObjects[0].Id, resultObjects[0].Id); Assert.AreEqual(sourceObjects[0].ChangeSetId, resultObjects[0].ChangeSetId); Assert.AreEqual(sourceObjects[0].TimeStamp.Value.Ticks, resultObjects[0].TimeStamp.Value.Ticks, 10000000); Assert.AreEqual(sourceObjects[0].UserId, resultObjects[0].UserId); Assert.AreEqual(sourceObjects[0].UserName, resultObjects[0].UserName); Assert.AreEqual(sourceObjects[0].Version, resultObjects[0].Version); Assert.AreEqual(sourceObjects[0].Tags.Count, resultObjects[0].Tags.Count); Assert.IsTrue(resultObjects[0].Tags.Contains(sourceObjects[0].Tags.First<Tag>())); var resultRelation = resultObjects[0] as Relation; Assert.AreEqual(sourceRelation.Members.Count, resultRelation.Members.Count); Assert.AreEqual(sourceRelation.Members[0].MemberId, resultRelation.Members[0].MemberId); Assert.AreEqual(sourceRelation.Members[0].MemberRole, resultRelation.Members[0].MemberRole); Assert.AreEqual(sourceRelation.Members[0].MemberType, resultRelation.Members[0].MemberType); Assert.AreEqual(sourceRelation.Members[1].MemberId, resultRelation.Members[1].MemberId); Assert.AreEqual(sourceRelation.Members[1].MemberRole, resultRelation.Members[1].MemberRole); Assert.AreEqual(sourceRelation.Members[1].MemberType, resultRelation.Members[1].MemberType); } }
/// <summary> /// Interprets an OSM-object and returns the correctponding geometry. /// </summary> /// <param name="simpleOsmGeo"></param> /// <param name="data"></param> /// <returns></returns> public virtual GeometryCollection Interpret(OsmGeo simpleOsmGeo, IDataSourceReadOnly data) { switch (simpleOsmGeo.Type) { case OsmGeoType.Node: return this.Interpret(simpleOsmGeo as Node); case OsmGeoType.Way: return this.Interpret(CompleteWay.CreateFrom(simpleOsmGeo as Way, data)); case OsmGeoType.Relation: return this.Interpret(CompleteRelation.CreateFrom(simpleOsmGeo as Relation, data)); } throw new ArgumentOutOfRangeException(); }
/// <summary> /// Moves to the next object. /// </summary> /// <returns></returns> public override bool MoveNext() { if (!_relationKeepObjects && !_wayKeepNodes) { // simple here just filter! const bool filterOk = false; while (!filterOk) { if (this.Source.MoveNext()) { OsmGeo current = this.Source.Current(); switch (current.Type) { case OsmGeoType.Node: if (_nodesFilter == null || _nodesFilter.Evaluate(current)) { _current = current; return true; } break; case OsmGeoType.Way: if (_waysFilter == null || _waysFilter.Evaluate(current)) { _current = current; return true; } break; case OsmGeoType.Relation: if (_relationsFilter == null || _relationsFilter.Evaluate(current)) { _current = current; return true; } break; } } else { // there is no more data in the source! return false; } } } return false; }
/// <summary> /// Returns true if this style applies to the given object. /// </summary> /// <param name="osmGeo"></param> /// <returns></returns> public abstract bool AppliesTo(OsmGeo osmGeo);
/// <summary> /// Resets this filter. /// </summary> public override void Reset() { _current = null; this.Source.Reset(); }
/// <summary> /// Initializes this source. /// </summary> public override void Initialize() { _current = null; _current_type = OsmGeoType.Node; _node_reader = null; }
/// <summary> /// Tests if the given object is usefull in this style. /// </summary> /// <param name="osmGeo"></param> /// <returns></returns> public override bool AppliesTo(OsmGeo osmGeo) { if (osmGeo == null) { return false; } // interpret the osm-objects. switch (osmGeo.Type) { case OsmGeoType.Node: return this.AppliesToNode(osmGeo as Node); case OsmGeoType.Way: if (this.AppliesToWay(osmGeo as Way)) { // this could possibly apply. return true; } // test also if it might apply as an area. if (_geometryInterpreter.IsPotentiallyArea(osmGeo.Tags)) { return this.AppliesToArea(osmGeo.Tags); } return false; case OsmGeoType.Relation: if (this.AppliesToRelation(osmGeo as Relation)) { // this could possibly apply. return true; } // test also if it might apply as an area. if (_geometryInterpreter.IsPotentiallyArea(osmGeo.Tags)) { return this.AppliesToArea(osmGeo.Tags); } return false; default: throw new ArgumentOutOfRangeException(); } }
private bool DoMoveNextNode() { if (_node_reader == null) { SQLiteCommand node_command = new SQLiteCommand("select * from node left join node_tags on node_tags.node_id = node.id order by node.id"); node_command.Connection = _connection; _node_reader = node_command.ExecuteReader(); if (!_node_reader.Read()) _node_reader.Close(); } // read next node. if (!_node_reader.IsClosed) { // load/parse data. Node node = new Node(); node.Id = _node_reader.GetInt64(0); node.Latitude = _node_reader.GetInt64(1) / 10000000.0; node.Longitude = _node_reader.GetInt64(2) / 10000000.0; node.ChangeSetId = _node_reader.GetInt64(3); node.TimeStamp = _node_reader.GetDateTime(5); node.Version = (ulong)_node_reader.GetInt64(7); node.Visible = _node_reader.GetInt64(4) == 1; //node.UserName = _node_reader.GetString(8); //node.UserId = _node_reader.IsDBNull(9) ? -1 : _node_reader.GetInt64(9); //Has tags? if (!_node_reader.IsDBNull(10)) { //if (node.Tags == null) //node.Tags = new Dictionary<string, string>(); long currentnode = node.Id.Value; while (currentnode == node.Id.Value) { //string key = _node_reader.GetString(11); //string value = _node_reader.GetString(12); //node.Tags.Add(key, value); if (!_node_reader.Read()) { _node_reader.Close(); break; } currentnode = _node_reader.GetInt64(0); } } else if (!_node_reader.Read()) _node_reader.Close(); // set the current variable! _current = node; return true; } _node_reader.Close(); _node_reader.Dispose(); _node_reader = null; _current_type = OsmGeoType.Way; return false; }
private void AddTags(OsmGeo geo, Dictionary<TagsCollectionBase, int> uniques, List<Tuple<int, Tag>> cache) { if (geo.Tags != null) { var filtered_tags = _tag_filter.Evaluate(geo); if (filtered_tags.Count > 0) { // ensure we haven't added this combination before if (!uniques.ContainsKey(filtered_tags)) { // add each tag to the tag cache foreach (var tag in filtered_tags) { if (cache.Count == TagsBatchCount) { BatchAddTags(cache, geo.Type); } cache.Add(new Tuple<int, Tag>(uniques.Count, tag)); } // add it to the hashset uniques[filtered_tags] = uniques.Count; } } } }
private bool DoMoveNextWay() { if (_way_reader == null) { SQLiteCommand way_command = new SQLiteCommand("select * from way where id > 26478817 order by id"); way_command.Connection = _connection; _way_reader = way_command.ExecuteReader(); if (!_way_reader.Read()) { _way_reader.Close(); } SQLiteCommand way_tag_command = new SQLiteCommand("select * from way_tags where way_id > 26478817 order by way_id"); way_tag_command.Connection = _connection; _way_tag_reader = way_tag_command.ExecuteReader(); if (!_way_tag_reader.IsClosed && !_way_tag_reader.Read()) { _way_tag_reader.Close(); } SQLiteCommand way_node_command = new SQLiteCommand("select * from way_nodes where way_id > 26478817 order by way_id,sequence_id"); way_node_command.Connection = _connection; _way_node_reader = way_node_command.ExecuteReader(); if (!_way_node_reader.IsClosed && !_way_node_reader.Read()) { _way_node_reader.Close(); } } // read next way. if (!_way_reader.IsClosed) { Way way = new Way(); way.Id = _way_reader.GetInt64(0); way.ChangeSetId = _way_reader.GetInt64(1); way.TimeStamp = _way_reader.IsDBNull(3) ? DateTime.MinValue : _way_reader.GetDateTime(3); //way.UserId = _way_reader.GetInt64(6); //way.UserName = _way_reader.GetString(5); way.Version = (ulong)_way_reader.GetInt64(4); way.Visible = _way_reader.GetInt64(2) == 1; if (!_way_tag_reader.IsClosed) { long returned_id = _way_tag_reader.GetInt64(_way_tag_reader.GetOrdinal("way_id")); while (returned_id == way.Id.Value) { if (way.Tags == null) { way.Tags = new TagsCollection(); } string key = _way_tag_reader.GetString(1); string value = _way_tag_reader.GetString(2); way.Tags.Add(key, value); if (!_way_tag_reader.Read()) { _way_tag_reader.Close(); returned_id = -1; } else { returned_id = _way_tag_reader.GetInt64(0); } } } if (!_way_node_reader.IsClosed) { long returned_id = _way_node_reader.GetInt64(_way_node_reader.GetOrdinal("way_id")); while (returned_id == way.Id.Value) { if (way.Nodes == null) { way.Nodes = new List<long>(); } long node_id = _way_node_reader.GetInt64(1); way.Nodes.Add(node_id); if (!_way_node_reader.Read()) { _way_node_reader.Close(); returned_id = -1; } else { returned_id = _way_node_reader.GetInt64(0); } } } // set the current variable! _current = way; // advance the reader(s). if (!_way_reader.Read()) { _way_reader.Close(); } if (!_way_tag_reader.IsClosed && !_way_tag_reader.Read()) { _way_tag_reader.Close(); } if (!_way_node_reader.IsClosed && !_way_node_reader.Read()) { _way_node_reader.Close(); } return true; } else { _way_reader.Close(); _way_reader.Dispose(); _way_reader = null; _way_tag_reader.Close(); _way_tag_reader.Dispose(); _way_tag_reader = null; _current_type = OsmGeoType.Relation; return false; } }
/// <summary> /// Does the filtering. /// </summary> /// <param name="osmGeos"></param> /// <param name="toExclude"></param> private List<OsmGeo> Filter(OsmGeo[] osmGeos, OsmGeo[] toExclude) { return this.Filter(osmGeos, toExclude, true, true, true); }