-
Notifications
You must be signed in to change notification settings - Fork 45
/
StjGeometryConverter.cs
258 lines (233 loc) · 10.8 KB
/
StjGeometryConverter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
using NetTopologySuite.Algorithm;
using NetTopologySuite.Geometries;
using NetTopologySuite.IO.Properties;
namespace NetTopologySuite.IO.Converters
{
internal partial class StjGeometryConverter : JsonConverter<Geometry>
{
/// <summary>
/// Gets the default geometry factory to use with this converter.
/// </summary>
public static GeometryFactory DefaultGeometryFactory { get; } = new GeometryFactory(new PrecisionModel(), 4326);
private readonly GeometryFactory _geometryFactory;
private readonly bool _writeGeometryBBox;
private readonly OrientationIndex _oriExterior, _oriInterior;
/// <summary>
/// Creates an instance of this class
/// </summary>
/// <param name="geometryFactory">The geometry factory to use.</param>
/// <param name="writeGeometryBBox">Whether or not to write "bbox" with the geometry.</param>
public StjGeometryConverter(GeometryFactory geometryFactory, bool writeGeometryBBox)
: this(geometryFactory, writeGeometryBBox, RingOrientationOption.EnforceRfc9746)
{
_geometryFactory = geometryFactory ?? DefaultGeometryFactory;
_writeGeometryBBox = writeGeometryBBox;
}
/// <summary>
/// Creates an instance of this class
/// </summary>
/// <param name="geometryFactory">The geometry factory to use.</param>
/// <param name="writeGeometryBBox">Whether or not to write "bbox" with the geometry.</param>
/// <param name="ringOrientationOption">Controls ring orientation for polygons when <b>writing</b>.</param>
public StjGeometryConverter(GeometryFactory geometryFactory, bool writeGeometryBBox,
RingOrientationOption ringOrientationOption)
{
_geometryFactory = geometryFactory ?? DefaultGeometryFactory;
_writeGeometryBBox = writeGeometryBBox;
switch (ringOrientationOption)
{
case RingOrientationOption.DoNotModify:
_oriExterior =
_oriInterior = OrientationIndex.None;
break;
case RingOrientationOption.EnforceRfc9746:
_oriExterior = OrientationIndex.CounterClockwise;
_oriInterior = OrientationIndex.Clockwise;
break;
case RingOrientationOption.NtsGeoJsonV2:
_oriExterior = OrientationIndex.Clockwise;
_oriInterior = OrientationIndex.CounterClockwise;
break;
}
}
public override Geometry Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Null)
{
reader.Read();
return null;
}
reader.ReadToken(JsonTokenType.StartObject);
reader.SkipComments();
GeoJsonObjectType? geometryType = null;
StjParsedCoordinates coordinateData = default;
Geometry[] geometries = null;
while (reader.TokenType == JsonTokenType.PropertyName)
{
// Get the property name and decide what to do
string propertyName = reader.GetString();
reader.Read();
switch (propertyName)
{
case "type":
geometryType = (GeoJsonObjectType) Enum.Parse(typeof(GeoJsonObjectType), reader.GetString());
reader.ReadToken(JsonTokenType.String);
break;
case "geometries":
geometries = ReadGeometries(ref reader, options);
break;
case "coordinates":
coordinateData = StjParsedCoordinates.Parse(ref reader, _geometryFactory);
reader.Read();
break;
default: // included "bbox" property
//read, but can't do anything with it (see NetTopologySuite.IO.GeoJSON => NetTopologySuite.IO.Converters.GeometryConverter.ParseGeometry)
reader.Skip();
reader.Read();
break;
}
// Skip comments
reader.SkipComments();
}
if (geometryType != GeoJsonObjectType.GeometryCollection)
{
if (!geometryType.HasValue)
{
throw new JsonException(Resources.EX_NoGeometryTypeDefined);
}
var supportedTypes = coordinateData.SupportedTypes;
if (supportedTypes.IsEmpty)
{
throw new JsonException(Resources.EX_NoCoordinatesDefined);
}
bool geometryTypeIsCompatible = false;
foreach (var supportedType in supportedTypes)
{
if (supportedType == geometryType)
{
geometryTypeIsCompatible = true;
break;
}
}
if (!geometryTypeIsCompatible)
{
throw new JsonException(string.Format(Resources.EX_CoordinatesIncompatibleWithType, geometryType));
}
}
Geometry geometry;
switch (geometryType)
{
case GeoJsonObjectType.Point:
geometry = coordinateData.ToPoint(_geometryFactory);
break;
case GeoJsonObjectType.LineString:
geometry = coordinateData.ToLineString(_geometryFactory);
break;
case GeoJsonObjectType.Polygon:
geometry = coordinateData.ToPolygon(_geometryFactory);
break;
case GeoJsonObjectType.MultiPoint:
geometry = coordinateData.ToMultiPoint(_geometryFactory);
break;
case GeoJsonObjectType.MultiLineString:
geometry = coordinateData.ToMultiLineString(_geometryFactory);
break;
case GeoJsonObjectType.MultiPolygon:
geometry = coordinateData.ToMultiPolygon(_geometryFactory);
break;
case GeoJsonObjectType.GeometryCollection:
if (geometries == null)
throw new JsonException(Resources.EX_GCTypeWithoutGeometries);
geometry = _geometryFactory.CreateGeometryCollection(geometries ?? new Geometry[0]);
break;
default:
throw new NotSupportedException();
}
return geometry;
}
private Geometry[] ReadGeometries(ref Utf8JsonReader reader, JsonSerializerOptions options)
{
reader.ReadToken(JsonTokenType.StartArray);
var geometries = new List<Geometry>();
while (reader.TokenType != JsonTokenType.EndArray)
{
geometries.Add(Read(ref reader, typeof(Geometry), options));
reader.ReadToken(JsonTokenType.EndObject);
}
reader.ReadToken(JsonTokenType.EndArray);
return geometries.ToArray();
}
public override void Write(Utf8JsonWriter writer, Geometry value, JsonSerializerOptions options)
{
if (value == null)
{
writer.WriteNullValue();
return;
}
writer.WriteStartObject();
writer.WriteString("type", Enum.GetName(typeof(OgcGeometryType), value.OgcGeometryType));
if (value.OgcGeometryType == OgcGeometryType.GeometryCollection)
{
writer.WritePropertyName("geometries");
writer.WriteStartArray();
for (int i = 0; i < value.NumGeometries; i++)
Write(writer, value.GetGeometryN(i), options);
writer.WriteEndArray();
}
else if (value.IsEmpty)
{
writer.WriteStartArray("coordinates");
writer.WriteEndArray();
}
else
{
writer.WritePropertyName("coordinates");
switch (value.OgcGeometryType)
{
case OgcGeometryType.Point:
WriteCoordinateSequence(writer, ((Point)value).CoordinateSequence, options, false);
break;
case OgcGeometryType.LineString:
WriteCoordinateSequence(writer, ((LineString)value).CoordinateSequence, options);
break;
case OgcGeometryType.Polygon:
WritePolygon(writer, (Polygon)value, options);
break;
case OgcGeometryType.MultiPoint:
writer.WriteStartArray();
for (int i = 0; i < value.NumGeometries; i++)
WriteCoordinateSequence(writer, ((Point)value.GetGeometryN(i)).CoordinateSequence, options, false);
writer.WriteEndArray();
break;
case OgcGeometryType.MultiLineString:
writer.WriteStartArray();
for (int i = 0; i < value.NumGeometries; i++)
WriteCoordinateSequence(writer, ((LineString)value.GetGeometryN(i)).CoordinateSequence, options);
writer.WriteEndArray();
break;
case OgcGeometryType.MultiPolygon:
writer.WriteStartArray();
for (int i = 0; i < value.NumGeometries; i++)
WritePolygon(writer, (Polygon)value.GetGeometryN(i), options);
writer.WriteEndArray();
break;
}
}
if (_writeGeometryBBox)
WriteBBox(writer, value.EnvelopeInternal, options);
writer.WriteEndObject();
}
private void WritePolygon(Utf8JsonWriter writer, Polygon value, JsonSerializerOptions options)
{
writer.WriteStartArray();
WriteCoordinateSequence(writer, value.ExteriorRing.CoordinateSequence, options, orientation:_oriExterior);
for (int i = 0; i < value.NumInteriorRings; i++)
WriteCoordinateSequence(writer, value.GetInteriorRingN(i).CoordinateSequence, options, orientation: _oriInterior);
writer.WriteEndArray();
}
}
}