Skip to content

Commit 2daf2b2

Browse files
committed
add convert methods
1 parent 6396ab1 commit 2daf2b2

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

src/geointerface.jl

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,17 @@ GI.geomtrait(g::MultiLineString) = GI.MultiLineStringTrait()
1212
GI.geomtrait(g::MultiPolygon) = GI.MultiPolygonTrait()
1313
GI.geomtrait(g::GeometryCollection) = GI.GeometryCollectionTrait()
1414

15+
GI.traittype(::Val{:GeoJSON}, ::GI.PointTrait) = Point
16+
GI.traittype(::Val{:GeoJSON}, ::GI.LineStringTrait) = LineString
17+
GI.traittype(::Val{:GeoJSON}, ::GI.PolygonTrait) = Polygon
18+
GI.traittype(::Val{:GeoJSON}, ::GI.MultiLineStringTrait) = MultiLineString
19+
GI.traittype(::Val{:GeoJSON}, ::GI.MultiPolygonTrait) = MultiPolygon
20+
GI.traittype(::Val{:GeoJSON}, ::GI.GeometryCollectionTrait) = GeometryCollection
21+
1522
# we have to make use of the GI fallbacks that call geomtrait on the input
1623
GI.ncoord(::GI.PointTrait, g::Point) = length(g)
1724
GI.getcoord(::GI.PointTrait, g::Point, i::Int) = g[i]
25+
GI.convert(::Type{<:Point}, ::GI.PointTrait, geom) = Point(; coordinates=collect(GI.getcoord(geom)))
1826

1927
GI.ncoord(::GI.LineStringTrait, g::LineString) = length(first(g))
2028
GI.ngeom(::GI.LineStringTrait, g::LineString) = length(g)
@@ -23,42 +31,65 @@ GI.getpoint(::GI.LineStringTrait, g::LineString, i::Int) = Point(coordinates = g
2331
# TODO what to return for length 0 and 1?
2432
# TODO should this be an approximate equals for floating point?
2533
GI.isclosed(::GI.LineStringTrait, g::LineString) = first(g) == last(g)
34+
GI.convert(::Type{<:LineString}, ::GI.LineStringTrait, geom) =
35+
LineString(; coordinates=coordinates(geom))
2636

2737
GI.ngeom(::GI.PolygonTrait, g::Polygon) = length(g)
2838
GI.getgeom(::GI.PolygonTrait, g::Polygon, i::Integer) = LineString(coordinates = g[i])
2939
GI.ncoord(::GI.PolygonTrait, g::Polygon) = length(first(first(g)))
3040
GI.getexterior(::GI.PolygonTrait, g::Polygon) = LineString(coordinates = first(g))
3141
GI.nhole(::GI.PolygonTrait, g::Polygon) = length(g) - 1
3242
GI.gethole(::GI.PolygonTrait, g::Polygon, i::Int) = LineString(coordinates = g[i+1])
43+
GI.convert(::Type{<:Polygon}, ::GI.PolygonTrait, geom) =
44+
Polygon(; coordinates=coordinates(geom))
3345

3446
GI.ncoord(::GI.MultiPointTrait, g::MultiPoint) = length(first(g))
3547
GI.ngeom(::GI.MultiPointTrait, g::MultiPoint) = length(g)
3648
GI.getgeom(::GI.MultiPointTrait, g::MultiPoint, i::Int) = Point(coordinates = g[i])
49+
GI.convert(::Type{<:MultiPoint}, ::GI.MultiPointTrait, geom) =
50+
MultiPoint(; coordinates=coordinates(geom))
3751

3852
GI.ncoord(::GI.MultiLineStringTrait, g::MultiLineString) = length(first(first(g)))
3953
GI.ngeom(::GI.MultiLineStringTrait, g::MultiLineString) = length(g)
4054
GI.getgeom(::GI.MultiLineStringTrait, g::MultiLineString, i::Int) =
4155
LineString(coordinates = g[i])
56+
GI.convert(::Type{<:MultiLineString}, ::GI.MultiLineStringTrait, geom) =
57+
MultiLineString(; coordinates=coordinates(geom))
4258

4359
GI.ncoord(::GI.MultiPolygonTrait, g::MultiPolygon) = length(first(first(first(g))))
4460
GI.ngeom(::GI.MultiPolygonTrait, g::MultiPolygon) = length(g)
4561
GI.getgeom(::GI.MultiPolygonTrait, g::MultiPolygon, i::Int) = Polygon(coordinates = g[i])
62+
GI.convert(::Type{<:MultiPolygon}, ::GI.MultiPolygonTrait, geom) =
63+
MultiLineString(; coordinates=coordinates(geom))
4664

4765
GI.ncoord(::GI.GeometryCollectionTrait, g::GeometryCollection) = GI.ncoord(first(g))
4866
GI.ngeom(::GI.GeometryCollectionTrait, g::GeometryCollection) = length(g)
49-
GI.getgeom(::GI.GeometryCollectionTrait, g::GeometryCollection, i::Int) = geometry(g[i])
67+
GI.getgeom(::GI.GeometryCollectionTrait, g::GeometryCollection, i::Int) =
68+
function GI.convert(::Type{<:GeometryCollection}, ::GI.GeometryCollectionTrait, geom)
69+
geometries = [GI.convert(Val{:GeoJSON}(), g) for g in GI.getgeoms(geom)]
70+
GeometryCollection(; geometries)
71+
end
5072

5173
# Feature
5274
GI.isfeature(::Type{<:Feature}) = true
5375
GI.trait(::Feature) = GI.FeatureTrait()
5476
GI.geometry(f::Feature) = geometry(f)
5577
GI.properties(f::Feature) = properties(f)
78+
function GI.convert(::Type{<:Feature}, ::GI.FeatureTrait, feature)
79+
Feature(;
80+
geometry=GI.convert(GeoJSON, GI.geometry(feature)),
81+
properties=GI.properties(feature),
82+
)
83+
end
5684

5785
# FeatureCollection
5886
GI.isfeaturecollection(::Type{<:FeatureCollection}) = true
5987
GI.trait(::FeatureCollection) = GI.FeatureCollectionTrait()
6088
GI.getfeature(::GI.FeatureCollectionTrait, fc::FeatureCollection, i::Integer) = fc[i]
6189
GI.nfeature(::GI.FeatureCollectionTrait, fc::FeatureCollection) = length(fc)
90+
function GI.convert(::Type{<:FeatureCollection}, ::GI.FeatureCollectionTrait, fc)
91+
FeatureCollection(GI.convert(Feature, GI.features(fc)))
92+
end
6293

6394
# Any GeoJSON Object
6495
function GI.extent(x::GeoJSONObject)

0 commit comments

Comments
 (0)