Skip to content

Commit c0e1ee3

Browse files
authored
Merge pull request #193 from rafaqz/fix_isgeometry
vectors are not `isgeometry` material
2 parents 484dd5c + dc16731 commit c0e1ee3

File tree

2 files changed

+60
-21
lines changed

2 files changed

+60
-21
lines changed

src/geointerface.jl

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
GeoInterface.isgeometry(::Type{<:AbstractGeometry}) = true
44
GeoInterface.isgeometry(::Type{<:AbstractFace}) = true
55
GeoInterface.isgeometry(::Type{<:AbstractPoint}) = true
6-
GeoInterface.isgeometry(::Type{<:AbstractVector{<:AbstractGeometry}}) = true
7-
GeoInterface.isgeometry(::Type{<:AbstractVector{<:AbstractPoint}}) = true
8-
GeoInterface.isgeometry(::Type{<:AbstractVector{<:LineString}}) = true
9-
GeoInterface.isgeometry(::Type{<:AbstractVector{<:AbstractPolygon}}) = true
10-
GeoInterface.isgeometry(::Type{<:AbstractVector{<:AbstractFace}}) = true
6+
GeoInterface.isgeometry(::Type{<:AbstractMesh}) = true
7+
GeoInterface.isgeometry(::Type{<:AbstractPolygon}) = true
8+
GeoInterface.isgeometry(::Type{<:LineString}) = true
9+
GeoInterface.isgeometry(::Type{<:MultiPoint}) = true
10+
GeoInterface.isgeometry(::Type{<:MultiLineString}) = true
11+
GeoInterface.isgeometry(::Type{<:MultiPolygon}) = true
1112
GeoInterface.isgeometry(::Type{<:Mesh}) = true
1213

1314
GeoInterface.geomtrait(::Point) = PointTrait()
@@ -23,6 +24,7 @@ GeoInterface.geomtrait(::AbstractMesh) = PolyhedralSurfaceTrait()
2324
# GeoInterface calls this method in `GeoInterface.convert(GeometryBasics, ...)`
2425
geointerface_geomtype(::GeoInterface.PointTrait) = Point
2526
geointerface_geomtype(::GeoInterface.MultiPointTrait) = MultiPoint
27+
geointerface_geomtype(::GeoInterface.LineTrait) = Line
2628
geointerface_geomtype(::GeoInterface.LineStringTrait) = LineString
2729
geointerface_geomtype(::GeoInterface.MultiLineStringTrait) = MultiLineString
2830
geointerface_geomtype(::GeoInterface.PolygonTrait) = Polygon
@@ -58,8 +60,7 @@ GeoInterface.getgeom(::MultiPointTrait, g::MultiPoint, i::Int) = g[i]
5860
function GeoInterface.ngeom(::MultiLineStringTrait, g::MultiLineString)
5961
return length(g)
6062
end
61-
function GeoInterface.getgeom(::MultiLineStringTrait, g::MultiLineString,
62-
i::Int)
63+
function GeoInterface.getgeom(::MultiLineStringTrait, g::MultiLineString, i::Int)
6364
return g[i]
6465
end
6566
GeoInterface.ncoord(::MultiLineStringTrait, g::MultiLineString{Dim}) where {Dim} = Dim
@@ -90,13 +91,29 @@ GeoInterface.ngeom(::PolyhedralSurfaceTrait, g::AbstractMesh) = length(g)
9091
GeoInterface.getgeom(::PolyhedralSurfaceTrait, g::AbstractMesh, i) = g[i]
9192

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

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

102119
function GeoInterface.convert(::Type{Polygon}, type::PolygonTrait, geom)
@@ -105,22 +122,30 @@ function GeoInterface.convert(::Type{Polygon}, type::PolygonTrait, geom)
105122
if GeoInterface.nhole(geom) == 0
106123
return Polygon(exterior)
107124
else
108-
interiors = GeoInterface.convert.(LineString, Ref(t), GeoInterface.gethole(geom))
125+
interiors = map(h -> GeoInterface.convert(LineString, t, h), GeoInterface.gethole(geom))
109126
return Polygon(exterior, interiors)
110127
end
111128
end
112129

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

118143
function GeoInterface.convert(::Type{MultiLineString}, type::MultiLineStringTrait, geom)
119144
t = LineStringTrait()
120-
return MultiLineString([GeoInterface.convert(LineString, t, l) for l in getgeom(geom)])
145+
return MultiLineString(map(l -> GeoInterface.convert(LineString, t, l), getgeom(geom)))
121146
end
122147

