< Summary

Class:Itinero.IO.Json.GeoJson.RouteExtensions
Assembly:Itinero
File(s):/home/runner/work/routing2/routing2/src/Itinero/IO/Json/GeoJson/RouteExtensions.cs
Covered lines:0
Uncovered lines:48
Coverable lines:48
Total lines:214
Line coverage:0% (0 of 48)
Covered branches:0
Total branches:6
Branch coverage:0% (0 of 6)
Tag:224_14471318300

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
ToGeoJson(...)100%10%
ToGeoJson(...)0%20%
WriteFeatures(...)0%40%

File(s)

/home/runner/work/routing2/routing2/src/Itinero/IO/Json/GeoJson/RouteExtensions.cs

#LineLine coverage
 1using System.Collections.Generic;
 2using System.IO;
 3using System.Text;
 4using System.Text.Json;
 5using Itinero.Routes;
 6
 7namespace Itinero.IO.Json.GeoJson;
 8
 9/// <summary>
 10/// Contains route geojson extensions
 11/// </summary>
 12public static class RouteExtensions
 13{
 14    /// <summary>
 15    /// Returns geojson for the given route.
 16    /// </summary>
 17    /// <param name="route">The route.</param>
 18    /// <returns>A geojson string.</returns>
 19    public static string ToGeoJson(this Route route)
 020    {
 021        using var stream = new MemoryStream();
 022        using (var jsonWriter = new Utf8JsonWriter(stream))
 023        {
 024            jsonWriter.WriteFeatureCollectionStart();
 025            jsonWriter.WriteFeatures(route);
 026            jsonWriter.WriteFeatureCollectionEnd();
 027        }
 28
 029        return Encoding.UTF8.GetString(stream.ToArray());
 030    }
 31
 32    public static string ToGeoJson(this IReadOnlyList<Route> routes)
 033    {
 034        using (var stream = new MemoryStream())
 035        {
 036            using (var jsonWriter = new Utf8JsonWriter(stream))
 037            {
 038                jsonWriter.WriteFeatureCollectionStart();
 039                foreach (var route in routes)
 040                {
 041                    jsonWriter.WriteFeatures(route);
 042                }
 043                jsonWriter.WriteFeatureCollectionEnd();
 044            }
 45
 046            return Encoding.UTF8.GetString(stream.ToArray());
 47        }
 048    }
 49
 50    /// <summary>
 51    /// Writes a few features representing the given route.
 52    /// </summary>
 53    /// <param name="route">The route.</param>
 54    /// <param name="jsonWriter">The json writer.</param>
 55    public static void WriteFeatures(this Utf8JsonWriter jsonWriter, Route route)
 056    {
 057        if (route.Shape != null)
 058        {
 059            jsonWriter.WriteFeatureStart();
 60
 061            jsonWriter.WritePropertyName("properties");
 062            jsonWriter.WriteStartObject();
 063            jsonWriter.WriteEndObject();
 64
 065            jsonWriter.WritePropertyName("geometry");
 66
 067            jsonWriter.WriteStartObject();
 068            jsonWriter.WriteString("type", "LineString");
 069            jsonWriter.WritePropertyName("coordinates");
 070            jsonWriter.WriteStartArray();
 071            for (var i = 0; i < route.Shape.Count; i++)
 072            {
 073                jsonWriter.WriteStartArray();
 074                jsonWriter.WriteNumberValue(route.Shape[i].longitude);
 075                jsonWriter.WriteNumberValue(route.Shape[i].latitude);
 76                //                    if (route.Shape[i].Elevation.HasValue)
 77                //                    {
 78                //                        jsonWriter.WriteArrayValue(route.Shape[i].Elevation.Value.ToInvariantString())
 79                //                    }
 80
 081                jsonWriter.WriteEndArray();
 082            }
 83
 084            jsonWriter.WriteEndArray();
 085            jsonWriter.WriteEndObject();
 86
 87            //                if (attributesCallback != null)
 88            //                {
 89            //                    jsonWriter.WritePropertyName("properties");
 90            //                    jsonWriter.WriteOpen();
 91            //                    var attributes = new AttributeCollection();
 92            //                    attributesCallback(attributes);
 93            //                    foreach (var attribute in attributes)
 94            //                    {
 95            //                        var raw = isRaw != null &&
 96            //                                  isRaw(attribute.Key, attribute.Value);
 97            //                        jsonWriter.WriteProperty(attribute.Key, attribute.Value, !raw, !raw);
 98            //                    }
 99            //
 100            //                    jsonWriter.WriteClose();
 101            //                }
 102
 0103            jsonWriter.WriteFeatureEnd();
 0104        }
 105
 106        //
 107        //            if (route.ShapeMeta != null &&
 108        //                includeShapeMeta)
 109        //            {
 110        //                for (var i = 0; i < route.ShapeMeta.Length; i++)
 111        //                {
 112        //                    var meta = route.ShapeMeta[i];
 113        //
 114        //                    jsonWriter.WriteOpen();
 115        //                    jsonWriter.WriteProperty("type", "Feature", true, false);
 116        //                    jsonWriter.WriteProperty("name", "ShapeMeta", true, false);
 117        //                    jsonWriter.WriteProperty("Shape", meta.Shape.ToInvariantString(), true, false);
 118        //                    jsonWriter.WritePropertyName("geometry", false);
 119        //
 120        //                    var coordinate = route.Shape[meta.Shape];
 121        //
 122        //                    jsonWriter.WriteOpen();
 123        //                    jsonWriter.WriteProperty("type", "Point", true, false);
 124        //                    jsonWriter.WritePropertyName("coordinates", false);
 125        //                    jsonWriter.WriteArrayOpen();
 126        //                    jsonWriter.WriteArrayValue(coordinate.Longitude.ToInvariantString());
 127        //                    jsonWriter.WriteArrayValue(coordinate.Latitude.ToInvariantString());
 128        //                    if (coordinate.Elevation.HasValue)
 129        //                    {
 130        //                        jsonWriter.WriteArrayValue(coordinate.Elevation.Value.ToInvariantString());
 131        //                    }
 132        //
 133        //                    jsonWriter.WriteArrayClose();
 134        //                    jsonWriter.WriteClose();
 135        //
 136        //                    jsonWriter.WritePropertyName("properties");
 137        //                    jsonWriter.WriteOpen();
 138        //
 139        //                    if (meta.Attributes != null)
 140        //                    {
 141        //                        var attributes = meta.Attributes;
 142        //                        if (attributesCallback != null)
 143        //                        {
 144        //                            attributes = new AttributeCollection(attributes);
 145        //                            attributesCallback(attributes);
 146        //                        }
 147        //
 148        //                        foreach (var attribute in attributes)
 149        //                        {
 150        //                            var raw = isRaw != null &&
 151        //                                      isRaw(attribute.Key, attribute.Value);
 152        //                            jsonWriter.WriteProperty(attribute.Key, attribute.Value, !raw, !raw);
 153        //                        }
 154        //                    }
 155        //
 156        //                    jsonWriter.WriteClose();
 157        //
 158        //                    jsonWriter.WriteClose();
 159        //                }
 160        //            }
 161        //
 162        //            if (route.Stops != null &&
 163        //                includeStops)
 164        //            {
 165        //                for (var i = 0; i < route.Stops.Length; i++)
 166        //                {
 167        //                    var stop = route.Stops[i];
 168        //
 169        //                    jsonWriter.WriteOpen();
 170        //                    jsonWriter.WriteProperty("type", "Feature", true, false);
 171        //                    jsonWriter.WriteProperty("name", "Stop", true, false);
 172        //                    jsonWriter.WriteProperty("Shape", stop.Shape.ToInvariantString(), true, false);
 173        //                    jsonWriter.WritePropertyName("geometry", false);
 174        //
 175        //                    jsonWriter.WriteOpen();
 176        //                    jsonWriter.WriteProperty("type", "Point", true, false);
 177        //                    jsonWriter.WritePropertyName("coordinates", false);
 178        //                    jsonWriter.WriteArrayOpen();
 179        //                    jsonWriter.WriteArrayValue(stop.Coordinate.Longitude.ToInvariantString());
 180        //                    jsonWriter.WriteArrayValue(stop.Coordinate.Latitude.ToInvariantString());
 181        //                    if (stop.Coordinate.Elevation.HasValue)
 182        //                    {
 183        //                        jsonWriter.WriteArrayValue(stop.Coordinate.Elevation.Value.ToInvariantString());
 184        //                    }
 185        //
 186        //                    jsonWriter.WriteArrayClose();
 187        //                    jsonWriter.WriteClose();
 188        //
 189        //                    jsonWriter.WritePropertyName("properties");
 190        //                    jsonWriter.WriteOpen();
 191        //                    if (stop.Attributes != null)
 192        //                    {
 193        //                        var attributes = stop.Attributes;
 194        //                        if (attributesCallback != null)
 195        //                        {
 196        //                            attributes = new AttributeCollection(attributes);
 197        //                            attributesCallback(attributes);
 198        //                        }
 199        //
 200        //                        foreach (var attribute in attributes)
 201        //                        {
 202        //                            var raw = isRaw != null &&
 203        //                                      isRaw(attribute.Key, attribute.Value);
 204        //                            jsonWriter.WriteProperty(attribute.Key, attribute.Value, !raw, !raw);
 205        //                        }
 206        //                    }
 207        //
 208        //                    jsonWriter.WriteClose();
 209        //
 210        //                    jsonWriter.WriteClose();
 211        //                }
 212        //            }
 0213    }
 214}