forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathGeoJSONTests.cs
More file actions
304 lines (280 loc) · 12.1 KB
/
GeoJSONTests.cs
File metadata and controls
304 lines (280 loc) · 12.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
using System;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using GeoJSON.Net;
using GeoJSON.Net.Converters;
using GeoJSON.Net.CoordinateReferenceSystem;
using GeoJSON.Net.Geometry;
using Newtonsoft.Json;
using Npgsql.GeoJSON.Internal;
using Npgsql.Tests;
using NUnit.Framework;
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 = new[] { 1d, 2d, 1d, 2d } },
CommandText = "st_makepoint(1,2)"
},
new()
{
Geometry = new LineString(new[] {
new Position(longitude: 1d, latitude: 1d),
new Position(longitude: 1d, latitude: 2d)
})
{ BoundingBoxes = new[] { 1d, 1d, 1d, 2d } },
CommandText = "st_makeline(st_makepoint(1,1), st_makepoint(1,2))"
},
new()
{
Geometry = new Polygon(new[] {
new LineString(new[] {
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 = new[] { 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[] {
new Point(new Position(longitude: 1d, latitude: 1d))
})
{ BoundingBoxes = new[] { 1d, 1d, 1d, 1d } },
CommandText = "st_multi(st_makepoint(1, 1))"
},
new()
{
Geometry = new MultiLineString(new[] {
new LineString(new[] {
new Position(longitude: 1d, latitude: 1d),
new Position(longitude: 1d, latitude: 2d)
})
})
{ BoundingBoxes = new[] { 1d, 1d, 1d, 2d } },
CommandText = "st_multi(st_makeline(st_makepoint(1,1), st_makepoint(1,2)))"
},
new()
{
Geometry = new MultiPolygon(new[] {
new Polygon(new[] {
new LineString(new[] {
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 = new[] { 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 IGeometryObject[] {
new Point(new Position(longitude: 1d, latitude: 1d)),
new MultiPolygon(new[] {
new Polygon(new[] {
new LineString(new[] {
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 = new[] { 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 void Read(TestData data)
{
using var conn = OpenConnection(option: GeoJSONOptions.BoundingBox);
using var cmd = new NpgsqlCommand($"SELECT {data.CommandText}, st_asgeojson({data.CommandText},options:=1)", conn);
using var reader = cmd.ExecuteReader();
Assert.That(reader.Read());
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 void Write(TestData data)
{
using var conn = OpenConnection();
using var cmd = new NpgsqlCommand($"SELECT st_asewkb(@p) = st_asewkb({data.CommandText})", conn);
cmd.Parameters.AddWithValue("p", data.Geometry);
Assert.That(cmd.ExecuteScalar(), Is.True);
}
[Test]
public void IgnoreM()
{
using var conn = OpenConnection();
using var cmd = new NpgsqlCommand("SELECT st_makepointm(1,1,1)", conn);
using var reader = cmd.ExecuteReader();
Assert.That(reader.Read());
Assert.That(reader.GetFieldValue<Point>(0), Is.EqualTo(new Point(new Position(1d, 1d))));
}
public static readonly TestData[] NotAllZSpecifiedTests =
{
new()
{
Geometry = new LineString(new[] {
new Position(1d, 1d, 0d),
new Position(2d, 2d)
})
},
new()
{
Geometry = new LineString(new[] {
new Position(1d, 1d, 0d),
new Position(2d, 2d),
new Position(3d, 3d),
new Position(4d, 4d)
})
}
};
[Test, TestCaseSource(nameof(NotAllZSpecifiedTests))]
public void NotAllZSpecified(TestData data)
{
using var conn = OpenConnection();
using var cmd = new NpgsqlCommand("SELECT @p", conn);
cmd.Parameters.AddWithValue("p", data.Geometry);
Assert.That(() => cmd.ExecuteScalar(), Throws.ArgumentException);
}
[Test]
public void ReadUnknownCRS()
{
using var conn = OpenConnection(option: GeoJSONOptions.ShortCRS);
using var cmd = new NpgsqlCommand("SELECT st_setsrid(st_makepoint(0,0), 1)", conn);
using var reader = cmd.ExecuteReader();
Assert.That(reader.Read());
Assert.That((TestDelegate)(() => reader.GetValue(0)), Throws.InvalidOperationException);
}
[Test]
public void ReadUnspecifiedCRS()
{
using var conn = OpenConnection(option: GeoJSONOptions.ShortCRS);
using var cmd = new NpgsqlCommand("SELECT st_setsrid(st_makepoint(0,0), 0)", conn);
using var reader = cmd.ExecuteReader();
Assert.That(reader.Read());
Assert.That(reader.GetFieldValue<Point>(0).CRS, Is.Null);
}
[Test]
public void ReadShortCRS()
{
using var conn = OpenConnection(option: GeoJSONOptions.ShortCRS);
using var cmd = new NpgsqlCommand("SELECT st_setsrid(st_makepoint(0,0), 4326)", conn);
var point = (Point)cmd.ExecuteScalar()!;
var crs = point.CRS as NamedCRS;
Assert.That(crs, Is.Not.Null);
Assert.That(crs!.Properties["name"], Is.EqualTo("EPSG:4326"));
}
[Test]
public void ReadLongCRS()
{
using var conn = OpenConnection(option: GeoJSONOptions.LongCRS);
using var cmd = new NpgsqlCommand("SELECT st_setsrid(st_makepoint(0,0), 4326)", conn);
var point = (Point)cmd.ExecuteScalar()!;
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 void WriteIllFormedCRS()
{
using var conn = OpenConnection();
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(() => cmd.ExecuteScalar(), Throws.TypeOf<FormatException>());
}
[Test]
public void WriteLinkedCRS()
{
using var conn = OpenConnection();
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(() => cmd.ExecuteScalar(), Throws.TypeOf<NotSupportedException>());
}
[Test]
public void WriteUnspecifiedCRS()
{
using var conn = OpenConnection();
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(cmd.ExecuteScalar(), Is.EqualTo(0));
}
[Test]
public void WriteShortCRS()
{
using var conn = OpenConnection();
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(cmd.ExecuteScalar(), Is.EqualTo(4326));
}
[Test]
public void WriteLongCRS()
{
using var conn = OpenConnection();
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(cmd.ExecuteScalar(), Is.EqualTo(4326));
}
[Test]
public void WriteCRS84()
{
using var conn = OpenConnection();
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(cmd.ExecuteScalar(), Is.EqualTo(4326));
}
[Test]
public void RoundtripGeometryGeography()
{
var point = new Point(new Position(0d, 0d));
using var conn = OpenConnection();
conn.ExecuteNonQuery("CREATE TEMP TABLE data (geom GEOMETRY, geog GEOGRAPHY)");
using (var cmd = new NpgsqlCommand("INSERT INTO data (geom, geog) VALUES (@p, @p)", conn))
{
cmd.Parameters.AddWithValue("p", point);
cmd.ExecuteNonQuery();
}
using (var cmd = new NpgsqlCommand("SELECT geom, geog FROM data", conn))
using (var reader = cmd.ExecuteReader())
{
reader.Read();
Assert.That(reader[0], Is.EqualTo(point));
Assert.That(reader[1], Is.EqualTo(point));
}
}
protected override NpgsqlConnection OpenConnection(string? connectionString = null)
=> OpenConnection(connectionString, GeoJSONOptions.None);
protected NpgsqlConnection OpenConnection(string? connectionString = null, GeoJSONOptions option = GeoJSONOptions.None)
{
var conn = base.OpenConnection(connectionString);
conn.TypeMapper.UseGeoJson(option);
return conn;
}
[OneTimeSetUp]
public async Task SetUp()
{
await using var conn = await base.OpenConnectionAsync();
await TestUtil.EnsurePostgis(conn);
}
}
}