PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ centroid()

Datum centroid ( PG_FUNCTION_ARGS  )

Definition at line 1348 of file postgis/lwgeom_geos.c.

References CIRCSTRINGTYPE, COMPOUNDTYPE, dumpnode::geom, geometry_serialize(), GEOS2POSTGIS(), gserialized_get_srid(), gserialized_get_type(), gserialized_has_m(), gserialized_has_z(), gserialized_is_empty(), HANDLE_GEOS_ERROR, lwgeom_free(), lwgeom_from_gserialized(), lwgeom_geos_error(), lwgeom_stroke(), lwpoint_as_lwgeom(), lwpoint_construct_empty(), lwpoint_free(), PG_FUNCTION_INFO_V1(), POSTGIS2GEOS(), ST_ClipByBox2d(), and ovdump::type.

Referenced by lwgeom_centroid(), lwgeom_cluster_2d_kmeans(), and pointonsurface().

1349 {
1350  GSERIALIZED *geom, *result;
1351  GEOSGeometry *geosgeom, *geosresult;
1352  LWGEOM *igeom = NULL, *linear_geom = NULL;
1353  int32 perQuad= 16;
1354  int type = 0;
1355  geom = PG_GETARG_GSERIALIZED_P(0);
1356 
1357  /* Empty.Centroid() == Point Empty */
1358  if ( gserialized_is_empty(geom) )
1359  {
1361  gserialized_get_srid(geom),
1362  gserialized_has_z(geom),
1363  gserialized_has_m(geom));
1364  result = geometry_serialize(lwpoint_as_lwgeom(lwp));
1365  lwpoint_free(lwp);
1366  PG_RETURN_POINTER(result);
1367  }
1368 
1369  type = gserialized_get_type(geom) ;
1370  /* Converting curve geometry to linestring if necessary*/
1371  if(type == CIRCSTRINGTYPE || type == COMPOUNDTYPE )
1372  {/* curve geometry?*/
1373  igeom = lwgeom_from_gserialized(geom);
1374  PG_FREE_IF_COPY(geom, 0); /*free memory, we already have a lwgeom geometry copy*/
1375  linear_geom = lwgeom_stroke(igeom, perQuad);
1376  lwgeom_free(igeom);
1377  if (linear_geom == NULL)
1378  PG_RETURN_NULL();
1379 
1380  geom = geometry_serialize(linear_geom);
1381  lwgeom_free(linear_geom);
1382  }
1383 
1384  initGEOS(lwpgnotice, lwgeom_geos_error);
1385 
1386  geosgeom = (GEOSGeometry *)POSTGIS2GEOS(geom);
1387 
1388  if ( 0 == geosgeom ) /* exception thrown at construction */
1389  {
1390  HANDLE_GEOS_ERROR("First argument geometry could not be converted to GEOS");
1391  PG_RETURN_NULL();
1392  }
1393 
1394  geosresult = GEOSGetCentroid(geosgeom);
1395 
1396  if ( geosresult == NULL )
1397  {
1398  GEOSGeom_destroy(geosgeom);
1399  HANDLE_GEOS_ERROR("GEOSGetCentroid");
1400  PG_RETURN_NULL();
1401  }
1402 
1403  GEOSSetSRID(geosresult, gserialized_get_srid(geom));
1404 
1405  result = GEOS2POSTGIS(geosresult, gserialized_has_z(geom));
1406 
1407  if (result == NULL)
1408  {
1409  GEOSGeom_destroy(geosgeom);
1410  GEOSGeom_destroy(geosresult);
1411  elog(ERROR,"Error in GEOS-PGIS conversion");
1412  PG_RETURN_NULL();
1413  }
1414  GEOSGeom_destroy(geosgeom);
1415  GEOSGeom_destroy(geosresult);
1416 
1417  PG_FREE_IF_COPY(geom, 0);
1418 
1419  PG_RETURN_POINTER(result);
1420 }
uint32_t gserialized_get_type(const GSERIALIZED *s)
Extract the geometry type from the serialized form (it hides in the anonymous data area...
Definition: g_serialized.c:86
LWGEOM * lwgeom_stroke(const LWGEOM *geom, uint32_t perQuad)
Definition: lwstroke.c:767
unsigned int int32
Definition: shpopen.c:273
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
int gserialized_has_m(const GSERIALIZED *gser)
Check if a GSERIALIZED has an M ordinate.
Definition: g_serialized.c:50
void lwpoint_free(LWPOINT *pt)
Definition: lwpoint.c:213
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1099
#define COMPOUNDTYPE
Definition: liblwgeom.h:93
LWPOINT * lwpoint_construct_empty(int srid, char hasz, char hasm)
Definition: lwpoint.c:151
int gserialized_has_z(const GSERIALIZED *gser)
Check if a GSERIALIZED has a Z ordinate.
Definition: g_serialized.c:45
int gserialized_is_empty(const GSERIALIZED *g)
Check if a GSERIALIZED is empty without deserializing first.
Definition: g_serialized.c:179
LWGEOM * geom
void lwgeom_geos_error(const char *fmt,...)
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
GEOSGeometry * POSTGIS2GEOS(GSERIALIZED *pglwgeom)
type
Definition: ovdump.py:41
GSERIALIZED * GEOS2POSTGIS(GEOSGeom geom, char want3d)
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
Definition: lwgeom.c:303
#define CIRCSTRINGTYPE
Definition: liblwgeom.h:92
#define HANDLE_GEOS_ERROR(label)
int32_t gserialized_get_srid(const GSERIALIZED *s)
Extract the SRID from the serialized form (it is packed into three bytes so this is a handy function)...
Definition: g_serialized.c:100
Here is the call graph for this function:
Here is the caller graph for this function: