|
| 1 | +# Implementation of trait based interface from https://github.com/JuliaGeo/GeoInterface.jl/ |
| 2 | + |
| 3 | +GeoInterface.isgeometry(::Type{<:AbstractGeometry}) = true |
| 4 | +GeoInterface.isgeometry(::Type{<:AbstractFace}) = true |
| 5 | +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 |
| 11 | +GeoInterface.isgeometry(::Type{<:Mesh}) = true |
| 12 | + |
| 13 | +GeoInterface.geomtrait(::Point) = PointTrait() |
| 14 | +GeoInterface.geomtrait(::Line) = LineTrait() |
| 15 | +GeoInterface.geomtrait(::LineString) = LineStringTrait() |
| 16 | +GeoInterface.geomtrait(::Polygon) = PolygonTrait() |
| 17 | +GeoInterface.geomtrait(::MultiPoint) = MultiPointTrait() |
| 18 | +GeoInterface.geomtrait(::MultiLineString) = MultiLineStringTrait() |
| 19 | +GeoInterface.geomtrait(::MultiPolygon) = MultiPolygonTrait() |
| 20 | +GeoInterface.geomtrait(::Ngon) = PolygonTrait() |
| 21 | +GeoInterface.geomtrait(::AbstractMesh) = PolyhedralSurfaceTrait() |
| 22 | + |
| 23 | +GeoInterface.geomtrait(::Simplex{Dim,T,1}) where {Dim,T} = PointTrait() |
| 24 | +GeoInterface.geomtrait(::Simplex{Dim,T,2}) where {Dim,T} = LineStringTrait() |
| 25 | +GeoInterface.geomtrait(::Simplex{Dim,T,3}) where {Dim,T} = PolygonTrait() |
| 26 | + |
| 27 | +GeoInterface.ncoord(::PointTrait, g::Point) = length(g) |
| 28 | +GeoInterface.getcoord(::PointTrait, g::Point, i::Int) = g[i] |
| 29 | + |
| 30 | +GeoInterface.ngeom(::LineTrait, g::Line) = length(g) |
| 31 | +GeoInterface.getgeom(::LineTrait, g::Line, i::Int) = g[i] |
| 32 | + |
| 33 | +GeoInterface.ngeom(::LineStringTrait, g::LineString) = length(g) + 1 # n line segments + 1 |
| 34 | +function GeoInterface.getgeom(::LineStringTrait, g::LineString, i::Int) |
| 35 | + return GeometryBasics.coordinates(g)[i] |
| 36 | +end |
| 37 | + |
| 38 | +GeoInterface.ngeom(::PolygonTrait, g::Polygon) = length(g.interiors) + 1 # +1 for exterior |
| 39 | +function GeoInterface.getgeom(::PolygonTrait, |
| 40 | + g::Polygon, |
| 41 | + i::Int)::typeof(g.exterior) |
| 42 | + return i > 1 ? g.interiors[i - 1] : g.exterior |
| 43 | +end |
| 44 | + |
| 45 | +GeoInterface.ngeom(::MultiPointTrait, g::MultiPoint) = length(g) |
| 46 | +GeoInterface.getgeom(::MultiPointTrait, g::MultiPoint, i::Int) = g[i] |
| 47 | + |
| 48 | +function GeoInterface.ngeom(::MultiLineStringTrait, g::MultiLineString) |
| 49 | + return length(g) |
| 50 | +end |
| 51 | +function GeoInterface.getgeom(::MultiLineStringTrait, g::MultiLineString, |
| 52 | + i::Int) |
| 53 | + return g[i] |
| 54 | +end |
| 55 | + |
| 56 | +GeoInterface.ngeom(::MultiPolygonTrait, g::MultiPolygon) = length(g) |
| 57 | +GeoInterface.getgeom(::MultiPolygonTrait, g::MultiPolygon, i::Int) = g[i] |
| 58 | + |
| 59 | +function GeoInterface.ncoord(::AbstractGeometryTrait, |
| 60 | + ::Simplex{Dim,T,N,P}) where {Dim,T,N,P} |
| 61 | + return Dim |
| 62 | +end |
| 63 | +function GeoInterface.ncoord(::AbstractGeometryTrait, |
| 64 | + ::AbstractGeometry{Dim,T}) where {Dim,T} |
| 65 | + return Dim |
| 66 | +end |
| 67 | +function GeoInterface.ngeom(::AbstractGeometryTrait, |
| 68 | + ::Simplex{Dim,T,N,P}) where {Dim,T,N,P} |
| 69 | + return N |
| 70 | +end |
| 71 | +GeoInterface.ngeom(::PolygonTrait, ::Ngon) = 1 # can't have any holes |
| 72 | +GeoInterface.getgeom(::PolygonTrait, g::Ngon, _) = LineString(g.points) |
| 73 | + |
| 74 | +function GeoInterface.ncoord(::PolyhedralSurfaceTrait, |
| 75 | + ::Mesh{Dim,T,E,V} where {Dim,T,E,V}) |
| 76 | + return Dim |
| 77 | +end |
| 78 | +GeoInterface.ngeom(::PolyhedralSurfaceTrait, g::AbstractMesh) = length(g) |
| 79 | +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 |
0 commit comments