PostGIS  2.5.0dev-r@@SVN_REVISION@@
Datum LWGEOM_collect ( PG_FUNCTION_ARGS  )

Definition at line 1128 of file lwgeom_functions_basic.c.

References COLLECTIONTYPE, error_if_srid_mismatch(), GSERIALIZED::flags, FLAGS_GET_ZM, geometry_serialize(), gserialized_get_srid(), gserialized_get_type(), lwcollection_construct(), lwgeom_drop_bbox(), lwgeom_drop_srid(), lwgeom_free(), lwgeom_from_gserialized(), lwgeom_is_collection(), lwtype_get_collectiontype(), lwtype_name(), and LWGEOM::type.

1129 {
1130  GSERIALIZED *gser1, *gser2, *result;
1131  LWGEOM *lwgeoms[2], *outlwg;
1132  uint32 type1, type2;
1133  uint8_t outtype;
1134  int srid;
1135 
1136  POSTGIS_DEBUG(2, "LWGEOM_collect called.");
1137 
1138  /* return null if both geoms are null */
1139  if ( PG_ARGISNULL(0) && PG_ARGISNULL(1) )
1140  PG_RETURN_NULL();
1141 
1142  /* Return the second geom if the first geom is null */
1143  if (PG_ARGISNULL(0))
1144  PG_RETURN_DATUM(PG_GETARG_DATUM(1));
1145 
1146  /* Return the first geom if the second geom is null */
1147  if (PG_ARGISNULL(1))
1148  PG_RETURN_DATUM(PG_GETARG_DATUM(0));
1149 
1150  gser1 = PG_GETARG_GSERIALIZED_P(0);
1151  gser2 = PG_GETARG_GSERIALIZED_P(1);
1152 
1153  POSTGIS_DEBUGF(3, "LWGEOM_collect(%s, %s): call", lwtype_name(gserialized_get_type(gser1)), lwtype_name(gserialized_get_type(gser2)));
1154 
1155  if ( FLAGS_GET_ZM(gser1->flags) != FLAGS_GET_ZM(gser2->flags) )
1156  {
1157  elog(ERROR,"Cannot ST_Collect geometries with differing dimensionality.");
1158  PG_RETURN_NULL();
1159  }
1160 
1161  srid = gserialized_get_srid(gser1);
1163 
1164  lwgeoms[0] = lwgeom_from_gserialized(gser1);
1165  lwgeoms[1] = lwgeom_from_gserialized(gser2);
1166 
1167  type1 = lwgeoms[0]->type;
1168  type2 = lwgeoms[1]->type;
1169 
1170  if ( (type1 == type2) && (!lwgeom_is_collection(lwgeoms[0])) )
1171  outtype = lwtype_get_collectiontype(type1);
1172  else
1173  outtype = COLLECTIONTYPE;
1174 
1175  POSTGIS_DEBUGF(3, " outtype = %d", outtype);
1176 
1177  /* Drop input geometries bbox and SRID */
1178  lwgeom_drop_bbox(lwgeoms[0]);
1179  lwgeom_drop_srid(lwgeoms[0]);
1180  lwgeom_drop_bbox(lwgeoms[1]);
1181  lwgeom_drop_srid(lwgeoms[1]);
1182 
1183  outlwg = (LWGEOM *)lwcollection_construct(outtype, srid, NULL, 2, lwgeoms);
1184  result = geometry_serialize(outlwg);
1185 
1186  lwgeom_free(lwgeoms[0]);
1187  lwgeom_free(lwgeoms[1]);
1188 
1189  PG_FREE_IF_COPY(gser1, 0);
1190  PG_FREE_IF_COPY(gser2, 1);
1191 
1192  PG_RETURN_POINTER(result);
1193 }
uint32_t gserialized_get_type(const GSERIALIZED *s)
Extract the geometry type from the serialized form (it hides in the anonymous data area...
Definition: g_serialized.c:86
int lwgeom_is_collection(const LWGEOM *lwgeom)
Determine whether a LWGEOM can contain sub-geometries or not.
Definition: lwgeom.c:1078
LWCOLLECTION * lwcollection_construct(uint8_t type, int srid, GBOX *bbox, uint32_t ngeoms, LWGEOM **geoms)
Definition: lwcollection.c:43
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1137
#define FLAGS_GET_ZM(flags)
Definition: liblwgeom.h:152
void error_if_srid_mismatch(int srid1, int srid2)
Definition: lwutil.c:338
void lwgeom_drop_bbox(LWGEOM *lwgeom)
Call this function to drop BBOX and SRID from LWGEOM.
Definition: lwgeom.c:666
const char * lwtype_name(uint8_t type)
Return the type name string associated with a type number (e.g.
Definition: lwutil.c:218
uint32_t lwtype_get_collectiontype(uint8_t type)
Given an lwtype number, what homogeneous collection can hold it?
Definition: lwgeom.c:1113
void lwgeom_drop_srid(LWGEOM *lwgeom)
Definition: lwgeom.c:749
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
uint8_t type
Definition: liblwgeom.h:395
unsigned char uint8_t
Definition: uthash.h:79
int32_t gserialized_get_srid(const GSERIALIZED *s)
Extract the SRID from the serialized form (it is packed into three bytes so this is a handy function)...
Definition: g_serialized.c:99
#define COLLECTIONTYPE
Definition: liblwgeom.h:90
uint8_t flags
Definition: liblwgeom.h:382

Here is the call graph for this function: