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

Definition at line 1301 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(), and POSTGIS2GEOS().

Referenced by lwgeom_centroid(), and lwgeom_cluster_2d_kmeans().

1302 {
1303  GSERIALIZED *geom, *result;
1304  GEOSGeometry *geosgeom, *geosresult;
1305  LWGEOM *igeom = NULL, *linear_geom = NULL;
1306  int32 perQuad= 16;
1307  int type = 0;
1308  geom = PG_GETARG_GSERIALIZED_P(0);
1309 
1310  /* Empty.Centroid() == Point Empty */
1311  if ( gserialized_is_empty(geom) )
1312  {
1314  gserialized_get_srid(geom),
1315  gserialized_has_z(geom),
1316  gserialized_has_m(geom));
1317  result = geometry_serialize(lwpoint_as_lwgeom(lwp));
1318  lwpoint_free(lwp);
1319  PG_RETURN_POINTER(result);
1320  }
1321 
1322  type = gserialized_get_type(geom) ;
1323  /* Converting curve geometry to linestring if necessary*/
1324  if(type == CIRCSTRINGTYPE || type == COMPOUNDTYPE )
1325  {/* curve geometry?*/
1326  igeom = lwgeom_from_gserialized(geom);
1327  PG_FREE_IF_COPY(geom, 0); /*free memory, we already have a lwgeom geometry copy*/
1328  linear_geom = lwgeom_stroke(igeom, perQuad);
1329  lwgeom_free(igeom);
1330  if (!linear_geom) PG_RETURN_NULL();
1331 
1332  geom = geometry_serialize(linear_geom);
1333  lwgeom_free(linear_geom);
1334  }
1335 
1336  initGEOS(lwpgnotice, lwgeom_geos_error);
1337 
1338  geosgeom = (GEOSGeometry *)POSTGIS2GEOS(geom);
1339 
1340  if (!geosgeom)
1341  HANDLE_GEOS_ERROR("First argument geometry could not be converted to GEOS");
1342 
1343  geosresult = GEOSGetCentroid(geosgeom);
1344 
1345  if (!geosresult)
1346  {
1347  GEOSGeom_destroy(geosgeom);
1348  HANDLE_GEOS_ERROR("GEOSGetCentroid");
1349  }
1350 
1351  GEOSSetSRID(geosresult, gserialized_get_srid(geom));
1352 
1353  result = GEOS2POSTGIS(geosresult, gserialized_has_z(geom));
1354 
1355  if (!result)
1356  {
1357  GEOSGeom_destroy(geosgeom);
1358  GEOSGeom_destroy(geosresult);
1359  elog(ERROR,"Error in GEOS-PGIS conversion");
1360  PG_RETURN_NULL();
1361  }
1362  GEOSGeom_destroy(geosgeom);
1363  GEOSGeom_destroy(geosresult);
1364 
1365  PG_FREE_IF_COPY(geom, 0);
1366 
1367  PG_RETURN_POINTER(result);
1368 }
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:714
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:1137
#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)
GSERIALIZED * GEOS2POSTGIS(GEOSGeom geom, char want3d)
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
Definition: lwgeom.c:334
#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: