PostGIS  3.0.0dev-r@@SVN_REVISION@@

◆ lwgeom_centroid()

LWGEOM* lwgeom_centroid ( const LWGEOM geom)

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

References AUTOFIX, LWGEOM::flags, FLAGS_GET_Z, GEOS2LWGEOM(), GEOS_FAIL, GEOS_FREE, GEOS_FREE_AND_FAIL, LWGEOM2GEOS(), lwgeom_geos_error(), lwgeom_has_m(), lwgeom_is_empty(), lwnotice(), lwpoint_as_lwgeom(), lwpoint_construct_empty(), RESULT_SRID, and SRID_INVALID.

Referenced by centroid(), and lwgeom_cluster_2d_kmeans().

760 {
761  LWGEOM* result;
762  int32_t srid = RESULT_SRID(geom);
763  uint8_t is3d = FLAGS_GET_Z(geom->flags);
764  GEOSGeometry *g1, *g3;
765 
766  if (srid == SRID_INVALID) return NULL;
767 
768  if (lwgeom_is_empty(geom))
769  {
770  LWPOINT* lwp = lwpoint_construct_empty(srid, is3d, lwgeom_has_m(geom));
771  return lwpoint_as_lwgeom(lwp);
772  }
773 
774  initGEOS(lwnotice, lwgeom_geos_error);
775 
776  if (!(g1 = LWGEOM2GEOS(geom, AUTOFIX))) GEOS_FAIL();
777 
778  g3 = GEOSGetCentroid(g1);
779 
780  if (!g3) GEOS_FREE_AND_FAIL(g1);
781  GEOSSetSRID(g3, srid);
782 
783  if (!(result = GEOS2LWGEOM(g3, is3d)))
784  GEOS_FREE_AND_FAIL(g1);
785 
786  GEOS_FREE(g1, g3);
787 
788  return result;
789 }
#define GEOS_FAIL()
LWGEOM * GEOS2LWGEOM(const GEOSGeometry *geom, uint8_t want3d)
void lwnotice(const char *fmt,...)
Write a notice out to the notice handler.
Definition: lwutil.c:177
uint8_t flags
Definition: liblwgeom.h:399
#define GEOS_FREE_AND_FAIL(...)
#define GEOS_FREE(...)
LWPOINT * lwpoint_construct_empty(int srid, char hasz, char hasm)
Definition: lwpoint.c:151
#define RESULT_SRID(...)
static int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members) ...
Definition: lwinline.h:172
#define AUTOFIX
void lwgeom_geos_error(const char *fmt,...)
#define SRID_INVALID
Definition: liblwgeom.h:191
#define FLAGS_GET_Z(flags)
Macros for manipulating the 'flags' byte.
Definition: liblwgeom.h:139
GEOSGeometry * LWGEOM2GEOS(const LWGEOM *lwgeom, uint8_t autofix)
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
Definition: lwgeom.c:326
unsigned char uint8_t
Definition: uthash.h:79
int lwgeom_has_m(const LWGEOM *geom)
Return LW_TRUE if geometry has M ordinates.
Definition: lwgeom.c:921
Here is the call graph for this function:
Here is the caller graph for this function: