PostGIS  3.4.0dev-r@@SVN_REVISION@@

◆ pgis_union_geometry_array()

Datum pgis_union_geometry_array ( PG_FUNCTION_ARGS  )

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

598 {
599  ArrayType *array;
600 
601  ArrayIterator iterator;
602  Datum value;
603  bool isnull;
604 
605  int is3d = LW_FALSE, gotsrid = LW_FALSE;
606  int nelems = 0, geoms_size = 0, curgeom = 0, count = 0;
607 
608  GSERIALIZED *gser_out = NULL;
609 
610  GEOSGeometry *g = NULL;
611  GEOSGeometry *g_union = NULL;
612  GEOSGeometry **geoms = NULL;
613 
614  int32_t srid = SRID_UNKNOWN;
615 
616  int empty_type = 0;
617 
618  /* Null array, null geometry (should be empty?) */
619  if ( PG_ARGISNULL(0) )
620  PG_RETURN_NULL();
621 
622  array = PG_GETARG_ARRAYTYPE_P(0);
623  nelems = ArrayGetNItems(ARR_NDIM(array), ARR_DIMS(array));
624 
625  /* Empty array? Null return */
626  if ( nelems == 0 ) PG_RETURN_NULL();
627 
628  /* Quick scan for nulls */
629  iterator = array_create_iterator(array, 0, NULL);
630  while (array_iterate(iterator, &value, &isnull))
631  {
632  /* Skip null array items */
633  if (isnull) continue;
634  count++;
635  }
636  array_free_iterator(iterator);
637 
638 
639  /* All-nulls? Return null */
640  if ( count == 0 )
641  PG_RETURN_NULL();
642 
643  /* One geom, good geom? Return it */
644  if ( count == 1 && nelems == 1 )
645  {
646 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
647 #pragma GCC diagnostic push
648 #pragma GCC diagnostic ignored "-Wsign-compare"
649 #endif
650  PG_RETURN_POINTER((GSERIALIZED *)(ARR_DATA_PTR(array)));
651 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
652 #pragma GCC diagnostic pop
653 #endif
654  }
655 
656  /* Ok, we really need GEOS now ;) */
657  initGEOS(lwpgnotice, lwgeom_geos_error);
658 
659  /*
660  ** Collect the non-empty inputs and stuff them into a GEOS collection
661  */
662  geoms_size = nelems;
663  geoms = palloc(sizeof(GEOSGeometry*) * geoms_size);
664 
665  /*
666  ** We need to convert the array of GSERIALIZED into a GEOS collection.
667  ** First make an array of GEOS geometries.
668  */
669  iterator = array_create_iterator(array, 0, NULL);
670  while (array_iterate(iterator, &value, &isnull))
671  {
672  GSERIALIZED *gser_in;
673 
674  /* Skip null array items */
675  if (isnull) continue;
676  gser_in = (GSERIALIZED *)DatumGetPointer(value);
677 
678  /* Check for SRID mismatch in array elements */
679  if ( gotsrid )
680  gserialized_error_if_srid_mismatch_reference(gser_in, srid, __func__);
681  else
682  {
683  /* Initialize SRID/dimensions info */
684  srid = gserialized_get_srid(gser_in);
685  is3d = gserialized_has_z(gser_in);
686  gotsrid = 1;
687  }
688 
689  /* Don't include empties in the union */
690  if ( gserialized_is_empty(gser_in) )
691  {
692  int gser_type = gserialized_get_type(gser_in);
693  if (gser_type > empty_type)
694  {
695  empty_type = gser_type;
696  POSTGIS_DEBUGF(4, "empty_type = %d gser_type = %d", empty_type, gser_type);
697  }
698  }
699  else
700  {
701  g = POSTGIS2GEOS(gser_in);
702 
703  /* Uh oh! Exception thrown at construction... */
704  if ( ! g )
705  {
707  "One of the geometries in the set "
708  "could not be converted to GEOS");
709  }
710 
711  /* Ensure we have enough space in our storage array */
712  if ( curgeom == geoms_size )
713  {
714  geoms_size *= 2;
715  geoms = repalloc( geoms, sizeof(GEOSGeometry*) * geoms_size );
716  }
717 
718  geoms[curgeom] = g;
719  curgeom++;
720  }
721 
722  }
723  array_free_iterator(iterator);
724 
725  /*
726  ** Take our GEOS geometries and turn them into a GEOS collection,
727  ** then pass that into cascaded union.
728  */
729  if (curgeom > 0)
730  {
731  g = GEOSGeom_createCollection(GEOS_GEOMETRYCOLLECTION, geoms, curgeom);
732  if (!g) HANDLE_GEOS_ERROR("Could not create GEOS COLLECTION from geometry array");
733 
734  g_union = GEOSUnaryUnion(g);
735  GEOSGeom_destroy(g);
736  if (!g_union) HANDLE_GEOS_ERROR("GEOSUnaryUnion");
737 
738  GEOSSetSRID(g_union, srid);
739  gser_out = GEOS2POSTGIS(g_union, is3d);
740  GEOSGeom_destroy(g_union);
741  }
742  /* No real geometries in our array, any empties? */
743  else
744  {
745  /* If it was only empties, we'll return the largest type number */
746  if ( empty_type > 0 )
747  {
748  PG_RETURN_POINTER(geometry_serialize(lwgeom_construct_empty(empty_type, srid, is3d, 0)));
749  }
750  /* Nothing but NULL, returns NULL */
751  else
752  {
753  PG_RETURN_NULL();
754  }
755  }
756 
757  if ( ! gser_out )
758  {
759  /* Union returned a NULL geometry */
760  PG_RETURN_NULL();
761  }
762 
763  PG_RETURN_POINTER(gser_out);
764 }
void gserialized_error_if_srid_mismatch_reference(const GSERIALIZED *g1, const int32_t srid2, const char *funcname)
Definition: gserialized.c:418
int32_t gserialized_get_srid(const GSERIALIZED *g)
Extract the SRID from the serialized form (it is packed into three bytes so this is a handy function)...
Definition: gserialized.c:126
int gserialized_is_empty(const GSERIALIZED *g)
Check if a GSERIALIZED is empty without deserializing first.
Definition: gserialized.c:152
int gserialized_has_z(const GSERIALIZED *g)
Check if a GSERIALIZED has a Z ordinate.
Definition: gserialized.c:174
uint32_t gserialized_get_type(const GSERIALIZED *g)
Extract the geometry type from the serialized form (it hides in the anonymous data area,...
Definition: gserialized.c:89
void lwgeom_geos_error(const char *fmt,...)
#define LW_FALSE
Definition: liblwgeom.h:94
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:215
LWGEOM * lwgeom_construct_empty(uint8_t type, int32_t srid, char hasz, char hasm)
Definition: lwgeom.c:2105
int value
Definition: genraster.py:62
int count
Definition: genraster.py:57
GSERIALIZED * GEOS2POSTGIS(GEOSGeom geom, char want3d)
GEOSGeometry * POSTGIS2GEOS(const GSERIALIZED *pglwgeom)
#define HANDLE_GEOS_ERROR(label)

References genraster::count, GEOS2POSTGIS(), gserialized_error_if_srid_mismatch_reference(), gserialized_get_srid(), gserialized_get_type(), gserialized_has_z(), gserialized_is_empty(), HANDLE_GEOS_ERROR, LW_FALSE, lwgeom_construct_empty(), lwgeom_geos_error(), POSTGIS2GEOS(), SRID_UNKNOWN, and genraster::value.

Here is the call graph for this function: