Skip to content

Commit e6e4a87

Browse files
committed
improve point conversions and make tests pass with the latest GeoJSON
1 parent cb87b80 commit e6e4a87

File tree

2 files changed

+36
-12
lines changed

2 files changed

+36
-12
lines changed

src/geointerface.jl

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ GeoInterface.isgeometry(::Type{<:AbstractGeometry}) = true
44
GeoInterface.isgeometry(::Type{<:AbstractFace}) = true
55
GeoInterface.isgeometry(::Type{<:AbstractPoint}) = true
66
GeoInterface.isgeometry(::Type{<:AbstractMesh}) = true
7-
GeoInterface.isgeometry(::Type{<:AbstractLineString}) = true
87
GeoInterface.isgeometry(::Type{<:AbstractPolygon}) = true
8+
GeoInterface.isgeometry(::Type{<:LineString}) = true
99
GeoInterface.isgeometry(::Type{<:MultiPoint}) = true
1010
GeoInterface.isgeometry(::Type{<:MultiLineString}) = true
1111
GeoInterface.isgeometry(::Type{<:MultiPolygon}) = true
@@ -92,13 +92,29 @@ GeoInterface.ngeom(::PolyhedralSurfaceTrait, g::AbstractMesh) = length(g)
9292
GeoInterface.getgeom(::PolyhedralSurfaceTrait, g::AbstractMesh, i) = g[i]
9393

9494
function GeoInterface.convert(::Type{Point}, type::PointTrait, geom)
95-
dim = Int(ncoord(geom))
96-
return Point{dim, Float64}(GeoInterface.coordinates(geom))
95+
x, y = GeoInterface.x(geom), GeoInterface.y(geom)
96+
if GeoInterface.is3d(geom)
97+
z = GeoInterface.z(geom)
98+
T = promote_type(typeof(x), typeof(y), typeof(z))
99+
return Point{3,T}(x, y, z)
100+
else
101+
GeoInterface.x(geom), GeoInterface.y(geom)
102+
T = promote_type(typeof(x), typeof(y))
103+
return Point{2,T}(x, y)
104+
end
97105
end
98106

99107
function GeoInterface.convert(::Type{LineString}, type::LineStringTrait, geom)
100-
dim = Int(ncoord(geom))
101-
return LineString([Point{dim, Float64}(GeoInterface.coordinates(p)) for p in getgeom(geom)])
108+
g1 = getgeom(geom, 1)
109+
x, y = GeoInterface.x(g1), GeoInterface.y(g1)
110+
if GeoInterface.is3d(geom)
111+
z = GeoInterface.z(g1)
112+
T = promote_type(typeof(x), typeof(y), typeof(z))
113+
return LineString([Point{3,T}(GeoInterface.x(p), GeoInterface.y(p), GeoInterface.z(p)) for p in getgeom(geom)])
114+
else
115+
T = promote_type(typeof(x), typeof(y))
116+
return LineString([Point{2,T}(GeoInterface.x(p), GeoInterface.y(p)) for p in getgeom(geom)])
117+
end
102118
end
103119

104120
function GeoInterface.convert(::Type{Polygon}, type::PolygonTrait, geom)
@@ -113,8 +129,16 @@ function GeoInterface.convert(::Type{Polygon}, type::PolygonTrait, geom)
113129
end
114130

115131
function GeoInterface.convert(::Type{MultiPoint}, type::MultiPointTrait, geom)
116-
dim = Int(ncoord(geom))
117-
return MultiPoint([Point{dim, Float64}(GeoInterface.coordinates(p)) for p in getgeom(geom)])
132+
g1 = getgeom(geom, 1)
133+
x, y = GeoInterface.x(g1), GeoInterface.y(g1)
134+
if GeoInterface.is3d(geom)
135+
z = GeoInterface.z(g1)
136+
T = promote_type(typeof(x), typeof(y), typeof(z))
137+
return MultiPoint([Point{3,T}(GeoInterface.x(p), GeoInterface.y(p), GeoInterface.z(p)) for p in getgeom(geom)])
138+
else
139+
T = promote_type(typeof(x), typeof(y))
140+
return MultiPoint([Point{2,T}(GeoInterface.x(p), GeoInterface.y(p)) for p in getgeom(geom)])
141+
end
118142
end
119143

120144
function GeoInterface.convert(::Type{MultiLineString}, type::MultiLineStringTrait, geom)

test/geointerface.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,19 +93,19 @@ end
9393
multilinestring_gb = GeoInterface.convert(GeometryBasics, multilinestring_json)
9494
multipolygon_gb = GeoInterface.convert(GeometryBasics, multipolygon_json)
9595
multipolygon_hole_gb = GeoInterface.convert(GeometryBasics, multipolygon_hole_json)
96-
97-
@test point_gb === Point{2, Float64}(30.1, 10.1)
98-
@test point_3d_gb === Point{3, Float64}(30.1, 10.1, 5.1)
96+
97+
@test point_gb === Point{2,Float32}(30.1, 10.1)
98+
@test point_3d_gb === Point{3,Float32}(30.1, 10.1, 5.1)
9999
@test linestring_gb isa LineString
100100
@test length(linestring_gb) == 2
101-
@test eltype(linestring_gb) == Line{2, Float64}
101+
@test eltype(linestring_gb) == Line{2,Float32}
102102
@test polygon_gb isa Polygon
103103
@test isempty(polygon_gb.interiors)
104104
@test polygon_hole_gb isa Polygon
105105
@test length(polygon_hole_gb.interiors) == 1
106106
@test multipoint_gb isa MultiPoint
107107
@test length(multipoint_gb) == 4
108-
@test multipoint_gb[4] === Point{2, Float64}(30.1, 10.1)
108+
@test multipoint_gb[4] === Point{2,Float32}(30.1, 10.1)
109109
@test multilinestring_gb isa MultiLineString
110110
@test length(multilinestring_gb) == 2
111111
@test multipolygon_gb isa MultiPolygon

0 commit comments

Comments
 (0)