PostGIS  2.5.0dev-r@@SVN_REVISION@@

◆ centroid()

Datum centroid ( PG_FUNCTION_ARGS  )

Definition at line 1342 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_cluster_2d_kmeans(), and pointonsurface().

1343 {
1344  GSERIALIZED *geom, *result;
1345  GEOSGeometry *geosgeom, *geosresult;
1346  LWGEOM *igeom = NULL, *linear_geom = NULL;
1347  int32 perQuad= 16;
1348  int type = 0;
1349  geom = PG_GETARG_GSERIALIZED_P(0);
1350 
1351  /* Empty.Centroid() == Point Empty */
1352  if ( gserialized_is_empty(geom) )
1353  {
1355  gserialized_get_srid(geom),
1356  gserialized_has_z(geom),
1357  gserialized_has_m(geom));
1358  result = geometry_serialize(lwpoint_as_lwgeom(lwp));
1359  lwpoint_free(lwp);
1360  PG_RETURN_POINTER(result);
1361  }
1362 
1363  type = gserialized_get_type(geom) ;
1364  /* Converting curve geometry to linestring if necessary*/
1365  if(type == CIRCSTRINGTYPE || type == COMPOUNDTYPE )
1366  {/* curve geometry?*/
1367  igeom = lwgeom_from_gserialized(geom);
1368  PG_FREE_IF_COPY(geom, 0); /*free memory, we already have a lwgeom geometry copy*/
1369  linear_geom = lwgeom_stroke(igeom, perQuad);
1370  lwgeom_free(igeom);
1371  if (!linear_geom) PG_RETURN_NULL();
1372 
1373  geom = geometry_serialize(linear_geom);
1374  lwgeom_free(linear_geom);
1375  }
1376 
1377  initGEOS(lwpgnotice, lwgeom_geos_error);
1378 
1379  geosgeom = POSTGIS2GEOS(geom);
1380 
1381  if (!geosgeom)
1382  HANDLE_GEOS_ERROR("First argument geometry could not be converted to GEOS");
1383 
1384  geosresult = GEOSGetCentroid(geosgeom);
1385 
1386  if (!geosresult)
1387  {
1388  GEOSGeom_destroy(geosgeom);
1389  HANDLE_GEOS_ERROR("GEOSGetCentroid");
1390  }
1391 
1392  GEOSSetSRID(geosresult, gserialized_get_srid(geom));
1393 
1394  result = GEOS2POSTGIS(geosresult, gserialized_has_z(geom));
1395 
1396  if (!result)
1397  {
1398  GEOSGeom_destroy(geosgeom);
1399  GEOSGeom_destroy(geosresult);
1400  elog(ERROR,"Error in GEOS-PGIS conversion");
1401  PG_RETURN_NULL();
1402  }
1403  GEOSGeom_destroy(geosgeom);
1404  GEOSGeom_destroy(geosresult);
1405 
1406  PG_FREE_IF_COPY(geom, 0);
1407 
1408  PG_RETURN_POINTER(result);
1409 }
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:715
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:1144
#define COMPOUNDTYPE
Definition: liblwgeom.h:92
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:178
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:335
#define CIRCSTRINGTYPE
Definition: liblwgeom.h:91
#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:99
Here is the call graph for this function:
Here is the caller graph for this function: