Skip to content

Commit e1e1f6e

Browse files
committed
add GeoInterface.convert
1 parent 839f463 commit e1e1f6e

File tree

4 files changed

+93
-1
lines changed

4 files changed

+93
-1
lines changed

Project.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ julia = "1.6"
2323

2424
[extras]
2525
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
26+
GeoJSON = "61d90e0f-e114-555e-ac52-39dfb47a3ef9"
2627
OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
2728
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
2829
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
2930

3031
[targets]
31-
test = ["Aqua", "Test", "Random", "OffsetArrays"]
32+
test = ["Aqua", "GeoJSON", "Test", "Random", "OffsetArrays"]

src/geointerface.jl

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,39 @@ function GeoInterface.ncoord(::PolyhedralSurfaceTrait,
7777
end
7878
GeoInterface.ngeom(::PolyhedralSurfaceTrait, g::AbstractMesh) = length(g)
7979
GeoInterface.getgeom(::PolyhedralSurfaceTrait, g::AbstractMesh, i) = g[i]
80+
81+
function GeoInterface.convert(::Type{Point}, type::PointTrait, geom)
82+
dim = Int(ncoord(geom))
83+
return Point{dim}(GeoInterface.coordinates(geom))
84+
end
85+
86+
function GeoInterface.convert(::Type{LineString}, type::LineStringTrait, geom)
87+
dim = Int(ncoord(geom))
88+
return LineString([Point{dim}(GeoInterface.coordinates(p)) for p in getgeom(geom)])
89+
end
90+
91+
function GeoInterface.convert(::Type{Polygon}, type::PolygonTrait, geom)
92+
t = LineStringTrait()
93+
exterior = GeoInterface.convert(LineString, t, GeoInterface.getexterior(geom))
94+
if GeoInterface.nhole(geom) == 0
95+
return Polygon(exterior)
96+
else
97+
interiors = GeoInterface.convert.(LineString, Ref(t), GeoInterface.gethole(geom))
98+
return Polygon(exterior, interiors)
99+
end
100+
end
101+
102+
function GeoInterface.convert(::Type{MultiPoint}, type::MultiPointTrait, geom)
103+
dim = Int(ncoord(geom))
104+
return MultiPoint([Point{dim}(GeoInterface.coordinates(p)) for p in getgeom(geom)])
105+
end
106+
107+
function GeoInterface.convert(::Type{MultiLineString}, type::MultiLineStringTrait, geom)
108+
t = LineStringTrait()
109+
return MultiLineString([GeoInterface.convert(LineString, t, l) for l in getgeom(geom)])
110+
end
111+
112+
function GeoInterface.convert(::Type{MultiPolygon}, type::MultiPolygonTrait, geom)
113+
t = PolygonTrait()
114+
return MultiPolygon([GeoInterface.convert(Polygon, t, poly) for poly in getgeom(geom)])
115+
end

test/geointerface.jl

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,57 @@ end
4545
mesh = triangle_mesh(Sphere(Point3f(0), 1))
4646
@test testgeometry(mesh)
4747
end
48+
49+
@testset "Convert" begin
50+
# convert GeoJSON geometry types to GeometryBasics via the GeoInterface
51+
point_str = """{"type":"Point","coordinates":[30.1,10.1]}"""
52+
point_3d_str = """{"type":"Point","coordinates":[30.1,10.1,5.1]}"""
53+
linestring_str = """{"type":"LineString","coordinates":[[30.1,10.1],[10.1,30.1],[40.1,40.1]]}"""
54+
polygon_str = """{"type":"Polygon","coordinates":[[[30.1,10.1],[40.1,40.1],[20.1,40.1],[10.1,20.1],[30.1,10.1]]]}"""
55+
polygon_hole_str = """{"type":"Polygon","coordinates":[[[35.1,10.1],[45.1,45.1],[15.1,40.1],[10.1,20.1],[35.1,10.1]],[[20.1,30.1],[35.1,35.1],[30.1,20.1],[20.1,30.1]]]}"""
56+
multipoint_str = """{"type":"MultiPoint","coordinates":[[10.1,40.1],[40.1,30.1],[20.1,20.1],[30.1,10.1]]}"""
57+
multilinestring_str = """{"type":"MultiLineString","coordinates":[[[10.1,10.1],[20.1,20.1],[10.1,40.1]],[[40.1,40.1],[30.1,30.1],[40.1,20.1],[30.1,10.1]]]}"""
58+
multipolygon_str = """{"type":"MultiPolygon","coordinates":[[[[30.1,20.1],[45.1,40.1],[10.1,40.1],[30.1,20.1]]],[[[15.1,5.1],[40.1,10.1],[10.1,20.1],[5.1,10.1],[15.1,5.1]]]]}"""
59+
multipolygon_hole_str = """{"type":"MultiPolygon","coordinates":[[[[40.1,40.1],[20.1,45.1],[45.1,30.1],[40.1,40.1]]],[[[20.1,35.1],[10.1,30.1],[10.1,10.1],[30.1,5.1],[45.1,20.1],[20.1,35.1]],[[30.1,20.1],[20.1,15.1],[20.1,25.1],[30.1,20.1]]]]}"""
60+
61+
point_json = GeoJSON.read(point_str)
62+
point_3d_json = GeoJSON.read(point_3d_str)
63+
linestring_json = GeoJSON.read(linestring_str)
64+
polygon_json = GeoJSON.read(polygon_str)
65+
polygon_hole_json = GeoJSON.read(polygon_hole_str)
66+
multipoint_json = GeoJSON.read(multipoint_str)
67+
multilinestring_json = GeoJSON.read(multilinestring_str)
68+
multipolygon_json = GeoJSON.read(multipolygon_str)
69+
multipolygon_hole_json = GeoJSON.read(multipolygon_hole_str)
70+
71+
point_gb = GeoInterface.convert(Point, point_json)
72+
point_3d_gb = GeoInterface.convert(Point, point_3d_json)
73+
linestring_gb = GeoInterface.convert(LineString, linestring_json)
74+
polygon_gb = GeoInterface.convert(Polygon, polygon_json)
75+
polygon_hole_gb = GeoInterface.convert(Polygon, polygon_hole_json)
76+
multipoint_gb = GeoInterface.convert(MultiPoint, multipoint_json)
77+
multilinestring_gb = GeoInterface.convert(MultiLineString, multilinestring_json)
78+
multipolygon_gb = GeoInterface.convert(MultiPolygon, multipolygon_json)
79+
multipolygon_hole_gb = GeoInterface.convert(MultiPolygon, multipolygon_hole_json)
80+
81+
@test point_gb === Point{2, Float64}(30.1, 10.1)
82+
@test point_3d_gb === Point{3, Float64}(30.1, 10.1, 5.1)
83+
@test linestring_gb isa LineString
84+
@test length(linestring_gb) == 2
85+
@test eltype(linestring_gb) == Line{2, Float64}
86+
@test polygon_gb isa Polygon
87+
@test isempty(polygon_gb.interiors)
88+
@test polygon_hole_gb isa Polygon
89+
@test length(polygon_hole_gb.interiors) == 1
90+
@test multipoint_gb isa MultiPoint
91+
@test length(multipoint_gb) == 4
92+
@test multipoint_gb[4] === Point{2, Float64}(30.1, 10.1)
93+
@test multilinestring_gb isa MultiLineString
94+
@test length(multilinestring_gb) == 2
95+
@test multipolygon_gb isa MultiPolygon
96+
@test length(multipolygon_gb) == 2
97+
@test multipolygon_hole_gb isa MultiPolygon
98+
@test length(multipolygon_hole_gb) == 2
99+
@test length(multipolygon_hole_gb[1].interiors) == 0
100+
@test length(multipolygon_hole_gb[2].interiors) == 1
101+
end

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ using GeometryBasics
33
using LinearAlgebra
44
using GeometryBasics: attributes
55
using GeoInterface
6+
using GeoJSON
67

78
@testset "GeometryBasics" begin
89

0 commit comments

Comments
 (0)