PostGIS  2.2.8dev-r@@SVN_REVISION@@

◆ centroid()

Datum centroid ( PG_FUNCTION_ARGS  )

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

References dumpnode::geom, geometry_serialize(), GEOS2POSTGIS(), gserialized_get_srid(), gserialized_has_m(), gserialized_has_z(), gserialized_is_empty(), HANDLE_GEOS_ERROR, lwgeom_geos_error(), lwpoint_as_lwgeom(), lwpoint_construct_empty(), lwpoint_free(), PG_FUNCTION_INFO_V1(), POSTGIS2GEOS(), and ST_ClipByBox2d().

Referenced by pointonsurface().

1516 {
1517  GSERIALIZED *geom, *result;
1518  GEOSGeometry *geosgeom, *geosresult;
1519 
1520  geom = PG_GETARG_GSERIALIZED_P(0);
1521 
1522  /* Empty.Centroid() == Point Empty */
1523  if ( gserialized_is_empty(geom) )
1524  {
1526  gserialized_get_srid(geom),
1527  gserialized_has_z(geom),
1528  gserialized_has_m(geom));
1529  result = geometry_serialize(lwpoint_as_lwgeom(lwp));
1530  lwpoint_free(lwp);
1531  PG_RETURN_POINTER(result);
1532  }
1533 
1534  initGEOS(lwpgnotice, lwgeom_geos_error);
1535 
1536  geosgeom = (GEOSGeometry *)POSTGIS2GEOS(geom);
1537 
1538  if ( 0 == geosgeom ) /* exception thrown at construction */
1539  {
1540  HANDLE_GEOS_ERROR("First argument geometry could not be converted to GEOS");
1541  PG_RETURN_NULL();
1542  }
1543 
1544  geosresult = GEOSGetCentroid(geosgeom);
1545 
1546  if ( geosresult == NULL )
1547  {
1548  GEOSGeom_destroy(geosgeom);
1549  HANDLE_GEOS_ERROR("GEOSGetCentroid");
1550  PG_RETURN_NULL();
1551  }
1552 
1553  GEOSSetSRID(geosresult, gserialized_get_srid(geom));
1554 
1555  result = GEOS2POSTGIS(geosresult, gserialized_has_z(geom));
1556 
1557  if (result == NULL)
1558  {
1559  GEOSGeom_destroy(geosgeom);
1560  GEOSGeom_destroy(geosresult);
1561  elog(ERROR,"Error in GEOS-PGIS conversion");
1562  PG_RETURN_NULL();
1563  }
1564  GEOSGeom_destroy(geosgeom);
1565  GEOSGeom_destroy(geosresult);
1566 
1567  PG_FREE_IF_COPY(geom, 0);
1568 
1569  PG_RETURN_POINTER(result);
1570 }
int gserialized_has_m(const GSERIALIZED *gser)
Check if a GSERIALIZED has an M ordinate.
Definition: g_serialized.c:29
void lwpoint_free(LWPOINT *pt)
Definition: lwpoint.c:182
LWPOINT * lwpoint_construct_empty(int srid, char hasz, char hasm)
Definition: lwpoint.c:120
int gserialized_has_z(const GSERIALIZED *gser)
Check if a GSERIALIZED has a Z ordinate.
Definition: g_serialized.c:24
int gserialized_is_empty(const GSERIALIZED *g)
Check if a GSERIALIZED is empty without deserializing first.
Definition: g_serialized.c:139
LWGEOM * geom
void lwgeom_geos_error(const char *fmt,...)
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
GEOSGeometry * POSTGIS2GEOS(GSERIALIZED *pglwgeom)
GSERIALIZED * GEOS2POSTGIS(GEOSGeom geom, char want3d)
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
Definition: lwgeom.c:254
#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:69
Here is the call graph for this function:
Here is the caller graph for this function: