public void ExtensionsPolygonAddWaysAcceptsCollectionOfOSMWays() { OSMDB db = new OSMDB(); OSMNode p1 = new OSMNode(1, 10, 15); OSMNode p2 = new OSMNode(2, 0, 15); OSMNode p3 = new OSMNode(3, 0, 0); OSMWay way1 = new OSMWay(10); OSMWay way2 = new OSMWay(11); db.Nodes.Add(p1); db.Nodes.Add(p2); db.Nodes.Add(p3); way1.Nodes.Add(p1.ID); way1.Nodes.Add(p2.ID); way1.Nodes.Add(p3.ID); way2.Nodes.Add(p3.ID); way2.Nodes.Add(p1.ID); Polygon<OSMNode> target = new Polygon<OSMNode>(); target.AddWays(new OSMWay[] {way1, way2}, db); Assert.Equal(3, target.VerticesCount); CompareVerticesLists(new IPointGeo[] { p3, p2, p1 }, target.Vertices); }
public void PolygonConstructorAcceptsCollectionOfOSMWaysEvenIfTheyAreRevesed() { OSMDB db = new OSMDB(); OSMNode p1 = new OSMNode(1, 10, 15); OSMNode p2 = new OSMNode(2, 0, 15); OSMNode p3 = new OSMNode(3, 0, 0); OSMNode p4 = new OSMNode(4, 10, 0); OSMWay way1 = new OSMWay(10); OSMWay way2 = new OSMWay(11); db.Nodes.Add(p1); db.Nodes.Add(p2); db.Nodes.Add(p3); db.Nodes.Add(p4); way1.Nodes.Add(p1.ID); way1.Nodes.Add(p2.ID); way1.Nodes.Add(p3.ID); way2.Nodes.Add(p1.ID); way2.Nodes.Add(p4.ID); way2.Nodes.Add(p3.ID); Polygon<OSMNode> target = new Polygon<OSMNode>(); target.AddWays(new OSMWay[] { way1, way2 }, db); Assert.Equal(4, target.VerticesCount); CompareVerticesLists(new IPointGeo[] { p1, p2, p3, p4 }, target.Vertices); }
public void OSMWayConstructorInitializesID() { int id = 1374; OSMWay target = new OSMWay(id); Assert.Equal(id, target.ID); Assert.NotNull(target.Nodes); }
public void RoadTypeMatchReturnsFalseForMissingTag() { RoadType target = new RoadType(); target.RequiredTags.Add(new OSMTag("highway", "residental")); OSMWay testObject = new OSMWay(1); Assert.False(target.Match(testObject)); }
public void OSMWayIsClosedReturnsFalseForWaysWithLessThan3Nodes() { int id = 1374; OSMWay target = new OSMWay(id); target.Nodes.Add(1); target.Nodes.Add(1); Assert.False(target.IsClosed); }
public void OSMWayIsClosedReturnsFalseForOpenedWays() { int id = 1374; OSMWay target = new OSMWay(id); target.Nodes.Add(1); target.Nodes.Add(2); target.Nodes.Add(3); Assert.False(target.IsClosed); }
public void RoadTypeMatchReturnsFalseForTagWithDifferentValue() { RoadType target = new RoadType(); target.RequiredTags.Add(new OSMTag("highway", "residental")); OSMWay testObject = new OSMWay(1); testObject.Tags.Add(new OSMTag("highway", "primary")); Assert.False(target.Match(testObject)); }
public void OSMWayConstructorInitializesIDAndNodes() { int id = 1374; int[] nodes = new int[] { 1, 2, 3 }; OSMWay target = new OSMWay(id, nodes); Assert.Equal(id, target.ID); Assert.Equal(nodes[0], target.Nodes[0]); Assert.Equal(nodes[1], target.Nodes[1]); Assert.Equal(nodes[2], target.Nodes[2]); }
public void OSMRouteConstructorSetsSpeedPropertyFromMaxSpeedTag() { OSMWay source = new OSMWay(11); source.Tags.Add(new OSMTag("maxspeed", "50")); RoadType sourceType = new RoadType(); OSMRoad target = new OSMRoad(source, sourceType); Assert.Equal(50, target.Speed); }
public void OSMRouteConstructorCopiesDataFromWay() { OSMWay source = new OSMWay(11); source.Nodes.Add(1); source.Nodes.Add(2); source.Tags.Add(new OSMTag("highway", "track")); RoadType sourceType = new RoadType(); sourceType.RequiredTags.Add(new OSMTag("highway", "track")); OSMRoad target = new OSMRoad(source, sourceType); Assert.Equal(source.Nodes.Count, target.Nodes.Count); Assert.Equal(source.Tags.First(), target.Tags.First()); Assert.Equal(sourceType, target.RoadType); }
public void RoadGraphBuildCreatesNodesAccordingToMap() { OSMDB map = new OSMDB(); map.Nodes.Add(new OSMNode(1, 1, 2)); map.Nodes.Add(new OSMNode(2, 2, 3)); OSMWay way = new OSMWay(3, new List<int>(new int[] { 1, 2 })); way.Tags.Add(new OSMTag("accessible", "no")); way.Tags.Add(new OSMTag("accessible-reverse", "yes")); way.Tags.Add(new OSMTag("speed", "50")); way.Tags.Add(new OSMTag("way-id", "123")); map.Ways.Add(way); RoadGraph target = new RoadGraph(); target.Build(map); Assert.Equal(2, target.Nodes.Count()); Assert.Equal(1, target.Nodes.Where(n => n.MapPoint == map.Nodes[1]).Count()); Assert.Equal(1, target.Nodes.Where(n => n.MapPoint == map.Nodes[2]).Count()); }
public void RoadGraphBuildCreatesCorrectConnectionGeometry() { OSMDB map = new OSMDB(); map.Nodes.Add(new OSMNode(1, 1, 2)); map.Nodes.Add(new OSMNode(2, 2, 3)); map.Nodes.Add(new OSMNode(3, 3, 4)); OSMWay way = new OSMWay(4, new int[] { 1, 2, 3 }); way.Tags.Add(new OSMTag("accessible", "yes")); way.Tags.Add(new OSMTag("accessible-reverse", "yes")); way.Tags.Add(new OSMTag("speed", "50")); way.Tags.Add(new OSMTag("way-id", "123")); map.Ways.Add(way); RoadGraph target = new RoadGraph(); target.Build(map); Assert.Equal(1, target.ConnectionGeometries.Count()); Assert.Equal(3, target.ConnectionGeometries.Single().Nodes.Count); }
public void RoadTypeMatchReturnsFalseForMultipleTagsWithDifferentValues() { RoadType target = new RoadType(); target.RequiredTags.Add(new OSMTag("highway", "track")); target.RequiredTags.Add(new OSMTag("grade", "1")); OSMWay testObject1 = new OSMWay(1); testObject1.Tags.Add(new OSMTag("highway", "primary")); target.RequiredTags.Add(new OSMTag("grade", "1")); OSMWay testObject2 = new OSMWay(1); testObject2.Tags.Add(new OSMTag("highway", "track")); target.RequiredTags.Add(new OSMTag("grade", "2")); OSMWay testObject3 = new OSMWay(1); testObject3.Tags.Add(new OSMTag("highway", "track")); Assert.False(target.Match(testObject1)); Assert.False(target.Match(testObject2)); Assert.False(target.Match(testObject3)); }
public static void SaveCandidateIncomingConnections(CandidatePoint candidate, string filename) { int counter = -1; OSMDB result = new OSMDB(); OSMNode osmCandidate = new OSMNode(counter--, candidate.MapPoint.Latitude, candidate.MapPoint.Longitude); osmCandidate.Tags.Add(new OSMTag("observation", candidate.ObservationProbability.ToString())); osmCandidate.Tags.Add(new OSMTag("time", candidate.Layer.TrackPoint.Time.ToString())); result.Nodes.Add(osmCandidate); foreach (var connection in candidate.IncomingConnections) { OSMNode from = new OSMNode(counter--, connection.From.MapPoint.Latitude, connection.From.MapPoint.Longitude); from.Tags.Add(new OSMTag("observation", connection.From.ObservationProbability.ToString())); from.Tags.Add(new OSMTag("time", connection.From.Layer.TrackPoint.Time.ToString())); result.Nodes.Add(from); OSMWay osmConnection = new OSMWay(counter--, new int[] { from.ID, osmCandidate.ID }); osmConnection.Tags.Add(new OSMTag("transmission", connection.TransmissionProbability.ToString())); result.Ways.Add(osmConnection); } result.Save(filename); }
/// <summary> /// Creates a new instance of OSMRoute based on specific OSMWay /// </summary> /// <param name="way">The way that defines geomery and tags</param> /// <param name="roadType">The RoadType of this OSMRoute</param> public OSMRoad(OSMWay way, RoadType roadType) : base(way.ID) { _tags = way.Tags; _nodes = new List<int>(way.Nodes); RoadType = roadType; Speed = RoadType.Speed; if (Tags.ContainsTag("maxspeed")) { string[] chunks = Tags["maxspeed"].Value.Split(' '); double numericSpeed = 0; if (double.TryParse(chunks[0], out numericSpeed)) { if (chunks.Length == 2 && chunks[1] == "mph") { Speed = numericSpeed * MphConversionFactor; } else { Speed = numericSpeed; } } } }
public void OSMDatabaseWaysAcceptsAndReturnsWays() { OSMDB target = new OSMDB(); OSMWay way = new OSMWay(1354); target.Ways.Add(way); Assert.Same(way, target.Ways[way.ID]); }
/// <summary> /// Get length of the OSMWay in meters /// </summary> /// <param name="way"></param> /// <param name="db"></param> /// <returns></returns> static double GetLength(OSMWay way, OSMDB db) { double result = 0; for (int i = 0; i < way.Nodes.Count - 1; i++) { result += LK.GeoUtils.Calculations.GetDistance2D(db.Nodes[way.Nodes[i]], db.Nodes[way.Nodes[i + 1]]); } return result; }
public void RoadTypeMatchReturnsTrueForTheSameMultipleTags() { RoadType target = new RoadType(); target.RequiredTags.Add(new OSMTag("highway", "track")); target.RequiredTags.Add(new OSMTag("grade", "1")); OSMWay testObject = new OSMWay(1); testObject.Tags.Add(new OSMTag("grade", "1")); testObject.Tags.Add(new OSMTag("highway", "track")); Assert.True(target.Match(testObject)); }
/// <summary> /// Writes the specific OSMWay to the output /// </summary> /// <param name="way">The OSMWay to be written.</param> public void WriteWay(OSMWay way) { _xmlWriter.WriteStartElement("way"); WriteOSMObjectAttributes(way); foreach (var nodeRef in way.Nodes) { _xmlWriter.WriteStartElement("nd"); _xmlWriter.WriteAttributeString("ref", nodeRef.ToString(System.Globalization.NumberFormatInfo.InvariantInfo)); _xmlWriter.WriteEndElement(); } WriteOSMObjectTags(way); _xmlWriter.WriteEndElement(); }
/// <summary> /// Callback function for OSMXmlDataReader, checks whether the way matches desired RoadTypes and adds the matched road into DB /// </summary> /// <param name="way">The way read form the OSM file</param> void WayRead(OSMWay way) { foreach (RoadType roadType in _acceptedRoads) { if (roadType.Match(way) && _ways.ContainsKey(way.ID) == false) { ExtractUsedNodes(way); _storage.Ways.Add(new OSMRoad(way, roadType)); _ways.Add(way.ID, way.ID); } } }
/// <summary> /// Tests whether specific way matched to this road type /// </summary> /// <param name="toMatch">The OSMWay to be tested</param> /// <returns>true if the way matched to this road type, otherwise returns false</returns> public bool Match(OSMWay toMatch) { foreach (OSMTag required in RequiredTags) { if (toMatch.Tags.ContainsTag(required.Key)) { if (required.Value != "*" && toMatch.Tags[required.Key].Value != required.Value) { return false; } } else { return false; } } return true; }
public void OSMXmlDataWriteWayCanWriteWayWithTags() { MemoryStream ms = new MemoryStream(); using (OSMXmlDataWriter target = new OSMXmlDataWriter(ms)) { OSMWay way = new OSMWay(1232); way.Nodes.Add(1); way.Tags.Add(new OSMTag("name", "test")); target.WriteWay(way); } ms.Seek(0, 0); XElement osmRoot = XDocument.Load(new StreamReader(ms)).Root; XElement wayElement = osmRoot.Element("way"); Assert.NotNull(wayElement); Assert.Equal(1232, int.Parse(wayElement.Attribute("id").Value, System.Globalization.NumberFormatInfo.InvariantInfo)); Assert.Equal(1, int.Parse(wayElement.Element("nd").Attribute("ref").Value, System.Globalization.NumberFormatInfo.InvariantInfo)); Assert.Equal("name", wayElement.Element("tag").Attribute("k").Value); }
public void PolygonConstructorThrowsExceptionIfInitializedWithSingleOpenWay() { OSMDB db = new OSMDB(); OSMNode p1 = new OSMNode(1, 10, 15); OSMNode p2 = new OSMNode(2, 0, 15); OSMNode p3 = new OSMNode(3, 0, 0); OSMWay way1 = new OSMWay(10); db.Nodes.Add(p1); db.Nodes.Add(p2); db.Nodes.Add(p3); way1.Nodes.Add(p1.ID); way1.Nodes.Add(p2.ID); way1.Nodes.Add(p3.ID); Polygon<OSMNode> target = new Polygon<OSMNode>(); Assert.Throws<ArgumentException>(delegate { target.AddWays(new OSMWay[] { way1 }, db); }); }
public void PolygonConstructorThrowsExceptionIfWaysDontFormClosedPolygon() { OSMDB db = new OSMDB(); OSMNode p1 = new OSMNode(1, 10, 15); OSMNode p2 = new OSMNode(2, 0, 15); OSMNode p3 = new OSMNode(3, 0, 0); OSMNode p4 = new OSMNode(4, 10, 0); OSMWay way1 = new OSMWay(10); OSMWay way2 = new OSMWay(11); db.Nodes.Add(p1); db.Nodes.Add(p2); db.Nodes.Add(p3); db.Nodes.Add(p4); way1.Nodes.Add(p1.ID); way1.Nodes.Add(p2.ID); way1.Nodes.Add(p3.ID); way2.Nodes.Add(p3.ID); way2.Nodes.Add(p4.ID); Polygon<OSMNode> target = new Polygon<OSMNode>(); Assert.Throws<ArgumentException>(delegate { target.AddWays(new OSMWay[] { way1, way2 }, db); }); }
/// <summary> /// Saves the read OSMWay to the internal storage /// </summary> /// <param name="node">The OSMWay read</param> public void ProcessWay(OSMWay way) { _readWays.Add(way); }
/// <summary> /// Raises the WayRead event /// </summary> /// <param name="node">The OSMWay read from the database</param> protected void OnWayRead(OSMWay way) { OSMWayReadHandler temp = WayRead; if (temp != null) { temp(way); } }
/// <summary> /// Exctract nodes from the way and adds them into UsedNodes list /// </summary> /// <param name="way"></param> void ExtractUsedNodes(OSMWay way) { foreach (int nodeID in way.Nodes) { if (_usedNodes.ContainsKey(nodeID) == false) { _usedNodes.Add(nodeID, new List<int>()); } _usedNodes[nodeID].Add(way.ID); } }
private void ReadWay() { string attId = _xmlReader.GetAttribute("id"); if (attId == null) throw new XmlException("Attribute 'id' is missing."); int wayId = int.Parse(attId, System.Globalization.CultureInfo.InvariantCulture); OSMWay way = new OSMWay(wayId); if (false == _xmlReader.IsEmptyElement) { _xmlReader.Read(); while (_xmlReader.NodeType != XmlNodeType.EndElement) { switch (_xmlReader.NodeType) { case XmlNodeType.Element: switch (_xmlReader.Name) { case "nd": way.Nodes.Add(ReadWayNd()); continue; case "tag": way.Tags.Add(ReadTag()); continue; default: _xmlReader.Skip(); continue; } default: _xmlReader.Skip(); break; } } } OnWayRead(way); _xmlReader.Skip(); }
/// <summary> /// Splits ways at road crossings, check for oneway roads and save results in OSMDB /// </summary> /// <returns>OSMDB object with road segments and used nodes</returns> public OSMDB BuildRoutableOSM() { OSMDB result = new OSMDB(); int counter = -1; foreach (OSMRoad route in _storage.Ways) { OSMWay segment = new OSMWay(counter--); OSMTag wayIDTag = new OSMTag("way-id", route.ID.ToString()); OSMTag speedTag = new OSMTag("speed", route.Speed.ToString()); string wayAccessibility = route.IsAccessible() ? "yes" : "no"; OSMTag wayAccessibilityTag = new OSMTag("accessible", wayAccessibility); string wayAccessibilityReverse = route.IsAccessibleReverse() ? "yes" : "no"; OSMTag wayAccessibilityReverseTag = new OSMTag("accessible-reverse", wayAccessibilityReverse); for (int i = 0; i < route.Nodes.Count; i++) { segment.Nodes.Add(route.Nodes[i]); if ((UsedNodes[route.Nodes[i]].Count > 1) && (i > 0) && (i < (route.Nodes.Count - 1))) { segment.Tags.Add(wayIDTag); segment.Tags.Add(speedTag); segment.Tags.Add(wayAccessibilityTag); segment.Tags.Add(wayAccessibilityReverseTag); result.Ways.Add(segment); segment = new OSMWay(counter--); segment.Nodes.Add(route.Nodes[i]); } } segment.Tags.Add(wayIDTag); segment.Tags.Add(speedTag); segment.Tags.Add(wayAccessibilityTag); segment.Tags.Add(wayAccessibilityReverseTag); result.Ways.Add(segment); } foreach (OSMNode node in _storage.Nodes) { OSMNode newNode = new OSMNode(node.ID, node.Latitude, node.Longitude); // preserve junction and highway tags on nodes if (node.Tags.ContainsTag("junction")) { newNode.Tags.Add(node.Tags["junction"]); } if (node.Tags.ContainsTag("highway")) { newNode.Tags.Add(node.Tags["highway"]); } if (_usedNodes[node.ID].Count > 1) { newNode.Tags.Add(new OSMTag("crossroad", "yes")); } result.Nodes.Add(newNode); } return result; }
public void RoadTypeMatchReturnsTrueForTheSameTags() { RoadType target = new RoadType(); target.RequiredTags.Add(new OSMTag("highway", "residental")); OSMWay testObject = new OSMWay(1); testObject.Tags.Add(new OSMTag("highway", "residental")); Assert.True(target.Match(testObject)); }