Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic support for curved geometries #215

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/LibGEOS.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@ using CEnum

const GI = GeoInterface

export GeometryCollection,
export CircularString,
CompoundCurve,
CurvePolygon,
GeometryCollection,
LineString,
LinearRing,
MultiCurve,
MultiLineString,
MultiPoint,
MultiPolygon,
MultiSurface,
Point,
Polygon,
STRtree
Expand Down
26 changes: 24 additions & 2 deletions src/geo_interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
geointerface_geomtype(::PolygonTrait) = Polygon
geointerface_geomtype(::MultiPolygonTrait) = MultiPolygon
geointerface_geomtype(::GeometryCollectionTrait) = GeometryCollection
geointerface_geomtype(::CircularStringTrait) = CircularString
geointerface_geomtype(::CompoundCurveTrait) = CompoundCurve
geointerface_geomtype(::CurvePolygonTrait) = CurvePolygon
geointerface_geomtype(::MultiSurfaceTrait) = MultiSurface
geointerface_geomtype(::MultiCurveTrait) = MultiCurve

Check warning on line 15 in src/geo_interface.jl

View check run for this annotation

Codecov / codecov/patch

src/geo_interface.jl#L11-L15

Added lines #L11 - L15 were not covered by tests

GeoInterface.geomtrait(::Point) = PointTrait()
GeoInterface.geomtrait(::MultiPoint) = MultiPointTrait()
Expand All @@ -17,23 +22,33 @@
GeoInterface.geomtrait(::Polygon) = PolygonTrait()
GeoInterface.geomtrait(::MultiPolygon) = MultiPolygonTrait()
GeoInterface.geomtrait(::GeometryCollection) = GeometryCollectionTrait()
GeoInterface.geomtrait(::CircularString) = CircularStringTrait()
GeoInterface.geomtrait(::CompoundCurve) = CompoundCurveTrait()
GeoInterface.geomtrait(::CurvePolygon) = CurvePolygonTrait()
GeoInterface.geomtrait(::MultiSurface) = MultiSurfaceTrait()
GeoInterface.geomtrait(::MultiCurve) = MultiCurveTrait()

Check warning on line 29 in src/geo_interface.jl

View check run for this annotation

Codecov / codecov/patch

src/geo_interface.jl#L28-L29

Added lines #L28 - L29 were not covered by tests
GeoInterface.geomtrait(geom::PreparedGeometry) = GeoInterface.geomtrait(geom.ownedby)

GeoInterface.isempty(::AbstractGeometryTrait, geom::AbstractGeometry) = isEmpty(geom)

GeoInterface.ngeom(::AbstractGeometryCollectionTrait, geom::AbstractMultiGeometry) =
isEmpty(geom) ? 0 : Int(numGeometries(geom))
GeoInterface.ngeom(::LineStringTrait, geom::LineString) = Int(numPoints(geom))
GeoInterface.ngeom(::CircularStringTrait, geom::CircularString) = Int(numPoints(geom))
GeoInterface.ngeom(::LinearRingTrait, geom::LinearRing) = Int(numPoints(geom))
GeoInterface.ngeom(::PolygonTrait, geom::Polygon) = Int(numInteriorRings(geom)) + 1
GeoInterface.ngeom(::CurvePolygonTrait, geom::CurvePolygon) =
Int(numInteriorRings(geom)) + 1
GeoInterface.ngeom(t::AbstractGeometryTrait, geom::PreparedGeometry) =
GeoInterface.ngeom(t, geom.ownedby)
GeoInterface.ngeom(::AbstractPointTrait, geom::Point) = 0
GeoInterface.ngeom(::AbstractPointTrait, geom::PreparedGeometry) = 0

GI.is3d(::AbstractGeometryTrait, geom::AbstractGeometry) = hasZ(geom)
GI.getexterior(::AbstractPolygonTrait, geom::Polygon) = exteriorRing(geom)
GI.gethole(::AbstractPolygonTrait, geom::Polygon, n) = interiorRing(geom, n)
GI.getexterior(::AbstractPolygonTrait, geom::Union{Polygon,CurvePolygon}) =
exteriorRing(geom)
GI.gethole(::AbstractPolygonTrait, geom::Union{Polygon,CurvePolygon}, n) =

Check warning on line 50 in src/geo_interface.jl

View check run for this annotation

Codecov / codecov/patch

src/geo_interface.jl#L50

Added line #L50 was not covered by tests
interiorRing(geom, n)

function GeoInterface.getgeom(
::AbstractGeometryCollectionTrait,
Expand All @@ -60,6 +75,13 @@
interiorRing(geom, i - 1)
end
end
function GeoInterface.getgeom(::CurvePolygonTrait, geom::CurvePolygon, i::Int)
if i == 1
exteriorRing(geom)
else
interiorRing(geom, i - 1)

Check warning on line 82 in src/geo_interface.jl

View check run for this annotation

Codecov / codecov/patch

src/geo_interface.jl#L82

Added line #L82 was not covered by tests
end
end
GeoInterface.getgeom(t::AbstractGeometryTrait, geom::PreparedGeometry, i) =
GeoInterface.getgeom(t, geom.ownedby, i)
GeoInterface.getgeom(t::AbstractPointTrait, geom::PreparedGeometry, i) = 0
Expand Down
84 changes: 81 additions & 3 deletions src/geos_functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,38 @@
result
end

function createCircularString(ptr::GEOSCoordSeq, context::GEOSContext = get_global_context())
result = GEOSGeom_createCircularString_r(context, ptr)
if result == C_NULL
error("LibGEOS: Error in GEOSGeom_createCircularString")

Check warning on line 561 in src/geos_functions.jl

View check run for this annotation

Codecov / codecov/patch

src/geos_functions.jl#L558-L561

Added lines #L558 - L561 were not covered by tests
end
result

Check warning on line 563 in src/geos_functions.jl

View check run for this annotation

Codecov / codecov/patch

src/geos_functions.jl#L563

Added line #L563 was not covered by tests
end
createCircularString(
coords::Vector{Vector{Float64}},
context::GEOSContext = get_global_context(),
) = GEOSGeom_createCircularString_r(context, createCoordSeq(coords, context))

# Second argument is an array of GEOSGeometry* objects.
# The caller remains owner of the array, but pointed-to
# objects become ownership of the returned GEOSGeometry.
function createCurvePolygon(
shell::Union{LinearRing,CircularString,CompoundCurve,GEOSGeom},
holes::AbstractVector,
context::GEOSContext = get_global_context(),
)
result = GEOSGeom_createCurvePolygon_r(context, shell, holes, length(holes))
if result == C_NULL
error("LibGEOS: Error in GEOSGeom_createCurvePolygon")

Check warning on line 580 in src/geos_functions.jl

View check run for this annotation

Codecov / codecov/patch

src/geos_functions.jl#L580

Added line #L580 was not covered by tests
end
result
end
# convenience function to create polygon without holes
createCurvePolygon(
shell::Union{LinearRing,CircularString,CompoundCurve,GEOSGeom},
context::GEOSContext = get_global_context(),
) = createCurvePolygon(shell, GEOSGeom[], context)

function createEmptyCollection(
geomtype::GEOSGeomTypes,
context::GEOSContext = get_global_context(),
Expand Down Expand Up @@ -1336,7 +1368,10 @@
end

# Return -1 on exception
function numInteriorRings(obj::Polygon, context::GEOSContext = get_context(obj))
function numInteriorRings(
obj::Union{Polygon,CurvePolygon},
context::GEOSContext = get_context(obj),
)
result = GEOSGetNumInteriorRings_r(context, obj)
if result == -1
error("LibGEOS: Error in GEOSGetNumInteriorRings")
Expand All @@ -1346,7 +1381,7 @@

# Call only on LINESTRING (returns -1 on exception)
function numPoints(
obj::Union{LineString,LinearRing},
obj::Union{LineString,LinearRing,CircularString},
context::GEOSContext = get_context(obj),
)
result = GEOSGeomGetNumPoints_r(context, obj)
Expand Down Expand Up @@ -1408,16 +1443,59 @@
end
end

# Polygon rings are of type LinearRing, and can be inferred, but CurvePolygon rings can be Union{LinearRing,CircularString,CompoundCurve}
function interiorRing(
obj::CurvePolygon,
n::Integer,
context::GEOSContext = get_context(obj),
)
if !(0 < n <= numInteriorRings(obj, context))
error(

Check warning on line 1453 in src/geos_functions.jl

View check run for this annotation

Codecov / codecov/patch

src/geos_functions.jl#L1453

Added line #L1453 was not covered by tests
"LibGEOS: n=$n is out of bounds for CurvePolygon with $(numInteriorRings(obj, context)) interior ring(s)",
)
end
result = GEOSGetInteriorRingN_r(context, obj, n - 1)
if result == C_NULL
error("LibGEOS: Error in GEOSGetInteriorRingN")

Check warning on line 1459 in src/geos_functions.jl

View check run for this annotation

Codecov / codecov/patch

src/geos_functions.jl#L1459

Added line #L1459 was not covered by tests
end
geomFromGEOS(cloneGeom(result, context), context)
end

function interiorRings(obj::CurvePolygon, context::GEOSContext = get_context(obj))
n = numInteriorRings(obj, context)
if n == 0
return LinearRing[]

Check warning on line 1467 in src/geos_functions.jl

View check run for this annotation

Codecov / codecov/patch

src/geos_functions.jl#L1467

Added line #L1467 was not covered by tests
else
return [interiorRing(obj, i, context) for i = 1:n]
end
end


# Return NULL on exception, Geometry must be a Polygon.
# Returned object is a pointer to internal storage: it must NOT be destroyed directly.
function exteriorRing(obj::Polygon, context::GEOSContext = get_context(obj))
function exteriorRing(
obj::Polygon,
context::GEOSContext = get_context(obj),
)::LinearRing
result = GEOSGetExteriorRing_r(context, obj)
if result == C_NULL
error("LibGEOS: Error in GEOSGetExteriorRing")
end
LinearRing(cloneGeom(result, context), context)
end

function exteriorRing(
obj::CurvePolygon,
context::GEOSContext = get_context(obj),
)::Union{LinearRing,CircularString,CompoundCurve}
result = GEOSGetExteriorRing_r(context, obj)
if result == C_NULL
error("LibGEOS: Error in GEOSGetExteriorRing")

Check warning on line 1493 in src/geos_functions.jl

View check run for this annotation

Codecov / codecov/patch

src/geos_functions.jl#L1493

Added line #L1493 was not covered by tests
end
geomFromGEOS(cloneGeom(result, context), context)
end


# Return -1 on exception
function numCoordinates(obj::Geometry, context::GEOSContext = get_context(obj))
result = GEOSGetNumCoordinates_r(context, obj)
Expand Down
Loading
Loading