| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. See License.txt in the project root for |
| | 3 | | // license information. |
| | 4 | |
|
| | 5 | | using System.Collections.Generic; |
| | 6 | | using Microsoft.Spatial; |
| | 7 | | using Newtonsoft.Json; |
| | 8 | | using Newtonsoft.Json.Linq; |
| | 9 | |
|
| | 10 | | namespace Microsoft.Azure.Search.Serialization |
| | 11 | | { |
| | 12 | | /// <summary> |
| | 13 | | /// Defines extension methods for various JSON.NET types that make it easier to recognize and read Geo-JSON. |
| | 14 | | /// </summary> |
| | 15 | | public static class GeoJsonExtensions |
| | 16 | | { |
| | 17 | | private const string Coordinates = "coordinates"; |
| | 18 | | private const string Crs = "crs"; |
| | 19 | | private const string Name = "name"; |
| | 20 | | private const string Point = "Point"; |
| | 21 | | private const string Properties = "properties"; |
| | 22 | | private const string Type = "type"; |
| | 23 | | private const string WorldGeodeticSystem1984 = "EPSG:4326"; // See https://epsg.io/4326 |
| | 24 | |
|
| 2 | 25 | | private static readonly IEnumerable<string> CrsOnly = new[] { Crs }; |
| 2 | 26 | | private static readonly IEnumerable<string> NameOnly = new[] { Name }; |
| 2 | 27 | | private static readonly IEnumerable<string> TypeAndCoordinates = new[] { Type, Coordinates }; |
| 2 | 28 | | private static readonly IEnumerable<string> TypeAndProperties = new[] { Type, Properties }; |
| | 29 | |
|
| | 30 | | /// <summary> |
| | 31 | | /// Determines whether the given <c cref="JObject">JObject</c> is a valid Geo-JSON point. |
| | 32 | | /// </summary> |
| | 33 | | /// <param name="obj">The JSON object to test.</param> |
| | 34 | | /// <returns><c>true</c> if the JSON object is not null and is a valid Geo-JSON point, <c>false</c> otherwise.</ |
| | 35 | | public static bool IsGeoJsonPoint(this JObject obj) => |
| 316 | 36 | | obj?.IsValid( |
| 316 | 37 | | requiredProperties: TypeAndCoordinates, |
| 316 | 38 | | isPropertyValid: property => |
| 316 | 39 | | { |
| 876 | 40 | | switch (property.Name) |
| 316 | 41 | | { |
| 316 | 42 | | case Type: |
| 454 | 43 | | return property.Value.IsString(Point); |
| 316 | 44 | |
|
| 316 | 45 | | case Coordinates: |
| 450 | 46 | | return AreCoordinates(property.Value); |
| 316 | 47 | |
|
| 316 | 48 | | case Crs: |
| 428 | 49 | | return property.Value is JObject possibleCrs && IsCrs(possibleCrs); |
| 316 | 50 | |
|
| 316 | 51 | | default: |
| 492 | 52 | | return false; |
| 316 | 53 | | } |
| 316 | 54 | | }) ?? false; |
| | 55 | |
|
| | 56 | | /// <summary> |
| | 57 | | /// Reads a Geo-JSON point into a <c cref="GeographyPoint">GeographyPoint</c> instance, or throws |
| | 58 | | /// <c cref="JsonSerializationException">JsonSerializationException</c> if the reader is not positioned on the |
| | 59 | | /// beginning of a valid Geo-JSON point. |
| | 60 | | /// </summary> |
| | 61 | | /// <param name="reader">The JSON reader from which to read a Geo-JSON point.</param> |
| | 62 | | /// <returns>A <c cref="GeographyPoint">GeographyPoint</c> instance.</returns> |
| | 63 | | public static GeographyPoint ReadGeoJsonPoint(this JsonReader reader) |
| | 64 | | { |
| | 65 | | // Check for null first. |
| 576 | 66 | | if (reader.TokenType == JsonToken.Null) |
| | 67 | | { |
| 90 | 68 | | return null; |
| | 69 | | } |
| | 70 | |
|
| 486 | 71 | | GeographyPoint result = null; |
| | 72 | |
|
| 486 | 73 | | reader.ReadObject( |
| 486 | 74 | | requiredProperties: TypeAndCoordinates, |
| 486 | 75 | | optionalProperties: CrsOnly, |
| 486 | 76 | | readProperty: (r, propertyName) => |
| 486 | 77 | | { |
| 486 | 78 | | switch (propertyName) |
| 486 | 79 | | { |
| 486 | 80 | | case Type: |
| 970 | 81 | | r.ExpectAndAdvance(JsonToken.String, Point); |
| 966 | 82 | | break; |
| 486 | 83 | |
|
| 486 | 84 | | case Coordinates: |
| 968 | 85 | | result = ReadCoordinates(r); |
| 968 | 86 | | break; |
| 486 | 87 | |
|
| 486 | 88 | | case Crs: |
| 946 | 89 | | ReadCrs(r); |
| 486 | 90 | | break; |
| 486 | 91 | | } |
| 942 | 92 | | }); |
| | 93 | |
|
| 474 | 94 | | return result; |
| | 95 | | } |
| | 96 | |
|
| | 97 | | /// <summary> |
| | 98 | | /// Writes a <c cref="GeographyPoint">GeographyPoint</c> instance as Geo-JSON format. |
| | 99 | | /// </summary> |
| | 100 | | /// <param name="writer">The JSON writer to which to write the Geo-JSON point.</param> |
| | 101 | | /// <param name="point">The <c cref="GeographyPoint">GeographyPoint</c> instance to write.</param> |
| | 102 | | public static void WriteGeoJsonPoint(this JsonWriter writer, GeographyPoint point) |
| | 103 | | { |
| 1994 | 104 | | writer.WriteStartObject(); |
| 1994 | 105 | | writer.WritePropertyName(Type); |
| 1994 | 106 | | writer.WriteValue(Point); |
| 1994 | 107 | | writer.WritePropertyName(Coordinates); |
| 1994 | 108 | | writer.WriteStartArray(); |
| 1994 | 109 | | writer.WriteValue(point.Longitude); |
| 1994 | 110 | | writer.WriteValue(point.Latitude); |
| 1994 | 111 | | writer.WriteEndArray(); |
| 1994 | 112 | | writer.WriteEndObject(); |
| 1994 | 113 | | } |
| | 114 | |
|
| | 115 | | private static bool IsCrsProperties(JObject possibleProperties) => |
| 112 | 116 | | possibleProperties.IsValid( |
| 112 | 117 | | requiredProperties: NameOnly, |
| 226 | 118 | | isPropertyValid: property => property.Name == Name && property.Value.IsString(WorldGeodeticSystem1984)); |
| | 119 | |
|
| | 120 | | private static void ReadCrsProperties(JsonReader propertiesReader) => |
| 460 | 121 | | propertiesReader.ReadObjectAndAdvance( |
| 460 | 122 | | requiredProperties: NameOnly, |
| 920 | 123 | | readProperty: (r, _) => r.ExpectAndAdvance(JsonToken.String, WorldGeodeticSystem1984)); |
| | 124 | |
|
| | 125 | | private static bool IsCrs(JObject possibleCrs) => |
| 112 | 126 | | possibleCrs.IsValid( |
| 112 | 127 | | requiredProperties: TypeAndProperties, |
| 112 | 128 | | isPropertyValid: property => |
| 112 | 129 | | { |
| 338 | 130 | | switch (property.Name) |
| 112 | 131 | | { |
| 112 | 132 | | case Type: |
| 224 | 133 | | return property.Value.IsString(Name); |
| 112 | 134 | |
|
| 112 | 135 | | case Properties: |
| 224 | 136 | | return property.Value is JObject possibleProperties && IsCrsProperties(possibleProperties); |
| 112 | 137 | |
|
| 112 | 138 | | default: |
| 114 | 139 | | return false; |
| 112 | 140 | | } |
| 112 | 141 | | }); |
| | 142 | |
|
| | 143 | | private static void ReadCrs(JsonReader crsReader) => |
| 460 | 144 | | crsReader.ReadObjectAndAdvance( |
| 460 | 145 | | requiredProperties: TypeAndProperties, |
| 460 | 146 | | readProperty: (r, propertyName) => |
| 460 | 147 | | { |
| 460 | 148 | | switch (propertyName) |
| 460 | 149 | | { |
| 460 | 150 | | case Type: |
| 920 | 151 | | r.ExpectAndAdvance(JsonToken.String, Name); |
| 920 | 152 | | break; |
| 460 | 153 | |
|
| 460 | 154 | | case Properties: |
| 920 | 155 | | ReadCrsProperties(r); |
| 460 | 156 | | break; |
| 460 | 157 | | } |
| 918 | 158 | | }); |
| | 159 | |
|
| | 160 | | private static bool AreCoordinates(JToken possibleCoordinates) => |
| 134 | 161 | | possibleCoordinates is JArray array && array.Count == 2 && array[0].IsNumber() && array[1].IsNumber(); |
| | 162 | |
|
| | 163 | | private static GeographyPoint ReadCoordinates(JsonReader coordinatesReader) |
| | 164 | | { |
| 482 | 165 | | coordinatesReader.ExpectAndAdvance(JsonToken.StartArray); |
| | 166 | |
|
| | 167 | | double ReadFloatOrInt() |
| | 168 | | { |
| 964 | 169 | | switch (coordinatesReader.TokenType) |
| | 170 | | { |
| | 171 | | case JsonToken.Integer: |
| 12 | 172 | | return coordinatesReader.ExpectAndAdvance<long>(JsonToken.Integer); |
| | 173 | |
|
| | 174 | | // Treat all other cases as Float and let ExpectAndAdvance() handle any errors. |
| | 175 | | default: |
| 952 | 176 | | return coordinatesReader.ExpectAndAdvance<double>(JsonToken.Float); |
| | 177 | | } |
| | 178 | | } |
| | 179 | |
|
| 482 | 180 | | double longitude = ReadFloatOrInt(); |
| 482 | 181 | | double latitude = ReadFloatOrInt(); |
| | 182 | |
|
| 482 | 183 | | coordinatesReader.ExpectAndAdvance(JsonToken.EndArray); |
| 482 | 184 | | return GeographyPoint.Create(latitude, longitude); |
| | 185 | | } |
| | 186 | | } |
| | 187 | | } |