Example #1
0
        /// <summary>
        /// Writes the route as json.
        /// </summary>
        public static void WriteJson(this Route route, TextWriter writer)
        {
            if (route == null)
            {
                throw new ArgumentNullException("route");
            }
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }

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

            jsonWriter.WriteOpen();
            if (route.Shape != null)
            {
                jsonWriter.WritePropertyName("Shape");
                jsonWriter.WriteArrayOpen();
                for (var i = 0; i < route.Shape.Length; i++)
                {
                    jsonWriter.WriteArrayOpen();
                    jsonWriter.WriteArrayValue(route.Shape[i].Longitude.ToInvariantString());
                    jsonWriter.WriteArrayValue(route.Shape[i].Latitude.ToInvariantString());
                    jsonWriter.WriteArrayClose();
                }
                jsonWriter.WriteArrayClose();
            }

            if (route.ShapeMeta != null)
            {
                jsonWriter.WritePropertyName("ShapeMeta");
                jsonWriter.WriteArrayOpen();
                for (var i = 0; i < route.ShapeMeta.Length; i++)
                {
                    var meta = route.ShapeMeta[i];

                    jsonWriter.WriteOpen();
                    jsonWriter.WritePropertyName("Shape");
                    jsonWriter.WritePropertyValue(meta.Shape.ToInvariantString());

                    if (meta.Attributes != null)
                    {
                        jsonWriter.WritePropertyName("Attributes");
                        jsonWriter.WriteOpen();
                        foreach (var attribute in meta.Attributes)
                        {
                            jsonWriter.WriteProperty(attribute.Key, attribute.Value, true, true);
                        }
                        jsonWriter.WriteClose();
                    }
                    jsonWriter.WriteClose();
                }
                jsonWriter.WriteArrayClose();
            }

            if (route.Stops != null)
            {
                jsonWriter.WritePropertyName("Stops");
                jsonWriter.WriteArrayOpen();
                for (var i = 0; i < route.Stops.Length; i++)
                {
                    var stop = route.Stops[i];

                    jsonWriter.WriteOpen();
                    jsonWriter.WritePropertyName("Shape");
                    jsonWriter.WritePropertyValue(stop.Shape.ToInvariantString());
                    jsonWriter.WritePropertyName("Coordinates");
                    jsonWriter.WriteArrayOpen();
                    jsonWriter.WriteArrayValue(route.Stops[i].Coordinate.Longitude.ToInvariantString());
                    jsonWriter.WriteArrayValue(route.Stops[i].Coordinate.Latitude.ToInvariantString());
                    jsonWriter.WriteArrayClose();

                    if (stop.Attributes != null)
                    {
                        jsonWriter.WritePropertyName("Attributes");
                        jsonWriter.WriteOpen();
                        foreach (var attribute in stop.Attributes)
                        {
                            jsonWriter.WriteProperty(attribute.Key, attribute.Value, true, true);
                        }
                        jsonWriter.WriteClose();
                    }
                    jsonWriter.WriteClose();
                }
                jsonWriter.WriteArrayClose();
            }

            if (route.Branches != null)
            {
                jsonWriter.WritePropertyName("Branches");
                jsonWriter.WriteArrayOpen();
                for (var i = 0; i < route.Branches.Length; i++)
                {
                    var stop = route.Branches[i];

                    jsonWriter.WriteOpen();
                    jsonWriter.WritePropertyName("Shape");
                    jsonWriter.WritePropertyValue(stop.Shape.ToInvariantString());
                    jsonWriter.WritePropertyName("Coordinates");
                    jsonWriter.WriteArrayOpen();
                    jsonWriter.WriteArrayValue(route.Branches[i].Coordinate.Longitude.ToInvariantString());
                    jsonWriter.WriteArrayValue(route.Branches[i].Coordinate.Latitude.ToInvariantString());
                    jsonWriter.WriteArrayClose();

                    if (stop.Attributes != null)
                    {
                        jsonWriter.WritePropertyName("Attributes");
                        jsonWriter.WriteOpen();
                        foreach (var attribute in stop.Attributes)
                        {
                            jsonWriter.WriteProperty(attribute.Key, attribute.Value, true, true);
                        }
                        jsonWriter.WriteClose();
                    }
                    jsonWriter.WriteClose();
                }
                jsonWriter.WriteArrayClose();
            }

            jsonWriter.WriteClose();
        }
Example #2
0
        /// <summary>
        /// Writes the route as geojson.
        /// </summary>
        public static void WriteGeoJson(this Route route, TextWriter writer, bool includeShapeMeta = true, bool includeStops = true, bool groupByShapeMeta = true)
        {
            if (route == null)
            {
                throw new ArgumentNullException("route");
            }
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }

            if (groupByShapeMeta)
            { // group by shape meta.
                var jsonWriter = new IO.Json.JsonWriter(writer);
                jsonWriter.WriteOpen();
                jsonWriter.WriteProperty("type", "FeatureCollection", true, false);
                jsonWriter.WritePropertyName("features", false);
                jsonWriter.WriteArrayOpen();

                if (route.Shape != null && route.ShapeMeta != null)
                {
                    for (var i = 0; i < route.ShapeMeta.Length; i++)
                    {
                        var shapeMeta  = route.ShapeMeta[i];
                        var lowerShape = -1;
                        if (i > 0)
                        {
                            lowerShape = route.ShapeMeta[i - 1].Shape;
                        }
                        var higherShape = route.ShapeMeta[i].Shape;
                        if (lowerShape >= higherShape)
                        {
                            throw new Exception(string.Format("Invalid route: {0}", route.ToJson()));
                        }

                        var coordinates = new List <Coordinate>();
                        for (var shape = lowerShape; shape <= higherShape; shape++)
                        {
                            if (shape >= 0 && shape < route.Shape.Length)
                            {
                                coordinates.Add(route.Shape[shape]);
                            }
                        }

                        if (coordinates.Count >= 2)
                        {
                            jsonWriter.WriteOpen();
                            jsonWriter.WriteProperty("type", "Feature", true, false);
                            jsonWriter.WriteProperty("name", "ShapeMeta", true, false);
                            jsonWriter.WritePropertyName("properties");
                            jsonWriter.WriteOpen();
                            jsonWriter.WriteClose();
                            jsonWriter.WritePropertyName("geometry", false);

                            jsonWriter.WriteOpen();
                            jsonWriter.WriteProperty("type", "LineString", true, false);
                            jsonWriter.WritePropertyName("coordinates", false);
                            jsonWriter.WriteArrayOpen();

                            for (var shape = 0; shape < coordinates.Count; 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();
                            if (shapeMeta.Attributes != null)
                            {
                                foreach (var attribute in shapeMeta.Attributes)
                                {
                                    jsonWriter.WriteProperty(attribute.Key, attribute.Value, true, true);
                                }
                            }
                            jsonWriter.WriteClose();

                            jsonWriter.WriteClose();
                        }
                    }
                }

                if (route.Stops != null &&
                    includeStops)
                {
                    for (var i = 0; i < route.Stops.Length; i++)
                    {
                        var stop = route.Stops[i];

                        jsonWriter.WriteOpen();
                        jsonWriter.WriteProperty("type", "Feature", true, false);
                        jsonWriter.WriteProperty("name", "Stop", true, false);
                        jsonWriter.WriteProperty("Shape", stop.Shape.ToInvariantString(), true, false);
                        jsonWriter.WritePropertyName("geometry", false);

                        jsonWriter.WriteOpen();
                        jsonWriter.WriteProperty("type", "Point", true, false);
                        jsonWriter.WritePropertyName("coordinates", false);
                        jsonWriter.WriteArrayOpen();
                        jsonWriter.WriteArrayValue(stop.Coordinate.Longitude.ToInvariantString());
                        jsonWriter.WriteArrayValue(stop.Coordinate.Latitude.ToInvariantString());
                        jsonWriter.WriteArrayClose();
                        jsonWriter.WriteClose();

                        jsonWriter.WritePropertyName("properties");
                        jsonWriter.WriteOpen();
                        if (stop.Attributes != null)
                        {
                            foreach (var attribute in stop.Attributes)
                            {
                                jsonWriter.WriteProperty(attribute.Key, attribute.Value, true, true);
                            }
                        }
                        jsonWriter.WriteClose();

                        jsonWriter.WriteClose();
                    }
                }

                jsonWriter.WriteArrayClose();
                jsonWriter.WriteClose();
            }
            else
            { // include shape meta as points if requested.
                var jsonWriter = new IO.Json.JsonWriter(writer);
                jsonWriter.WriteOpen();
                jsonWriter.WriteProperty("type", "FeatureCollection", true, false);
                jsonWriter.WritePropertyName("features", false);
                jsonWriter.WriteArrayOpen();

                if (route.Shape != null)
                {
                    jsonWriter.WriteOpen();
                    jsonWriter.WriteProperty("type", "Feature", true, false);
                    jsonWriter.WriteProperty("name", "Shape", true, false);
                    jsonWriter.WritePropertyName("properties");
                    jsonWriter.WriteOpen();
                    jsonWriter.WriteClose();
                    jsonWriter.WritePropertyName("geometry", false);


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

                    jsonWriter.WriteClose();
                }

                if (route.ShapeMeta != null &&
                    includeShapeMeta)
                {
                    for (var i = 0; i < route.ShapeMeta.Length; i++)
                    {
                        var meta = route.ShapeMeta[i];

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

                        var coordinate = route.Shape[meta.Shape];

                        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();
                        if (meta.Attributes != null)
                        {
                            foreach (var attribute in meta.Attributes)
                            {
                                jsonWriter.WriteProperty(attribute.Key, attribute.Value, true, true);
                            }
                        }
                        jsonWriter.WriteClose();

                        jsonWriter.WriteClose();
                    }
                }

                if (route.Stops != null &&
                    includeStops)
                {
                    for (var i = 0; i < route.Stops.Length; i++)
                    {
                        var stop = route.Stops[i];

                        jsonWriter.WriteOpen();
                        jsonWriter.WriteProperty("type", "Feature", true, false);
                        jsonWriter.WriteProperty("name", "Stop", true, false);
                        jsonWriter.WriteProperty("Shape", stop.Shape.ToInvariantString(), true, false);
                        jsonWriter.WritePropertyName("geometry", false);

                        jsonWriter.WriteOpen();
                        jsonWriter.WriteProperty("type", "Point", true, false);
                        jsonWriter.WritePropertyName("coordinates", false);
                        jsonWriter.WriteArrayOpen();
                        jsonWriter.WriteArrayValue(stop.Coordinate.Longitude.ToInvariantString());
                        jsonWriter.WriteArrayValue(stop.Coordinate.Latitude.ToInvariantString());
                        jsonWriter.WriteArrayClose();
                        jsonWriter.WriteClose();

                        jsonWriter.WritePropertyName("properties");
                        jsonWriter.WriteOpen();
                        if (stop.Attributes != null)
                        {
                            foreach (var attribute in stop.Attributes)
                            {
                                jsonWriter.WriteProperty(attribute.Key, attribute.Value, true, true);
                            }
                        }
                        jsonWriter.WriteClose();

                        jsonWriter.WriteClose();
                    }
                }

                jsonWriter.WriteArrayClose();
                jsonWriter.WriteClose();
            }
        }
