Skip to content

Commit 5264a54

Browse files
committed
add convert methods
1 parent 2f7fe9d commit 5264a54

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/geointerface.jl

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,48 +13,78 @@ GI.ncoord(::GI.AbstractTrait, ::AbstractGeometry{D,T}) where {D,T} = D
1313
GI.coordinates(::GI.AbstractGeometryTrait, g::AbstractGeometry) = coordinates(g)
1414
GI.coordinates(::GI.AbstractPointTrait, g::AbstractGeometry) = coordinates(g) # prevent ambiguity
1515

16+
GI.traittype(::Val{:GeoJSON}, ::GI.PointTrait) = Point
17+
GI.traittype(::Val{:GeoJSON}, ::GI.LineStringTrait) = LineString
18+
GI.traittype(::Val{:GeoJSON}, ::GI.PolygonTrait) = Polygon
19+
GI.traittype(::Val{:GeoJSON}, ::GI.MultiLineStringTrait) = MultiLineString
20+
GI.traittype(::Val{:GeoJSON}, ::GI.MultiPolygonTrait) = MultiPolygon
21+
GI.traittype(::Val{:GeoJSON}, ::GI.GeometryCollectionTrait) = GeometryCollection
22+
1623
# we have to make use of the GI fallbacks that call geomtrait on the input
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.ngeom(::GI.LineStringTrait, g::LineString) = length(g)
2028
GI.getgeom(::GI.LineStringTrait, g::LineString{D,T}, i::Integer) where {D,T} = Point{D,T}(nothing, g[i])
2129
GI.getpoint(::GI.LineStringTrait, g::LineString{D,T}, i::Int) where {D,T} = Point{D,T}(nothing, g[i])
2230
# TODO what to return for length 0 and 1?
2331
# TODO should this be an approximate equals for floating point?
2432
GI.isclosed(::GI.LineStringTrait, g::LineString) = first(g) == last(g)
33+
GI.convert(::Type{<:LineString}, ::GI.LineStringTrait, geom) =
34+
LineString(; coordinates=GI.coordinates(geom))
2535

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

3345
GI.ngeom(::GI.MultiPointTrait, g::MultiPoint) = length(g)
3446
GI.getgeom(::GI.MultiPointTrait, g::MultiPoint{D,T}, i::Int) where {D,T} = Point{D,T}(nothing, g[i])
47+
GI.convert(::Type{<:MultiPoint}, ::GI.MultiPointTrait, geom) =
48+
MultiPoint(; coordinates=GI.coordinates(geom))
3549

3650
GI.ngeom(::GI.MultiLineStringTrait, g::MultiLineString) = length(g)
3751
GI.getgeom(::GI.MultiLineStringTrait, g::MultiLineString{D,T}, i::Int) where {D,T} =
3852
LineString{D,T}(nothing, g[i])
53+
GI.convert(::Type{<:MultiLineString}, ::GI.MultiLineStringTrait, geom) =
54+
MultiLineString(; coordinates=GI.coordinates(geom))
3955

4056
GI.ngeom(::GI.MultiPolygonTrait, g::MultiPolygon) = length(g)
4157
GI.getgeom(::GI.MultiPolygonTrait, g::MultiPolygon{D,T}, i::Int) where {D,T} = Polygon{D,T}(nothing, g[i])
58+
GI.convert(::Type{<:MultiPolygon}, ::GI.MultiPolygonTrait, geom) =
59+
MultiLineString(; coordinates=GI.coordinates(geom))
4260

4361
GI.ngeom(::GI.GeometryCollectionTrait, g::GeometryCollection) = length(g)
4462
GI.getgeom(::GI.GeometryCollectionTrait, g::GeometryCollection, i::Int) = g[i]
4563
GI.coordinates(::GI.GeometryCollectionTrait, g::GeometryCollection) = coordinates.(geometry(g))
64+
function GI.convert(::Type{<:GeometryCollection}, ::GI.GeometryCollectionTrait, geom)
65+
geometries = [GI.convert(Val{:GeoJSON}(), g) for g in GI.getgeoms(geom)]
66+
GeometryCollection(; geometries)
67+
end
4668

4769
# Feature
4870
GI.isfeature(::Type{<:Feature}) = true
4971
GI.trait(::Feature) = GI.FeatureTrait()
5072
GI.geometry(f::Feature) = geometry(f)
5173
GI.properties(f::Feature) = properties(f)
74+
function GI.convert(::Type{<:Feature}, ::GI.FeatureTrait, feature)
75+
Feature(;
76+
geometry=GI.convert(GeoJSON, GI.geometry(feature)),
77+
properties=GI.properties(feature),
78+
)
79+
end
5280

5381
# FeatureCollection
5482
GI.isfeaturecollection(::Type{<:AbstractFeatureCollection}) = true
5583
GI.trait(::AbstractFeatureCollection) = GI.FeatureCollectionTrait()
5684
GI.getfeature(::GI.FeatureCollectionTrait, fc::AbstractFeatureCollection, i::Integer) = fc[i]
5785
GI.nfeature(::GI.FeatureCollectionTrait, fc::AbstractFeatureCollection) = length(fc)
86+
GI.convert(::Type{<:FeatureCollection}, ::GI.FeatureCollectionTrait, fc) =
87+
FeatureCollection(GI.convert(Feature, GI.features(fc)))
5888

5989
# Any GeoJSON Object
6090
GI.extent(::GI.FeatureTrait, x::GeoJSONT{2}) = _extent2(x)

0 commit comments

Comments
 (0)