PostGIS  3.1.6dev-r@@SVN_REVISION@@

◆ pgis_geometry_union_finalfn()

Datum pgis_geometry_union_finalfn ( PG_FUNCTION_ARGS  )

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

646 {
647  CollectionBuildState *state;
648  ListCell *l;
649  LWGEOM **geoms;
650  GSERIALIZED *gser_out;
651  size_t ngeoms = 0;
652  int empty_type = 0;
653  bool first = true;
654  int32_t srid = SRID_UNKNOWN;
655  int has_z = LW_FALSE;
656 
657  if (PG_ARGISNULL(0))
658  PG_RETURN_NULL(); /* returns null iff no input values */
659 
660  state = (CollectionBuildState *)PG_GETARG_POINTER(0);
661  geoms = palloc(list_length(state->geoms) * sizeof(LWGEOM*));
662 
663  /* Read contents of list into an array of only non-null values */
664  foreach (l, state->geoms)
665  {
666  LWGEOM *geom = (LWGEOM*)(lfirst(l));
667  if (geom)
668  {
669  if (!lwgeom_is_empty(geom))
670  {
671  geoms[ngeoms++] = geom;
672  if (first)
673  {
674  srid = lwgeom_get_srid(geom);
675  has_z = lwgeom_has_z(geom);
676  first = false;
677  }
678  }
679  else
680  {
681  int type = lwgeom_get_type(geom);
682  empty_type = type > empty_type ? type : empty_type;
683  srid = (srid != SRID_UNKNOWN ? srid : lwgeom_get_srid(geom));
684  }
685  }
686  }
687 
688  /*
689  ** Take our array of LWGEOM* and turn it into a GEOS collection,
690  ** then pass that into cascaded union.
691  */
692  if (ngeoms > 0)
693  {
694  LWCOLLECTION* col = lwcollection_construct(COLLECTIONTYPE, srid, NULL, ngeoms, geoms);
696  if ( ! out )
697  {
698  lwcollection_free(col);
699  }
700  gser_out = geometry_serialize(out);
701  }
702  /* No real geometries in our array, any empties? */
703  else
704  {
705  /* If it was only empties, we'll return the largest type number */
706  if (empty_type > 0)
707  PG_RETURN_POINTER(
708  geometry_serialize(lwgeom_construct_empty(empty_type, srid, has_z, 0)));
709 
710  /* Nothing but NULL, returns NULL */
711  else
712  PG_RETURN_NULL();
713  }
714 
715  if (!gser_out)
716  {
717  /* Union returned a NULL geometry */
718  PG_RETURN_NULL();
719  }
720 
721  PG_RETURN_POINTER(gser_out);
722 }
#define LW_FALSE
Definition: liblwgeom.h:108
LWGEOM * lwcollection_as_lwgeom(const LWCOLLECTION *obj)
Definition: lwgeom.c:292
#define COLLECTIONTYPE
Definition: liblwgeom.h:122
int32_t lwgeom_get_srid(const LWGEOM *geom)
Return SRID number.
Definition: lwgeom.c:910
int lwgeom_has_z(const LWGEOM *geom)
Return LW_TRUE if geometry has Z ordinates.
Definition: lwgeom.c:917
LWGEOM * lwgeom_unaryunion_prec(const LWGEOM *geom1, double gridSize)
void lwcollection_free(LWCOLLECTION *col)
Definition: lwcollection.c:357
LWCOLLECTION * lwcollection_construct(uint8_t type, int32_t srid, GBOX *bbox, uint32_t ngeoms, LWGEOM **geoms)
Definition: lwcollection.c:42
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:229
LWGEOM * lwgeom_construct_empty(uint8_t type, int32_t srid, char hasz, char hasm)
Definition: lwgeom.c:2082
static uint32_t lwgeom_get_type(const LWGEOM *geom)
Return LWTYPE number.
Definition: lwinline.h:145
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:203
type
Definition: ovdump.py:42

References COLLECTIONTYPE, CollectionBuildState::geoms, CollectionBuildState::gridSize, LW_FALSE, lwcollection_as_lwgeom(), lwcollection_construct(), lwcollection_free(), lwgeom_construct_empty(), lwgeom_get_srid(), lwgeom_get_type(), lwgeom_has_z(), lwgeom_is_empty(), lwgeom_unaryunion_prec(), SRID_UNKNOWN, and ovdump::type.

Here is the call graph for this function: