public static string ToGeoJson(this RoutingEdge edge, RouterDb db)
        {
            var edgeShape = db.Network.GetShape(edge);

            var writer     = new StringWriter();
            var jsonWriter = new IO.Json.JsonWriter(writer);

            jsonWriter.WriteOpen();
            jsonWriter.WriteProperty("type", "FeatureCollection", true, false);
            jsonWriter.WritePropertyName("features", false);
            jsonWriter.WriteArrayOpen();

            jsonWriter.WriteOpen();
            jsonWriter.WriteProperty("type", "Feature", true, false);
            jsonWriter.WriteProperty("name", "ShapeMeta", true, false);
            jsonWriter.WritePropertyName("geometry", false);

            jsonWriter.WriteOpen();
            jsonWriter.WriteProperty("type", "LineString", true, false);
            jsonWriter.WritePropertyName("coordinates", false);
            jsonWriter.WriteArrayOpen();
            for (var shape = 0; shape < edgeShape.Count; shape++)
            {
                jsonWriter.WriteArrayOpen();
                jsonWriter.WriteArrayValue(edgeShape[shape].Longitude.ToInvariantString());
                jsonWriter.WriteArrayValue(edgeShape[shape].Latitude.ToInvariantString());
                if (edgeShape[shape].Elevation.HasValue)
                {
                    jsonWriter.WriteArrayValue(edgeShape[shape].Elevation.Value.ToInvariantString());
                }

                jsonWriter.WriteArrayClose();
            }

            jsonWriter.WriteArrayClose();
            jsonWriter.WriteClose();

            jsonWriter.WritePropertyName("properties");
            jsonWriter.WriteOpen();
            jsonWriter.WriteProperty("id", edge.Id.ToString(), false, false);
            jsonWriter.WriteProperty("profile", edge.Data.Profile.ToString(), false, false);
            jsonWriter.WriteProperty("meta", edge.Data.MetaId.ToString(), false, false);
            jsonWriter.WriteProperty("from", edge.From.ToString(), false, false);
            jsonWriter.WriteProperty("to", edge.To.ToString(), false, false);
            jsonWriter.WriteClose();

            jsonWriter.WriteClose();

            WriteVertex(db, jsonWriter, edge.From);
            WriteVertex(db, jsonWriter, edge.To);

            jsonWriter.WriteArrayClose();
            jsonWriter.WriteClose();

            return(writer.ToString());
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Converts the router db meta to json.
        /// </summary>
        public static void ToJson(this InstanceMeta routerDbMeta, TextWriter writer)
        {
            var jsonWriter = new IO.Json.JsonWriter(writer);

            jsonWriter.WriteOpen();

            jsonWriter.WritePropertyName("id");
            jsonWriter.WritePropertyValue(routerDbMeta.Id);

            if (routerDbMeta.Meta != null)
            {
                jsonWriter.WriteOpen();

                foreach (var attribute in routerDbMeta.Meta)
                {
                    jsonWriter.WritePropertyName(attribute.Key);
                    jsonWriter.WritePropertyValue(attribute.Value);
                }

                jsonWriter.WriteClose();
            }

            if (routerDbMeta.Profiles != null)
            {
                jsonWriter.WritePropertyName("Profiles");
                jsonWriter.WriteArrayOpen();
                for (var i = 0; i < routerDbMeta.Profiles.Length; i++)
                {
                    var profile = routerDbMeta.Profiles[i];

                    jsonWriter.WriteArrayOpen();
                    jsonWriter.WriteOpen();

                    jsonWriter.WritePropertyName("name");
                    jsonWriter.WritePropertyValue(profile.Name);

                    jsonWriter.WritePropertyName("metric");
                    jsonWriter.WritePropertyValue(profile.Metric.ToInvariantString());

                    jsonWriter.WriteClose();
                    jsonWriter.WriteArrayClose();
                }
                jsonWriter.WriteArrayClose();
            }

            if (routerDbMeta.Contracted != null)
            {
                jsonWriter.WritePropertyName("Profiles");
                jsonWriter.WriteArrayOpen();
                for (var i = 0; i < routerDbMeta.Contracted.Length; i++)
                {
                    var profile = routerDbMeta.Contracted[i];

                    jsonWriter.WriteArrayOpen();

                    jsonWriter.WriteArrayValue(profile);

                    jsonWriter.WriteArrayClose();
                }
                jsonWriter.WriteArrayClose();
            }
        }
        /// <summary>
        /// Writes the tile as geojson.
        /// </summary>
        public static void WriteGeoJson(this Segment[] segments, RouterDb routerDb, TextWriter writer)
        {
            if (segments == null)
            {
                throw new ArgumentNullException("segments");
            }
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }

            var edgeProfile = routerDb.EdgeProfiles;
            var edgeMeta    = routerDb.EdgeMeta;

            var jsonWriter = new IO.Json.JsonWriter(writer);

            jsonWriter.WriteOpen();
            jsonWriter.WriteProperty("type", "FeatureCollection", true, false);
            jsonWriter.WritePropertyName("features", false);
            jsonWriter.WriteArrayOpen();

            for (var i = 0; i < segments.Length; i++)
            {
                var segment     = segments[i];
                var coordinates = segment.Shape;

                if (coordinates.Length >= 2)
                {
                    jsonWriter.WriteOpen();
                    jsonWriter.WriteProperty("type", "Feature", true, false);
                    jsonWriter.WritePropertyName("geometry", false);

                    jsonWriter.WriteOpen();
                    jsonWriter.WriteProperty("type", "LineString", true, false);
                    jsonWriter.WritePropertyName("coordinates", false);
                    jsonWriter.WriteArrayOpen();
                    for (var shape = 0; shape < coordinates.Length; shape++)
                    {
                        jsonWriter.WriteArrayOpen();
                        jsonWriter.WriteArrayValue(coordinates[shape].Longitude.ToInvariantString());
                        jsonWriter.WriteArrayValue(coordinates[shape].Latitude.ToInvariantString());
                        jsonWriter.WriteArrayClose();
                    }
                    jsonWriter.WriteArrayClose();
                    jsonWriter.WriteClose();

                    jsonWriter.WritePropertyName("properties");
                    jsonWriter.WriteOpen();
                    var profile = edgeProfile.Get(segment.Profile);
                    if (profile != null)
                    {
                        foreach (var attribute in profile)
                        {
                            jsonWriter.WriteProperty(attribute.Key, attribute.Value, true, true);
                        }
                    }
                    var meta = edgeMeta.Get(segment.Meta);
                    if (meta != null)
                    {
                        foreach (var attribute in meta)
                        {
                            jsonWriter.WriteProperty(attribute.Key, attribute.Value, true, true);
                        }
                    }
                    jsonWriter.WriteClose();

                    jsonWriter.WriteClose();
                }
            }

            jsonWriter.WriteArrayClose();
            jsonWriter.WriteClose();
        }