PostGIS  2.1.10dev-r@@SVN_REVISION@@
Datum centroid ( PG_FUNCTION_ARGS  )

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

References geometry_serialize(), GEOS2POSTGIS(), gserialized_get_srid(), gserialized_has_m(), gserialized_has_z(), gserialized_is_empty(), lwerror(), lwgeom_geos_errmsg, lwgeom_geos_error(), lwnotice(), lwpoint_as_lwgeom(), lwpoint_construct_empty(), lwpoint_free(), POSTGIS2GEOS(), and result.

1584 {
1585  GSERIALIZED *geom, *result;
1586  GEOSGeometry *geosgeom, *geosresult;
1587 
1588  geom = (GSERIALIZED *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
1589 
1590  /* Empty.Centroid() == Point Empty */
1591  if ( gserialized_is_empty(geom) )
1592  {
1594  gserialized_get_srid(geom),
1595  gserialized_has_z(geom),
1596  gserialized_has_m(geom));
1597  result = geometry_serialize(lwpoint_as_lwgeom(lwp));
1598  lwpoint_free(lwp);
1599  PG_RETURN_POINTER(result);
1600  }
1601 
1602  initGEOS(lwnotice, lwgeom_geos_error);
1603 
1604  geosgeom = (GEOSGeometry *)POSTGIS2GEOS(geom);
1605 
1606  if ( 0 == geosgeom ) /* exception thrown at construction */
1607  {
1608  lwerror("First argument geometry could not be converted to GEOS: %s", lwgeom_geos_errmsg);
1609  PG_RETURN_NULL();
1610  }
1611 
1612  geosresult = GEOSGetCentroid(geosgeom);
1613 
1614  if ( geosresult == NULL )
1615  {
1616  GEOSGeom_destroy(geosgeom);
1617  lwerror("GEOSGetCentroid: %s", lwgeom_geos_errmsg);
1618  PG_RETURN_NULL();
1619  }
1620 
1621  GEOSSetSRID(geosresult, gserialized_get_srid(geom));
1622 
1623  result = GEOS2POSTGIS(geosresult, gserialized_has_z(geom));
1624 
1625  if (result == NULL)
1626  {
1627  GEOSGeom_destroy(geosgeom);
1628  GEOSGeom_destroy(geosresult);
1629  elog(ERROR,"Error in GEOS-PGIS conversion");
1630  PG_RETURN_NULL();
1631  }
1632  GEOSGeom_destroy(geosgeom);
1633  GEOSGeom_destroy(geosresult);
1634 
1635  PG_FREE_IF_COPY(geom, 0);
1636 
1637  PG_RETURN_POINTER(result);
1638 }
int gserialized_has_m(const GSERIALIZED *gser)
Check if a GSERIALIZED has an M ordinate.
Definition: g_serialized.c:30
void lwpoint_free(LWPOINT *pt)
Definition: lwpoint.c:180
char lwgeom_geos_errmsg[LWGEOM_GEOS_ERRMSG_MAXSIZE]
LWPOINT * lwpoint_construct_empty(int srid, char hasz, char hasm)
Definition: lwpoint.c:118
int gserialized_has_z(const GSERIALIZED *gser)
Check if a GSERIALIZED has a Z ordinate.
Definition: g_serialized.c:25
char ** result
Definition: liblwgeom.h:218
int gserialized_is_empty(const GSERIALIZED *g)
Check if a GSERIALIZED is empty without deserializing first.
Definition: g_serialized.c:140
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:67
void lwnotice(const char *fmt,...)
Write a notice out to the notice handler.
Definition: lwutil.c:54
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
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:70

Here is the call graph for this function: