From 2f3c2195f89ceab5f6ab51f4d50fe978f8bbfa9d Mon Sep 17 00:00:00 2001 From: Paul Ramsey Date: Wed, 26 Nov 2008 19:04:15 +0000 Subject: [PATCH] ST_IsValidReason(geometry) returns text reason for validity failure. Requires GEOS >= 3.1. GBT#51 git-svn-id: http://svn.osgeo.org/postgis/trunk@3341 b70326c6-7e19-0410-871a-916f4a2858ee --- lwgeom/lwgeom_geos.c | 39 +++++++++++++++++++++++++++++++++++++++ lwgeom/lwpostgis.sql.in.c | 11 ++++++++++- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/lwgeom/lwgeom_geos.c b/lwgeom/lwgeom_geos.c index bc75710f9..60ff0d6e5 100644 --- a/lwgeom/lwgeom_geos.c +++ b/lwgeom/lwgeom_geos.c @@ -37,6 +37,7 @@ Datum containsproperly(PG_FUNCTION_ARGS); Datum covers(PG_FUNCTION_ARGS); Datum overlaps(PG_FUNCTION_ARGS); Datum isvalid(PG_FUNCTION_ARGS); +Datum isvalidreason(PG_FUNCTION_ARGS); Datum buffer(PG_FUNCTION_ARGS); Datum intersection(PG_FUNCTION_ARGS); Datum convexhull(PG_FUNCTION_ARGS); @@ -1080,6 +1081,44 @@ Datum isvalid(PG_FUNCTION_ARGS) } +PG_FUNCTION_INFO_V1(isvalidreason); +Datum isvalidreason(PG_FUNCTION_ARGS) +{ + PG_LWGEOM *geom = NULL; + char *reason_str = NULL; + int len = 0; + char *result = NULL; + GEOSGeom g1 = NULL; + + geom = (PG_LWGEOM *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0)); + + initGEOS(lwnotice, lwnotice); + + g1 = POSTGIS2GEOS(geom); + if ( ! g1 ) + { + PG_RETURN_NULL(); + } + + reason_str = GEOSisValidReason(g1); + GEOSGeom_destroy(g1); + + if (reason_str == NULL) + { + elog(ERROR,"GEOS isvalidreason() threw an error!"); + PG_RETURN_NULL(); /* never get here */ + } + len = strlen(reason_str); + result = palloc(VARHDRSZ + len); + SET_VARSIZE(result, VARHDRSZ + len); + memcpy(VARDATA(result), reason_str, len); + free(reason_str); + + PG_FREE_IF_COPY(geom, 0); + PG_RETURN_POINTER(result); + +} + /* * overlaps(PG_LWGEOM g1,PG_LWGEOM g2) * returns if GEOS::g1->overlaps(g2) returns true diff --git a/lwgeom/lwpostgis.sql.in.c b/lwgeom/lwpostgis.sql.in.c index 9f4d942f4..7cc0ac61e 100644 --- a/lwgeom/lwpostgis.sql.in.c +++ b/lwgeom/lwpostgis.sql.in.c @@ -2352,7 +2352,7 @@ LANGUAGE 'SQL' _IMMUTABLE; ----------------------------------------------------------------------- -- This function will: -- --- o try to fix the schema of records with an invalid one +-- o try to fix the schema of records with an integer one -- (for PG>=73) -- -- o link records to system tables through attrelid and varattnum @@ -3939,6 +3939,15 @@ CREATEFUNCTION ST_SimplifyPreserveTopology(geometry, float8) LANGUAGE 'C' _IMMUTABLE_STRICT; -- WITH (isstrict,iscachable); #endif +#if POSTGIS_GEOS_VERSION >= 31 +-- Requires GEOS >= 3.1.0 +-- Availability: 1.4.0 +CREATEFUNCTION ST_IsValidReason(geometry) + RETURNS text + AS 'MODULE_PATHNAME', 'isvalidreason' + LANGUAGE 'C' _IMMUTABLE_STRICT; -- WITH (isstrict,iscachable); +#endif + -- Deprecation in 1.2.3 CREATEFUNCTION difference(geometry,geometry) RETURNS geometry -- 2.49.0