PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ lwgeom_centroid()

LWGEOM* lwgeom_centroid ( const LWGEOM geom)

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

944 {
945  GEOSGeometry *g, *g_centroid;
946  LWGEOM *centroid;
947  int srid, is3d;
948 
949  if (lwgeom_is_empty(geom))
950  {
952  lwgeom_get_srid(geom),
953  lwgeom_has_z(geom),
954  lwgeom_has_m(geom));
955  return lwpoint_as_lwgeom(lwp);
956  }
957 
958  srid = lwgeom_get_srid(geom);
959  is3d = lwgeom_has_z(geom);
960 
961  initGEOS(lwnotice, lwgeom_geos_error);
962 
963  g = LWGEOM2GEOS(geom, 0);
964 
965  if (0 == g) /* exception thrown at construction */
966  {
967  lwerror("Geometry could not be converted to GEOS: %s", lwgeom_geos_errmsg);
968  return NULL;
969  }
970 
971  g_centroid = GEOSGetCentroid(g);
972  GEOSGeom_destroy(g);
973 
974  if (g_centroid == NULL)
975  {
976  lwerror("GEOSGetCentroid: %s", lwgeom_geos_errmsg);
977  return NULL; /*never get here */
978  }
979 
980  LWDEBUGF(3, "result: %s", GEOSGeomToWKT(g_centroid));
981 
982  GEOSSetSRID(g_centroid, srid);
983 
984  centroid = GEOS2LWGEOM(g_centroid, is3d);
985  GEOSGeom_destroy(g_centroid);
986 
987  if (centroid == NULL)
988  {
989  lwerror("GEOS GEOSGetCentroid() threw an error (result postgis geometry formation)!");
990  return NULL ; /*never get here */
991  }
992 
993  return centroid;
994 }
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:871
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:885
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:303
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:1346
#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:892
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: