Skip to content

Commit

Permalink
Add ConcaveHull from geos lib (#598)
Browse files Browse the repository at this point in the history
  • Loading branch information
valri11 committed Apr 5, 2024
1 parent c6d26e9 commit db33b8c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
7 changes: 7 additions & 0 deletions geos/entrypoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,10 @@ func CoverageUnion(g geom.Geometry) (geom.Geometry, error) {
func UnaryUnion(g geom.Geometry) (geom.Geometry, error) {
return rawgeos.UnaryUnion(g)
}

// ConcaveHull returns concave hull of input geometry.
// concavenessRatio - ratio 0 to 1 (0 - max concaveness, 1 - convex hull)
// allowHoles - true to allow holes inside of polygons.
func ConcaveHull(g geom.Geometry, concavenessRatio float64, allowHoles bool) (geom.Geometry, error) {
return rawgeos.ConcaveHull(g, concavenessRatio, allowHoles)
}
26 changes: 26 additions & 0 deletions internal/rawgeos/entrypoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ GEOSGeometry *GEOSMakeValid_r(GEOSContextHandle_t handle, const GEOSGeometry* g)
GEOSGeometry *GEOSCoverageUnion_r(GEOSContextHandle_t handle, const GEOSGeometry* g) { return NULL; }
#endif
#define CONCAVE_HULL_MIN_VERSION "3.11.0"
#define CONCAVE_HULL_MISSING ( \
GEOS_VERSION_MAJOR < 3 || \
(GEOS_VERSION_MAJOR == 3 && GEOS_VERSION_MINOR < 11) \
)
#if CONCAVE_HULL_MISSING
// This stub implementation always fails:
GEOSGeometry* GEOSConcaveHull_r(GEOSContextHandle_t handle, const GEOSGeometry *g, double ratio, unsigned int allowHoles) { return NULL; }
#endif
*/
import "C"

Expand Down Expand Up @@ -356,6 +366,22 @@ func ConvexHull(g geom.Geometry) (geom.Geometry, error) {
return result, wrap(err, "executing GEOSConvexHull_r")
}

func ConcaveHull(g geom.Geometry, pctconvex float64, allowHoles bool) (geom.Geometry, error) {
if C.CONCAVE_HULL_MISSING != 0 {
return geom.Geometry{}, unsupportedGEOSVersionError{
C.CONCAVE_HULL_MIN_VERSION, "ConcaveHull",
}
}
result, err := unaryOpG(g, func(ctx C.GEOSContextHandle_t, g *C.GEOSGeometry) *C.GEOSGeometry {
ah := C.uint(0)
if allowHoles {
ah = 1
}
return C.GEOSConcaveHull_r(ctx, g, C.double(pctconvex), ah)
})
return result, wrap(err, "executing GEOSConcaveHull_r")
}

func Centroid(g geom.Geometry) (geom.Geometry, error) {
result, err := unaryOpG(g, func(ctx C.GEOSContextHandle_t, g *C.GEOSGeometry) *C.GEOSGeometry {
return C.GEOSGetCentroid_r(ctx, g)
Expand Down

0 comments on commit db33b8c

Please sign in to comment.