Skip to content

Commit 363a496

Browse files
authored
Fix coord order and performance of write (#64)
* write with NTuple not vector * test named tuple order * fix change to extent * bugfix map write
1 parent 2f7fe9d commit 363a496

File tree

2 files changed

+47
-13
lines changed

2 files changed

+47
-13
lines changed

src/io.jl

+30-13
Original file line numberDiff line numberDiff line change
@@ -61,26 +61,43 @@ function _lower(obj)
6161
)
6262
return _add_bbox(GI.extent(obj), base)
6363
elseif GI.isgeometry(obj)
64-
_lower(GI.geomtrait(obj), obj)
64+
if GI.is3d(obj)
65+
_lower(GI.geomtrait(obj), Val{true}(), obj)
66+
else
67+
_lower(GI.geomtrait(obj), Val{false}(), obj)
68+
end
6569
else
6670
# null geometry
6771
nothing
6872
end
6973
end
70-
_lower(::GI.AbstractPointTrait, obj) = (type="Point", coordinates=GI.coordinates(obj))
71-
_lower(::GI.AbstractLineStringTrait, obj) =
72-
(type="LineString", coordinates=GI.coordinates(obj))
73-
_lower(::GI.AbstractPolygonTrait, obj) =
74-
(type="Polygon", coordinates=GI.coordinates(obj))
75-
_lower(::GI.AbstractMultiPointTrait, obj) =
76-
(type="MultiPoint", coordinates=GI.coordinates(obj))
77-
_lower(::GI.AbstractMultiLineStringTrait, obj) =
78-
(type="Polygon", coordinates=GI.coordinates(obj))
79-
_lower(::GI.AbstractMultiPolygonTrait, obj) =
80-
(type="MultiPolygon", coordinates=collect(GI.coordinates(obj)))
81-
_lower(::GI.AbstractGeometryCollectionTrait, obj) =
74+
_lower(t::GI.AbstractPointTrait, d, obj) =
75+
(type="Point", coordinates=_to_vector_ntuple(t, d, obj))
76+
_lower(t::GI.AbstractLineStringTrait, d, obj) =
77+
(type="LineString", coordinates=_to_vector_ntuple(t, d, obj))
78+
_lower(t::GI.AbstractPolygonTrait, d, obj) =
79+
(type="Polygon", coordinates=_to_vector_ntuple(t, d, obj))
80+
_lower(t::GI.AbstractMultiPointTrait, d, obj) =
81+
(type="MultiPoint", coordinates=_to_vector_ntuple(t, d, obj))
82+
_lower(t::GI.AbstractMultiLineStringTrait, d, obj) =
83+
(type="Polygon", coordinates=_to_vector_ntuple(t, d, obj))
84+
_lower(t::GI.AbstractMultiPolygonTrait, d, obj) =
85+
(type="MultiPolygon", coordinates=_to_vector_ntuple(t, d, obj))
86+
_lower(t::GI.AbstractGeometryCollectionTrait, d, obj) =
8287
(type="GeometryCollection", geometries=_lower.(GI.getgeom(obj)))
8388

89+
function _to_vector_ntuple(::GI.PointTrait, is3d::Val{false}, geom)
90+
(GI.x(geom), GI.y(geom))
91+
end
92+
function _to_vector_ntuple(::GI.PointTrait, is3d::Val{true}, geom)
93+
(GI.x(geom), GI.y(geom), GI.z(geom))
94+
end
95+
function _to_vector_ntuple(::GI.AbstractGeometryTrait, is3d, geom)
96+
map(GI.getgeom(geom)) do child_geom
97+
_to_vector_ntuple(GI.geomtrait(child_geom), is3d, child_geom)
98+
end
99+
end
100+
84101
_add_bbox(::Nothing, nt::NamedTuple) = nt
85102
function _add_bbox(ext::Extents.Extent, nt::NamedTuple)
86103
if haskey(ext, :Z)

test/runtests.jl

+17
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,23 @@ include("geojson_samples.jl")
316316
GeoJSON.read(Samples.null_prop_feat)
317317
end
318318

319+
@testset "NamedTuple point order doesn't matter as long as it's known" begin
320+
@test GeoJSON.write((X=1.0, Y=2.0)) ==
321+
GeoJSON.write((Y=2.0, X=1.0)) ==
322+
"{\"type\":\"Point\",\"coordinates\":[1.0,2.0]}"
323+
@test GeoJSON.write((Z=3, X=1.0, Y=2.0)) ==
324+
GeoJSON.write((Y=2.0, X=1.0, Z=3)) ==
325+
GeoJSON.write((Y=2.0, Z=3, X=1.0)) ==
326+
GeoJSON.write((X=1.0, Z=3, Y=2.0)) ==
327+
"{\"type\":\"Point\",\"coordinates\":[1.0,2.0,3]}"
328+
# M is not in the spec
329+
@test GeoJSON.write((Z=3, X=1.0, Y=2.0, M=4)) ==
330+
GeoJSON.write((Y=2.0, X=1.0, M=4, Z=3)) ==
331+
GeoJSON.write((M=4, Y=2.0, Z=3, X=1.0)) ==
332+
GeoJSON.write((X=1.0, Z=3, M=4, Y=2.0)) ==
333+
"{\"type\":\"Point\",\"coordinates\":[1.0,2.0,3]}"
334+
end
335+
319336
Aqua.test_all(GeoJSON)
320337

321338
end # testset "GeoJSON"

0 commit comments

Comments
 (0)