PostGIS  2.3.7dev-r@@SVN_REVISION@@
LWGEOM* lwgeom_centroid ( const LWGEOM geom)

Definition at line 914 of file liblwgeom/lwgeom_geos.c.

References centroid(), GEOS2LWGEOM(), LWDEBUGF, lwerror(), LWGEOM2GEOS(), lwgeom_geos_errmsg, lwgeom_geos_error(), lwgeom_get_srid(), lwgeom_has_m(), lwgeom_has_z(), lwgeom_is_empty(), lwnotice(), lwpoint_as_lwgeom(), and lwpoint_construct_empty().

Referenced by lwgeom_cluster_2d_kmeans().

915 {
916  GEOSGeometry *g, *g_centroid;
917  LWGEOM *centroid;
918  int srid, is3d;
919 
920  if (lwgeom_is_empty(geom))
921  {
923  lwgeom_get_srid(geom),
924  lwgeom_has_z(geom),
925  lwgeom_has_m(geom));
926  return lwpoint_as_lwgeom(lwp);
927  }
928 
929  srid = lwgeom_get_srid(geom);
930  is3d = lwgeom_has_z(geom);
931 
932  initGEOS(lwnotice, lwgeom_geos_error);
933 
934  g = LWGEOM2GEOS(geom, 0);
935 
936  if (0 == g) /* exception thrown at construction */
937  {
938  lwerror("Geometry could not be converted to GEOS: %s", lwgeom_geos_errmsg);
939  return NULL;
940  }
941 
942  g_centroid = GEOSGetCentroid(g);
943  GEOSGeom_destroy(g);
944 
945  if (g_centroid == NULL)
946  {
947  lwerror("GEOSGetCentroid: %s", lwgeom_geos_errmsg);
948  return NULL; /*never get here */
949  }
950 
951  LWDEBUGF(3, "result: %s", GEOSGeomToWKT(g_centroid));
952 
953  GEOSSetSRID(g_centroid, srid);
954 
955  centroid = GEOS2LWGEOM(g_centroid, is3d);
956  GEOSGeom_destroy(g_centroid);
957 
958  if (centroid == NULL)
959  {
960  lwerror("GEOS GEOSGetCentroid() threw an error (result postgis geometry formation)!");
961  return NULL ; /*never get here */
962  }
963 
964  return centroid;
965 }
void lwnotice(const char *fmt,...)
Write a notice out to the notice handler.
Definition: lwutil.c:89
char lwgeom_geos_errmsg[LWGEOM_GEOS_ERRMSG_MAXSIZE]
Datum centroid(PG_FUNCTION_ARGS)
int32_t lwgeom_get_srid(const LWGEOM *geom)
Return SRID number.
Definition: lwgeom.c:835
LWPOINT * lwpoint_construct_empty(int srid, char hasz, char hasm)
Definition: lwpoint.c:133
int lwgeom_has_z(const LWGEOM *geom)
Return LW_TRUE if geometry has Z ordinates.
Definition: lwgeom.c:849
void lwgeom_geos_error(const char *fmt,...)
GEOSGeometry * LWGEOM2GEOS(const LWGEOM *lwgeom, int autofix)
LWGEOM * GEOS2LWGEOM(const GEOSGeometry *geom, char want3d)
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
Definition: lwgeom.c:267
int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members) ...
Definition: lwgeom.c:1310
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:88
int lwgeom_has_m(const LWGEOM *geom)
Return LW_TRUE if geometry has M ordinates.
Definition: lwgeom.c:856
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:102

Here is the call graph for this function:

Here is the caller graph for this function: