Skip to content
This repository has been archived by the owner on May 16, 2022. It is now read-only.

Commit

Permalink
Added more types.
Browse files Browse the repository at this point in the history
  • Loading branch information
evetion committed Mar 23, 2020
1 parent 4d4bb62 commit 6785c80
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 22 deletions.
76 changes: 56 additions & 20 deletions src/GeoInterfaceRFC.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,83 @@ struct Geometry <: AbstractGeometry end

abstract type AbstractPoint <: AbstractGeometry end
struct Point <: AbstractPoint end
abstract type AbstractLineString <: AbstractGeometry end

abstract type AbstractCurve <: AbstractGeometry end
abstract type AbstractLineString <: AbstractCurve end
struct LineString <: AbstractLineString end
abstract type AbstractPolygon <: AbstractGeometry end
struct Line <: AbstractLineString end # LineString with just two points
struct LinearRing <: AbstractLineString end # Closed LineString

abstract type AbstractSurface <: AbstractGeometry end
abstract type AbstractPolygon <: AbstractSurface end
abstract type AbstractPolyHedralSurface <: AbstractSurface end
struct Polygon <: AbstractPolygon end
struct Triangle <: AbstractPolygon end # Polygon with just three points
struct Box <: AbstractPolygon end # Polygon with just 4 points
struct PolyHedralSurface <: AbstractPolyHedralSurface end
struct TIN <: AbstractPolyHedralSurface end

abstract type AbstractMultiPoint <: AbstractGeometry end
struct MultiPoint <: AbstractMultiPoint end
abstract type AbstractMultiLineString <: AbstractGeometry end

abstract type AbstractMultiCurve <: AbstractGeometry end
abstract type AbstractMultiLineString <: AbstractMultiCurve end
struct MultiLineString <: AbstractMultiLineString end
abstract type AbstractMultiPolygon <: AbstractGeometry end

abstract type AbstractMultiSurface <: AbstractGeometry end
abstract type AbstractMultiPolygon <: AbstractMultiSurface end
struct MultiPolygon <: AbstractMultiPolygon end

abstract type AbstractGeometryCollection <: AbstractGeometry end
struct GeometryCollection <: AbstractGeometryCollection end

# All Geometries
function geomtype(x::Any)
function geomtype(x::T) where T
throw(ErrorException(string("Unknown Geometry type. ",
"Define GeoInterface.geomtype(::$(typeof(x))) to return the desired type.")))
end
ncoord(geom::Any) = ncoord(geomtype(geom), geom)

# All types
ncoord(geom::T) where T = ncoord(geomtype(T), geom)
isempty(geom::T) where T = ncoord(geom) == 0 # TODO or ngeom?
issimple(geom::T) where T = issimple(geomtype(T), geom)

# Point
getcoord(geom::Any, i::Integer) = getcoord(geomtype(geom), geom, i)
getcoord(geom::T, i::Integer) where T = getcoord(geomtype(T), geom, i)

# LineString, MultiPoint
npoint(geom::Any) = npoint(geomtype(geom), geom)
getpoint(geom::Any, i::Integer) = getpoint(geomtype(geom), geom, i)
isclosed(geom::Any) = isclosed(geomtype(geom), geom)
# Polygon
getexterior(geom::Any) = getexterior(geomtype(geom), geom)
nhole(geom::Any) = nhole(geomtype(geom), geom)
gethole(geom::Any, i::Integer) = gethole(geomtype(geom), geom, i)
npoint(geom::T) where T = npoint(geomtype(T), geom)
getpoint(geom::T, i::Integer) where T = getpoint(geomtype(T), geom, i)
# LineString
isclosed(geom::T) where T = isclosed(geomtype(T), geom)
isring(geom::T) where T = isclosed(geom) && issimple(geom)

# Polygon/Triangle
getexterior(geom::T) where T = getexterior(geomtype(T), geom)
nhole(geom::T) where T = nhole(geomtype(T), geom) # TODO shouldn't this be interior? doesn't have to be a hole
gethole(geom::T, i::Integer) where T = gethole(geomtype(T), geom, i)

# PolyHedralSurface
npatch(geom::T) where T = npatch(geomtype(T), geom)
getpatch(geom::T, i::Integer) where T = getpatch(geomtype(T), geom, i)

# GeometryCollection
ngeom(geom::Any) = ngeom(geomtype(geom), geom)
getgeom(geom::Any, i::Integer) = getgeom(geomtype(geom), geom, i)
ngeom(geom::T) where T = ngeom(geomtype(T), geom)
getgeom(geom::T, i::Integer) where T = getgeom(geomtype(T), geom, i)

# MultiLineString
nlinestring(geom::Any) = nlinestring(geomtype(geom), geom)
getlinestring(geom::Any, i::Integer) = getlinestring(geomtype(geom), geom, i)
nlinestring(geom::T) where T = nlinestring(geomtype(T), geom)
getlinestring(geom::T, i::Integer) where T = getlinestring(geomtype(T), geom, i)

# MultiPolygon
npolygon(geom::Any) = npolygon(geomtype(geom), geom)
getpolygon(geom::Any, i::Integer) = getpolygon(geomtype(geom), geom, i)
npolygon(geom::T) where T = npolygon(geomtype(T), geom)
getpolygon(geom::T, i::Integer) where T = getpolygon(geomtype(T), geom, i)

# Other methods
crs(geom::T) where T = missing # or conforming to <:CoordinateReferenceSystemFormat in GeoFormatTypes


include("defaults.jl")
include("primitives.jl")

end # module
2 changes: 2 additions & 0 deletions src/defaults.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
npoint(::Line, _::Any) = 2
npoint(::Triangle, _::Any) = 3
11 changes: 9 additions & 2 deletions src/primitives.jl
Original file line number Diff line number Diff line change
@@ -1,39 +1,46 @@
const Indexable = Union{AbstractVector,Tuple}

# Point
geomtype(geom::AbstractVector{T}) where {T<:Real} = Point()
geomtype(geom::Tuple{T,U}) where {T,U<:Real} = Point()
geomtype(geom::Tuple{T,U,V}) where {T,U,V<:Real} = Point()
ncoord(::AbstractPoint, geom::Indexable) = length(geom)
getcoord(::AbstractPoint, geom::Indexable, i::Integer) = geom[i]

# LineString
ncoord(::AbstractLineString, geom::Indexable) =
ncoord(Point, getpoint(geom, 1))
npoint(::AbstractLineString, geom::Indexable) = length(geom)
getpoint(::AbstractLineString, geom::Indexable, i::Integer) = geom[i]

# Polygon
ncoord(::AbstractPolygon, geom::Indexable) =
ncoord(LineString, getexterior(geom))
getexterior(::AbstractPolygon, geom::Indexable) = geom[1]
nhole(::AbstractPolygon, geom::Indexable) = length(geom) - 1
gethole(::AbstractPolygon, geom::Indexable, i::Integer) = geom[i+1]

# MultiPoint
ncoord(::AbstractMultiPoint, geom::Indexable) =
ncoord(Point, getpoint(geom, 1))
npoint(::AbstractMultiPoint, geom::Indexable) = length(geom)
getpoint(::AbstractMultiPoint, geom::Indexable, i::Integer) = geom[i]

# MultiLineString
ncoord(::AbstractMultiLineString, geom::Indexable) =
ncoord(::AbstractMultiLineString, geom::Indexable) =
ncoord(LineString, getlinestring(geom, 1))
nlinestring(::AbstractMultiLineString, geom::Indexable) = length(geom)
getlinestring(::AbstractMultiLineString, geom::Indexable, i::Integer) =
geom[i]

# MultiPolygon
ncoord(::AbstractMultiPolygon, geom::Indexable) =
ncoord(Polygon, getpolygon(geom, 1))
npolygon(::AbstractMultiPolygon, geom::Indexable) = length(geom)
getpolygon(::AbstractMultiPolygon, geom::Indexable, i::Integer) = geom[i]

# GeometryCollection
ncoord(::AbstractGeometryCollection, collection::Indexable) =
ncoord(::AbstractGeometryCollection, collection::Indexable) =
ncoord(geomtype(geom), getgeom(collection, 1))
ngeom(::AbstractGeometryCollection, collection::Indexable) =
length(collection)
Expand Down

0 comments on commit 6785c80

Please sign in to comment.