public override TagsCollectionBase Evaluate(OsmGeo obj) { var a_result = _a.Evaluate(obj); var b_result = _b.Evaluate(obj); return a_result.Union(b_result); }
public override TagsCollectionBase Evaluate(OsmGeo obj) { if (obj == null) { throw new ArgumentNullException("obj"); } return obj.Tags; }
/// <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> /// Interprets the object and reports the findings to the client. /// </summary> /// <param name="client"></param> /// <param name="geo"></param> public void Interpret(IFeatureClient client, OsmGeo geo) { // call the onbefore function client.OnBeforeInterpretation(geo); // interpret the object. bool succes = _root_feature.Interpret(geo,client); // call the onafter function client.OnAfterInterpretation(succes,geo); }
public override TagsCollectionBase Evaluate(OsmGeo obj) { if (obj == null) { throw new ArgumentNullException("obj"); } if (obj.Type == _type) { return obj.Tags.KeepKeysOf(_keys); } return TagsCollectionBase.Empty; }
public override TagsCollectionBase Evaluate(OsmGeo obj) { if (obj == null) { throw new ArgumentNullException("obj"); } var result = obj.Tags; if (obj.Type == _type) { result = TagsCollectionBase.Empty; } return result; }
/// <summary> /// Interprets a feature and reports to the client if the feature matches the filter. /// </summary> /// <param name="obj"></param> /// <param name="client"></param> /// <returns></returns> public bool Interpret( OsmGeo obj, IFeatureClient client) { if (_filter.Evaluate(obj)) { // add the meta tags. Dictionary<string,string> meta = new Dictionary<string,string>(); foreach(string value in _meta.Keys) { meta.Add(value,string.Empty); // check all the tags in the list. foreach(string tag in _meta[value]) { if(obj.Tags.ContainsKey(value)) { string tag_value = obj.Tags["tag"]; if(tag_value != null && tag_value.Length > 0) { meta[value] = tag_value; } } } } // report the interpretation succes. client.InterpretationSucces(_path, obj, meta); return true; } return false; }
private bool DoMoveNextRelation() { if (_relationReader == null) { OracleCommand relation_command = new OracleCommand("select * from relation order by id"); relation_command.Connection = _connection; _relationReader = relation_command.ExecuteReader(); if (!_relationReader.Read()) { _relationReader.Close(); } OracleCommand relation_tag_command = new OracleCommand("select * from relation_tags order by relation_id"); relation_tag_command.Connection = _connection; _relationTagReader = relation_tag_command.ExecuteReader(); if (!_relationTagReader.IsClosed && !_relationTagReader.Read()) { _relationTagReader.Close(); } OracleCommand relation_node_command = new OracleCommand("select * from relation_members order by relation_id,sequence_id"); relation_node_command.Connection = _connection; _relationMemberReader = relation_node_command.ExecuteReader(); if (!_relationMemberReader.IsClosed && !_relationMemberReader.Read()) { _relationMemberReader.Close(); } } // read next relation. if (!_relationReader.IsClosed) { // load/parse data. long id = _relationReader.GetInt64(0); long changeset_id = _relationReader.GetInt64(1); DateTime timestamp = _relationReader.GetDateTime(2); bool visible = _relationReader.GetInt64(3) == 1; long version = _relationReader.GetInt64(4); string user = _relationReader.GetString(5); long uid = _relationReader.GetInt64(6); Relation relation = new Relation(); relation.Id = id; relation.ChangeSetId = changeset_id; relation.TimeStamp = timestamp; relation.UserId = null; relation.UserName = null; relation.Version = (ulong)version; relation.Visible = visible; relation.UserName = user; relation.UserId = uid; if (!_relationTagReader.IsClosed) { long returned_id = _relationTagReader.GetInt64(0); while (returned_id == relation.Id.Value) { if (relation.Tags == null) { relation.Tags = new TagsCollection(); } string key = _relationTagReader.GetString(1); string value = _relationTagReader.GetString(2); relation.Tags.Add(key, value); if (!_relationTagReader.Read()) { _relationTagReader.Close(); returned_id = -1; } else { returned_id = _relationTagReader.GetInt64(0); } } } if (!_relationMemberReader.IsClosed) { long returned_id = _relationMemberReader.GetInt64(0); while (returned_id == relation.Id.Value) { if (relation.Members == null) { relation.Members = new List <RelationMember>(); } string member_type = _relationMemberReader.GetString(1); long member_id = _relationMemberReader.GetInt64(2); object member_role = _relationMemberReader.GetValue(3); RelationMember member = new RelationMember(); member.MemberId = member_id; if (member_role != DBNull.Value) { member.MemberRole = member_role as string; } switch (member_type) { 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 (!_relationMemberReader.Read()) { _relationMemberReader.Close(); returned_id = -1; } else { returned_id = _relationMemberReader.GetInt64(0); } } } // set the current variable! _current = relation; // advance the reader(s). if (!_relationReader.Read()) { _relationReader.Close(); } if (!_relationTagReader.IsClosed && !_relationTagReader.Read()) { _relationTagReader.Close(); } if (!_relationMemberReader.IsClosed && !_relationMemberReader.Read()) { _relationMemberReader.Close(); } return(true); } else { _relationReader.Close(); _relationReader.Dispose(); _relationReader = null; _relationTagReader.Close(); _relationTagReader.Dispose(); _relationTagReader = null; _currentType = OsmGeoType.Relation; return(false); } }
private bool DoMoveNextWay() { 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; _currentType = OsmGeoType.Relation; return(false); } }
public void TestWriteMix() { var sourceNode = new Node() { Id = 1, Latitude = 1.1f, Longitude = 1.2f }; 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 = new Way() { Id = 1, Nodes = new long[] { 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 = new Relation() { Id = 1, Members = new RelationMember[] { new RelationMember() { Id = 1, Role = "fake role", Type = OsmGeoType.Node }, new RelationMember() { Id = 1, Role = "fake role", Type = 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); 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.Length, resultWay.Nodes.Length); 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.Length, resultRelation.Members.Length); Assert.AreEqual(sourceRelation.Members[0].Id, resultRelation.Members[0].Id); Assert.AreEqual(sourceRelation.Members[0].Role, resultRelation.Members[0].Role); Assert.AreEqual(sourceRelation.Members[0].Type, resultRelation.Members[0].Type); Assert.AreEqual(sourceRelation.Members[1].Id, resultRelation.Members[1].Id); Assert.AreEqual(sourceRelation.Members[1].Role, resultRelation.Members[1].Role); Assert.AreEqual(sourceRelation.Members[1].Type, resultRelation.Members[1].Type); } }
private bool ConcurrentMoveNext(OsmStreamSource source, out OsmGeo current, bool ignore_nodes = false, bool igonre_ways = false, bool ignore_relations = false) { bool available = false; current = null; lock (_source_lock) { if (_cancel_pull) { return false; } available = source.MoveNext(); if (available) { current = source.Current(); _pull_progress++; } } return available; }
private bool DoMoveNextNode() { 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; _currentType = OsmGeoType.Way; return false; } }
public void TestBicycleUseSidepath() { // the input osm-data. var osmGeos = new OsmGeo[] { new Node() { Id = 1, Latitude = 12.608657753733688f, Longitude = -7.966136634349823f }, new Node() { Id = 2, Latitude = 12.608647283636317f, Longitude = -7.967574298381805f }, new Node() { Id = 3, Latitude = 12.609173405500497f, Longitude = -7.966713309288025f }, new Way() { Id = 1, Nodes = new long[] { 1, 2 }, Tags = new TagsCollection( new Tag("highway", "residential"), new Tag("bicycle", "use_sidepath")) }, new Way() { Id = 2, Nodes = new long[] { 1, 3, 2 }, Tags = new TagsCollection( new Tag("highway", "cycleway")) } }; // build router db. var routerDb = new RouterDb(); routerDb.LoadOsmData(osmGeos, Vehicle.Car, Vehicle.Bicycle); // test some routes. var router = new Router(routerDb); // confirm it's working for bicycles but that the cycleway is used. var route = router.TryCalculate(Vehicle.Bicycle.Fastest(), new Coordinate(12.608657753733688f, -7.966136634349823f), new Coordinate(12.608647283636317f, -7.967574298381805f)); Assert.IsFalse(route.IsError); Assert.IsTrue(route.Value.TotalDistance > 180); route = router.TryCalculate(Vehicle.Bicycle.Fastest(), new Coordinate(12.608657753733688f, -7.966136634349823f), new Coordinate(12.608647283636317f, -7.967574298381805f)); Assert.IsFalse(route.IsError); Assert.IsTrue(route.Value.TotalDistance > 180); // confirm it's not working for cars. route = router.TryCalculate(Vehicle.Car.Fastest(), new Coordinate(12.608657753733688f, -7.966136634349823f), new Coordinate(12.608647283636317f, -7.967574298381805f)); Assert.IsFalse(route.IsError); Assert.IsTrue(route.Value.TotalDistance < 180); route = router.TryCalculate(Vehicle.Car.Fastest(), new Coordinate(12.608657753733688f, -7.966136634349823f), new Coordinate(12.608647283636317f, -7.967574298381805f)); Assert.IsFalse(route.IsError); Assert.IsTrue(route.Value.TotalDistance < 180); }
/// <summary> /// Returns the current object. /// </summary> /// <returns></returns> public override OsmGeo Current() { OsmGeo current = _reader.Current(); switch (current.Type) { case OsmGeoType.Node: _node++; if ((_node % 10000) == 0) { #if !WINDOWS_PHONE Process p = Process.GetCurrentProcess(); long stop = DateTime.Now.Ticks; float seconds = ((float)(stop - _start)) / (float)TimeSpan.TicksPerSecond; if ((_node % 1000000) == 0) { GC.Collect(); } OsmSharp.Logging.Log.TraceEvent("OsmSharp.Osm.Data.Streams.Filters.OsmStreamFilterProgress", TraceEventType.Information, "Node[{0}]: {1}nodes/s @ {2}MB", _node, (int)((double)_node / seconds), p.PrivateMemorySize64 / 1024 / 1024); #endif #if WINDOWS_PHONE long stop = DateTime.Now.Ticks; float seconds = ((float)(stop - _start)) / (float)TimeSpan.TicksPerSecond; if ((_node % 1000000) == 0) { GC.Collect(); } OsmSharp.Logging.Log.TraceEvent("OsmSharp.Osm.Data.Streams.Filters.OsmStreamFilterProgress", TraceEventType.Information, "Node[{0}]: {1}nodes/s", _node, (int)((double)_node / seconds)); #endif } break; case OsmGeoType.Relation: _relation++; if ((_relation % 1000) == 0) { #if !WINDOWS_PHONE Process p = Process.GetCurrentProcess(); long stop = DateTime.Now.Ticks; float seconds = ((float)(stop - _start)) / (float)TimeSpan.TicksPerSecond; if ((_relation % 10000) == 0) { GC.Collect(); } OsmSharp.Logging.Log.TraceEvent("OsmSharp.Osm.Data.Streams.Filters.OsmStreamFilterProgress", TraceEventType.Information, "Relation[{0}]: {1}relations/s @ {2}MB", _relation, (int)((double)_relation / seconds), p.PrivateMemorySize64 / 1024 / 1024); #endif #if WINDOWS_PHONE long stop = DateTime.Now.Ticks; float seconds = ((float)(stop - _start)) / (float)TimeSpan.TicksPerSecond; if ((_relation % 10000) == 0) { GC.Collect(); } OsmSharp.Logging.Log.TraceEvent("OsmSharp.Osm.Data.Streams.Filters.OsmStreamFilterProgress", TraceEventType.Information, "Relation[{0}]: {1}relations/s", _relation, (int)((double)_relation / seconds)); #endif } break; case OsmGeoType.Way: _way++; if ((_way % 10000) == 0) { #if !WINDOWS_PHONE Process p = Process.GetCurrentProcess(); long stop = DateTime.Now.Ticks; float seconds = ((float)(stop - _start)) / (float)TimeSpan.TicksPerSecond; if ((_way % 100000) == 0) { GC.Collect(); } OsmSharp.Logging.Log.TraceEvent("OsmSharp.Osm.Data.Streams.Filters.OsmStreamFilterProgress", TraceEventType.Information, "Way[{0}]: {1}ways/s @ {2}MB", _way, (int)((double)_way / seconds), p.PrivateMemorySize64 / 1024 / 1024); #endif #if WINDOWS_PHONE long stop = DateTime.Now.Ticks; float seconds = ((float)(stop - _start)) / (float)TimeSpan.TicksPerSecond; if ((_way % 100000) == 0) { GC.Collect(); } OsmSharp.Logging.Log.TraceEvent("OsmSharp.Osm.Data.Streams.Filters.OsmStreamFilterProgress", TraceEventType.Information, "Way[{0}]: {1}ways/s", _way, (int)((double)_way / seconds)); #endif } break; } return(current); }
/// <summary> /// Creates a version key for the given object. /// </summary> public OsmGeoVersionKey(OsmGeo osmGeo) { this.Type = osmGeo.Type; this.Id = osmGeo.Id.Value; this.Version = osmGeo.Version.Value; }
private bool IsRelevantPointOfInterest(OsmGeo osm, List <KeyValuePair <string, string> > relevantTagsDictionary) { return(osm.Tags != null && (osm.Tags.GetName() != string.Empty || osm.Tags.HasAny(relevantTagsDictionary))); }
/// <summary> /// Moves this filter to the next object. /// </summary> private bool DoMoveNext() { if (_selectNode == null) { return(false); } if (_completeWays) { while (this.Source.MoveNext()) { _current = this.Source.Current(); switch (_current.Type) { case OsmGeoType.Node: if (_nodesToInclude.Contains(_current.Id.Value) || _extraNodesToInclude.Contains(_current.Id.Value)) { return(true); } break; case OsmGeoType.Way: if (_waysToInclude.Contains(_current.Id.Value)) { return(true); } break; case OsmGeoType.Relation: if ((_current as Relation).HasMemberIn(_nodesToInclude, _waysToInclude, _relationsToInclude)) { return(true); } break; } } } else { while (this.Source.MoveNext()) { _current = this.Source.Current(); if (_current.Type == OsmGeoType.Node) { if (_selectNode(_current as Node)) { _nodesToInclude.Add(_current.Id.Value); return(true); } } else if (_current.Type == OsmGeoType.Way) { if ((_current as Way).HasNodeIn(_nodesToInclude)) { _waysToInclude.Add(_current.Id.Value); return(true); } } else if (_current.Type == OsmGeoType.Relation) { if ((_current as Relation).HasMemberIn(_nodesToInclude, _waysToInclude, _relationsToInclude)) { _relationsToInclude.Add(_current.Id.Value); // only one level of relations included. return(true); } } } } return(false); }
/// <summary> /// Move to the next object. /// </summary> public override bool MoveNext(bool ignoreNodes, bool ignoreWays, bool ignoreRelations) { OsmGeo source1Current = null; OsmGeo source2Current = null; // get currents or move to first. if (_source1Status == null) { _source1Status = _source1.MoveNext(ignoreNodes, ignoreWays, ignoreRelations); } if (_source1Status.Value) { source1Current = _source1.Current(); } if (_source2Status == null) { _source2Status = _source2.MoveNext(ignoreNodes, ignoreWays, ignoreRelations); } if (_source2Status.Value) { source2Current = _source2.Current(); } // compare currents and select next. OsmGeo newCurrent; if (source1Current == null && source2Current == null) { return(false); } else if (source1Current == null) { newCurrent = source2Current; _source2Status = _source2.MoveNext(ignoreNodes, ignoreWays, ignoreRelations); } else if (source2Current == null) { newCurrent = source1Current; _source1Status = _source1.MoveNext(ignoreNodes, ignoreWays, ignoreRelations); } else { var comp = source1Current.CompareByIdAndType(source2Current); if (comp == 0) { // oeps, conflict here! if (_resolutionType == ConflictResolutionType.None) { // no conflict resolution return both. newCurrent = source1Current; _source1Status = _source1.MoveNext(ignoreNodes, ignoreWays, ignoreRelations); } else if (_resolutionType == ConflictResolutionType.FirstStream) { // return only the object from the first stream. newCurrent = source1Current; _source1Status = _source1.MoveNext(ignoreNodes, ignoreWays, ignoreRelations); _source2Status = _source2.MoveNext(ignoreNodes, ignoreWays, ignoreRelations); } else { throw new NotImplementedException(string.Format("Conflict resolution {0} not implemented.", _resolutionType)); } } else if (comp < 0) { // return from first stream. newCurrent = source1Current; _source1Status = _source1.MoveNext(ignoreNodes, ignoreWays, ignoreRelations); } else { // return from second stream. newCurrent = source2Current; _source2Status = _source2.MoveNext(ignoreNodes, ignoreWays, ignoreRelations); } } // make sure the result is sorted. if (_current != null && _current.CompareByIdAndType(newCurrent) > 0) { throw new Exceptions.StreamNotSortedException(); } _current = newCurrent; return(true); }
public void TestMergeConflict() { var stream1 = new OsmGeo[] { new Node() { Id = 1, Version = 1 }, new Way() { Id = 1, Version = 1 }, new Relation() { Id = 1, Version = 1 } }; var stream2 = new OsmGeo[] { new Node() { Id = 1 }, new Node() { Id = 2 }, new Way() { Id = 1 }, new Way() { Id = 2 }, new Relation() { Id = 1 }, new Relation() { Id = 2 } }; var merge = new OsmStreamFilterMerge(); merge.RegisterSource(stream1); merge.RegisterSource(stream2); var result = new List <OsmGeo>(merge); Assert.AreEqual(6, result.Count); Assert.AreEqual(1, result[0].Id); Assert.AreEqual(1, result[0].Version); Assert.AreEqual(OsmGeoType.Node, result[0].Type); Assert.AreEqual(2, result[1].Id); Assert.AreEqual(OsmGeoType.Node, result[1].Type); Assert.AreEqual(1, result[2].Id); Assert.AreEqual(1, result[2].Version); Assert.AreEqual(OsmGeoType.Way, result[2].Type); Assert.AreEqual(2, result[3].Id); Assert.AreEqual(OsmGeoType.Way, result[3].Type); Assert.AreEqual(1, result[4].Id); Assert.AreEqual(1, result[4].Version); Assert.AreEqual(OsmGeoType.Relation, result[4].Type); Assert.AreEqual(2, result[5].Id); Assert.AreEqual(OsmGeoType.Relation, result[5].Type); }
public override bool MoveNext(bool ignoreNodes, bool ignoreWays, bool ignoreRelations) { this._nodesToExclude = new HashSet <long>(); this._waysToExclude = new HashSet <long>(); this._relationsToExclude = new HashSet <long>(); long?id; for (int index = 1; index < this._sources.Count; ++index) { while (this._sources[index].MoveNext()) { OsmGeo osmGeo = this._sources[index].Current(); switch (osmGeo.Type) { case OsmGeoType.Node: HashSet <long> nodesToExclude = this._nodesToExclude; id = osmGeo.Id; long num1 = id.Value; nodesToExclude.Add(num1); continue; case OsmGeoType.Way: HashSet <long> waysToExclude = this._waysToExclude; id = osmGeo.Id; long num2 = id.Value; waysToExclude.Add(num2); continue; case OsmGeoType.Relation: HashSet <long> relationsToExclude = this._relationsToExclude; id = osmGeo.Id; long num3 = id.Value; relationsToExclude.Add(num3); continue; default: continue; } } } while (this._sources[0].MoveNext(ignoreNodes, ignoreWays, ignoreRelations)) { OsmGeo osmGeo = this._sources[0].Current(); bool flag = false; switch (osmGeo.Type) { case OsmGeoType.Node: int num1; if (this._excludeNodes) { HashSet <long> nodesToExclude = this._nodesToExclude; id = osmGeo.Id; long num2 = id.Value; num1 = nodesToExclude.Contains(num2) ? 1 : 0; } else { num1 = 0; } flag = num1 != 0; break; case OsmGeoType.Way: int num3; if (this._excludeWays) { HashSet <long> waysToExclude = this._waysToExclude; id = osmGeo.Id; long num2 = id.Value; num3 = waysToExclude.Contains(num2) ? 1 : 0; } else { num3 = 0; } flag = num3 != 0; break; case OsmGeoType.Relation: int num4; if (this._excludeRelations) { HashSet <long> relationsToExclude = this._relationsToExclude; id = osmGeo.Id; long num2 = id.Value; num4 = relationsToExclude.Contains(num2) ? 1 : 0; } else { num4 = 0; } flag = num4 != 0; break; } if (!flag) { return(true); } } return(false); }
/// <summary> /// Stores an osmGeo object to disk. /// </summary> /// <param name="osmGeo"></param> private void Store(OsmGeo osmGeo) { XmlOsmStreamTarget target = new XmlOsmStreamTarget(this.StoreFileName(osmGeo)); target.RegisterSource(new OsmGeo[] { osmGeo }.ToOsmStreamSource()); target.Pull(); target.Flush(); }
/// <summary> /// Evaluates the filter against the osm object. /// </summary> /// <param name="obj"></param> /// <returns></returns> public abstract bool Evaluate(OsmGeo obj);
/// <summary> /// Initialiers this oracle source. /// </summary> public override void Initialize() { _connection = new OracleConnection(_connectionString); _connection.Open(); _current = null; _currentType = OsmGeoType.Node; _nodeReader = null; _nodeTagReader = null; }
/// <summary> /// Returns the current object. /// </summary> /// <returns></returns> public override OsmGeo Current() { OsmGeo current = this.Source.Current(); // keep the start ticks. long ticksStart = DateTime.Now.Ticks; if (!_lastType.HasValue) { // has a last type. _lastTypeStart = DateTime.Now.Ticks; _lastType = current.Type; } if (_lastType != current.Type) { // the last type has changed. long lastTicks = ticksStart - _lastTypeStart; switch (_lastType) { case OsmGeoType.Node: _nodeTicks = _nodeTicks + lastTicks; break; case OsmGeoType.Way: _wayTicks = _wayTicks + lastTicks; break; case OsmGeoType.Relation: _relationTicks = _relationTicks + lastTicks; break; } // start new ticks. _lastTypeStart = DateTime.Now.Ticks; _lastType = current.Type; } switch (current.Type) { case OsmGeoType.Node: _node++; if ((_node % _nodeInterval) == 0) { var nodeSpan = new TimeSpan(_nodeTicks + (ticksStart - _lastTypeStart)); var nodePerSecond = System.Math.Round((double)_node / nodeSpan.TotalSeconds, 0); OsmSharp.Logging.Log.TraceEvent("StreamProgress", TraceEventType.Information, "Pass {2} - Node[{0}] @ {1}/s", _node, nodePerSecond, _pass); } break; case OsmGeoType.Relation: _relation++; if ((_relation % _relationInterval) == 0) { var relationSpan = new TimeSpan(_relationTicks + (ticksStart - _lastTypeStart)); var relationPerSecond = System.Math.Round((double)_relation / relationSpan.TotalSeconds, 2); OsmSharp.Logging.Log.TraceEvent("StreamProgress", TraceEventType.Information, "Pass {2} - Relation[{0}] @ {1}/s", _relation, relationPerSecond, _pass); } break; case OsmGeoType.Way: _way++; if ((_way % _wayInterval) == 0) { var waySpan = new TimeSpan(_wayTicks + (ticksStart - _lastTypeStart)); var wayPerSecond = System.Math.Round((double)_way / waySpan.TotalSeconds, 2); OsmSharp.Logging.Log.TraceEvent("StreamProgress", TraceEventType.Information, "Pass {2} - Way[{0}] @ {1}/s", _way, wayPerSecond, _pass); } break; } return(current); }
private bool DoMoveNextWay() { 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; _currentType = OsmGeoType.Relation; return false; } }
/// <summary> /// Before interpretation; remove the object. /// </summary> /// <param name="obj"></param> public void OnBeforeInterpretation(OsmGeo obj) { _layer.RemoveElements(obj); }
/// <summary> /// Adds elements for the given geo object. /// </summary> /// <param name="geo"></param> /// <param name="elements"></param> internal void AddElements(OsmGeo geo, IList<IElement> elements) { long id = geo.Id; // add list. if (_elements_per_object.ContainsKey(id)) { } else { // add new list for this object. _elements_per_object.Add(id, new List<IElement>()); } // add elements. if (elements != null && elements.Count > 0) { _elements.AddRange(elements); _elements_per_object[id].AddRange(elements); } }
public void TestOverlappingWaysResidentialCycleway() { // the input osm-data. var osmGeos = new OsmGeo[] { new Node() { Id = 1, Latitude = 51.04963322083945f, Longitude = 3.719692826271057f }, new Node() { Id = 2, Latitude = 51.05062804602733f, Longitude = 3.7198376655578613f }, new Way() { Id = 1, Nodes = new long[] { 1, 2 }, Tags = new TagsCollection( new Tag("highway", "residential"), new Tag("bicycle", "no")) }, new Way() { Id = 2, Nodes = new long[] { 1, 2 }, Tags = new TagsCollection( new Tag("highway", "cycleway")) } }; // build router db. var routerDb = new RouterDb(); routerDb.LoadOsmData(osmGeos, Vehicle.Car, Vehicle.Bicycle, Vehicle.Pedestrian); // test some routes. var router = new Router(routerDb); // confirm it's working for cars. var route = router.TryCalculate(Vehicle.Car.Fastest(), new Coordinate(51.04963322083945f, 3.719692826271057f), new Coordinate(51.05062804602733f, 3.7198376655578613f)); Assert.IsFalse(route.IsError); route = router.TryCalculate(Vehicle.Car.Fastest(), new Coordinate(51.05062804602733f, 3.7198376655578613f), new Coordinate(51.04963322083945f, 3.719692826271057f)); Assert.IsFalse(route.IsError); // confirm it's working for bicycle. route = router.TryCalculate(Vehicle.Bicycle.Fastest(), new Coordinate(51.04963322083945f, 3.719692826271057f), new Coordinate(51.05062804602733f, 3.7198376655578613f)); Assert.IsFalse(route.IsError); route = router.TryCalculate(Vehicle.Bicycle.Fastest(), new Coordinate(51.05062804602733f, 3.7198376655578613f), new Coordinate(51.04963322083945f, 3.719692826271057f)); Assert.IsFalse(route.IsError); }
/// <summary> /// Moves this source to the next object. /// </summary> /// <returns></returns> public override bool MoveNext() { while (_reader.Read()) { if (_reader.NodeType == XmlNodeType.Element && (_reader.Name == "node" || _reader.Name == "way" || _reader.Name == "relation")) { // create a stream for only this element. string name = _reader.Name; string nextElement = _reader.ReadOuterXml(); XmlReader reader = XmlReader.Create(new MemoryStream(Encoding.UTF8.GetBytes(nextElement))); object osmObj = null; // select type of element. switch (name) { case "node": osmObj = _serNode.Deserialize(reader); if (osmObj is OsmSharp.Osm.Xml.v0_6.node) { _next = XmlSimpleConverter.ConvertToSimple(osmObj as OsmSharp.Osm.Xml.v0_6.node); return true; } break; case "way": osmObj = _serWay.Deserialize(reader); if (osmObj is OsmSharp.Osm.Xml.v0_6.way) { _next = XmlSimpleConverter.ConvertToSimple(osmObj as OsmSharp.Osm.Xml.v0_6.way); return true; } break; case "relation": osmObj = _serRelation.Deserialize(reader); if (osmObj is OsmSharp.Osm.Xml.v0_6.relation) { _next = XmlSimpleConverter.ConvertToSimple(osmObj as OsmSharp.Osm.Xml.v0_6.relation); return true; } break; } } } _next = null; return false; }
private bool DoMoveNextNode() { if (_nodeReader == 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; _nodeReader = node_command.ExecuteReader(); if (!_nodeReader.Read()) { _nodeReader.Close(); } } // read next node. if (!_nodeReader.IsClosed) { // load/parse data. Node node = new Node(); node.Id = _nodeReader.GetInt64(0); node.Latitude = _nodeReader.GetInt64(1) / 10000000.0; node.Longitude = _nodeReader.GetInt64(2) / 10000000.0; node.ChangeSetId = _nodeReader.GetInt64(3); node.TimeStamp = _nodeReader.GetDateTime(5); node.Version = (ulong)_nodeReader.GetInt64(7); node.Visible = _nodeReader.GetInt64(4) == 1; //node.UserName = _node_reader.GetString(8); //node.UserId = _node_reader.IsDBNull(9) ? -1 : _node_reader.GetInt64(9); //Has tags? if (!_nodeReader.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 (!_nodeReader.Read()) { _nodeReader.Close(); break; } currentnode = _nodeReader.GetInt64(0); } } else if (!_nodeReader.Read()) { _nodeReader.Close(); } // set the current variable! _current = node; return(true); } _nodeReader.Close(); _nodeReader.Dispose(); _nodeReader = null; _currentType = OsmGeoType.Way; return(false); }
public void TestRestrictionViaNode() { var location1 = new Coordinate(51.265137311403734f, 4.783644676208496f); var location2 = new Coordinate(51.264425704628850f, 4.784331321716309f); var location3 = new Coordinate(51.263975909757960f, 4.784958958625793f); var location4 = new Coordinate(51.264251157888160f, 4.785377383232116f); var location5 = new Coordinate(51.264758012937406f, 4.785699248313904f); var osmGeos = new OsmGeo[] { new Node() { Id = 1, Latitude = location1.Latitude, Longitude = location1.Longitude }, new Node() { Id = 2, Latitude = location2.Latitude, Longitude = location2.Longitude }, new Node() { Id = 3, Latitude = location3.Latitude, Longitude = location3.Longitude }, new Node() { Id = 4, Latitude = location4.Latitude, Longitude = location4.Longitude }, new Node() { Id = 5, Latitude = location5.Latitude, Longitude = location5.Longitude }, new Way() { Id = 1, Nodes = new long[] { 1, 2 }, Tags = new TagsCollection( new Tag("highway", "residential")) }, new Way() { Id = 2, Nodes = new long[] { 2, 3 }, Tags = new TagsCollection( new Tag("highway", "residential")) }, new Way() { Id = 3, Nodes = new long[] { 3, 4 }, Tags = new TagsCollection( new Tag("highway", "residential")) }, new Way() { Id = 4, Nodes = new long[] { 4, 5 }, Tags = new TagsCollection( new Tag("highway", "residential")) }, new Relation() { Id = 1, Members = new RelationMember[] { new RelationMember() { Id = 3, Role = "via", Type = OsmGeoType.Node }, new RelationMember() { Id = 2, Role = "from", Type = OsmGeoType.Way }, new RelationMember() { Id = 3, Role = "to", Type = OsmGeoType.Way } }, Tags = new TagsCollection( new Tag("type", "restriction"), new Tag("restriction", "no_right_turn")) } }; // build router db. var routerDb = new RouterDb(); routerDb.LoadOsmData(osmGeos, false, true, Vehicle.Car); // test some routes. var router = new Router(routerDb); // confirm it's working for cars only in one direction. var route = router.TryCalculate(Vehicle.Car.Fastest(), location1, location5); Assert.IsTrue(route.IsError); route = router.TryCalculate(Vehicle.Car.Fastest(), location5, location1); Assert.IsFalse(route.IsError); }
private bool DoMoveNextWay() { if (_wayReader == null) { SQLiteCommand way_command = new SQLiteCommand("select * from way where id > 26478817 order by id"); way_command.Connection = _connection; _wayReader = way_command.ExecuteReader(); if (!_wayReader.Read()) { _wayReader.Close(); } SQLiteCommand way_tag_command = new SQLiteCommand("select * from way_tags where way_id > 26478817 order by way_id"); way_tag_command.Connection = _connection; _wayTagReader = way_tag_command.ExecuteReader(); if (!_wayTagReader.IsClosed && !_wayTagReader.Read()) { _wayTagReader.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; _wayNodeReader = way_node_command.ExecuteReader(); if (!_wayNodeReader.IsClosed && !_wayNodeReader.Read()) { _wayNodeReader.Close(); } } // read next way. if (!_wayReader.IsClosed) { Way way = new Way(); way.Id = _wayReader.GetInt64(0); way.ChangeSetId = _wayReader.GetInt64(1); way.TimeStamp = _wayReader.IsDBNull(3) ? DateTime.MinValue : _wayReader.GetDateTime(3); //way.UserId = _way_reader.GetInt64(6); //way.UserName = _way_reader.GetString(5); way.Version = (ulong)_wayReader.GetInt64(4); way.Visible = _wayReader.GetInt64(2) == 1; 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; _currentType = OsmGeoType.Relation; return(false); } }
private bool DoMoveNextNode() { 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; _currentType = OsmGeoType.Way; 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) { return(obj.Tags.ContainsKey(_tag)); }
/// <summary> /// Evaluates the filter against the osm object. /// </summary> /// <param name="obj"></param> /// <returns></returns> public bool Evaluate(OsmGeo obj) { return this.Evaluate(obj.ToSimple()); }
public Key(OsmGeo osmGeo) : this(osmGeo.Id.Value, osmGeo.Version.Value) { }
/// <summary> /// Returns all relations containg the given object as a member. /// </summary> /// <param name="obj"></param> /// <returns></returns> public IList<Relation> GetRelationsFor(OsmGeo obj) { return this.GetRelationsFor(obj.Type, obj.Id.Value); }
/// <summary> /// Resets this filter. /// </summary> public override void Reset() { _current = null; this.Source.Reset(); }
/// <summary> /// Returns the storage file name for this given object. /// </summary> /// <param name="osmGeo"></param> /// <returns></returns> private string StoreFileName(OsmGeo osmGeo) { return this.StoreFileName(osmGeo.Id.Value, osmGeo.Type); }
/// <summary> /// Moves this filter to the next object. /// </summary> /// <returns></returns> private bool DoMoveNext() { _current = null; while (_current == null) { if(!this.Source.MoveNext()) { // source is finished. return false; } _current = this.Source.Current(); if (this.MovedToNextEvent != null) { _current = this.MovedToNextEvent(_current, _param); } } return true; }
/// <summary> /// Resets the current source. /// </summary> public override void Reset() { _current = null; _currentType = OsmGeoType.Node; if (_nodeReader != null) { _nodeReader.Close(); _nodeReader.Dispose(); _nodeReader = null; } if (_nodeTagReader != null) { _nodeTagReader.Close(); _nodeTagReader.Dispose(); _nodeTagReader = null; } }
public override bool Evaluate(OsmGeo obj) { return(true); }
private bool DoMoveNextRelation() { if (_relationReader == null) { OracleCommand relation_command = new OracleCommand("select * from relation order by id"); relation_command.Connection = _connection; _relationReader = relation_command.ExecuteReader(); if (!_relationReader.Read()) { _relationReader.Close(); } OracleCommand relation_tag_command = new OracleCommand("select * from relation_tags order by relation_id"); relation_tag_command.Connection = _connection; _relationTagReader = relation_tag_command.ExecuteReader(); if (!_relationTagReader.IsClosed && !_relationTagReader.Read()) { _relationTagReader.Close(); } OracleCommand relation_node_command = new OracleCommand("select * from relation_members order by relation_id,sequence_id"); relation_node_command.Connection = _connection; _relationMemberReader = relation_node_command.ExecuteReader(); if (!_relationMemberReader.IsClosed && !_relationMemberReader.Read()) { _relationMemberReader.Close(); } } // read next relation. if (!_relationReader.IsClosed) { // load/parse data. long id = _relationReader.GetInt64(0); long changeset_id = _relationReader.GetInt64(1); DateTime timestamp = _relationReader.GetDateTime(2); bool visible = _relationReader.GetInt64(3) == 1; long version = _relationReader.GetInt64(4); string user = _relationReader.GetString(5); long uid = _relationReader.GetInt64(6); Relation relation = new Relation(); relation.Id = id; relation.ChangeSetId = changeset_id; relation.TimeStamp = timestamp; relation.UserId = null; relation.UserName = null; relation.Version = (ulong)version; relation.Visible = visible; relation.UserName = user; relation.UserId = uid; if (!_relationTagReader.IsClosed) { long returned_id = _relationTagReader.GetInt64(0); while (returned_id == relation.Id.Value) { if (relation.Tags == null) { relation.Tags = new TagsCollection(); } string key = _relationTagReader.GetString(1); string value = _relationTagReader.GetString(2); relation.Tags.Add(key, value); if (!_relationTagReader.Read()) { _relationTagReader.Close(); returned_id = -1; } else { returned_id = _relationTagReader.GetInt64(0); } } } if (!_relationMemberReader.IsClosed) { long returned_id = _relationMemberReader.GetInt64(0); while (returned_id == relation.Id.Value) { if (relation.Members == null) { relation.Members = new List<RelationMember>(); } string member_type = _relationMemberReader.GetString(1); long member_id = _relationMemberReader.GetInt64(2); object member_role = _relationMemberReader.GetValue(3); RelationMember member = new RelationMember(); member.MemberId = member_id; if (member_role != DBNull.Value) { member.MemberRole = member_role as string; } switch (member_type) { 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 (!_relationMemberReader.Read()) { _relationMemberReader.Close(); returned_id = -1; } else { returned_id = _relationMemberReader.GetInt64(0); } } } // set the current variable! _current = relation; // advance the reader(s). if (!_relationReader.Read()) { _relationReader.Close(); } if (!_relationTagReader.IsClosed && !_relationTagReader.Read()) { _relationTagReader.Close(); } if (!_relationMemberReader.IsClosed && !_relationMemberReader.Read()) { _relationMemberReader.Close(); } return true; } else { _relationReader.Close(); _relationReader.Dispose(); _relationReader = null; _relationTagReader.Close(); _relationTagReader.Dispose(); _relationTagReader = null; _currentType = OsmGeoType.Relation; return false; } }
/// <summary> /// Returns the current object. /// </summary> /// <returns></returns> public override OsmGeo Current() { OsmGeo current = this.Source.Current(); // keep the start ticks. long ticksStart = DateTime.Now.Ticks; if (!_lastType.HasValue) { // has a last type. _lastTypeStart = DateTime.Now.Ticks; _lastType = current.Type; } if (_lastType != current.Type) { // the last type has changed. long lastTicks = ticksStart - _lastTypeStart; switch (_lastType) { case OsmGeoType.Node: _nodeTicks = _nodeTicks + lastTicks; break; case OsmGeoType.Way: _wayTicks = _wayTicks + lastTicks; break; case OsmGeoType.Relation: _relationTicks = _relationTicks + lastTicks; break; } // start new ticks. _lastTypeStart = DateTime.Now.Ticks; _lastType = current.Type; } switch (current.Type) { case OsmGeoType.Node: _node++; if ((_node % 10000) == 0) { TimeSpan nodeSpan = new TimeSpan(_nodeTicks + (ticksStart - _lastTypeStart)); double nodePerSecond = System.Math.Round((double)_node / nodeSpan.TotalSeconds, 2); OsmSharp.Logging.Log.TraceEvent("OsmSharp.Osm.Data.Streams.Filters.OsmStreamFilterProgress", TraceEventType.Information, "Node[{0}]: {1}nodes/s", _node, nodePerSecond); } break; case OsmGeoType.Relation: _relation++; if ((_relation % 1000) == 0) { TimeSpan relationSpan = new TimeSpan(_relationTicks + (ticksStart - _lastTypeStart)); double relationPerSecond = System.Math.Round((double)_relation / relationSpan.TotalSeconds, 2); OsmSharp.Logging.Log.TraceEvent("OsmSharp.Osm.Data.Streams.Filters.OsmStreamFilterProgress", TraceEventType.Information, "Relation[{0}]: {1}relations/s", _relation, relationPerSecond); } break; case OsmGeoType.Way: _way++; if ((_way % 10000) == 0) { TimeSpan waySpan = new TimeSpan(_wayTicks + (ticksStart - _lastTypeStart)); double wayPerSecond = System.Math.Round((double)_way / waySpan.TotalSeconds, 2); OsmSharp.Logging.Log.TraceEvent("OsmSharp.Osm.Data.Streams.Filters.OsmStreamFilterProgress", TraceEventType.Information, "Way[{0}]: {1}ways/s", _way, wayPerSecond); } break; } return(current); }
/// <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> /// Resets this filter. /// </summary> public override void Reset() { _current = null; this.Source.Reset(); }
/// <summary> /// Removes all elements for the given geo object in all the sub layers. /// </summary> /// <param name="geo"></param> /// <param name="elements"></param> internal void RemoveElements(OsmGeo geo) { foreach (DataSourceSubLayer layer in _layers) { layer.RemoveElements(geo); } }
// TODO: investigate lambda usage for this! /// <summary> /// Returns true if the given object has to be kept. /// </summary> /// <param name="osmGeo"></param> /// <returns></returns> public abstract bool Include(OsmGeo osmGeo);
/// <summary> /// Removes all elements for the given geo object. /// </summary> /// <param name="geo"></param> /// <param name="elements"></param> internal void RemoveElements(OsmGeo geo) { long id = geo.Id; // clear the list for the object. foreach (IElement element in _elements_per_object[id]) { _elements.Remove(element); } _elements_per_object.Remove(id); }
/// <summary> /// Returns true if the given object is an object that needs to be included. /// </summary> /// <param name="currentSimple"></param> /// <returns></returns> private bool IsChild(OsmGeo currentSimple) { switch (currentSimple.Type) { case OsmGeoType.Node: return _nodesToInclude.Contains(currentSimple.Id.Value) || _dataCache.ContainsNode(currentSimple.Id.Value); case OsmGeoType.Way: return _waysToInclude.Contains(currentSimple.Id.Value) || _dataCache.ContainsWay(currentSimple.Id.Value); case OsmGeoType.Relation: return _relationsToInclude.Contains(currentSimple.Id.Value) || _dataCache.ContainsRelation(currentSimple.Id.Value); } return false; }
/// <summary> /// Initializes this source. /// </summary> public override void Initialize() { _next = null; _serNode = new XmlSerializer(typeof(Osm.Xml.v0_6.node)); _serWay = new XmlSerializer(typeof(Osm.Xml.v0_6.way)); _serRelation = new XmlSerializer(typeof(Osm.Xml.v0_6.relation)); this.Reset(); }
public void TestWriteNode() { // build source stream. var sourceNode = new Node() { Id = 1, Latitude = 1.1f, Longitude = 1.2f, ChangeSetId = 1092, TimeStamp = DateTime.UnixEpoch, UserId = 9034, Version = 12 }; var sourceObjects = new OsmGeo[] { sourceNode }; // build pfb stream target. using (var stream = new MemoryStream()) { var target = new PBFOsmStreamTarget(stream); target.RegisterSource(sourceObjects); 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(1092, resultObjects[0].ChangeSetId); Assert.AreEqual(OsmSharp.IO.PBF.PBFExtensions.FromUnixTime(0), resultObjects[0].TimeStamp); Assert.AreEqual(9034, resultObjects[0].UserId); Assert.AreEqual(sourceObjects[0].UserName, resultObjects[0].UserName); Assert.AreEqual(12, resultObjects[0].Version); var resultNode = resultObjects[0] as Node; Assert.AreEqual(sourceNode.Latitude.Value, resultNode.Latitude.Value, .0001f); Assert.AreEqual(sourceNode.Longitude.Value, resultNode.Longitude.Value, .0001f); } // build source stream. sourceNode = new Node() { Id = 1, Latitude = 1.1f, Longitude = 1.2f, ChangeSetId = 1092, TimeStamp = DateTime.UnixEpoch, UserId = 9034, Version = 12 }; sourceNode.Tags = new TagsCollection(); sourceNode.Tags.Add("highway", "residential"); sourceObjects = new OsmGeo[] { sourceNode }; // build pfb stream target. using (var stream = new MemoryStream()) { var target = new PBFOsmStreamTarget(stream); target.RegisterSource(sourceObjects); 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(1092, resultObjects[0].ChangeSetId); Assert.AreEqual(OsmSharp.IO.PBF.PBFExtensions.FromUnixTime(0), resultObjects[0].TimeStamp); Assert.AreEqual(9034, resultObjects[0].UserId); Assert.AreEqual(string.Empty, resultObjects[0].UserName); Assert.AreEqual(12, 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); } // build source stream. sourceNode = new Node() { Id = 1, Latitude = 1.1f, Longitude = 1.2f, ChangeSetId = 1092, TimeStamp = DateTime.UnixEpoch, UserId = 9034, Version = 12 }; 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; sourceObjects = new OsmGeo[] { sourceNode }; // build pfb stream target. using (var stream = new MemoryStream()) { var target = new PBFOsmStreamTarget(stream); target.RegisterSource(sourceObjects); 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 resultNode = resultObjects[0] as Node; Assert.AreEqual(sourceNode.Latitude.Value, resultNode.Latitude.Value, .0001f); Assert.AreEqual(sourceNode.Longitude.Value, resultNode.Longitude.Value, .0001f); } }
/// <summary> /// Adds a geometry to the target /// </summary> public void Add(OsmGeo geo) { if (geo == null) { throw new NullReferenceException(); } if (geo.Type == OsmGeoType.Node) { AddNode(geo as Node); } else if (geo.Type == OsmGeoType.Way) { AddWay(geo as Way); } else if (geo.Type == OsmGeoType.Relation) { AddRelation(geo as Relation); } }
/// <summary> /// Returns all relations containg the given object as a member. /// </summary> /// <param name="obj"></param> /// <returns></returns> public IList <Relation> GetRelationsFor(OsmGeo obj) { return(this.GetRelationsFor(obj.Type, obj.Id.Value)); }
/// <summary> /// Moves to the next object. /// </summary> /// <returns></returns> private bool DoMoveNext() { if (!_includeExtraMode) { // just go over all nodes and ways. if (this.Source.MoveNext()) { bool finished = false; bool isIn = false; // move to the next object of the current type. while (this.Current().Type != _currentType) { if (!this.Source.MoveNext()) { finished = true; break; } } if (!finished) { while (this.Current().Type == _currentType && !isIn) { OsmGeo current = this.Source.Current(); isIn = this.IsInBB(current); // check and keep the extras. if (isIn) { // add to the actual in-boundingbox indexes. switch (current.Type) { case OsmGeoType.Node: _nodesIn.Add(current.Id.Value); break; case OsmGeoType.Way: _waysIn.Add(current.Id.Value); break; case OsmGeoType.Relation: _relationIn.Add(current.Id.Value); break; } break; } // move to the next object of the current type. if (!this.Source.MoveNext()) { finished = true; break; } while (this.Current().Type != _currentType) { if (!this.Source.MoveNext()) { finished = true; break; } } // stop when finished. if (finished) { break; } } } if (!finished && this.Current().Type == _currentType) { // nothing was finished and the types match. return(true); } } // switch to the next mode. switch (_currentType) { case OsmGeoType.Node: this.Source.Reset(); _currentType = OsmGeoType.Way; return(this.MoveNext()); case OsmGeoType.Way: this.Source.Reset(); _currentType = OsmGeoType.Relation; return(this.MoveNext()); case OsmGeoType.Relation: this.Source.Reset(); _includeExtraMode = true; return(this.MoveNext()); } throw new InvalidOperationException("Unkown SimpleOsmGeoType"); } else { while (this.Source.MoveNext()) { switch (this.Source.Current().Type) { case OsmGeoType.Node: if (_nodesToInclude.Contains(this.Source.Current().Id.Value)) { if (!_nodesIn.Contains(this.Source.Current().Id.Value)) { return(true); } } break; case OsmGeoType.Way: if (_waysToInclude.Contains(this.Source.Current().Id.Value)) { if (!_waysIn.Contains(this.Source.Current().Id.Value)) { return(true); } } break; case OsmGeoType.Relation: if (_relationsToInclude.Contains(this.Source.Current().Id.Value)) { if (!_relationIn.Contains(this.Source.Current().Id.Value)) { return(true); } } break; } } return(false); } }
/// <summary> /// Reads an OSM object starting at the stream's current position. /// </summary> public static OsmGeo ReadOsmGeo(this Stream stream, byte[] buffer) { if (stream.CanSeek && stream.Length == stream.Position) { return(null); } if (!stream.TryReadOsmGeoHeader(out var type, out var hasId, out var hasChangesetId, out var hasTimestamp, out var hasUserId, out var hasVersion, out var hasVisible)) { // couldn't read header. return(null); } // read the basics. long?id = null; if (hasId) { id = stream.ReadInt64(buffer); } long?changesetId = null; if (hasChangesetId) { changesetId = stream.ReadInt64(buffer); } DateTime?timestamp = null; if (hasTimestamp) { timestamp = stream.ReadDateTime(buffer); } long?userId = null; if (hasUserId) { userId = stream.ReadInt64(buffer); } var username = stream.ReadWithSizeString(buffer); int?version = null; if (hasVersion) { version = stream.ReadInt32(buffer); } bool?visible = null; if (hasVisible) { visible = stream.ReadBool(); } // read tags. var tagsCount = stream.ReadInt32(buffer); TagsCollection tags = null; if (tagsCount > 0) { tags = new TagsCollection(tagsCount); for (var i = 0; i < tagsCount; i++) { var key = stream.ReadWithSizeString(buffer); var value = stream.ReadWithSizeString(buffer); tags.AddOrReplace(key, value); } } OsmGeo osmGeo = null; switch (type) { case OsmGeoType.Node: osmGeo = stream.ReadNode(buffer); break; case OsmGeoType.Way: osmGeo = stream.ReadWay(buffer); break; case OsmGeoType.Relation: default: osmGeo = stream.ReadRelation(buffer); break; } osmGeo.Id = id; osmGeo.ChangeSetId = changesetId; osmGeo.TimeStamp = timestamp; osmGeo.UserId = userId; osmGeo.UserName = username; osmGeo.Version = version; osmGeo.Visible = visible; osmGeo.Tags = tags; return(osmGeo); }
/// <summary> /// Moves to the next object. /// </summary> /// <returns></returns> public override bool MoveNext() { if (!_cachingDone) { // first seek & cache. this.Seek(); this.CacheRelations(); _cachingDone = true; } if (this.Source.MoveNext()) { // there is data. OsmGeo currentSimple = this.Source.Current(); // make sure the object needs to be included. while (! this.IsChild(currentSimple) && !this.Include(currentSimple)) { // don't include this object! if (!this.Source.MoveNext()) { // oeps no more data! return false; } currentSimple = this.Source.Current(); } switch (currentSimple.Type) { case OsmGeoType.Node: // create complete version. _current = currentSimple; if (_nodesToInclude.Contains(currentSimple.Id.Value)) { // node needs to be cached. _dataCache.AddNode(currentSimple as Node); _nodesToInclude.Remove(currentSimple.Id.Value); } break; case OsmGeoType.Way: // create complete way. _current = currentSimple; if (_waysToInclude.Contains(currentSimple.Id.Value)) { // keep the way because it is needed later on. _dataCache.AddWay(currentSimple as Way); _waysToInclude.Remove(currentSimple.Id.Value); } else { // only report that the nodes have been used when the way can be let-go. Way way = currentSimple as Way; if (way.Nodes != null) { // report usage. way.Nodes.ForEach(x => this.ReportNodeUsage(x)); } } break; case OsmGeoType.Relation: // create complate relation. _current = currentSimple; if(!_relationsToInclude.Contains(currentSimple.Id.Value)) { // only report relation usage when the relation can be let go. Relation relation = currentSimple as Relation; if (relation.Members != null) { foreach (RelationMember member in relation.Members) { switch (member.MemberType.Value) { case OsmGeoType.Node: this.ReportNodeUsage(member.MemberId.Value); break; case OsmGeoType.Way: this.ReportWayUsage(member.MemberId.Value); break; case OsmGeoType.Relation: this.ReportRelationUsage(member.MemberId.Value); break; } } } } break; } return true; } return false; }
/// <summary> /// Returns true if the given object is relevant in the bounding box. /// </summary> /// <param name="osmGeo"></param> /// <returns></returns> private bool IsInBB(OsmGeo osmGeo) { bool isIn = false; switch (osmGeo.Type) { case OsmGeoType.Node: isIn = _box.Contains(new GeoCoordinate( (osmGeo as Node).Latitude.Value, (osmGeo as Node).Longitude.Value)); break; case OsmGeoType.Way: foreach (long nodeId in (osmGeo as Way).Nodes) { if (_nodesIn.Contains(nodeId)) { isIn = true; break; } } if (isIn) { foreach (long nodeId in (osmGeo as Way).Nodes) { _nodesToInclude.Add(nodeId); } } break; case OsmGeoType.Relation: if (!_relationsConsidered.Contains(osmGeo.Id.Value)) { foreach (RelationMember member in (osmGeo as Relation).Members) { switch (member.MemberType.Value) { case OsmGeoType.Node: if (_nodesIn.Contains(member.MemberId.Value)) { isIn = true; break; } break; case OsmGeoType.Way: if (_waysIn.Contains(member.MemberId.Value)) { isIn = true; break; } break; case OsmGeoType.Relation: if (_relationIn.Contains(member.MemberId.Value)) { isIn = true; break; } break; } } if (isIn) { foreach (RelationMember member in (osmGeo as Relation).Members) { switch (member.MemberType.Value) { case OsmGeoType.Node: _nodesToInclude.Add(member.MemberId.Value); break; case OsmGeoType.Way: _waysToInclude.Add(member.MemberId.Value); break; case OsmGeoType.Relation: _relationsToInclude.Add(member.MemberId.Value); break; } } } } break; } return(isIn); }
public void TestCreate() { var source = new OsmGeo[] { new Node() { Id = 2, Version = 1 }, new Way() { Id = 3, Version = 2 }, new Relation() { Id = 3, Version = 2 } }; var changeset = new OsmChange() { Create = new OsmGeo[] { new Node() { Id = 1, Version = 1 }, new Node() { Id = 3, Version = 1 }, new Way() { Id = 2, Version = 1 }, new Relation() { Id = 4, Version = 1 } } }; var filter = new OsmStreamFilterApplyChangeset(changeset); filter.RegisterSource(source); var result = new List <OsmGeo>(filter); Assert.AreEqual(7, result.Count); Assert.AreEqual(1, result[0].Id); Assert.AreEqual(OsmGeoType.Node, result[0].Type); Assert.AreEqual(2, result[1].Id); Assert.AreEqual(OsmGeoType.Node, result[1].Type); Assert.AreEqual(3, result[2].Id); Assert.AreEqual(OsmGeoType.Node, result[2].Type); Assert.AreEqual(2, result[3].Id); Assert.AreEqual(OsmGeoType.Way, result[3].Type); Assert.AreEqual(3, result[4].Id); Assert.AreEqual(OsmGeoType.Way, result[4].Type); Assert.AreEqual(3, result[5].Id); Assert.AreEqual(OsmGeoType.Relation, result[5].Type); Assert.AreEqual(4, result[6].Id); Assert.AreEqual(OsmGeoType.Relation, result[6].Type); }
public void TestWriteRelation() { // build source stream. var sourceRelation = new Relation() { Id = 1, Members = new RelationMember[] { new RelationMember() { Id = 1, Role = "fake role", Type = OsmGeoType.Node }, new RelationMember() { Id = 1, Role = "fake role", Type = OsmGeoType.Way } } }; var sourceObjects = new OsmGeo[] { sourceRelation }; // build pfb stream target. using (var stream = new MemoryStream()) { var target = new PBFOsmStreamTarget(stream); target.RegisterSource(sourceObjects); 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(OsmSharp.IO.PBF.PBFExtensions.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.Length, resultRelation.Members.Length); Assert.AreEqual(sourceRelation.Members[0].Id, resultRelation.Members[0].Id); Assert.AreEqual(sourceRelation.Members[0].Role, resultRelation.Members[0].Role); Assert.AreEqual(sourceRelation.Members[0].Type, resultRelation.Members[0].Type); Assert.AreEqual(sourceRelation.Members[1].Id, resultRelation.Members[1].Id); Assert.AreEqual(sourceRelation.Members[1].Role, resultRelation.Members[1].Role); Assert.AreEqual(sourceRelation.Members[1].Type, resultRelation.Members[1].Type); } // build source stream. sourceRelation = new Relation() { Id = 1, Members = new RelationMember[] { new RelationMember() { Id = 1, Role = "fake role", Type = OsmGeoType.Node }, new RelationMember() { Id = 1, Role = "fake role", Type = 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); 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(OsmSharp.IO.PBF.PBFExtensions.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.Length, resultRelation.Members.Length); Assert.AreEqual(sourceRelation.Members[0].Id, resultRelation.Members[0].Id); Assert.AreEqual(sourceRelation.Members[0].Role, resultRelation.Members[0].Role); Assert.AreEqual(sourceRelation.Members[0].Type, resultRelation.Members[0].Type); Assert.AreEqual(sourceRelation.Members[1].Id, resultRelation.Members[1].Id); Assert.AreEqual(sourceRelation.Members[1].Role, resultRelation.Members[1].Role); Assert.AreEqual(sourceRelation.Members[1].Type, resultRelation.Members[1].Type); } // build source stream. sourceRelation = new Relation() { Id = 1, Members = new RelationMember[] { new RelationMember() { Id = 1, Role = "fake role", Type = OsmGeoType.Node }, new RelationMember() { Id = 1, Role = "fake role", Type = 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); 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.Length, resultRelation.Members.Length); Assert.AreEqual(sourceRelation.Members[0].Id, resultRelation.Members[0].Id); Assert.AreEqual(sourceRelation.Members[0].Role, resultRelation.Members[0].Role); Assert.AreEqual(sourceRelation.Members[0].Type, resultRelation.Members[0].Type); Assert.AreEqual(sourceRelation.Members[1].Id, resultRelation.Members[1].Id); Assert.AreEqual(sourceRelation.Members[1].Role, resultRelation.Members[1].Role); Assert.AreEqual(sourceRelation.Members[1].Type, resultRelation.Members[1].Type); } }