/// <summary> /// Adds the router point as a vertex. /// </summary> public static uint AddAsVertex(this RouterDb routerDb, RouterPoint point) { if (routerDb.HasContracted) { throw new InvalidOperationException("Cannot add new vertices to a routerDb with contracted versions of the network."); } if (point.IsVertex()) { // the router point is already a vertex. return(point.VertexId(routerDb)); } // add a new vertex at the router point location. var location = point.LocationOnNetwork(routerDb); var vertex = routerDb.Network.VertexCount; routerDb.Network.AddVertex(vertex, location.Latitude, location.Longitude); // add two new edges. var edge = routerDb.Network.GetEdge(point.EdgeId); var shapeFrom = point.ShapePointsTo(routerDb, edge.From); shapeFrom.Reverse(); // we need this shape from edge.From -> vertex. var shapeTo = point.ShapePointsTo(routerDb, edge.To); var distanceFrom = point.DistanceTo(routerDb, edge.From); var distanceTo = point.DistanceTo(routerDb, edge.To); // remove edge id. routerDb.Network.RemoveEdge(point.EdgeId); // split in two. routerDb.Network.AddEdge(edge.From, vertex, new Data.Network.Edges.EdgeData() { Distance = distanceFrom, MetaId = edge.Data.MetaId, Profile = edge.Data.Profile }, shapeFrom); routerDb.Network.AddEdge(vertex, edge.To, new Data.Network.Edges.EdgeData() { Distance = distanceTo, MetaId = edge.Data.MetaId, Profile = edge.Data.Profile }, shapeTo); // sort the vertices again. routerDb.Network.Sort((v1, v2) => { if (vertex == v1) { vertex = (uint)v2; } else if (vertex == v2) { vertex = (uint)v1; } }); return(vertex); }
/// <summary> /// Writes geojson describing the given routerpoint. /// </summary> internal static void WriteGeoJson(this RouterPoint routerPoint, TextWriter writer, RouterDb db) { if (db == null) { throw new ArgumentNullException("db"); } if (writer == null) { throw new ArgumentNullException("writer"); } var jsonWriter = new JsonWriter(writer); jsonWriter.WriteOpen(); jsonWriter.WriteProperty("type", "FeatureCollection", true, false); jsonWriter.WritePropertyName("features", false); jsonWriter.WriteArrayOpen(); var edgeEnumerator = db.Network.GetEdgeEnumerator(); edgeEnumerator.MoveToEdge(routerPoint.EdgeId); db.WriteEdge(jsonWriter, edgeEnumerator); db.WriteVertex(jsonWriter, edgeEnumerator.From); db.WriteVertex(jsonWriter, edgeEnumerator.To); // write location on network. var coordinate = routerPoint.LocationOnNetwork(db); jsonWriter.WriteOpen(); jsonWriter.WriteProperty("type", "Feature", true, false); jsonWriter.WritePropertyName("geometry", false); jsonWriter.WriteOpen(); jsonWriter.WriteProperty("type", "Point", true, false); jsonWriter.WritePropertyName("coordinates", false); jsonWriter.WriteArrayOpen(); jsonWriter.WriteArrayValue(coordinate.Longitude.ToInvariantString()); jsonWriter.WriteArrayValue(coordinate.Latitude.ToInvariantString()); jsonWriter.WriteArrayClose(); jsonWriter.WriteClose(); jsonWriter.WritePropertyName("properties"); jsonWriter.WriteOpen(); jsonWriter.WriteProperty("type", "location_on_network", true); jsonWriter.WriteProperty("offset", routerPoint.Offset.ToInvariantString()); jsonWriter.WriteClose(); jsonWriter.WriteClose(); // write original location. coordinate = routerPoint.Location(); jsonWriter.WriteOpen(); jsonWriter.WriteProperty("type", "Feature", true, false); jsonWriter.WritePropertyName("geometry", false); jsonWriter.WriteOpen(); jsonWriter.WriteProperty("type", "Point", true, false); jsonWriter.WritePropertyName("coordinates", false); jsonWriter.WriteArrayOpen(); jsonWriter.WriteArrayValue(coordinate.Longitude.ToInvariantString()); jsonWriter.WriteArrayValue(coordinate.Latitude.ToInvariantString()); jsonWriter.WriteArrayClose(); jsonWriter.WriteClose(); jsonWriter.WritePropertyName("properties"); jsonWriter.WriteOpen(); jsonWriter.WriteProperty("type", "original_location", true); jsonWriter.WriteClose(); jsonWriter.WriteClose(); jsonWriter.WriteArrayClose(); jsonWriter.WriteClose(); }