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

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

939 {
940  GEOSGeometry *g, *g_centroid;
941  LWGEOM *centroid;
942  int srid, is3d;
943 
944  if (lwgeom_is_empty(geom))
945  {
947  lwgeom_get_srid(geom),
948  lwgeom_has_z(geom),
949  lwgeom_has_m(geom));
950  return lwpoint_as_lwgeom(lwp);
951  }
952 
953  srid = lwgeom_get_srid(geom);
954  is3d = lwgeom_has_z(geom);
955 
956  initGEOS(lwnotice, lwgeom_geos_error);
957 
958  g = LWGEOM2GEOS(geom, 0);
959 
960  if (0 == g) /* exception thrown at construction */
961  {
962  lwerror("Geometry could not be converted to GEOS: %s", lwgeom_geos_errmsg);
963  return NULL;
964  }
965 
966  g_centroid = GEOSGetCentroid(g);
967  GEOSGeom_destroy(g);
968 
969  if (g_centroid == NULL)
970  {
971  lwerror("GEOSGetCentroid: %s", lwgeom_geos_errmsg);
972  return NULL; /*never get here */
973  }
974 
975  LWDEBUGF(3, "result: %s", GEOSGeomToWKT(g_centroid));
976 
977  GEOSSetSRID(g_centroid, srid);
978 
979  centroid = GEOS2LWGEOM(g_centroid, is3d);
980  GEOSGeom_destroy(g_centroid);
981 
982  if (centroid == NULL)
983  {
984  lwerror("GEOS GEOSGetCentroid() threw an error (result postgis geometry formation)!");
985  return NULL ; /*never get here */
986  }
987 
988  return centroid;
989 }
void lwnotice(const char *fmt,...)
Write a notice out to the notice handler.
Definition: lwutil.c:177
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:909
LWPOINT * lwpoint_construct_empty(int srid, char hasz, char hasm)
Definition: lwpoint.c:151
int lwgeom_has_z(const LWGEOM *geom)
Return LW_TRUE if geometry has Z ordinates.
Definition: lwgeom.c:923
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:334
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:1386
#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:930
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:190

Here is the call graph for this function:

Here is the caller graph for this function: