PostGIS  3.7.0dev-r@@SVN_REVISION@@

◆ LWGEOM_collect_garray()

Datum LWGEOM_collect_garray ( PG_FUNCTION_ARGS  )

Definition at line 1247 of file lwgeom_functions_basic.c.

1248 {
1249  ArrayType *array;
1250  int nelems;
1251  /*GSERIALIZED **geoms; */
1252  GSERIALIZED *result = NULL;
1253  LWGEOM **lwgeoms, *outlwg;
1254  uint32 outtype;
1255  int count;
1256  int32_t srid = SRID_UNKNOWN;
1257  GBOX *box = NULL;
1258 
1259  ArrayIterator iterator;
1260  Datum value;
1261  bool isnull;
1262 
1263  POSTGIS_DEBUG(2, "LWGEOM_collect_garray called.");
1264 
1265  if (PG_ARGISNULL(0))
1266  PG_RETURN_NULL();
1267 
1268  /* Get actual ArrayType */
1269  array = PG_GETARG_ARRAYTYPE_P(0);
1270  nelems = ArrayGetNItems(ARR_NDIM(array), ARR_DIMS(array));
1271 
1272  POSTGIS_DEBUGF(3,
1273  " array is %d-bytes in size, %ld w/out header",
1274  ARR_SIZE(array),
1275  ARR_SIZE(array) - ARR_OVERHEAD_NONULLS(ARR_NDIM(array)));
1276 
1277  POSTGIS_DEBUGF(3, "LWGEOM_collect_garray: array has %d elements", nelems);
1278 
1279  /* Return null on 0-elements input array */
1280  if (nelems == 0)
1281  PG_RETURN_NULL();
1282 
1283  /*
1284  * Deserialize all geometries in array into the lwgeoms pointers
1285  * array. Check input types to form output type.
1286  */
1287  lwgeoms = palloc(sizeof(LWGEOM *) * nelems);
1288  count = 0;
1289  outtype = 0;
1290 
1291  iterator = array_create_iterator(array, 0, NULL);
1292 
1293  while (array_iterate(iterator, &value, &isnull))
1294  {
1295  GSERIALIZED *geom;
1296  uint8_t intype;
1297 
1298  /* Don't do anything for NULL values */
1299  if (isnull)
1300  continue;
1301 
1302  geom = (GSERIALIZED *)DatumGetPointer(value);
1303  intype = gserialized_get_type(geom);
1304 
1305  lwgeoms[count] = lwgeom_from_gserialized(geom);
1306 
1307  POSTGIS_DEBUGF(3, "%s: geom %d deserialized", __func__, count);
1308 
1309  if (!count)
1310  {
1311  /* Get first geometry SRID */
1312  srid = lwgeoms[count]->srid;
1313 
1314  /* COMPUTE_BBOX WHEN_SIMPLE */
1315  if (lwgeoms[count]->bbox)
1316  box = gbox_copy(lwgeoms[count]->bbox);
1317  }
1318  else
1319  {
1320  /* Check SRID homogeneity */
1321  gserialized_error_if_srid_mismatch_reference(geom, srid, __func__);
1322 
1323  /* COMPUTE_BBOX WHEN_SIMPLE */
1324  if (box)
1325  {
1326  if (lwgeoms[count]->bbox)
1327  gbox_merge(lwgeoms[count]->bbox, box);
1328  else
1329  {
1330  pfree(box);
1331  box = NULL;
1332  }
1333  }
1334  }
1335 
1336  lwgeom_drop_srid(lwgeoms[count]);
1337  lwgeom_drop_bbox(lwgeoms[count]);
1338 
1339  /* Output type not initialized */
1340  if (!outtype)
1341  {
1342  outtype = lwtype_get_collectiontype(intype);
1343  }
1344  /* Input type not compatible with output */
1345  /* make output type a collection */
1346  else if (outtype != COLLECTIONTYPE && lwtype_get_collectiontype(intype) != outtype)
1347  {
1348  outtype = COLLECTIONTYPE;
1349  }
1350 
1351  count++;
1352  }
1353  array_free_iterator(iterator);
1354 
1355  POSTGIS_DEBUGF(3, "LWGEOM_collect_garray: outtype = %d", outtype);
1356 
1357  /* If we have been passed a complete set of NULLs then return NULL */
1358  if (!outtype)
1359  {
1360  PG_RETURN_NULL();
1361  }
1362  else
1363  {
1364  outlwg = (LWGEOM *)lwcollection_construct(outtype, srid, box, count, lwgeoms);
1365 
1366  result = geometry_serialize(outlwg);
1367 
1368  PG_RETURN_POINTER(result);
1369  }
1370 }
char result[OUT_DOUBLE_BUFFER_SIZE]
Definition: cu_print.c:267
int gbox_merge(const GBOX *new_box, GBOX *merge_box)
Update the merged GBOX to be large enough to include itself and the new box.
Definition: gbox.c:257
GBOX * gbox_copy(const GBOX *box)
Return a copy of the GBOX, based on dimensionality of flags.
Definition: gbox.c:438
void gserialized_error_if_srid_mismatch_reference(const GSERIALIZED *g1, const int32_t srid2, const char *funcname)
Definition: gserialized.c:447
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
Definition: gserialized.c:268
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:118
uint32_t lwtype_get_collectiontype(uint8_t type)
Given an lwtype number, what homogeneous collection can hold it?
Definition: lwgeom.c:1194
#define COLLECTIONTYPE
Definition: liblwgeom.h:108
void lwgeom_drop_bbox(LWGEOM *lwgeom)
Call this function to drop BBOX and SRID from LWGEOM.
Definition: lwgeom.c:682
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:215
void lwgeom_drop_srid(LWGEOM *lwgeom)
Definition: lwgeom.c:765
int value
Definition: genraster.py:62
int count
Definition: genraster.py:57
int32_t srid
Definition: liblwgeom.h:460

References COLLECTIONTYPE, genraster::count, gbox_copy(), gbox_merge(), gserialized_error_if_srid_mismatch_reference(), gserialized_get_type(), lwcollection_construct(), lwgeom_drop_bbox(), lwgeom_drop_srid(), lwgeom_from_gserialized(), lwtype_get_collectiontype(), result, LWGEOM::srid, SRID_UNKNOWN, and genraster::value.

Referenced by pgis_geometry_collect_finalfn().

Here is the call graph for this function:
Here is the caller graph for this function: