| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Text.Json; |
| | 7 | | using System.Text.Json.Serialization; |
| | 8 | |
|
| | 9 | | namespace Azure.Core.GeoJson |
| | 10 | | { |
| | 11 | | /// <summary> |
| | 12 | | /// Converts a <see cref="GeoObject"/> value from and to JSON in GeoJSON format. |
| | 13 | | /// </summary> |
| | 14 | | public sealed class GeoJsonConverter : JsonConverter<GeoObject> |
| | 15 | | { |
| | 16 | | private const string PointType = "Point"; |
| | 17 | | private const string LineStringType = "LineString"; |
| | 18 | | private const string MultiPointType = "MultiPoint"; |
| | 19 | | private const string PolygonType = "Polygon"; |
| | 20 | | private const string MultiLineStringType = "MultiLineString"; |
| | 21 | | private const string MultiPolygonType = "MultiPolygon"; |
| | 22 | | private const string GeometryCollectionType = "GeometryCollection"; |
| | 23 | | private const string TypeProperty = "type"; |
| | 24 | | private const string GeometriesProperty = "geometries"; |
| | 25 | | private const string CoordinatesProperty = "coordinates"; |
| | 26 | | private const string BBoxProperty = "bbox"; |
| | 27 | |
|
| | 28 | | /// <inheritdoc /> |
| | 29 | | public override bool CanConvert(Type typeToConvert) |
| | 30 | | { |
| 74 | 31 | | return typeof(GeoObject).IsAssignableFrom(typeToConvert); |
| | 32 | | } |
| | 33 | |
|
| | 34 | | /// <inheritdoc /> |
| | 35 | | public override GeoObject Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| | 36 | | { |
| 72 | 37 | | var document = JsonDocument.ParseValue(ref reader); |
| 72 | 38 | | return Read(document.RootElement); |
| | 39 | | } |
| | 40 | |
|
| | 41 | | /// <inheritdoc /> |
| | 42 | | public override void Write(Utf8JsonWriter writer, GeoObject value, JsonSerializerOptions options) |
| | 43 | | { |
| 74 | 44 | | Write(writer, value); |
| 74 | 45 | | } |
| | 46 | |
|
| | 47 | | internal static GeoObject Read(JsonElement element) |
| | 48 | | { |
| 176 | 49 | | JsonElement typeProperty = GetRequiredProperty(element, TypeProperty); |
| | 50 | |
|
| 176 | 51 | | string type = typeProperty.GetString(); |
| | 52 | |
|
| 176 | 53 | | GeoBoundingBox? boundingBox = ReadBoundingBox(element); |
| | 54 | |
|
| 176 | 55 | | if (type == GeometryCollectionType) |
| | 56 | | { |
| 16 | 57 | | var geometries = new List<GeoObject>(); |
| 96 | 58 | | foreach (var geometry in GetRequiredProperty(element, GeometriesProperty).EnumerateArray()) |
| | 59 | | { |
| 32 | 60 | | geometries.Add(Read(geometry)); |
| | 61 | | } |
| | 62 | |
|
| 16 | 63 | | return new GeoCollection(geometries, boundingBox, ReadAdditionalProperties(element, GeometriesProperty)) |
| | 64 | | } |
| | 65 | |
|
| 160 | 66 | | IReadOnlyDictionary<string, object?> additionalProperties = ReadAdditionalProperties(element); |
| 160 | 67 | | JsonElement coordinates = GetRequiredProperty(element, CoordinatesProperty); |
| | 68 | |
|
| | 69 | | switch (type) |
| | 70 | | { |
| | 71 | | case PointType: |
| 64 | 72 | | return new GeoPoint(ReadCoordinate(coordinates), boundingBox, additionalProperties); |
| | 73 | | case LineStringType: |
| 16 | 74 | | return new GeoLine(ReadCoordinates(coordinates), boundingBox, additionalProperties); |
| | 75 | | case MultiPointType: |
| 16 | 76 | | var points = new List<GeoPoint>(); |
| 96 | 77 | | foreach (GeoPosition coordinate in ReadCoordinates(coordinates)) |
| | 78 | | { |
| 32 | 79 | | points.Add(new GeoPoint(coordinate, null, GeoObject.DefaultProperties)); |
| | 80 | | } |
| | 81 | |
|
| 16 | 82 | | return new GeoPointCollection(points, boundingBox, additionalProperties); |
| | 83 | |
|
| | 84 | | case PolygonType: |
| 32 | 85 | | var rings = new List<GeoLine>(); |
| 160 | 86 | | foreach (JsonElement ringArray in coordinates.EnumerateArray()) |
| | 87 | | { |
| 48 | 88 | | rings.Add(new GeoLine(ReadCoordinates(ringArray), null, GeoObject.DefaultProperties)); |
| | 89 | | } |
| | 90 | |
|
| 32 | 91 | | return new GeoPolygon(rings, boundingBox, additionalProperties); |
| | 92 | |
|
| | 93 | | case MultiLineStringType: |
| 16 | 94 | | var lineStrings = new List<GeoLine>(); |
| 96 | 95 | | foreach (JsonElement ringArray in coordinates.EnumerateArray()) |
| | 96 | | { |
| 32 | 97 | | lineStrings.Add(new GeoLine(ReadCoordinates(ringArray), null, GeoObject.DefaultProperties)); |
| | 98 | | } |
| | 99 | |
|
| 16 | 100 | | return new GeoLineCollection(lineStrings, boundingBox, additionalProperties); |
| | 101 | |
|
| | 102 | | case MultiPolygonType: |
| | 103 | |
|
| 16 | 104 | | var polygons = new List<GeoPolygon>(); |
| 96 | 105 | | foreach (JsonElement polygon in coordinates.EnumerateArray()) |
| | 106 | | { |
| 32 | 107 | | var polygonRings = new List<GeoLine>(); |
| 160 | 108 | | foreach (JsonElement ringArray in polygon.EnumerateArray()) |
| | 109 | | { |
| 48 | 110 | | polygonRings.Add(new GeoLine(ReadCoordinates(ringArray), null, GeoObject.DefaultProperties)) |
| | 111 | | } |
| | 112 | |
|
| 32 | 113 | | polygons.Add(new GeoPolygon(polygonRings)); |
| | 114 | | } |
| | 115 | |
|
| 16 | 116 | | return new GeoPolygonCollection(polygons, boundingBox, additionalProperties); |
| | 117 | |
|
| | 118 | | default: |
| 0 | 119 | | throw new NotSupportedException($"Unsupported geometry type '{type}' "); |
| | 120 | | } |
| | 121 | | } |
| | 122 | |
|
| | 123 | | private static GeoBoundingBox? ReadBoundingBox(in JsonElement element) |
| | 124 | | { |
| 176 | 125 | | GeoBoundingBox? bbox = null; |
| | 126 | |
|
| 176 | 127 | | if (element.TryGetProperty(BBoxProperty, out JsonElement bboxElement)) |
| | 128 | | { |
| 16 | 129 | | var arrayLength = bboxElement.GetArrayLength(); |
| | 130 | |
|
| | 131 | | switch (arrayLength) |
| | 132 | | { |
| | 133 | | case 4: |
| 8 | 134 | | bbox = new GeoBoundingBox( |
| 8 | 135 | | bboxElement[0].GetDouble(), |
| 8 | 136 | | bboxElement[1].GetDouble(), |
| 8 | 137 | | bboxElement[2].GetDouble(), |
| 8 | 138 | | bboxElement[3].GetDouble() |
| 8 | 139 | | ); |
| 8 | 140 | | break; |
| | 141 | | case 6: |
| 8 | 142 | | bbox = new GeoBoundingBox( |
| 8 | 143 | | bboxElement[0].GetDouble(), |
| 8 | 144 | | bboxElement[1].GetDouble(), |
| 8 | 145 | | bboxElement[3].GetDouble(), |
| 8 | 146 | | bboxElement[4].GetDouble(), |
| 8 | 147 | | bboxElement[2].GetDouble(), |
| 8 | 148 | | bboxElement[5].GetDouble() |
| 8 | 149 | | ); |
| 8 | 150 | | break; |
| | 151 | | default: |
| 0 | 152 | | throw new JsonException("Only 2 or 3 element coordinates supported"); |
| | 153 | | } |
| | 154 | | } |
| | 155 | |
|
| 176 | 156 | | return bbox; |
| | 157 | | } |
| | 158 | |
|
| | 159 | | private static IReadOnlyDictionary<string, object?> ReadAdditionalProperties(in JsonElement element, string know |
| | 160 | | { |
| 176 | 161 | | Dictionary<string, object?>? additionalProperties = null; |
| 1312 | 162 | | foreach (var property in element.EnumerateObject()) |
| | 163 | | { |
| 480 | 164 | | var propertyName = property.Name; |
| 480 | 165 | | if (propertyName.Equals(TypeProperty, StringComparison.Ordinal) || |
| 480 | 166 | | propertyName.Equals(BBoxProperty, StringComparison.Ordinal) || |
| 480 | 167 | | propertyName.Equals(knownProperty, StringComparison.Ordinal)) |
| | 168 | | { |
| | 169 | | continue; |
| | 170 | | } |
| | 171 | |
|
| 112 | 172 | | additionalProperties ??= new Dictionary<string, object?>(); |
| 112 | 173 | | additionalProperties.Add(propertyName, ReadAdditionalPropertyValue(property.Value)); |
| | 174 | | } |
| | 175 | |
|
| 176 | 176 | | return additionalProperties ?? GeoObject.DefaultProperties; |
| | 177 | | } |
| | 178 | |
|
| | 179 | | private static object? ReadAdditionalPropertyValue(in JsonElement element) |
| | 180 | | { |
| 208 | 181 | | switch (element.ValueKind) |
| | 182 | | { |
| | 183 | | case JsonValueKind.String: |
| 32 | 184 | | return element.GetString(); |
| | 185 | | case JsonValueKind.Number: |
| 96 | 186 | | if (element.TryGetInt32(out int intValue)) |
| | 187 | | { |
| 32 | 188 | | return intValue; |
| | 189 | | } |
| 64 | 190 | | if (element.TryGetInt64(out long longValue)) |
| | 191 | | { |
| 0 | 192 | | return longValue; |
| | 193 | | } |
| 64 | 194 | | return element.GetDouble(); |
| | 195 | | case JsonValueKind.True: |
| 32 | 196 | | return true; |
| | 197 | | case JsonValueKind.False: |
| 0 | 198 | | return false; |
| | 199 | | case JsonValueKind.Undefined: |
| | 200 | | case JsonValueKind.Null: |
| 32 | 201 | | return null; |
| | 202 | | case JsonValueKind.Object: |
| 0 | 203 | | var dictionary = new Dictionary<string, object?>(); |
| 0 | 204 | | foreach (JsonProperty jsonProperty in element.EnumerateObject()) |
| | 205 | | { |
| 0 | 206 | | dictionary.Add(jsonProperty.Name, ReadAdditionalPropertyValue(jsonProperty.Value)); |
| | 207 | | } |
| 0 | 208 | | return dictionary; |
| | 209 | | case JsonValueKind.Array: |
| 16 | 210 | | var list = new List<object?>(); |
| 224 | 211 | | foreach (JsonElement item in element.EnumerateArray()) |
| | 212 | | { |
| 96 | 213 | | list.Add(ReadAdditionalPropertyValue(item)); |
| | 214 | | } |
| 16 | 215 | | return list.ToArray(); |
| | 216 | | default: |
| 0 | 217 | | throw new NotSupportedException("Not supported value kind " + element.ValueKind); |
| | 218 | | } |
| | 219 | | } |
| | 220 | |
|
| | 221 | | private static IReadOnlyList<GeoPosition> ReadCoordinates(JsonElement coordinatesElement) |
| | 222 | | { |
| 160 | 223 | | GeoPosition[] coordinates = new GeoPosition[coordinatesElement.GetArrayLength()]; |
| | 224 | |
|
| 160 | 225 | | int i = 0; |
| 1536 | 226 | | foreach (JsonElement coordinate in coordinatesElement.EnumerateArray()) |
| | 227 | | { |
| 608 | 228 | | coordinates[i] = ReadCoordinate(coordinate); |
| 608 | 229 | | i++; |
| | 230 | | } |
| | 231 | |
|
| 160 | 232 | | return coordinates; |
| | 233 | | } |
| | 234 | |
|
| | 235 | | private static GeoPosition ReadCoordinate(JsonElement coordinate) |
| | 236 | | { |
| 672 | 237 | | var arrayLength = coordinate.GetArrayLength(); |
| 672 | 238 | | if (arrayLength < 2 || arrayLength > 3) |
| | 239 | | { |
| 0 | 240 | | throw new JsonException("Only 2 or 3 element coordinates supported"); |
| | 241 | | } |
| | 242 | |
|
| 672 | 243 | | var lon = coordinate[0].GetDouble(); |
| 672 | 244 | | var lat = coordinate[1].GetDouble(); |
| 672 | 245 | | double? altitude = null; |
| | 246 | |
|
| 672 | 247 | | if (arrayLength > 2) |
| | 248 | | { |
| 336 | 249 | | altitude = coordinate[2].GetDouble(); |
| | 250 | | } |
| | 251 | |
|
| 672 | 252 | | return new GeoPosition(lon, lat, altitude); |
| | 253 | | } |
| | 254 | |
|
| | 255 | | internal static void Write(Utf8JsonWriter writer, GeoObject value) |
| | 256 | | { |
| | 257 | | void WritePositionValues(GeoPosition type) |
| | 258 | | { |
| 506 | 259 | | writer.WriteNumberValue(type.Longitude); |
| 506 | 260 | | writer.WriteNumberValue(type.Latitude); |
| 506 | 261 | | if (type.Altitude != null) |
| | 262 | | { |
| 252 | 263 | | writer.WriteNumberValue(type.Altitude.Value); |
| | 264 | | } |
| 506 | 265 | | } |
| | 266 | |
|
| | 267 | | void WriteType(string type) |
| | 268 | | { |
| 134 | 269 | | writer.WriteString(TypeProperty, type); |
| 134 | 270 | | } |
| | 271 | |
|
| | 272 | | void WritePosition(GeoPosition type) |
| | 273 | | { |
| 506 | 274 | | writer.WriteStartArray(); |
| 506 | 275 | | WritePositionValues(type); |
| | 276 | |
|
| 506 | 277 | | writer.WriteEndArray(); |
| 506 | 278 | | } |
| | 279 | |
|
| | 280 | | void WritePositions(IEnumerable<GeoPosition> positions) |
| | 281 | | { |
| 108 | 282 | | writer.WriteStartArray(); |
| 1080 | 283 | | foreach (var position in positions) |
| | 284 | | { |
| 432 | 285 | | WritePosition(position); |
| | 286 | | } |
| | 287 | |
|
| 108 | 288 | | writer.WriteEndArray(); |
| 108 | 289 | | } |
| | 290 | |
|
| 134 | 291 | | writer.WriteStartObject(); |
| | 292 | | switch (value) |
| | 293 | | { |
| | 294 | | case GeoPoint point: |
| 50 | 295 | | WriteType(PointType); |
| 50 | 296 | | writer.WritePropertyName(CoordinatesProperty); |
| 50 | 297 | | WritePosition(point.Position); |
| 50 | 298 | | break; |
| | 299 | |
|
| | 300 | | case GeoLine lineString: |
| 12 | 301 | | WriteType(LineStringType); |
| 12 | 302 | | writer.WritePropertyName(CoordinatesProperty); |
| 12 | 303 | | WritePositions(lineString.Positions); |
| 12 | 304 | | break; |
| | 305 | |
|
| | 306 | | case GeoPolygon polygon: |
| 24 | 307 | | WriteType(PolygonType); |
| 24 | 308 | | writer.WritePropertyName(CoordinatesProperty); |
| 24 | 309 | | writer.WriteStartArray(); |
| 120 | 310 | | foreach (var ring in polygon.Rings) |
| | 311 | | { |
| 36 | 312 | | WritePositions(ring.Positions); |
| | 313 | | } |
| | 314 | |
|
| 24 | 315 | | writer.WriteEndArray(); |
| 24 | 316 | | break; |
| | 317 | |
|
| | 318 | | case GeoPointCollection multiPoint: |
| 12 | 319 | | WriteType(MultiPointType); |
| 12 | 320 | | writer.WritePropertyName(CoordinatesProperty); |
| 12 | 321 | | writer.WriteStartArray(); |
| 72 | 322 | | foreach (var point in multiPoint.Points) |
| | 323 | | { |
| 24 | 324 | | WritePosition(point.Position); |
| | 325 | | } |
| | 326 | |
|
| 12 | 327 | | writer.WriteEndArray(); |
| 12 | 328 | | break; |
| | 329 | |
|
| | 330 | | case GeoLineCollection multiLineString: |
| 12 | 331 | | WriteType(MultiLineStringType); |
| 12 | 332 | | writer.WritePropertyName(CoordinatesProperty); |
| 12 | 333 | | writer.WriteStartArray(); |
| 72 | 334 | | foreach (var lineString in multiLineString.Lines) |
| | 335 | | { |
| 24 | 336 | | WritePositions(lineString.Positions); |
| | 337 | | } |
| | 338 | |
|
| 12 | 339 | | writer.WriteEndArray(); |
| 12 | 340 | | break; |
| | 341 | |
|
| | 342 | | case GeoPolygonCollection multiPolygon: |
| 12 | 343 | | WriteType(MultiPolygonType); |
| 12 | 344 | | writer.WritePropertyName(CoordinatesProperty); |
| 12 | 345 | | writer.WriteStartArray(); |
| 72 | 346 | | foreach (var polygon in multiPolygon.Polygons) |
| | 347 | | { |
| 24 | 348 | | writer.WriteStartArray(); |
| 120 | 349 | | foreach (var polygonRing in polygon.Rings) |
| | 350 | | { |
| 36 | 351 | | WritePositions(polygonRing.Positions); |
| | 352 | | } |
| 24 | 353 | | writer.WriteEndArray(); |
| | 354 | | } |
| | 355 | |
|
| 12 | 356 | | writer.WriteEndArray(); |
| 12 | 357 | | break; |
| | 358 | |
|
| | 359 | | case GeoCollection geometryCollection: |
| 12 | 360 | | WriteType(GeometryCollectionType); |
| 12 | 361 | | writer.WritePropertyName(GeometriesProperty); |
| 12 | 362 | | writer.WriteStartArray(); |
| 72 | 363 | | foreach (var geometry in geometryCollection.Geometries) |
| | 364 | | { |
| 24 | 365 | | Write(writer, geometry); |
| | 366 | | } |
| | 367 | |
|
| 12 | 368 | | writer.WriteEndArray(); |
| 12 | 369 | | break; |
| | 370 | |
|
| | 371 | | default: |
| 0 | 372 | | throw new NotSupportedException($"Geometry type '{value?.GetType()}' not supported"); |
| | 373 | | } |
| | 374 | |
|
| 134 | 375 | | if (value.BoundingBox is GeoBoundingBox bbox) |
| | 376 | | { |
| 12 | 377 | | writer.WritePropertyName(BBoxProperty); |
| 12 | 378 | | writer.WriteStartArray(); |
| 12 | 379 | | writer.WriteNumberValue(bbox.West); |
| 12 | 380 | | writer.WriteNumberValue(bbox.South); |
| 12 | 381 | | if (bbox.MinAltitude != null) |
| | 382 | | { |
| 6 | 383 | | writer.WriteNumberValue(bbox.MinAltitude.Value); |
| | 384 | | } |
| 12 | 385 | | writer.WriteNumberValue(bbox.East); |
| 12 | 386 | | writer.WriteNumberValue(bbox.North); |
| 12 | 387 | | if (bbox.MaxAltitude != null) |
| | 388 | | { |
| 6 | 389 | | writer.WriteNumberValue(bbox.MaxAltitude.Value); |
| | 390 | | } |
| 12 | 391 | | writer.WriteEndArray(); |
| | 392 | | } |
| | 393 | |
|
| 436 | 394 | | foreach (var additionalProperty in value.AdditionalProperties) |
| | 395 | | { |
| 84 | 396 | | writer.WritePropertyName(additionalProperty.Key); |
| 84 | 397 | | WriteAdditionalPropertyValue(writer, additionalProperty.Value); |
| | 398 | | } |
| | 399 | |
|
| 134 | 400 | | writer.WriteEndObject(); |
| 134 | 401 | | } |
| | 402 | | private static void WriteAdditionalPropertyValue(Utf8JsonWriter writer, object? value) |
| | 403 | | { |
| | 404 | | switch (value) |
| | 405 | | { |
| | 406 | | case null: |
| 24 | 407 | | writer.WriteNullValue(); |
| 24 | 408 | | break; |
| | 409 | | case int i: |
| 24 | 410 | | writer.WriteNumberValue(i); |
| 24 | 411 | | break; |
| | 412 | | case double d: |
| 48 | 413 | | writer.WriteNumberValue(d); |
| 48 | 414 | | break; |
| | 415 | | case float f: |
| 0 | 416 | | writer.WriteNumberValue(f); |
| 0 | 417 | | break; |
| | 418 | | case long l: |
| 0 | 419 | | writer.WriteNumberValue(l); |
| 0 | 420 | | break; |
| | 421 | | case string s: |
| 24 | 422 | | writer.WriteStringValue(s); |
| 24 | 423 | | break; |
| | 424 | | case bool b: |
| 24 | 425 | | writer.WriteBooleanValue(b); |
| 24 | 426 | | break; |
| | 427 | | case IEnumerable<KeyValuePair<string, object?>> enumerable: |
| 0 | 428 | | writer.WriteStartObject(); |
| 0 | 429 | | foreach (KeyValuePair<string, object?> pair in enumerable) |
| | 430 | | { |
| 0 | 431 | | writer.WritePropertyName(pair.Key); |
| 0 | 432 | | WriteAdditionalPropertyValue(writer, pair.Value); |
| | 433 | | } |
| 0 | 434 | | writer.WriteEndObject(); |
| 0 | 435 | | break; |
| | 436 | | case IEnumerable<object?> objectEnumerable: |
| 12 | 437 | | writer.WriteStartArray(); |
| 168 | 438 | | foreach (object? item in objectEnumerable) |
| | 439 | | { |
| 72 | 440 | | WriteAdditionalPropertyValue(writer, item); |
| | 441 | | } |
| 12 | 442 | | writer.WriteEndArray(); |
| 12 | 443 | | break; |
| | 444 | |
|
| | 445 | | default: |
| 0 | 446 | | throw new NotSupportedException("Not supported type " + value.GetType()); |
| | 447 | | } |
| | 448 | | } |
| | 449 | |
|
| | 450 | | private static JsonElement GetRequiredProperty(JsonElement element, string name) |
| | 451 | | { |
| 352 | 452 | | if (!element.TryGetProperty(name, out JsonElement property)) |
| | 453 | | { |
| 0 | 454 | | throw new JsonException($"GeoJSON object expected to have '{name}' property."); |
| | 455 | | } |
| | 456 | |
|
| 352 | 457 | | return property; |
| | 458 | | } |
| | 459 | | } |
| | 460 | | } |