PostGIS 3.0.6dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches

◆ LWGEOM_collect_garray()

Datum LWGEOM_collect_garray ( PG_FUNCTION_ARGS  )

Definition at line 1220 of file lwgeom_functions_basic.c.

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

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

Referenced by pgis_geometry_collect_finalfn().

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