-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathruntests.jl
More file actions
478 lines (430 loc) · 19.7 KB
/
runtests.jl
File metadata and controls
478 lines (430 loc) · 19.7 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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
using GeoJSON
import GeoInterface as GI
import GeoFormatTypes
import Aqua
using Extents
using JSON3
using Tables
using Test
using Plots
using Makie
using DataFrames
include("geojson_samples.jl")
@testset "GeoJSON" begin
@testset "Aqua.jl" begin
Aqua.test_all(GeoJSON)
end
@testset "Features" begin
geometries = [
nothing,
[(-155.52f0, 19.61f0), (-156.22f0, 20.74f0), (-157.97f0, 21.46f0)],
nothing,
[
[(3.75f0, 9.25f0), (-130.95f0, 1.52f0)],
[(23.15f0, -34.25f0), (-1.35f0, -4.65f0), (3.45f0, 77.95f0)],
],
(53.0f0, -4.0f0),
nothing,
[
[(3.75f0, 9.25f0), (-130.95f0, 1.52f0)],
[(23.15f0, -34.25f0), (-1.35f0, -4.65f0), (3.45f0, 77.95f0)],
],
]
properties = [
[:Ã => "Ã"],
[:type => "é"],
[:type => "meow"],
[:title => "Dict 1"],
[
:link => "http://example.org/features/1",
:title => "Feature 1",
:summary => "The first feature",
],
[:foo => "bar"],
[:title => "Dict 1", :bbox => [-180.0f0, -90.0f0, 180.0f0, 90.0f0]],
]
foreach(Samples.features, geometries, properties) do s, g, p
@test collect(pairs(GeoJSON.properties(GeoJSON.read(s)))) == p
geom = GeoJSON.geometry(GeoJSON.read(s))
if !isnothing(geom)
@test GeoJSON.coordinates(geom) == g
Plots.plot(geom)
geom isa GeoJSON.MultiPoint || Makie.plot(geom)
end
end
end
@testset "Geometries" begin
geom = GeoJSON.read(Samples.multi)
@test geom isa GeoJSON.MultiPolygon
@test GI.coordinates(geom) == [
[[(180.0f0, 40.0f0), (180.0f0, 50.0f0), (170.0f0, 50.0f0), (170.0f0, 40.0f0), (180.0f0, 40.0f0)]],
[[
(-170.0f0, 40.0f0),
(-170.0f0, 50.0f0),
(-180.0f0, 50.0f0),
(-180.0f0, 40.0f0),
(-170.0f0, 40.0f0),
]],
]
Plots.plot(geom)
Makie.plot(geom)
geom = GeoJSON.read(Samples.bbox)
Plots.plot(geom)
Makie.plot(geom)
@test geom isa GeoJSON.LineString
@test GI.crs(geom) == GeoFormatTypes.EPSG(4326)
@test GeoJSON.coordinates(geom) == [(-35.1f0, -6.6f0), (8.1f0, 3.8f0)]
@test GeoJSON.bbox(geom) == [-35.1f0, -6.6f0, 8.1f0, 3.8f0]
@test GI.extent(geom) == Extent(X=(-35.1f0, 8.1f0), Y=(-6.6f0, 3.8f0))
geom = GeoJSON.read(Samples.bbox_z, ndim=3)
@test geom isa GeoJSON.LineString
@test GeoJSON.coordinates(geom) == [(-35.1f0, -6.6f0, 5.5f0), (8.1f0, 3.8f0, 6.5f0)]
@test GeoJSON.bbox(geom) == [-35.1f0, -6.6f0, 5.5f0, 8.1f0, 3.8f0, 6.5f0]
@test GI.extent(geom) == Extent(X=(-35.1f0, 8.1f0), Y=(-6.6f0, 3.8f0), Z=(5.5f0, 6.5f0))
end
@testset "Construct from NamedTuple" begin
# Geometry
p = GeoJSON.Point(coordinates=(1.1f0, 2.2f0))
@test propertynames(p) === (:bbox, :coordinates)
@test GeoJSON.typestring(typeof(p)) === "Point"
@test p.coordinates === GeoJSON.coordinates(p) == (1.1f0, 2.2f0)
# Feature
# properties named "geometry" are *not* shadowed by the geometry
f = GeoJSON.Feature(geometry=p, properties=pairs((a=1, geometry="g", b=2)))
@test GeoJSON.coordinates(f) == (1.1f0, 2.2f0)
@test propertynames(f) === (:geometry, :a, :b)
@test GeoJSON.geometry(f) === p
@test GeoJSON.properties(f)[:a] === 1
@test GeoJSON.properties(f)[:b] === 2
@test ismissing(f.not_a_col)
@test iterate(f) isa Tuple
@test_throws MethodError f[1]
# but can still be retrieved from the properties directly
@test GeoJSON.properties(f)[:geometry] === "g"
# FeatureCollection
features = [f]
fc = GeoJSON.FeatureCollection(features=features)
@test GeoJSON.features(fc) == features
@test propertynames(fc) == Tables.columnnames(fc) == [:a, :b, :geometry]
@test GeoJSON.geometry.(fc) == [p]
@test iterate(p) === (1.1f0, 2)
@test iterate(p, 2) === (2.2f0, 3)
@test iterate(p, 3) === nothing
# other constructors
GeoJSON.Feature(geometry=p, properties=pairs((a=1, geometry="g", b=2)))
GeoJSON.FeatureCollection(features=[f])
# Mixed name vector
f2 = GeoJSON.Feature(geometry=p, properties=pairs((a=1, geometry="g", b=2, c=3)))
GeoJSON.FeatureCollection(features=[f, f2])
end
@testset "extent" begin
@test GI.extent(GeoJSON.read(Samples.d)) ==
Extent(X=(-180.0f0, 180.0f0), Y=(-90.0f0, 90.0f0))
@test GI.extent(GeoJSON.read(Samples.e), fallback=false) === nothing
@test GI.extent(GeoJSON.read(Samples.g, ndim=2)) ==
Extent(X=(100.0f0, 105.0f0), Y=(0.0f0, 1.0f0))
end
@testset "crs" begin
@test GI.crs(GeoJSON.read(Samples.a)) == GeoFormatTypes.EPSG(4326)
@test GI.crs(GeoJSON.read(Samples.collection)) == GeoFormatTypes.EPSG(4326)
@test GI.crs(GeoJSON.read(Samples.multi)) == GeoFormatTypes.EPSG(4326)
end
@testset "read not crash" begin
for str in vcat(Samples.featurecollections, Samples.features, Samples.geometries)
GeoJSON.read(str)
end
end
@testset "write" begin
# Round trip read/write and compare prettified output to prettified original
foreach(Samples.features) do json
f = GeoJSON.read(json)
f1 = GeoJSON.read(GeoJSON.write(f))
@test GeoJSON.geometry(f) == GeoJSON.geometry(f1)
@test GeoJSON.properties(f) == GeoJSON.properties(f1)
@test GI.extent(f) == GI.extent(f1)
end
foreach(Samples.featurecollections) do json
fc = GeoJSON.read(json)
f1c = GeoJSON.read(GeoJSON.write(fc))
foreach(fc, f1c) do f, f1
@test GeoJSON.geometry(f) == GeoJSON.geometry(f1)
@test GeoJSON.properties(f) == GeoJSON.properties(f1)
@test GI.extent(f) == GI.extent(f1)
end
end
# GeoInterface support
foreach(Samples.featuresgeom) do json
geom = GeoJSON.geometry(GeoJSON.read(json))
geom1 = GeoJSON.read(GeoJSON.write(GI.convert(GI, geom)))
@test geom == geom1
@test GI.extent(geom) == GI.extent(geom1)
end
end
@testset "FeatureCollection of one MultiPolygon" begin
t = GeoJSON.read(Samples.g)
@test Tables.istable(t)
@test Tables.rows(t) === t
@test Tables.columns(t) isa Tables.CopiedColumns
@test t isa GeoJSON.FeatureCollection{2}
@test sort(propertynames(t)) == [:addr1, :addr2, :cartodb_id, :geometry, :park,]
@test Tables.rowtable(t) isa Vector{<:NamedTuple}
@test Tables.columntable(t) isa NamedTuple
@inferred first(t)
f1, _ = iterate(t)
@test f1 isa GeoJSON.Feature{2}
@test propertynames(f1) === (:geometry, :park, :cartodb_id, :addr1, :addr2)
@test all(propertynames(f1)) do pn
getproperty(f1, pn) == getproperty(GI.getfeature(t, 1), pn)
end
@inferred t[1]
@test f1 == t[1]
geom = GeoJSON.geometry(f1)
@test geom isa GeoJSON.MultiPolygon{2}
@test geom isa GeoJSON.AbstractGeometry{2}
@test GeoJSON.coordinates(geom) isa Vector{Vector{Vector{Tuple{Float32,Float32}}}}
@test GI.coordinates(geom) isa Vector{Vector{Vector{Tuple{Float32,Float32}}}}
@test GeoJSON.coordinates(geom)[1][1] == geom[1][1]
@test length(geom[1][1]) == 4
@test geom[1][1][1] == (-117.913883f0, 33.96657f0)
@test geom[1][1][2] == (-117.907767f0, 33.967747f0)
@test geom[1][1][3] == (-117.912919f0, 33.96445f0)
@test geom[1][1][4] == (-117.913883f0, 33.96657f0)
@testset "With NamedTuple feature" begin
nt_feature = GeoJSON.Feature(
geometry=t[1].geometry,
properties=pairs((cartodb_id=t[1].cartodb_id, addr1=t[1].addr1, addr2=t[1].addr2, park=t[1].park))
)
fc = GeoJSON.FeatureCollection(features=[nt_feature])
@test fc isa GeoJSON.FeatureCollection
@test occursin("(:geometry,", sprint(show, MIME"text/plain"(), fc[1]))
@test occursin(":park", sprint(show, MIME"text/plain"(), fc[1]))
end
@testset "read and write methods" begin
# read string
fc = t
f = GI.getfeature(fc, 1)
geom = GI.geometry(f)
prop = GI.properties(f)
path = tempname()
# write to path
GeoJSON.write(path, fc)
bytes = read(path)
# write to io
mktemp() do path, io
GeoJSON.write(io, fc)
close(io)
@test read(path) == bytes
end
# read bytes
fc_bytes = GeoJSON.read(bytes)
@test GI.extent(fc) == GI.extent(fc_bytes) == Extent(X=(100.0f0, 105.0f0), Y=(0.0f0, 1.0f0))
f_bytes = GI.getfeature(fc_bytes, 1)
@test GI.geometry(f_bytes) == geom
@test GI.properties(f_bytes) == prop
# read file
fc_file = GeoJSON.read(path)
@test GI.geometry(fc_file[1]) == geom
@test GI.properties(fc_file[1]) == prop
# read io
fc_io = open(path) do io
GeoJSON.read(io)
end
@test GI.geometry(fc_io[1]) == geom
@test GI.properties(fc_io[1]) == prop
end
@testset "GeoInterface" begin
@test GI.trait(t) == GI.FeatureCollectionTrait()
geom = GeoJSON.geometry(f1)
@test GI.trait(f1) === GI.FeatureTrait()
@test GI.geomtrait(geom) === GI.MultiPolygonTrait()
properties = GeoJSON.properties(f1)
@test properties isa Dict{Symbol,Any}
@test properties[:addr2] === "Rowland Heights"
@test !GI.isclosed(GeoJSON.read(Samples.bbox))
@test GI.isclosed(GeoJSON.read(Samples.bermuda_triangle))
end
end
@testset "Tables with missings" begin
t = GeoJSON.read(Samples.tablenull)
@test t[1] isa GeoJSON.Feature
@test occursin("(:geometry, :a, :b)", sprint(show, MIME"text/plain"(), t[1]))
@test ismissing(t[1].geometry)
GeoJSON.geometry(t[1])
@test t.geometry isa Vector{Union{T,Missing}} where {T<:GeoJSON.Point}
@test ismissing(t.geometry[3])
@test t.a isa Vector{Union{Int64,Missing}}
@test isequal(t.a, [1, missing, 3])
@test t.b isa Vector{Missing}
@test Tables.columntable(t) isa NamedTuple
t = GeoJSON.read(Samples.table_not_present)
@test occursin("(:geometry, :a, :b, :c)", sprint(show, MIME"text/plain"(), t[1]))
@test sort(propertynames(t)) == sort([:a, :b, :c, :geometry, :d])
@test propertynames(t[1]) == (:geometry, :a, :b, :c)
@test propertynames(t[2]) == (:geometry, :a, :b, :d)
# "c" and "d" are only present in the properties of a single row
@test all(t.c .=== ["only-here", missing, missing])
@test all(t.d .=== [missing, "appears-later", missing])
@testset "With NamedTuple feature" begin
nt_props = [
(a=t[1].a, b=t[1].b, c=t[1].c),
(a=t[2].a, b=t[2].b, d=t[2].d),
(a=t[3].a, b=t[3].b),
]
features = map(t.geometry, nt_props) do geometry, properties
# Setting 2 here is required because of the missing geometry
GeoJSON.Feature{2,Float32}(geometry=ismissing(geometry) ? nothing : geometry, properties=pairs(properties))
end
fc = GeoJSON.FeatureCollection(features=features)
@test fc isa GeoJSON.FeatureCollection
@test occursin("(:geometry, :a, :b, :c)", sprint(show, MIME"text/plain"(), fc[1]))
end
end
@testset "FeatureCollection of one GeometryCollection" begin
fc = GeoJSON.read(Samples.collection)
gc = GeoJSON.geometry(GI.getfeature(fc, 1))
@test GI.geomtrait(gc) isa GI.GeometryCollectionTrait
@testset "Mixed geometry types are returned" begin
@test GI.getgeom(gc, 1) isa GeoJSON.Polygon
@test GI.getgeom(gc, 2) isa GeoJSON.Polygon
@test GI.getgeom(gc, 3) isa GeoJSON.Point
end
end
@testset "Extents" begin
# First, test that `Extents.extent` returns nothing for a geometry with no bbox
feature = GeoJSON.read(Samples.b)
@test isnothing(GI.Extents.extent(feature))
# Next, test that `GI.extent` returns the correct extent for a geometry with a bbox
@test GI.extent(feature) == mapreduce(GI.extent, GI.Extents.union, GI.getpoint(feature.geometry))
end
@testset "GeoInterface tests" begin
@test all(GI.testgeometry, GeoJSON.read.(Samples.geometries))
@test all(GI.testfeature, GeoJSON.read.(Samples.features))
@test all(GI.testfeaturecollection, GeoJSON.read.(Samples.featurecollections))
end
@testset "GeoFormatTypes" begin
gft_str = GeoFormatTypes.GeoJSON(Samples.b)
f = GeoJSON.read(gft_str)
@test f isa GeoJSON.Feature
dict = Dict{String,Any}("type" => "Point", "coordinates" => [-105.0, 39.5])
gft_dict = GeoFormatTypes.GeoJSON(dict)
p = GeoJSON.read(gft_dict)
@test p isa GeoJSON.Point
end
@testset "numbertype" begin
# all numbers are Float32 since we use numbertype=Float32
p = GeoJSON.read(Samples.point_int)
@test p isa GeoJSON.Point
coords = GeoJSON.coordinates(p)
@test eltype(coords) == Float32
@test coords == (1.0, 2.0)
@test collect(coords) isa Vector{Float32}
end
@testset "equality" begin
p = GeoJSON.read(Samples.point_int)
@test p == p
@test GeoJSON.Point(coordinates=(1, 2)) != GeoJSON.Point(coordinates=(2, 3))
@test GeoJSON.LineString(coordinates=[(1, 2)]) != GeoJSON.MultiPoint(coordinates=[(1, 2)])
end
@testset "regression" begin
GeoJSON.read(Samples.null_prop_feat)
end
@testset "NamedTuple point order doesn't matter as long as it's known" begin
@test GeoJSON.write((X=1.0, Y=2.0)) ==
GeoJSON.write((Y=2.0, X=1.0)) ==
"{\"type\":\"Point\",\"coordinates\":[1.0,2.0]}"
@test GeoJSON.write((Z=3, X=1.0, Y=2.0)) ==
GeoJSON.write((Y=2.0, X=1.0, Z=3)) ==
GeoJSON.write((Y=2.0, Z=3, X=1.0)) ==
GeoJSON.write((X=1.0, Z=3, Y=2.0)) ==
"{\"type\":\"Point\",\"coordinates\":[1.0,2.0,3]}"
# M is not in the spec
@test GeoJSON.write((Z=3, X=1.0, Y=2.0, M=4)) ==
GeoJSON.write((Y=2.0, X=1.0, M=4, Z=3)) ==
GeoJSON.write((M=4, Y=2.0, Z=3, X=1.0)) ==
GeoJSON.write((X=1.0, Z=3, M=4, Y=2.0)) ==
"{\"type\":\"Point\",\"coordinates\":[1.0,2.0,3]}"
end
@testset "firstindex and begin indexing" begin
# Test Point geometry
p = GeoJSON.read("""{"type":"Point","coordinates":[30,10]}""")
@test firstindex(p) == 1
@test p[begin] == p[1]
@test p[begin] == 30.0f0
# Test LineString geometry
ls = GeoJSON.read("""{"type":"LineString","coordinates":[[100.0,0.0],[101.0,1.0]]}""")
@test firstindex(ls) == 1
@test ls[begin] == ls[1]
@test ls[begin] == (100.0f0, 0.0f0)
# Test Polygon geometry
poly = GeoJSON.read("""{"type":"Polygon","coordinates":[[[100.0,0.0],[101.0,0.0],[101.0,1.0],[100.0,1.0],[100.0,0.0]]]}""")
@test firstindex(poly) == 1
@test poly[begin] == poly[1]
# Test GeometryCollection
gc = GeoJSON.read("""{"type":"GeometryCollection","geometries":[{"type":"Point","coordinates":[100.0,0.0]},{"type":"LineString","coordinates":[[101.0,0.0],[102.0,1.0]]}]}""")
@test firstindex(gc) == 1
@test gc[begin] == gc[1]
@test gc[begin] isa GeoJSON.AbstractGeometry
# Test FeatureCollection
fc = GeoJSON.read("""{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Point","coordinates":[102.0,0.5]},"properties":{"prop0":"value0"}}]}""")
@test firstindex(fc) == 1
@test fc[begin] == fc[1]
@test fc[begin] isa GeoJSON.Feature
end
@testset "lastindex and end indexing" begin
# Test Point geometry
p = GeoJSON.read("""{"type":"Point","coordinates":[30,10]}""")
@test lastindex(p) == length(p)
@test p[end] == p[lastindex(p)]
@test p[end] == 10.0f0
# Test LineString geometry
ls = GeoJSON.read("""{"type":"LineString","coordinates":[[100.0,0.0],[101.0,1.0]]}""")
@test lastindex(ls) == length(ls)
@test ls[end] == ls[lastindex(ls)]
@test ls[end] == (101.0f0, 1.0f0)
# Test Polygon geometry
poly = GeoJSON.read("""{"type":"Polygon","coordinates":[[[100.0,0.0],[101.0,0.0],[101.0,1.0],[100.0,1.0],[100.0,0.0]]]}""")
@test lastindex(poly) == length(poly)
@test poly[end] == poly[lastindex(poly)]
# Test GeometryCollection
gc = GeoJSON.read("""{"type":"GeometryCollection","geometries":[{"type":"Point","coordinates":[100.0,0.0]},{"type":"LineString","coordinates":[[101.0,0.0],[102.0,1.0]]}]}""")
@test lastindex(gc) == length(gc)
@test gc[end] == gc[lastindex(gc)]
@test gc[end] isa GeoJSON.AbstractGeometry
# Test FeatureCollection
fc = GeoJSON.read("""{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Point","coordinates":[102.0,0.5]},"properties":{"prop0":"value0"}}]}""")
@test lastindex(fc) == length(fc)
@test fc[end] == fc[lastindex(fc)]
@test fc[end] isa GeoJSON.Feature
end
@testset "Tables" begin
# try a namedtuple table
t1 = map(tuple.(1:10, 1:10), rand(10), ["abc" for i in 1:10]) do geometry, prop1, prop2
(; geometry, prop1, prop2)
end
t1_geojson_str = GeoJSON.write(t1)
t1_geojson = GeoJSON.read(t1_geojson_str)
@test map(x -> (GI.x(x), GI.y(x)), t1_geojson.geometry) == getproperty.(t1, :geometry)
@test t1_geojson.prop1 == getproperty.(t1, :prop1)
@test t1_geojson.prop2 == getproperty.(t1, :prop2)
t2 = map(tuple.(1:10, 1:10), rand(10), ["abc" for i in 1:10]) do somethingelse, prop1, prop2
(; somethingelse, prop1, prop2)
end
t2_geojson_str = GeoJSON.write(t2; geometrycolumn = :somethingelse)
t2_geojson = GeoJSON.read(t2_geojson_str)
@test map(x -> (GI.x(x), GI.y(x)), t2_geojson.geometry) == getproperty.(t2, :somethingelse)
@test t2_geojson.prop1 == getproperty.(t2, :prop1)
@test t2_geojson.prop2 == getproperty.(t2, :prop2)
@testset "Metadata" begin
@test GI.DataAPI.metadatasupport(typeof(t2_geojson)) == (; read = true, write = false)
@test GI.DataAPI.metadatakeys(t2_geojson) == ("GEOINTERFACE:geometrycolumns", "GEOINTERFACE:crs")
@test GI.DataAPI.metadata(t2_geojson, "GEOINTERFACE:geometrycolumns") == (:geometry,)
@test GI.DataAPI.metadata(t2_geojson, "GEOINTERFACE:crs") == GI.crs(t2_geojson)
@test_throws KeyError GI.DataAPI.metadata(t2_geojson, "not_a_key")
df = DataFrames.DataFrame(t2_geojson)
m = DataFrames.metadata(df)
@test isempty(setdiff(keys(m), (GI.GEOINTERFACE_CRS_KEY, GI.GEOINTERFACE_GEOMETRYCOLUMNS_KEY)))
@test m[GI.GEOINTERFACE_CRS_KEY] == GI.crs(t2_geojson)
@test m[GI.GEOINTERFACE_GEOMETRYCOLUMNS_KEY] == (:geometry,)
end
end
end # testset "GeoJSON"