123148
function GeoInterface.convert(::Type{MultiPolygon}, type::MultiPolygonTrait, geom)
124149
t = PolygonTrait()
125-
return MultiPolygon([GeoInterface.convert(Polygon, t, poly) for poly in getgeom(geom)])
150+
return MultiPolygon(map(poly -> GeoInterface.convert(Polygon, t, poly), getgeom(geom)))
126151
end

test/geointerface.jl

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
11
@testset "Basic types" begin
22
point = Point(2, 3)
3+
@test geomtrait(point) isa PointTrait
34
@test testgeometry(point)
45
@test ncoord(point) == 2
56
@test getcoord(point, 2) == 3
67
@test GeoInterface.coordinates(point) == [2, 3]
78

9+
line = Line(Point(2, 3), Point(4, 5))
10+
@test geomtrait(line) isa LineTrait
11+
@test testgeometry(line)
12+
@test ngeom(line) == 2
13+
@test getgeom(line, 2) == Point(4, 5)
14+
@test GeoInterface.coordinates(line) == [[2, 3], [4, 5]]
15+
816
mp = MultiPoint([point, point])
17+
@test geomtrait(mp) isa MultiPointTrait
918
@test testgeometry(mp)
1019
@test ngeom(mp) == 2
1120
@test getgeom(mp, 2) == point
1221
@test GeoInterface.coordinates(mp) == [[2, 3], [2, 3]]
1322

1423
linestring = LineString(Point{2,Int}[(10, 10), (20, 20), (10, 40)])
24+
@test geomtrait(linestring) isa LineStringTrait
1525
@test testgeometry(linestring)
1626
@test ngeom(linestring) == 3
1727
@test ncoord(linestring) == 2
@@ -21,22 +31,26 @@
2131
@test GeoInterface.coordinates(linestring) == [[10, 10], [20, 20], [10, 40]]
2232

2333
multilinestring = MultiLineString([linestring, linestring])
34+
@test geomtrait(multilinestring) isa MultiLineStringTrait
2435
@test testgeometry(multilinestring)
2536
@test GeoInterface.coordinates(multilinestring) ==
2637
[[[10, 10], [20, 20], [10, 40]], [[10, 10], [20, 20], [10, 40]]]
2738
@test ncoord(multilinestring) == 2
2839

2940
poly = Polygon(rand(Point{2,Float32}, 5), [rand(Point{2,Float32}, 5)])
41+
@test geomtrait(poly) isa PolygonTrait
3042
@test testgeometry(poly)
3143
@test length(GeoInterface.coordinates(poly)) == 2
3244
@test length(GeoInterface.coordinates(poly)[1]) == 5
3345

3446
triangle = Triangle(point, point, point)
47+
@test geomtrait(triangle) isa PolygonTrait # ?? should it be a Triangle trait
3548
@test testgeometry(triangle)
3649
@test length(GeoInterface.coordinates(triangle)) == 1
3750
@test length(GeoInterface.coordinates(triangle)[1]) == 3
3851

3952
polys = MultiPolygon([poly, poly])
53+
@test geomtrait(polys) isa MultiPolygonTrait
4054
@test testgeometry(polys)
4155
@test length(GeoInterface.coordinates(polys)) == 2
4256
@test length(GeoInterface.coordinates(polys)[1]) == 2
@@ -79,19 +93,19 @@ end
7993
multilinestring_gb = GeoInterface.convert(GeometryBasics, multilinestring_json)
8094
multipolygon_gb = GeoInterface.convert(GeometryBasics, multipolygon_json)
8195
multipolygon_hole_gb = GeoInterface.convert(GeometryBasics, multipolygon_hole_json)
82-
83-
@test point_gb === Point{2, Float64}(30.1, 10.1)
84-
@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)
8599
@test linestring_gb isa LineString
86100
@test length(linestring_gb) == 2
87-
@test eltype(linestring_gb) == Line{2, Float64}
101+
@test eltype(linestring_gb) == Line{2,Float32}
88102
@test polygon_gb isa Polygon
89103
@test isempty(polygon_gb.interiors)
90104
@test polygon_hole_gb isa Polygon
91105
@test length(polygon_hole_gb.interiors) == 1
92106
@test multipoint_gb isa MultiPoint
93107
@test length(multipoint_gb) == 4
94-
@test multipoint_gb[4] === Point{2, Float64}(30.1, 10.1)
108+
@test multipoint_gb[4] === Point{2,Float32}(30.1, 10.1)
95109
@test multilinestring_gb isa MultiLineString
96110
@test length(multilinestring_gb) == 2
97111
@test multipolygon_gb isa MultiPolygon

0 commit comments

Comments
 (0)