/
GeometryConverter.cs
315 lines (286 loc) · 12.5 KB
/
GeometryConverter.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using GeoAPI.Geometries;
using NetTopologySuite.Geometries;
using Newtonsoft.Json;
namespace NetTopologySuite.IO.Converters
{
public class GeometryConverter : JsonConverter
{
private readonly IGeometryFactory _factory;
public GeometryConverter() : this(GeometryFactory.Default) { }
public GeometryConverter(IGeometryFactory geometryFactory)
{
_factory = geometryFactory;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
IGeometry geom = value as IGeometry;
if (geom == null)
{
writer.WriteNull();
return;
}
writer.WriteStartObject();
GeoJsonObjectType geomType = ToGeoJsonObject(geom);
writer.WritePropertyName("type");
writer.WriteValue(Enum.GetName(typeof(GeoJsonObjectType), geomType));
switch (geomType)
{
case GeoJsonObjectType.Point:
if (serializer.NullValueHandling == NullValueHandling.Include || geom.Coordinate != null)
{
writer.WritePropertyName("coordinates");
serializer.Serialize(writer, geom.Coordinate);
}
break;
case GeoJsonObjectType.LineString:
case GeoJsonObjectType.MultiPoint:
var linealCoords = geom.Coordinates;
if (serializer.NullValueHandling == NullValueHandling.Include || linealCoords != null)
{
writer.WritePropertyName("coordinates");
serializer.Serialize(writer, linealCoords);
}
break;
case GeoJsonObjectType.Polygon:
IPolygon poly = geom as IPolygon;
Debug.Assert(poly != null);
var polygonCoords = PolygonCoordinates(poly);
if (serializer.NullValueHandling == NullValueHandling.Include || polygonCoords != null)
{
writer.WritePropertyName("coordinates");
serializer.Serialize(writer, polygonCoords);
}
break;
case GeoJsonObjectType.MultiPolygon:
IMultiPolygon mpoly = geom as IMultiPolygon;
Debug.Assert(mpoly != null);
var list = new List<List<Coordinate[]>>();
foreach (IPolygon mempoly in mpoly.Geometries)
list.Add(PolygonCoordinates(mempoly));
if (serializer.NullValueHandling == NullValueHandling.Include || list.Count > 0)
{
writer.WritePropertyName("coordinates");
serializer.Serialize(writer, list);
}
break;
case GeoJsonObjectType.GeometryCollection:
IGeometryCollection gc = geom as IGeometryCollection;
Debug.Assert(gc != null);
serializer.Serialize(writer, gc.Geometries);
break;
default:
List<Coordinate[]> coordinates = new List<Coordinate[]>();
foreach (IGeometry geometry in ((IGeometryCollection)geom).Geometries)
coordinates.Add(geometry.Coordinates);
if (serializer.NullValueHandling == NullValueHandling.Include || coordinates.Count > 0)
{
writer.WritePropertyName("coordinates");
serializer.Serialize(writer, coordinates);
}
break;
}
writer.WriteEndObject();
}
private GeoJsonObjectType ToGeoJsonObject(IGeometry geom)
{
if (geom is IPoint)
return GeoJsonObjectType.Point;
if (geom is ILineString)
return GeoJsonObjectType.LineString;
if (geom is IPolygon)
return GeoJsonObjectType.Polygon;
if (geom is IMultiPoint)
return GeoJsonObjectType.MultiPoint;
if (geom is IMultiLineString)
return GeoJsonObjectType.MultiLineString;
if (geom is IMultiPolygon)
return GeoJsonObjectType.MultiPolygon;
if (geom is IGeometryCollection)
return GeoJsonObjectType.GeometryCollection;
throw new ArgumentException("geom");
}
private static GeoJsonObjectType GetType(JsonReader reader)
{
return (GeoJsonObjectType)Enum.Parse(typeof(GeoJsonObjectType), (string)reader.Value, true);
}
private static List<object> ReadCoordinates(JsonReader reader)
{
List<object> coords = new List<object>();
while (reader.Read())
{
if (reader.TokenType == JsonToken.StartArray)
{
coords.Add(ReadCoordinates(reader));
}
else if (reader.TokenType == JsonToken.EndArray)
{
break;
}
if (reader.Value != null)
{
coords.Add(reader.Value);
}
}
return coords;
}
private List<object> ParseGeomCollection(JsonReader reader, JsonSerializer serializer)
{
List<object> geometries = new List<object>();
while (reader.Read())
{
// Exit if we are at the end
if (reader.TokenType == JsonToken.EndArray)
break;
if (reader.TokenType == JsonToken.StartObject)
{
geometries.Add(ParseGeometry(reader, serializer));
}
}
return geometries;
}
private static Coordinate GetPointCoordinate(IList list)
{
Coordinate c = new Coordinate();
c.X = Convert.ToDouble(list[0]);
c.Y = Convert.ToDouble(list[1]);
if (list.Count > 2)
c.Z = Convert.ToDouble(list[2]);
return c;
}
private static Coordinate[] GetLineStringCoordinates(IEnumerable list)
{
List<Coordinate> coordinates = new List<Coordinate>();
foreach (List<object> coord in list)
{
coordinates.Add(GetPointCoordinate(coord));
}
return coordinates.ToArray();
}
private static List<Coordinate[]> GetPolygonCoordinates(IEnumerable list)
{
List<Coordinate[]> coordinates = new List<Coordinate[]>();
foreach (List<object> coord in list)
{
coordinates.Add(GetLineStringCoordinates(coord));
}
return coordinates;
}
private static IEnumerable<List<Coordinate[]>> GetMultiPolygonCoordinates(IEnumerable list)
{
List<List<Coordinate[]>> coordinates = new List<List<Coordinate[]>>();
foreach (List<object> coord in list)
{
coordinates.Add(GetPolygonCoordinates(coord));
}
return coordinates;
}
private static IGeometry[] GetGeometries(IEnumerable list)
{
List<IGeometry> geometries = new List<IGeometry>();
foreach (IGeometry geom in list)
{
geometries.Add(geom);
}
return geometries.ToArray();
}
private IGeometry ParseGeometry(JsonReader reader, JsonSerializer serializer)
{
GeoJsonObjectType? geometryType = null;
List<object> coords = null;
while (reader.Read())
{
if (reader.TokenType == JsonToken.EndObject)
{
//we are at the end of the geometry block, do not read further
break;
}
//read the tokens, type may come before coordinates or geometries as pr spec
if (reader.TokenType == JsonToken.PropertyName)
{
if ((String)reader.Value == "type" && geometryType == null)
{
//get the type name
reader.Read();
geometryType = GetType(reader);
}
else if ((String)reader.Value == "geometries")
{
//only geom collection has "geometries"
reader.Read(); //read past start array tag
coords = ParseGeomCollection(reader, serializer);
}
else if ((String)reader.Value == "coordinates")
{
reader.Read(); //read past start array tag
coords = ReadCoordinates(reader);
}
else if ((String) reader.Value == "bbox")
{
//Read but discard
var bbox = serializer.Deserialize<Envelope>(reader);
}
}
}
if (coords == null || geometryType == null)
{
return null;
}
switch (geometryType)
{
case GeoJsonObjectType.Point:
return _factory.CreatePoint(GetPointCoordinate(coords));
case GeoJsonObjectType.LineString:
return _factory.CreateLineString(GetLineStringCoordinates(coords));
case GeoJsonObjectType.Polygon:
return CreatePolygon(GetPolygonCoordinates(coords));
case GeoJsonObjectType.MultiPoint:
return _factory.CreateMultiPoint(GetLineStringCoordinates(coords));
case GeoJsonObjectType.MultiLineString:
List<ILineString> strings = new List<ILineString>();
foreach (Coordinate[] multiLineStringCoordinate in GetPolygonCoordinates(coords))
{
strings.Add(_factory.CreateLineString(multiLineStringCoordinate));
}
return _factory.CreateMultiLineString(strings.ToArray());
case GeoJsonObjectType.MultiPolygon:
List<IPolygon> polygons = new List<IPolygon>();
foreach (List<Coordinate[]> multiPolygonCoordinate in GetMultiPolygonCoordinates(coords))
{
polygons.Add(CreatePolygon(multiPolygonCoordinate));
}
return _factory.CreateMultiPolygon(polygons.ToArray());
case GeoJsonObjectType.GeometryCollection:
return _factory.CreateGeometryCollection(GetGeometries(coords));
}
return null;
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
return ParseGeometry(reader, serializer);
}
private static List<Coordinate[]> PolygonCoordinates(IPolygon polygon)
{
List<Coordinate[]> res = new List<Coordinate[]>();
res.Add(polygon.Shell.Coordinates);
foreach (ILineString interiorRing in polygon.InteriorRings)
res.Add(interiorRing.Coordinates);
return res;
}
private IPolygon CreatePolygon(IList<Coordinate[]> coordinatess)
{
ILinearRing shell = _factory.CreateLinearRing(coordinatess[0]);
List<ILinearRing> rings = new List<ILinearRing>();
for (int i = 1; i < coordinatess.Count; i++)
rings.Add(_factory.CreateLinearRing(coordinatess[i]));
return _factory.CreatePolygon(shell, rings.ToArray());
}
public override bool CanConvert(Type objectType)
{
return typeof(IGeometry).IsAssignableFrom(objectType);
}
}
}