Пример #1
0
        /// <summary>
        /// Gets all features inside the given bounding box and writes them as a geojson string.
        /// </summary>
        public static void WriteGeoJson(this RouterDb db, TextWriter writer, float minLatitude, float minLongitude,
                                        float maxLatitude, float maxLongitude, bool includeEdges = true, bool includeVertices = true)
        {
            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 vertices = HilbertExtensions.Search(db.Network.GeometricGraph, minLatitude, minLongitude,
                                                    maxLatitude, maxLongitude);
            var edges = new HashSet <long>();

            var edgeEnumerator = db.Network.GetEdgeEnumerator();

            foreach (var vertex in vertices)
            {
                if (includeVertices)
                {
                    db.WriteVertex(jsonWriter, vertex);
                }

                if (includeEdges)
                {
                    edgeEnumerator.MoveTo(vertex);
                    edgeEnumerator.Reset();
                    while (edgeEnumerator.MoveNext())
                    {
                        if (edges.Contains(edgeEnumerator.Id))
                        {
                            continue;
                        }
                        edges.Add(edgeEnumerator.Id);

                        db.WriteEdge(jsonWriter, edgeEnumerator);
                    }
                }
            }

            jsonWriter.WriteArrayClose();
            jsonWriter.WriteClose();
        }
Пример #2
0
        /// <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();
        }