PostGIS  2.3.8dev-r@@SVN_REVISION@@

◆ centroid()

Datum centroid ( PG_FUNCTION_ARGS  )

Definition at line 1273 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().

1274 {
1275  GSERIALIZED *geom, *result;
1276  GEOSGeometry *geosgeom, *geosresult;
1277  LWGEOM *igeom = NULL, *linear_geom = NULL;
1278  int32 perQuad= 16;
1279  int type = 0;
1280  geom = PG_GETARG_GSERIALIZED_P(0);
1281 
1282  /* Empty.Centroid() == Point Empty */
1283  if ( gserialized_is_empty(geom) )
1284  {
1286  gserialized_get_srid(geom),
1287  gserialized_has_z(geom),
1288  gserialized_has_m(geom));
1289  result = geometry_serialize(lwpoint_as_lwgeom(lwp));
1290  lwpoint_free(lwp);
1291  PG_RETURN_POINTER(result);
1292  }
1293 
1294  type = gserialized_get_type(geom) ;
1295  /* Converting curve geometry to linestring if necessary*/
1296  if(type == CIRCSTRINGTYPE || type == COMPOUNDTYPE )
1297  {/* curve geometry?*/
1298  igeom = lwgeom_from_gserialized(geom);
1299  PG_FREE_IF_COPY(geom, 0); /*free memory, we already have a lwgeom geometry copy*/
1300  linear_geom = lwgeom_stroke(igeom, perQuad);
1301  lwgeom_free(igeom);
1302  if (linear_geom == NULL)
1303  PG_RETURN_NULL();
1304 
1305  geom = geometry_serialize(linear_geom);
1306  lwgeom_free(linear_geom);
1307  }
1308 
1309  initGEOS(lwpgnotice, lwgeom_geos_error);
1310 
1311  geosgeom = (GEOSGeometry *)POSTGIS2GEOS(geom);
1312 
1313  if ( 0 == geosgeom ) /* exception thrown at construction */
1314  {
1315  HANDLE_GEOS_ERROR("First argument geometry could not be converted to GEOS");
1316  PG_RETURN_NULL();
1317  }
1318 
1319  geosresult = GEOSGetCentroid(geosgeom);
1320 
1321  if ( geosresult == NULL )
1322  {
1323  GEOSGeom_destroy(geosgeom);
1324  HANDLE_GEOS_ERROR("GEOSGetCentroid");
1325  PG_RETURN_NULL();
1326  }
1327 
1328  GEOSSetSRID(geosresult, gserialized_get_srid(geom));
1329 
1330  result = GEOS2POSTGIS(geosresult, gserialized_has_z(geom));
1331 
1332  if (result == NULL)
1333  {
1334  GEOSGeom_destroy(geosgeom);
1335  GEOSGeom_destroy(geosresult);
1336  elog(ERROR,"Error in GEOS-PGIS conversion");
1337  PG_RETURN_NULL();
1338  }
1339  GEOSGeom_destroy(geosgeom);
1340  GEOSGeom_destroy(geosresult);
1341 
1342  PG_FREE_IF_COPY(geom, 0);
1343 
1344  PG_RETURN_POINTER(result);
1345 }
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:69
LWGEOM * lwgeom_stroke(const LWGEOM *geom, uint32_t perQuad)
Definition: lwstroke.c:462
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:43
void lwpoint_free(LWPOINT *pt)
Definition: lwpoint.c:195
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1063
#define COMPOUNDTYPE
Definition: liblwgeom.h:92
LWPOINT * lwpoint_construct_empty(int srid, char hasz, char hasm)
Definition: lwpoint.c:133
int gserialized_has_z(const GSERIALIZED *gser)
Check if a GSERIALIZED has a Z ordinate.
Definition: g_serialized.c:38
int gserialized_is_empty(const GSERIALIZED *g)
Check if a GSERIALIZED is empty without deserializing first.
Definition: g_serialized.c:153
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:267
#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:83
Here is the call graph for this function:
Here is the caller graph for this function: