PostGIS  3.1.6dev-r@@SVN_REVISION@@

◆ LWGEOM_collect_garray()

Datum LWGEOM_collect_garray ( PG_FUNCTION_ARGS  )

Definition at line 1219 of file lwgeom_functions_basic.c.

1220 {
1221  ArrayType *array;
1222  int nelems;
1223  /*GSERIALIZED **geoms; */
1224  GSERIALIZED *result = NULL;
1225  LWGEOM **lwgeoms, *outlwg;
1226  uint32 outtype;
1227  int count;
1228  int32_t srid = SRID_UNKNOWN;
1229  GBOX *box = NULL;
1230 
1231  ArrayIterator iterator;
1232  Datum value;
1233  bool isnull;
1234 
1235  POSTGIS_DEBUG(2, "LWGEOM_collect_garray called.");
1236 
1237  if (PG_ARGISNULL(0))
1238  PG_RETURN_NULL();
1239 
1240  /* Get actual ArrayType */
1241  array = PG_GETARG_ARRAYTYPE_P(0);
1242  nelems = ArrayGetNItems(ARR_NDIM(array), ARR_DIMS(array));
1243 
1244  POSTGIS_DEBUGF(3,
1245  " array is %d-bytes in size, %ld w/out header",
1246  ARR_SIZE(array),
1247  ARR_SIZE(array) - ARR_OVERHEAD_NONULLS(ARR_NDIM(array)));
1248 
1249  POSTGIS_DEBUGF(3, "LWGEOM_collect_garray: array has %d elements", nelems);
1250 
1251  /* Return null on 0-elements input array */
1252  if (nelems == 0)
1253  PG_RETURN_NULL();
1254 
1255  /*
1256  * Deserialize all geometries in array into the lwgeoms pointers
1257  * array. Check input types to form output type.
1258  */
1259  lwgeoms = palloc(sizeof(LWGEOM *) * nelems);
1260  count = 0;
1261  outtype = 0;
1262 
1263  iterator = array_create_iterator(array, 0, NULL);
1264 
1265  while (array_iterate(iterator, &value, &isnull))
1266  {
1267  GSERIALIZED *geom;
1268  uint8_t intype;
1269 
1270  /* Don't do anything for NULL values */
1271  if (isnull)
1272  continue;
1273 
1274  geom = (GSERIALIZED *)DatumGetPointer(value);
1275  intype = gserialized_get_type(geom);
1276 
1277  lwgeoms[count] = lwgeom_from_gserialized(geom);
1278 
1279  POSTGIS_DEBUGF(3, "%s: geom %d deserialized", __func__, count);
1280 
1281  if (!count)
1282  {
1283  /* Get first geometry SRID */
1284  srid = lwgeoms[count]->srid;
1285 
1286  /* COMPUTE_BBOX WHEN_SIMPLE */
1287  if (lwgeoms[count]->bbox)
1288  box = gbox_copy(lwgeoms[count]->bbox);
1289  }
1290  else
1291  {
1292  /* Check SRID homogeneity */
1293  gserialized_error_if_srid_mismatch_reference(geom, srid, __func__);
1294 
1295  /* COMPUTE_BBOX WHEN_SIMPLE */
1296  if (box)
1297  {
1298  if (lwgeoms[count]->bbox)
1299  gbox_merge(lwgeoms[count]->bbox, box);
1300  else
1301  {
1302  pfree(box);
1303  box = NULL;
1304  }
1305  }
1306  }
1307 
1308  lwgeom_drop_srid(lwgeoms[count]);
1309  lwgeom_drop_bbox(lwgeoms[count]);
1310 
1311  /* Output type not initialized */
1312  if (!outtype)
1313  {
1314  outtype = lwtype_get_collectiontype(intype);
1315  }
1316  /* Input type not compatible with output */
1317  /* make output type a collection */
1318  else if (outtype != COLLECTIONTYPE && lwtype_get_collectiontype(intype) != outtype)
1319  {
1320  outtype = COLLECTIONTYPE;
1321  }
1322 
1323  count++;
1324  }
1325  array_free_iterator(iterator);
1326 
1327  POSTGIS_DEBUGF(3, "LWGEOM_collect_garray: outtype = %d", outtype);
1328 
1329  /* If we have been passed a complete set of NULLs then return NULL */
1330  if (!outtype)
1331  {
1332  PG_RETURN_NULL();
1333  }
1334  else
1335  {
1336  outlwg = (LWGEOM *)lwcollection_construct(outtype, srid, box, count, lwgeoms);
1337 
1338  result = geometry_serialize(outlwg);
1339 
1340  PG_RETURN_POINTER(result);
1341  }
1342 }
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:426
void gserialized_error_if_srid_mismatch_reference(const GSERIALIZED *g1, const int32_t srid2, const char *funcname)
Definition: gserialized.c:419
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
Definition: gserialized.c:239
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
uint32_t lwtype_get_collectiontype(uint8_t type)
Given an lwtype number, what homogeneous collection can hold it?
Definition: lwgeom.c:1114
#define COLLECTIONTYPE
Definition: liblwgeom.h:122
void lwgeom_drop_bbox(LWGEOM *lwgeom)
Call this function to drop BBOX and SRID from LWGEOM.
Definition: lwgeom.c:665
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
void lwgeom_drop_srid(LWGEOM *lwgeom)
Definition: lwgeom.c:748
int value
Definition: genraster.py:62
int count
Definition: genraster.py:57
int32_t srid
Definition: liblwgeom.h:474

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: