-
Notifications
You must be signed in to change notification settings - Fork 874
Expand file tree
/
Copy pathGeoJSONTests.cs
More file actions
434 lines (388 loc) · 17.1 KB
/
GeoJSONTests.cs
File metadata and controls
434 lines (388 loc) · 17.1 KB
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Threading.Tasks;
using GeoJSON.Net;
using GeoJSON.Net.Converters;
using GeoJSON.Net.CoordinateReferenceSystem;
using GeoJSON.Net.Geometry;
using Newtonsoft.Json;
using Npgsql.Tests;
using NpgsqlTypes;
using NUnit.Framework;
using static Npgsql.Tests.TestUtil;
namespace Npgsql.PluginTests;
public class GeoJSONTests : TestBase
{
public struct TestData
{
public GeoJSONObject Geometry;
public string CommandText;
}
public static readonly TestData[] Tests =
[
new()
{
Geometry = new Point(
new Position(longitude: 1d, latitude: 2d))
{ BoundingBoxes = [1d, 2d, 1d, 2d] },
CommandText = "st_makepoint(1,2)"
},
new()
{
Geometry = new LineString([
new Position(longitude: 1d, latitude: 1d),
new Position(longitude: 1d, latitude: 2d)
])
{ BoundingBoxes = [1d, 1d, 1d, 2d] },
CommandText = "st_makeline(st_makepoint(1,1), st_makepoint(1,2))"
},
new()
{
Geometry = new Polygon([
new LineString([
new Position(longitude: 1d, latitude: 1d),
new Position(longitude: 2d, latitude: 2d),
new Position(longitude: 3d, latitude: 3d),
new Position(longitude: 1d, latitude: 1d)
])
])
{ BoundingBoxes = [1d, 1d, 3d, 3d] },
CommandText = "st_makepolygon(st_makeline(ARRAY[st_makepoint(1,1), st_makepoint(2,2), st_makepoint(3,3), st_makepoint(1,1)]))"
},
new()
{
Geometry = new MultiPoint([
new Point(new Position(longitude: 1d, latitude: 1d))
])
{ BoundingBoxes = [1d, 1d, 1d, 1d] },
CommandText = "st_multi(st_makepoint(1, 1))"
},
new()
{
Geometry = new MultiLineString([
new LineString([
new Position(longitude: 1d, latitude: 1d),
new Position(longitude: 1d, latitude: 2d)
])
])
{ BoundingBoxes = [1d, 1d, 1d, 2d] },
CommandText = "st_multi(st_makeline(st_makepoint(1,1), st_makepoint(1,2)))"
},
new()
{
Geometry = new MultiPolygon([
new Polygon([
new LineString([
new Position(longitude: 1d, latitude: 1d),
new Position(longitude: 2d, latitude: 2d),
new Position(longitude: 3d, latitude: 3d),
new Position(longitude: 1d, latitude: 1d)
])
])
])
{ BoundingBoxes = [1d, 1d, 3d, 3d] },
CommandText = "st_multi(st_makepolygon(st_makeline(ARRAY[st_makepoint(1,1), st_makepoint(2,2), st_makepoint(3,3), st_makepoint(1,1)])))"
},
new()
{
Geometry = new GeometryCollection([
new Point(new Position(longitude: 1d, latitude: 1d)),
new MultiPolygon([
new Polygon([
new LineString([
new Position(longitude: 1d, latitude: 1d),
new Position(longitude: 2d, latitude: 2d),
new Position(longitude: 3d, latitude: 3d),
new Position(longitude: 1d, latitude: 1d)
])
])
])
])
{ BoundingBoxes = [1d, 1d, 3d, 3d] },
CommandText = "st_collect(st_makepoint(1,1),st_multi(st_makepolygon(st_makeline(ARRAY[st_makepoint(1,1), st_makepoint(2,2), st_makepoint(3,3), st_makepoint(1,1)]))))"
}
];
[Test, TestCaseSource(nameof(Tests))]
public async Task Read(TestData data)
{
await using var conn = await OpenConnectionAsync(GeoJSONOptions.BoundingBox);
await using var cmd = new NpgsqlCommand($"SELECT {data.CommandText}, st_asgeojson({data.CommandText},options:=1)", conn);
await using var reader = await cmd.ExecuteReaderAsync();
Assert.That(await reader.ReadAsync());
Assert.That(reader.GetFieldValue<GeoJSONObject>(0), Is.EqualTo(data.Geometry));
Assert.That(reader.GetFieldValue<GeoJSONObject>(0), Is.EqualTo(JsonConvert.DeserializeObject<IGeometryObject>(reader.GetFieldValue<string>(1), new GeometryConverter())));
}
[Test, TestCaseSource(nameof(Tests))]
public async Task Write(TestData data)
{
await using var conn = await OpenConnectionAsync();
await using var cmd = new NpgsqlCommand($"SELECT st_asewkb(@p) = st_asewkb({data.CommandText})", conn);
cmd.Parameters.AddWithValue("p", data.Geometry);
Assert.That(await cmd.ExecuteScalarAsync(), Is.True);
}
[Test]
public async Task IgnoreM()
{
await using var conn = await OpenConnectionAsync();
await using var cmd = new NpgsqlCommand("SELECT st_makepointm(1,1,1)", conn);
await using var reader = await cmd.ExecuteReaderAsync();
Assert.That(await reader.ReadAsync());
Assert.That(reader.GetFieldValue<Point>(0), Is.EqualTo(new Point(new Position(1d, 1d))));
}
public static readonly TestData[] NotAllZSpecifiedTests =
[
new()
{
Geometry = new LineString([
new Position(1d, 1d, 0d),
new Position(2d, 2d)
])
},
new()
{
Geometry = new LineString([
new Position(1d, 1d, 0d),
new Position(2d, 2d),
new Position(3d, 3d),
new Position(4d, 4d)
])
}
];
[Test, TestCaseSource(nameof(NotAllZSpecifiedTests))]
public async Task Not_all_Z_specified(TestData data)
{
await using var conn = await OpenConnectionAsync();
await using var cmd = new NpgsqlCommand("SELECT @p", conn);
cmd.Parameters.AddWithValue("p", data.Geometry);
Assert.That(async () => await cmd.ExecuteScalarAsync(), Throws.ArgumentException);
}
[Test]
public async Task Read_unknown_CRS()
{
await using var conn = await OpenConnectionAsync(GeoJSONOptions.ShortCRS);
await using var cmd = new NpgsqlCommand("SELECT st_setsrid(st_makepoint(0,0), 1)", conn);
await using var reader = await cmd.ExecuteReaderAsync();
Assert.That(await reader.ReadAsync());
Assert.That(() => reader.GetValue(0), Throws.InvalidOperationException);
}
[Test]
public async Task Read_unspecified_CRS()
{
await using var conn = await OpenConnectionAsync(GeoJSONOptions.ShortCRS);
await using var cmd = new NpgsqlCommand("SELECT st_setsrid(st_makepoint(0,0), 0)", conn);
await using var reader = await cmd.ExecuteReaderAsync();
Assert.That(await reader.ReadAsync());
Assert.That(reader.GetFieldValue<Point>(0).CRS, Is.Null);
}
[Test]
public async Task Read_short_CRS()
{
await using var conn = await OpenConnectionAsync(GeoJSONOptions.ShortCRS);
await using var cmd = new NpgsqlCommand("SELECT st_setsrid(st_makepoint(0,0), 4326)", conn);
var point = (Point)(await cmd.ExecuteScalarAsync())!;
var crs = point.CRS as NamedCRS;
Assert.That(crs, Is.Not.Null);
Assert.That(crs!.Properties["name"], Is.EqualTo("EPSG:4326"));
}
[Test]
public async Task Read_long_CRS()
{
await using var conn = await OpenConnectionAsync(GeoJSONOptions.LongCRS);
await using var cmd = new NpgsqlCommand("SELECT st_setsrid(st_makepoint(0,0), 4326)", conn);
var point = (Point)(await cmd.ExecuteScalarAsync())!;
var crs = point.CRS as NamedCRS;
Assert.That(crs, Is.Not.Null);
Assert.That(crs!.Properties["name"], Is.EqualTo("urn:ogc:def:crs:EPSG::4326"));
}
[Test]
public async Task Write_ill_formed_CRS()
{
await using var conn = await OpenConnectionAsync();
await using var cmd = new NpgsqlCommand("SELECT st_srid(@p)", conn);
cmd.Parameters.AddWithValue("p", new Point(new Position(0d, 0d)) { CRS = new NamedCRS("ill:formed") });
Assert.That(async () => await cmd.ExecuteScalarAsync(), Throws.TypeOf<FormatException>());
}
[Test]
public async Task Write_linked_CRS()
{
await using var conn = await OpenConnectionAsync();
await using var cmd = new NpgsqlCommand("SELECT st_srid(@p)", conn);
cmd.Parameters.AddWithValue("p", new Point(new Position(0d, 0d)) { CRS = new LinkedCRS("href") });
Assert.That(async () => await cmd.ExecuteScalarAsync(), Throws.TypeOf<NotSupportedException>());
}
[Test]
public async Task Write_unspecified_CRS()
{
await using var conn = await OpenConnectionAsync();
await using var cmd = new NpgsqlCommand("SELECT st_srid(@p)", conn);
cmd.Parameters.AddWithValue("p", new Point(new Position(0d, 0d)) { CRS = new UnspecifiedCRS() });
Assert.That(await cmd.ExecuteScalarAsync(), Is.EqualTo(0));
}
[Test]
public async Task Write_short_CRS()
{
await using var conn = await OpenConnectionAsync();
await using var cmd = new NpgsqlCommand("SELECT st_srid(@p)", conn);
cmd.Parameters.AddWithValue("p", new Point(new Position(0d, 0d)) { CRS = new NamedCRS("EPSG:4326") });
Assert.That(await cmd.ExecuteScalarAsync(), Is.EqualTo(4326));
}
[Test]
public async Task Write_long_CRS()
{
await using var conn = await OpenConnectionAsync();
await using var cmd = new NpgsqlCommand("SELECT st_srid(@p)", conn);
cmd.Parameters.AddWithValue("p", new Point(new Position(0d, 0d)) { CRS = new NamedCRS("urn:ogc:def:crs:EPSG::4326") });
Assert.That(await cmd.ExecuteScalarAsync(), Is.EqualTo(4326));
}
[Test]
public async Task Write_CRS84()
{
await using var conn = await OpenConnectionAsync();
await using var cmd = new NpgsqlCommand("SELECT st_srid(@p)", conn);
cmd.Parameters.AddWithValue("p", new Point(new Position(0d, 0d)) { CRS = new NamedCRS("urn:ogc:def:crs:OGC::CRS84") });
Assert.That(await cmd.ExecuteScalarAsync(), Is.EqualTo(4326));
}
[Test]
public async Task Roundtrip_geometry_geography()
{
await using var conn = await OpenConnectionAsync();
var table = await CreateTempTable(conn, "geom GEOMETRY, geog GEOGRAPHY");
var point = new Point(new Position(0d, 0d));
await using (var cmd = new NpgsqlCommand($"INSERT INTO {table} (geom, geog) VALUES (@p, @p)", conn))
{
cmd.Parameters.AddWithValue("p", point);
await cmd.ExecuteNonQueryAsync();
}
await using (var cmd = new NpgsqlCommand($"SELECT geom, geog FROM {table}", conn))
await using (var reader = await cmd.ExecuteReaderAsync())
{
await reader.ReadAsync();
Assert.That(reader[0], Is.EqualTo(point));
Assert.That(reader[1], Is.EqualTo(point));
}
}
[Test, TestCaseSource(nameof(Tests))]
public async Task Import_geometry(TestData data)
{
await using var conn = await OpenConnectionAsync(options: GeoJSONOptions.BoundingBox);
var table = await CreateTempTable(conn, "field geometry");
await using (var writer = await conn.BeginBinaryImportAsync($"COPY {table} (field) FROM STDIN BINARY"))
{
await writer.StartRowAsync();
await writer.WriteAsync(data.Geometry, NpgsqlDbType.Geometry);
var rowsWritten = await writer.CompleteAsync();
Assert.That(rowsWritten, Is.EqualTo(1));
}
await using var cmd = conn.CreateCommand();
cmd.CommandText = $"SELECT field FROM {table}";
await using var reader = await cmd.ExecuteReaderAsync();
Assert.That(await reader.ReadAsync());
var actual = reader.GetValue(0);
Assert.That(actual, Is.EqualTo(data.Geometry));
}
[Test, IssueLink("https://github.com/npgsql/npgsql/issues/4827")]
public async Task Import_big_geometry()
{
await using var conn = await OpenConnectionAsync();
var table = await CreateTempTable(conn, "id text, field geometry");
var geometry = new MultiLineString([
new LineString(
Enumerable.Range(1, 507)
.Select(i => new Position(longitude: i, latitude: i))
.Append(new Position(longitude: 1d, latitude: 1d))),
new LineString([
new Position(longitude: 1d, latitude: 1d),
new Position(longitude: 1d, latitude: 2d),
new Position(longitude: 1d, latitude: 3d),
new Position(longitude: 1d, latitude: 1d)
])
]);
await using (var writer = await conn.BeginBinaryImportAsync($"COPY {table} (id, field) FROM STDIN BINARY"))
{
await writer.StartRowAsync();
await writer.WriteAsync("a", NpgsqlDbType.Text);
await writer.WriteAsync(geometry, NpgsqlDbType.Geometry);
var rowsWritten = await writer.CompleteAsync();
Assert.That(rowsWritten, Is.EqualTo(1));
}
await using var cmd = conn.CreateCommand();
cmd.CommandText = $"SELECT field FROM {table}";
await using var reader = await cmd.ExecuteReaderAsync();
Assert.That(await reader.ReadAsync());
var actual = reader.GetValue(0);
Assert.That(actual, Is.EqualTo(geometry));
}
[Test, TestCaseSource(nameof(Tests))]
public async Task Export_geometry(TestData data)
{
await using var conn = await OpenConnectionAsync(options: GeoJSONOptions.BoundingBox);
var table = await CreateTempTable(conn, "field geometry");
await using (var writer = await conn.BeginBinaryImportAsync($"COPY {table} (field) FROM STDIN BINARY"))
{
await writer.StartRowAsync();
await writer.WriteAsync(data.Geometry, NpgsqlDbType.Geometry);
var rowsWritten = await writer.CompleteAsync();
Assert.That(rowsWritten, Is.EqualTo(1));
}
await using (var reader = await conn.BeginBinaryExportAsync($"COPY {table} (field) TO STDOUT BINARY"))
{
await reader.StartRowAsync();
var field = await reader.ReadAsync<GeoJSONObject>(NpgsqlDbType.Geometry);
Assert.That(field, Is.EqualTo(data.Geometry));
}
}
[Test, IssueLink("https://github.com/npgsql/npgsql/issues/4830")]
public async Task Export_big_geometry()
{
await using var conn = await OpenConnectionAsync();
var table = await CreateTempTable(conn, "id text, field geometry");
var geometry = new Polygon([
new LineString(
Enumerable.Range(1, 507)
.Select(i => new Position(longitude: i, latitude: i))
.Append(new Position(longitude: 1d, latitude: 1d))),
new LineString([
new Position(longitude: 1d, latitude: 1d),
new Position(longitude: 1d, latitude: 2d),
new Position(longitude: 1d, latitude: 3d),
new Position(longitude: 1d, latitude: 1d)
])
]);
await using (var writer = await conn.BeginBinaryImportAsync($"COPY {table} (id, field) FROM STDIN BINARY"))
{
await writer.StartRowAsync();
await writer.WriteAsync("aaaa", NpgsqlDbType.Text);
await writer.WriteAsync(geometry, NpgsqlDbType.Geometry);
var rowsWritten = await writer.CompleteAsync();
Assert.That(rowsWritten, Is.EqualTo(1));
}
await using (var reader = await conn.BeginBinaryExportAsync($"COPY {table} (id, field) TO STDOUT BINARY"))
{
await reader.StartRowAsync();
var id = await reader.ReadAsync<string>();
var field = await reader.ReadAsync<GeoJSONObject>(NpgsqlDbType.Geometry);
Assert.That(id, Is.EqualTo("aaaa"));
Assert.That(field, Is.EqualTo(geometry));
}
}
ValueTask<NpgsqlConnection> OpenConnectionAsync(GeoJSONOptions options = GeoJSONOptions.None)
=> GetDataSource(options).OpenConnectionAsync();
NpgsqlDataSource GetDataSource(GeoJSONOptions options = GeoJSONOptions.None)
=> GeoJsonDataSources.GetOrAdd(options, _ =>
{
var dataSourceBuilder = CreateDataSourceBuilder();
dataSourceBuilder.UseGeoJson(options);
return dataSourceBuilder.Build();
});
[OneTimeSetUp]
public async Task SetUp()
{
await using var conn = await OpenConnectionAsync();
await EnsurePostgis(conn);
}
[OneTimeTearDown]
public async Task Teardown()
=> await Task.WhenAll(GeoJsonDataSources.Values.Select(async ds => await ds.DisposeAsync()));
ConcurrentDictionary<GeoJSONOptions, NpgsqlDataSource> GeoJsonDataSources = new();
}