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

Definition at line 975 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(), lwerror(), lwgeom_free(), lwgeom_geos_errmsg, lwgeom_geos_error(), lwnotice(), POSTGIS2GEOS(), and result.

976 {
977  GSERIALIZED *geom1;
978  GEOSGeometry *g1, *g3;
980  LWGEOM *lwout;
981  int srid;
982  GBOX bbox;
983 
984  geom1 = (GSERIALIZED *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
985 
986  /* Empty.ConvexHull() == Empty */
987  if ( gserialized_is_empty(geom1) )
988  PG_RETURN_POINTER(geom1);
989 
990  srid = gserialized_get_srid(geom1);
991 
992  initGEOS(lwnotice, lwgeom_geos_error);
993 
994  g1 = (GEOSGeometry *)POSTGIS2GEOS(geom1);
995 
996  if ( 0 == g1 ) /* exception thrown at construction */
997  {
998  lwerror("First argument geometry could not be converted to GEOS: %s", lwgeom_geos_errmsg);
999  PG_RETURN_NULL();
1000  }
1001 
1002  g3 = (GEOSGeometry *)GEOSConvexHull(g1);
1003  GEOSGeom_destroy(g1);
1004 
1005  if (g3 == NULL)
1006  {
1007  lwerror("GEOSConvexHull: %s", lwgeom_geos_errmsg);
1008  PG_RETURN_NULL(); /* never get here */
1009  }
1010 
1011  POSTGIS_DEBUGF(3, "result: %s", GEOSGeomToWKT(g3));
1012 
1013  GEOSSetSRID(g3, srid);
1014 
1015  lwout = GEOS2LWGEOM(g3, gserialized_has_z(geom1));
1016  GEOSGeom_destroy(g3);
1017 
1018  if (lwout == NULL)
1019  {
1020  elog(ERROR,"convexhull() failed to convert GEOS geometry to LWGEOM");
1021  PG_RETURN_NULL(); /* never get here */
1022  }
1023 
1024  /* Copy input bbox if any */
1025  if ( gserialized_get_gbox_p(geom1, &bbox) )
1026  {
1027  /* Force the box to have the same dimensionality as the lwgeom */
1028  bbox.flags = lwout->flags;
1029  lwout->bbox = gbox_copy(&bbox);
1030  }
1031 
1032  result = geometry_serialize(lwout);
1033  lwgeom_free(lwout);
1034 
1035  if (result == NULL)
1036  {
1037  elog(ERROR,"GEOS convexhull() threw an error (result postgis geometry formation)!");
1038  PG_RETURN_NULL(); /* never get here */
1039  }
1040 
1041  PG_FREE_IF_COPY(geom1, 0);
1042  PG_RETURN_POINTER(result);
1043 }
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:373
GBOX * gbox_copy(const GBOX *box)
Return a copy of the GBOX, based on dimensionality of flags.
Definition: g_box.c:362
GBOX * bbox
Definition: liblwgeom.h:354
uint8_t flags
Definition: liblwgeom.h:353
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1006
char lwgeom_geos_errmsg[LWGEOM_GEOS_ERRMSG_MAXSIZE]
int gserialized_has_z(const GSERIALIZED *gser)
Check if a GSERIALIZED has a Z ordinate.
Definition: g_serialized.c:25
char ** result
Definition: liblwgeom.h:218
int gserialized_is_empty(const GSERIALIZED *g)
Check if a GSERIALIZED is empty without deserializing first.
Definition: g_serialized.c:140
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:67
void lwnotice(const char *fmt,...)
Write a notice out to the notice handler.
Definition: lwutil.c:54
void lwgeom_geos_error(const char *fmt,...)
uint8_t flags
Definition: liblwgeom.h:247
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
GEOSGeometry * POSTGIS2GEOS(GSERIALIZED *pglwgeom)
LWGEOM * GEOS2LWGEOM(const GEOSGeometry *geom, char want3d)
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:70

Here is the call graph for this function: