/// <summary> /// Tests a simple node read/write operation. /// </summary> /// <param name="cache"></param> public void DoOsmDataCacheTestNode(OsmDataCache cache) { Node node = Node.Create(1, new TagsCollection( Tag.Create("node", "yes")), 1, 2); // test invalid stuff. Assert.Throws <ArgumentNullException>(() => cache.AddNode(null)); Assert.Throws <Exception>(() => cache.AddNode(new Node())); Assert.IsNull(cache.GetNode(node.Id.Value)); cache.AddNode(node); Assert.IsTrue(cache.ContainsNode(node.Id.Value)); Node readNode = cache.GetNode(node.Id.Value); Assert.IsNotNull(readNode); Assert.AreEqual(1, readNode.Id.Value); Assert.AreEqual(1, readNode.Latitude.Value); Assert.AreEqual(2, readNode.Longitude.Value); Assert.IsNotNull(node.Tags); Assert.AreEqual(1, node.Tags.Count); Assert.AreEqual("yes", node.Tags["node"]); Assert.IsTrue(cache.TryGetNode(node.Id.Value, out readNode)); Assert.IsNotNull(readNode); Assert.AreEqual(1, readNode.Id.Value); Assert.AreEqual(1, readNode.Latitude.Value); Assert.AreEqual(2, readNode.Longitude.Value); Assert.IsNotNull(node.Tags); Assert.AreEqual(1, node.Tags.Count); Assert.AreEqual("yes", node.Tags["node"]); Assert.IsTrue(cache.RemoveNode(node.Id.Value)); Assert.IsFalse(cache.ContainsNode(node.Id.Value)); Assert.IsFalse(cache.RemoveNode(node.Id.Value)); }
/// <summary> /// Adds the given node. /// </summary> /// <param name="node"></param> public override void AddNode(Node node) { if (!_preIndexMode) { if (_nodesToCache != null && _nodesToCache.Contains(node.Id.Value)) { // cache this node? _dataCache.AddNode(node); } if (_preIndex != null && _preIndex.Contains(node.Id.Value)) { // only save the coordinates for relevant nodes. // save the node-coordinates. // add the relevant nodes. _coordinates[node.Id.Value] = new GeoCoordinateSimple() { Latitude = (float)node.Latitude.Value, Longitude = (float)node.Longitude.Value }; // add the node as a possible restriction. if (_interpreter.IsRestriction(OsmGeoType.Node, node.Tags)) { // tests quickly if a given node is possibly a restriction. var vehicleTypes = _interpreter.CalculateRestrictions(node); if (vehicleTypes != null && vehicleTypes.Count > 0) { // add all the restrictions. this._relevantNodes.Add(node.Id.Value); var vertexId = this.AddRoadNode(node.Id.Value).Value; // will always exists, has just been added to coordinates. var restriction = new uint[] { vertexId }; if (vehicleTypes.Contains(null)) { // restriction is valid for all vehicles. _graph.AddRestriction(restriction); } else { // restriction is restricted to some vehicles only. foreach (string vehicle in vehicleTypes) { _graph.AddRestriction(vehicle, restriction); } } } } } } }
/// <summary> /// Tests the clear functionality on the datacache. /// </summary> /// <param name="cache"></param> public void DoOsmDataCacheTestClear(OsmDataCache cache) { Node node = Node.Create(1, new TagsCollection( Tag.Create("node", "yes")), 1, 2); Way way = Way.Create(1, new TagsCollection( Tag.Create("way", "yes")), 1, 2); Relation relation = Relation.Create(1, new TagsCollection( Tag.Create("relation", "yes")), RelationMember.Create(1, "something", OsmGeoType.Node)); cache.AddNode(node); cache.AddWay(way); cache.AddRelation(relation); Assert.IsTrue(cache.ContainsNode(node.Id.Value)); Assert.IsTrue(cache.ContainsWay(way.Id.Value)); Assert.IsTrue(cache.ContainsRelation(relation.Id.Value)); cache.Clear(); Assert.IsFalse(cache.ContainsNode(node.Id.Value)); Assert.IsFalse(cache.ContainsWay(way.Id.Value)); Assert.IsFalse(cache.ContainsRelation(relation.Id.Value)); }
/// <summary> /// Adds the given node. /// </summary> /// <param name="node"></param> public override void AddNode(Node node) { if (!_preIndexMode) { if (_nodesToCache != null && _nodesToCache.Contains(node.Id.Value)) { // cache this node? _dataCache.AddNode(node); } if (_preIndex != null && _preIndex.Contains(node.Id.Value)) { // only save the coordinates for relevant nodes. // save the node-coordinates. // add the relevant nodes. if (_box == null || _box.Contains(new GeoCoordinate((float)node.Latitude.Value, (float)node.Longitude.Value))) { // the coordinate is acceptable. _coordinates[node.Id.Value] = new GeoCoordinateSimple() { Latitude = (float)node.Latitude.Value, Longitude = (float)node.Longitude.Value }; if (_coordinates.Count == _preIndex.Count) { _preIndex.Clear(); _preIndex = null; } if (_bounds == null) { // create bounds. _bounds = new GeoCoordinateBox( new GeoCoordinate(node.Latitude.Value, node.Longitude.Value), new GeoCoordinate(node.Latitude.Value, node.Longitude.Value)); } else { // expand bounds. _bounds.ExpandWith( new GeoCoordinate(node.Latitude.Value, node.Longitude.Value)); } // add the node as a possible restriction. if (_interpreter.IsRestriction(OsmGeoType.Node, node.Tags)) { // tests quickly if a given node is possibly a restriction. List <Vehicle> vehicles = _interpreter.CalculateRestrictions(node); if (vehicles != null && vehicles.Count > 0) { // add all the restrictions. uint vertexId = this.AddRoadNode(node.Id.Value).Value; // will always exists, has just been added to coordinates. uint[] restriction = new uint[] { vertexId }; if (vehicles.Contains(null)) { // restriction is valid for all vehicles. _dynamicGraph.AddRestriction(restriction); } else { // restriction is restricted to some vehicles only. foreach (Vehicle vehicle in vehicles) { _dynamicGraph.AddRestriction(vehicle, restriction); } } } } } } } }
/// <summary> /// Moves to the next object. /// </summary> /// <returns></returns> public override bool MoveNext() { if (!_cachingDone) { // first seek & cache. this.Seek(); _cachingDone = true; } while (_simpleSource.MoveNext()) { // there is data. OsmGeo currentSimple = _simpleSource.Current(); switch (currentSimple.Type) { case OsmGeoType.Node: // create complete version. _current = currentSimple as Node; if (_current != null && _current.Tags == null) { // make sure nodes have a default tag collection that is empty not null. _current.Tags = new OsmSharp.Collections.Tags.TagsCollection(); } 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 = CompleteWay.CreateFrom(currentSimple as Way, _dataCache); 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 = CompleteRelation.CreateFrom(currentSimple as Relation, _dataCache); 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; } if (_current != null) { // only return complete objects. return(true); } } return(false); }
/// <summary> /// Moves to the next object. /// </summary> /// <returns></returns> private bool DoMoveNext() { 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> /// Moves to the next object. /// </summary> /// <returns></returns> public override bool MoveNext() { if (!_cachingDone) { // first seek & cache. this.Seek(); this.CacheRelations(); _cachingDone = true; } while (_simpleSource.MoveNext()) { // there is data. OsmGeo currentSimple = _simpleSource.Current(); if (currentSimple.Id == 198214128 || currentSimple.Id == 1014892489) { System.Diagnostics.Debug.WriteLine(""); } switch (currentSimple.Type) { case OsmGeoType.Node: // create complete version. _current = CompleteNode.CreateFrom(currentSimple as Node); 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 = CompleteWay.CreateFrom(currentSimple as Way, _dataCache); 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 = CompleteRelation.CreateFrom(currentSimple as Relation, _dataCache); 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; } if (_current != null) { // only return complete objects. return(true); } } return(false); }