Example #3
0
        internal static void WriteGeoJsonFeatures(this Route route, JsonWriter jsonWriter, bool includeShapeMeta = true, bool includeStops   = true, bool groupByShapeMeta = true,
                                                  Action <IAttributeCollection> attributesCallback = null, Func <string, string, bool> isRaw = null)
        {
            if (route == null)
            {
                throw new ArgumentNullException(nameof(route));
            }
            if (jsonWriter == null)
            {
                throw new ArgumentNullException(nameof(jsonWriter));
            }

            if (groupByShapeMeta)
            { // group by shape meta.
                if (route.Shape != null && route.ShapeMeta != null)
                {
                    for (var i = 0; i < route.ShapeMeta.Count; i++)
                    {
                        var shapeMeta  = route.ShapeMeta[i];
                        var lowerShape = -1;
                        if (i > 0)
                        {
                            lowerShape = route.ShapeMeta[i - 1].Shape;
                        }
                        var higherShape = route.ShapeMeta[i].Shape;
                        if (lowerShape >= higherShape)
                        {
                            throw new Exception($"Invalid route: Shapes overlap.");
                        }

                        var coordinates = new List <Coordinate>();
                        for (var shape = lowerShape; shape <= higherShape; shape++)
                        {
                            if (shape >= 0 && shape < route.Shape.Count)
                            {
                                coordinates.Add(route.Shape[shape]);
                            }
                        }

                        if (coordinates.Count >= 2)
                        {
                            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();

                            foreach (var t in coordinates)
                            {
                                jsonWriter.WriteArrayOpen();
                                jsonWriter.WriteArrayValue(t.Longitude.ToInvariantString());
                                jsonWriter.WriteArrayValue(t.Latitude.ToInvariantString());
                                if (t.Elevation.HasValue)
                                {
                                    jsonWriter.WriteArrayValue(t.Elevation.Value.ToInvariantString());
                                }
                                jsonWriter.WriteArrayClose();
                            }

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

                            jsonWriter.WritePropertyName("properties");
                            jsonWriter.WriteOpen();
                            if (shapeMeta.Attributes != null)
                            {
                                var attributes = shapeMeta.Attributes;
                                if (attributesCallback != null)
                                {
                                    attributes = new AttributeCollection(attributes);
                                    attributesCallback(attributes);
                                }
                                foreach (var attribute in attributes)
                                {
                                    var raw = isRaw != null &&
                                              isRaw(attribute.Key, attribute.Value);
                                    jsonWriter.WriteProperty(attribute.Key, attribute.Value, !raw, !raw);
                                }
                            }
                            jsonWriter.WriteClose();

                            jsonWriter.WriteClose();
                        }
                    }
                }

                if (route.Stops != null &&
                    includeStops)
                {
                    foreach (var stop in route.Stops)
                    {
                        jsonWriter.WriteOpen();
                        jsonWriter.WriteProperty("type", "Feature", true, false);
                        jsonWriter.WriteProperty("name", "Stop", true, false);
                        jsonWriter.WriteProperty("Shape", stop.Shape.ToInvariantString(), true, false);
                        jsonWriter.WritePropertyName("geometry", false);

                        jsonWriter.WriteOpen();
                        jsonWriter.WriteProperty("type", "Point", true, false);
                        jsonWriter.WritePropertyName("coordinates", false);
                        jsonWriter.WriteArrayOpen();
                        jsonWriter.WriteArrayValue(stop.Coordinate.Longitude.ToInvariantString());
                        jsonWriter.WriteArrayValue(stop.Coordinate.Latitude.ToInvariantString());
                        if (stop.Coordinate.Elevation.HasValue)
                        {
                            jsonWriter.WriteArrayValue(stop.Coordinate.Elevation.Value.ToInvariantString());
                        }
                        jsonWriter.WriteArrayClose();
                        jsonWriter.WriteClose();

                        jsonWriter.WritePropertyName("properties");
                        jsonWriter.WriteOpen();
                        if (stop.Attributes != null)
                        {
                            var attributes = stop.Attributes;
                            if (attributesCallback != null)
                            {
                                attributes = new AttributeCollection(attributes);
                                attributesCallback(attributes);
                            }
                            foreach (var attribute in attributes)
                            {
                                var raw = isRaw != null &&
                                          isRaw(attribute.Key, attribute.Value);
                                jsonWriter.WriteProperty(attribute.Key, attribute.Value, !raw, !raw);
                            }
                        }
                        jsonWriter.WriteClose();

                        jsonWriter.WriteClose();
                    }
                }
            }
            else
            { // include shape meta as points if requested.
                if (route.Shape != null)
                {
                    jsonWriter.WriteOpen();
                    jsonWriter.WriteProperty("type", "Feature", true, false);
                    jsonWriter.WriteProperty("name", "Shape", true, false);
                    jsonWriter.WritePropertyName("properties");
                    jsonWriter.WriteOpen();
                    jsonWriter.WriteClose();
                    jsonWriter.WritePropertyName("geometry", false);

                    jsonWriter.WriteOpen();
                    jsonWriter.WriteProperty("type", "LineString", true, false);
                    jsonWriter.WritePropertyName("coordinates", false);
                    jsonWriter.WriteArrayOpen();
                    foreach (var t in route.Shape)
                    {
                        jsonWriter.WriteArrayOpen();
                        jsonWriter.WriteArrayValue(t.Longitude.ToInvariantString());
                        jsonWriter.WriteArrayValue(t.Latitude.ToInvariantString());
                        if (t.Elevation.HasValue)
                        {
                            jsonWriter.WriteArrayValue(t.Elevation.Value.ToInvariantString());
                        }
                        jsonWriter.WriteArrayClose();
                    }
                    jsonWriter.WriteArrayClose();
                    jsonWriter.WriteClose();

                    if (attributesCallback != null)
                    {
                        jsonWriter.WritePropertyName("properties");
                        jsonWriter.WriteOpen();
                        var attributes = new AttributeCollection();
                        attributesCallback(attributes);
                        foreach (var attribute in attributes)
                        {
                            var raw = isRaw != null &&
                                      isRaw(attribute.Key, attribute.Value);
                            jsonWriter.WriteProperty(attribute.Key, attribute.Value, !raw, !raw);
                        }
                        jsonWriter.WriteClose();
                    }

                    jsonWriter.WriteClose();
                }

                if (route.ShapeMeta != null &&
                    includeShapeMeta)
                {
                    foreach (var meta in route.ShapeMeta)
                    {
                        jsonWriter.WriteOpen();
                        jsonWriter.WriteProperty("type", "Feature", true, false);
                        jsonWriter.WriteProperty("name", "ShapeMeta", true, false);
                        jsonWriter.WriteProperty("Shape", meta.Shape.ToInvariantString(), true, false);
                        jsonWriter.WritePropertyName("geometry", false);

                        if (route.Shape != null)
                        {
                            var coordinate = route.Shape[meta.Shape];

                            jsonWriter.WriteOpen();
                            jsonWriter.WriteProperty("type", "Point", true, false);
                            jsonWriter.WritePropertyName("coordinates", false);
                            jsonWriter.WriteArrayOpen();
                            jsonWriter.WriteArrayValue(coordinate.Longitude.ToInvariantString());
                            jsonWriter.WriteArrayValue(coordinate.Latitude.ToInvariantString());
                            if (coordinate.Elevation.HasValue)
                            {
                                jsonWriter.WriteArrayValue(coordinate.Elevation.Value.ToInvariantString());
                            }
                        }

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

                        jsonWriter.WritePropertyName("properties");
                        jsonWriter.WriteOpen();

                        if (meta.Attributes != null)
                        {
                            var attributes = meta.Attributes;
                            if (attributesCallback != null)
                            {
                                attributes = new AttributeCollection(attributes);
                                attributesCallback(attributes);
                            }
                            foreach (var attribute in attributes)
                            {
                                var raw = isRaw != null &&
                                          isRaw(attribute.Key, attribute.Value);
                                jsonWriter.WriteProperty(attribute.Key, attribute.Value, !raw, !raw);
                            }
                        }

                        jsonWriter.WriteClose();

                        jsonWriter.WriteClose();
                    }
                }

                if (route.Stops != null &&
                    includeStops)
                {
                    foreach (var stop in route.Stops)
                    {
                        jsonWriter.WriteOpen();
                        jsonWriter.WriteProperty("type", "Feature", true, false);
                        jsonWriter.WriteProperty("name", "Stop", true, false);
                        jsonWriter.WriteProperty("Shape", stop.Shape.ToInvariantString(), true, false);
                        jsonWriter.WritePropertyName("geometry", false);

                        jsonWriter.WriteOpen();
                        jsonWriter.WriteProperty("type", "Point", true, false);
                        jsonWriter.WritePropertyName("coordinates", false);
                        jsonWriter.WriteArrayOpen();
                        jsonWriter.WriteArrayValue(stop.Coordinate.Longitude.ToInvariantString());
                        jsonWriter.WriteArrayValue(stop.Coordinate.Latitude.ToInvariantString());
                        if (stop.Coordinate.Elevation.HasValue)
                        {
                            jsonWriter.WriteArrayValue(stop.Coordinate.Elevation.Value.ToInvariantString());
                        }
                        jsonWriter.WriteArrayClose();
                        jsonWriter.WriteClose();

                        jsonWriter.WritePropertyName("properties");
                        jsonWriter.WriteOpen();
                        if (stop.Attributes != null)
                        {
                            var attributes = stop.Attributes;
                            if (attributesCallback != null)
                            {
                                attributes = new AttributeCollection(attributes);
                                attributesCallback(attributes);
                            }
                            foreach (var attribute in attributes)
                            {
                                var raw = isRaw != null &&
                                          isRaw(attribute.Key, attribute.Value);
                                jsonWriter.WriteProperty(attribute.Key, attribute.Value, !raw, !raw);
                            }
                        }
                        jsonWriter.WriteClose();

                        jsonWriter.WriteClose();
                    }
                }
            }
        }