| | | 1 | | using System.Collections.Generic; |
| | | 2 | | using System.IO; |
| | | 3 | | using System.Text; |
| | | 4 | | using System.Text.Json; |
| | | 5 | | using Itinero.Routes; |
| | | 6 | | |
| | | 7 | | namespace Itinero.IO.Json.GeoJson; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Contains route geojson extensions |
| | | 11 | | /// </summary> |
| | | 12 | | public 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) |
| | 0 | 20 | | { |
| | 0 | 21 | | using var stream = new MemoryStream(); |
| | 0 | 22 | | using (var jsonWriter = new Utf8JsonWriter(stream)) |
| | 0 | 23 | | { |
| | 0 | 24 | | jsonWriter.WriteFeatureCollectionStart(); |
| | 0 | 25 | | jsonWriter.WriteFeatures(route); |
| | 0 | 26 | | jsonWriter.WriteFeatureCollectionEnd(); |
| | 0 | 27 | | } |
| | | 28 | | |
| | 0 | 29 | | return Encoding.UTF8.GetString(stream.ToArray()); |
| | 0 | 30 | | } |
| | | 31 | | |
| | | 32 | | public static string ToGeoJson(this IReadOnlyList<Route> routes) |
| | 0 | 33 | | { |
| | 0 | 34 | | using (var stream = new MemoryStream()) |
| | 0 | 35 | | { |
| | 0 | 36 | | using (var jsonWriter = new Utf8JsonWriter(stream)) |
| | 0 | 37 | | { |
| | 0 | 38 | | jsonWriter.WriteFeatureCollectionStart(); |
| | 0 | 39 | | foreach (var route in routes) |
| | 0 | 40 | | { |
| | 0 | 41 | | jsonWriter.WriteFeatures(route); |
| | 0 | 42 | | } |
| | 0 | 43 | | jsonWriter.WriteFeatureCollectionEnd(); |
| | 0 | 44 | | } |
| | | 45 | | |
| | 0 | 46 | | return Encoding.UTF8.GetString(stream.ToArray()); |
| | | 47 | | } |
| | 0 | 48 | | } |
| | | 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) |
| | 0 | 56 | | { |
| | 0 | 57 | | if (route.Shape != null) |
| | 0 | 58 | | { |
| | 0 | 59 | | jsonWriter.WriteFeatureStart(); |
| | | 60 | | |
| | 0 | 61 | | jsonWriter.WritePropertyName("properties"); |
| | 0 | 62 | | jsonWriter.WriteStartObject(); |
| | 0 | 63 | | jsonWriter.WriteEndObject(); |
| | | 64 | | |
| | 0 | 65 | | jsonWriter.WritePropertyName("geometry"); |
| | | 66 | | |
| | 0 | 67 | | jsonWriter.WriteStartObject(); |
| | 0 | 68 | | jsonWriter.WriteString("type", "LineString"); |
| | 0 | 69 | | jsonWriter.WritePropertyName("coordinates"); |
| | 0 | 70 | | jsonWriter.WriteStartArray(); |
| | 0 | 71 | | for (var i = 0; i < route.Shape.Count; i++) |
| | 0 | 72 | | { |
| | 0 | 73 | | jsonWriter.WriteStartArray(); |
| | 0 | 74 | | jsonWriter.WriteNumberValue(route.Shape[i].longitude); |
| | 0 | 75 | | 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 | | |
| | 0 | 81 | | jsonWriter.WriteEndArray(); |
| | 0 | 82 | | } |
| | | 83 | | |
| | 0 | 84 | | jsonWriter.WriteEndArray(); |
| | 0 | 85 | | 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 | | |
| | 0 | 103 | | jsonWriter.WriteFeatureEnd(); |
| | 0 | 104 | | } |
| | | 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 | | // } |
| | 0 | 213 | | } |
| | | 214 | | } |