From 17f6c5c0c87fe0d8dbcdd4476f22df60a7e000d6 Mon Sep 17 00:00:00 2001 From: Darafei Praliaskouski Date: Tue, 5 Jun 2018 14:40:45 +0000 Subject: [PATCH] Unify geometry centroid functions Make ST_Centroid call lwgeom_centroid. Closes #3960 Closes https://github.com/postgis/postgis/pull/256 git-svn-id: http://svn.osgeo.org/postgis/trunk@16608 b70326c6-7e19-0410-871a-916f4a2858ee --- NEWS | 8 ++--- postgis/lwgeom_geos.c | 68 ++++++------------------------------------ postgis/postgis.sql.in | 1 - 3 files changed, 12 insertions(+), 65 deletions(-) diff --git a/NEWS b/NEWS index 947a8b2be..eeb88139d 100644 --- a/NEWS +++ b/NEWS @@ -51,10 +51,8 @@ PostGIS 2.5.0 - #3097, Really allow MULTILINESTRING blades in ST_Split() (Paul Ramsey) - #3942, geojson: Do not include private header for json-c >= 0.13 (Björn Esser) - #3954, ST_GeometricMedian now supports point weights (Darafei Praliaskouski) - - #3965, ST_ClusterKMeans used to lose some clusters on initialization - (Darafei Praliaskouski) - - #3971, ST_ClusterKMeans now uses better initial seed (Darafei Praliaskouski) - - #3977, ST_ClusterKMeans is now faster and simpler (Darafei Praliaskouski) + - #3965, #3971, #3977, #4071 ST_ClusterKMeans rewritten: better initialization, + faster convergence, K=2 even faster (Darafei Praliaskouski) - #3982, ST_AsEncodedPolyline supports LINESTRING EMPTY and MULTIPOINT EMPTY (Darafei Praliaskouski) - #3986, ST_AsText now has second argument to limit decimal digits @@ -72,13 +70,13 @@ PostGIS 2.5.0 robustness issues. (Darafei Praliaskouski) - #4025, #4032 Fixed precision issue in ST_ClosestPointOfApproach, ST_DistanceCPA, and ST_CPAWithin (Paul Ramsey, Darafei Praliaskouski) - - #4071, ST_ClusterKMeans crash on NULL/EMPTY fixed (Darafei Praliaskouski) - #4076, Reduce use of GEOS in topology implementation (Björn Harrtell) - #4080, Add external raster band index to ST_BandMetaData - Add Raster Tips section to Documentation for information about Raster behavior (e.g. Out-DB performance, maximum open files) - #4084: Fixed wrong code-comment regarding front/back of BOX3D (Matthias Bay) - #4060, #4094, PostgreSQL JIT support (Raúl Marín, Laurenz Albe) + - #3960, ST_Centroid now uses lwgeom_centroid (Darafei Praliaskouski) PostGIS 2.4.4 2018/04/08 diff --git a/postgis/lwgeom_geos.c b/postgis/lwgeom_geos.c index 2e91b7b36..cf915affa 100644 --- a/postgis/lwgeom_geos.c +++ b/postgis/lwgeom_geos.c @@ -1306,69 +1306,19 @@ PG_FUNCTION_INFO_V1(centroid); Datum centroid(PG_FUNCTION_ARGS) { GSERIALIZED *geom, *result; - GEOSGeometry *geosgeom, *geosresult; - LWGEOM *igeom = NULL, *linear_geom = NULL; - int32 perQuad= 16; - int type = 0; - geom = PG_GETARG_GSERIALIZED_P(0); - - /* Empty.Centroid() == Point Empty */ - if ( gserialized_is_empty(geom) ) - { - LWPOINT *lwp = lwpoint_construct_empty( - gserialized_get_srid(geom), - gserialized_has_z(geom), - gserialized_has_m(geom)); - result = geometry_serialize(lwpoint_as_lwgeom(lwp)); - lwpoint_free(lwp); - PG_RETURN_POINTER(result); - } - - type = gserialized_get_type(geom) ; - /* Converting curve geometry to linestring if necessary*/ - if(type == CIRCSTRINGTYPE || type == COMPOUNDTYPE ) - {/* curve geometry?*/ - igeom = lwgeom_from_gserialized(geom); - PG_FREE_IF_COPY(geom, 0); /*free memory, we already have a lwgeom geometry copy*/ - linear_geom = lwgeom_stroke(igeom, perQuad); - lwgeom_free(igeom); - if (!linear_geom) PG_RETURN_NULL(); - - geom = geometry_serialize(linear_geom); - lwgeom_free(linear_geom); - } - - initGEOS(lwpgnotice, lwgeom_geos_error); - - geosgeom = POSTGIS2GEOS(geom); + LWGEOM *lwgeom, *lwresult; - if (!geosgeom) - HANDLE_GEOS_ERROR("First argument geometry could not be converted to GEOS"); - - geosresult = GEOSGetCentroid(geosgeom); - - if (!geosresult) - { - GEOSGeom_destroy(geosgeom); - HANDLE_GEOS_ERROR("GEOSGetCentroid"); - } - - GEOSSetSRID(geosresult, gserialized_get_srid(geom)); - - result = GEOS2POSTGIS(geosresult, gserialized_has_z(geom)); - - if (!result) - { - GEOSGeom_destroy(geosgeom); - GEOSGeom_destroy(geosresult); - elog(ERROR,"Error in GEOS-PGIS conversion"); - PG_RETURN_NULL(); - } - GEOSGeom_destroy(geosgeom); - GEOSGeom_destroy(geosresult); + geom = PG_GETARG_GSERIALIZED_P(0); + lwgeom = lwgeom_from_gserialized(geom); + lwresult = lwgeom_centroid(lwgeom); + lwgeom_free(lwgeom); PG_FREE_IF_COPY(geom, 0); + if (!lwresult) PG_RETURN_NULL(); + + result = geometry_serialize(lwresult); + lwgeom_free(lwresult); PG_RETURN_POINTER(result); } diff --git a/postgis/postgis.sql.in b/postgis/postgis.sql.in index b99cefde8..6e878bd02 100644 --- a/postgis/postgis.sql.in +++ b/postgis/postgis.sql.in @@ -3710,7 +3710,6 @@ CREATE OR REPLACE FUNCTION ST_Node(g geometry) -- -- -- Availability: 2.1.0 --- Requires GEOS >= 3.4.0 -- CREATE OR REPLACE FUNCTION ST_DelaunayTriangles(g1 geometry, tolerance float8 DEFAULT 0.0, flags int4 DEFAULT 0) RETURNS geometry -- 2.40.0