PostGIS  3.4.0dev-r@@SVN_REVISION@@

◆ LWGEOM_collect()

Datum LWGEOM_collect ( PG_FUNCTION_ARGS  )

Definition at line 1202 of file lwgeom_functions_basic.c.

1203 {
1204  GSERIALIZED *gser1, *gser2, *result;
1205  LWGEOM *lwgeoms[2], *outlwg;
1206  uint32 type1, type2;
1207  uint8_t outtype;
1208  int32_t srid;
1209 
1210  POSTGIS_DEBUG(2, "LWGEOM_collect called.");
1211 
1212  /* return null if both geoms are null */
1213  if (PG_ARGISNULL(0) && PG_ARGISNULL(1))
1214  PG_RETURN_NULL();
1215 
1216  /* Return the second geom if the first geom is null */
1217  if (PG_ARGISNULL(0))
1218  PG_RETURN_DATUM(PG_GETARG_DATUM(1));
1219 
1220  /* Return the first geom if the second geom is null */
1221  if (PG_ARGISNULL(1))
1222  PG_RETURN_DATUM(PG_GETARG_DATUM(0));
1223 
1224  gser1 = PG_GETARG_GSERIALIZED_P(0);
1225  gser2 = PG_GETARG_GSERIALIZED_P(1);
1226  gserialized_error_if_srid_mismatch(gser1, gser2, __func__);
1227 
1228  POSTGIS_DEBUGF(3,
1229  "LWGEOM_collect(%s, %s): call",
1232 
1233  if ((gserialized_has_z(gser1) != gserialized_has_z(gser2)) ||
1234  (gserialized_has_m(gser1) != gserialized_has_m(gser2)))
1235  {
1236  elog(ERROR, "Cannot ST_Collect geometries with differing dimensionality.");
1237  PG_RETURN_NULL();
1238  }
1239 
1240  srid = gserialized_get_srid(gser1);
1241 
1242  lwgeoms[0] = lwgeom_from_gserialized(gser1);
1243  lwgeoms[1] = lwgeom_from_gserialized(gser2);
1244 
1245  type1 = lwgeoms[0]->type;
1246  type2 = lwgeoms[1]->type;
1247 
1248  if ((type1 == type2) && (!lwgeom_is_collection(lwgeoms[0])))
1249  outtype = lwtype_get_collectiontype(type1);
1250  else
1251  outtype = COLLECTIONTYPE;
1252 
1253  POSTGIS_DEBUGF(3, " outtype = %d", outtype);
1254 
1255  /* Drop input geometries bbox and SRID */
1256  lwgeom_drop_bbox(lwgeoms[0]);
1257  lwgeom_drop_srid(lwgeoms[0]);
1258  lwgeom_drop_bbox(lwgeoms[1]);
1259  lwgeom_drop_srid(lwgeoms[1]);
1260 
1261  outlwg = (LWGEOM *)lwcollection_construct(outtype, srid, NULL, 2, lwgeoms);
1262  result = geometry_serialize(outlwg);
1263 
1264  lwgeom_free(lwgeoms[0]);
1265  lwgeom_free(lwgeoms[1]);
1266 
1267  PG_FREE_IF_COPY(gser1, 0);
1268  PG_FREE_IF_COPY(gser2, 1);
1269 
1270  PG_RETURN_POINTER(result);
1271 }
char result[OUT_DOUBLE_BUFFER_SIZE]
Definition: cu_print.c:262
void gserialized_error_if_srid_mismatch(const GSERIALIZED *g1, const GSERIALIZED *g2, const char *funcname)
Definition: gserialized.c:403
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_has_m(const GSERIALIZED *g)
Check if a GSERIALIZED has an M ordinate.
Definition: gserialized.c:185
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
Definition: gserialized.c:239
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
uint32_t lwtype_get_collectiontype(uint8_t type)
Given an lwtype number, what homogeneous collection can hold it?
Definition: lwgeom.c:1131
#define COLLECTIONTYPE
Definition: liblwgeom.h:108
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1155
void lwgeom_drop_bbox(LWGEOM *lwgeom)
Call this function to drop BBOX and SRID from LWGEOM.
Definition: lwgeom.c:682
int lwgeom_is_collection(const LWGEOM *lwgeom)
Determine whether a LWGEOM can contain sub-geometries or not.
Definition: lwgeom.c:1097
const char * lwtype_name(uint8_t type)
Return the type name string associated with a type number (e.g.
Definition: lwutil.c:216
LWCOLLECTION * lwcollection_construct(uint8_t type, int32_t srid, GBOX *bbox, uint32_t ngeoms, LWGEOM **geoms)
Definition: lwcollection.c:42
void lwgeom_drop_srid(LWGEOM *lwgeom)
Definition: lwgeom.c:765
uint8_t type
Definition: liblwgeom.h:462

References COLLECTIONTYPE, gserialized_error_if_srid_mismatch(), gserialized_get_srid(), gserialized_get_type(), gserialized_has_m(), gserialized_has_z(), lwcollection_construct(), lwgeom_drop_bbox(), lwgeom_drop_srid(), lwgeom_free(), lwgeom_from_gserialized(), lwgeom_is_collection(), lwtype_get_collectiontype(), lwtype_name(), result, and LWGEOM::type.

Here is the call graph for this function: