PostGIS  2.2.7dev-r@@SVN_REVISION@@
Datum convexhull ( PG_FUNCTION_ARGS  )

Definition at line 905 of file postgis/lwgeom_geos.c.

References LWGEOM::bbox, GBOX::flags, LWGEOM::flags, gbox_copy(), geometry_serialize(), GEOS2LWGEOM(), gserialized_get_gbox_p(), gserialized_get_srid(), gserialized_has_z(), gserialized_is_empty(), HANDLE_GEOS_ERROR, lwgeom_free(), lwgeom_geos_error(), and POSTGIS2GEOS().

906 {
907  GSERIALIZED *geom1;
908  GEOSGeometry *g1, *g3;
909  GSERIALIZED *result;
910  LWGEOM *lwout;
911  int srid;
912  GBOX bbox;
913 
914  geom1 = PG_GETARG_GSERIALIZED_P(0);
915 
916  /* Empty.ConvexHull() == Empty */
917  if ( gserialized_is_empty(geom1) )
918  PG_RETURN_POINTER(geom1);
919 
920  srid = gserialized_get_srid(geom1);
921 
922  initGEOS(lwpgnotice, lwgeom_geos_error);
923 
924  g1 = (GEOSGeometry *)POSTGIS2GEOS(geom1);
925 
926  if ( 0 == g1 ) /* exception thrown at construction */
927  {
928  HANDLE_GEOS_ERROR("First argument geometry could not be converted to GEOS");
929  PG_RETURN_NULL();
930  }
931 
932  g3 = (GEOSGeometry *)GEOSConvexHull(g1);
933  GEOSGeom_destroy(g1);
934 
935  if (g3 == NULL)
936  {
937  HANDLE_GEOS_ERROR("GEOSConvexHull");
938  PG_RETURN_NULL(); /* never get here */
939  }
940 
941  POSTGIS_DEBUGF(3, "result: %s", GEOSGeomToWKT(g3));
942 
943  GEOSSetSRID(g3, srid);
944 
945  lwout = GEOS2LWGEOM(g3, gserialized_has_z(geom1));
946  GEOSGeom_destroy(g3);
947 
948  if (lwout == NULL)
949  {
950  elog(ERROR,"convexhull() failed to convert GEOS geometry to LWGEOM");
951  PG_RETURN_NULL(); /* never get here */
952  }
953 
954  /* Copy input bbox if any */
955  if ( gserialized_get_gbox_p(geom1, &bbox) )
956  {
957  /* Force the box to have the same dimensionality as the lwgeom */
958  bbox.flags = lwout->flags;
959  lwout->bbox = gbox_copy(&bbox);
960  }
961 
962  result = geometry_serialize(lwout);
963  lwgeom_free(lwout);
964 
965  if (result == NULL)
966  {
967  elog(ERROR,"GEOS convexhull() threw an error (result postgis geometry formation)!");
968  PG_RETURN_NULL(); /* never get here */
969  }
970 
971  PG_FREE_IF_COPY(geom1, 0);
972  PG_RETURN_POINTER(result);
973 }
int gserialized_get_gbox_p(const GSERIALIZED *g, GBOX *box)
Read the bounding box off a serialization and calculate one if it is not already there.
Definition: g_serialized.c:371
GBOX * gbox_copy(const GBOX *box)
Return a copy of the GBOX, based on dimensionality of flags.
Definition: g_box.c:403
GBOX * bbox
Definition: liblwgeom.h:382
uint8_t flags
Definition: liblwgeom.h:381
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1050
int gserialized_has_z(const GSERIALIZED *gser)
Check if a GSERIALIZED has a Z ordinate.
Definition: g_serialized.c:24
int gserialized_is_empty(const GSERIALIZED *g)
Check if a GSERIALIZED is empty without deserializing first.
Definition: g_serialized.c:139
void lwgeom_geos_error(const char *fmt,...)
uint8_t flags
Definition: liblwgeom.h:275
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
GEOSGeometry * POSTGIS2GEOS(GSERIALIZED *pglwgeom)
LWGEOM * GEOS2LWGEOM(const GEOSGeometry *geom, char want3d)
#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:69

Here is the call graph for this function: