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

Definition at line 1081 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.

1082 {
1083  GSERIALIZED *gser1, *gser2, *result;
1084  LWGEOM *lwgeoms[2], *outlwg;
1085  uint32 type1, type2;
1086  uint8_t outtype;
1087  int srid;
1088 
1089  POSTGIS_DEBUG(2, "LWGEOM_collect called.");
1090 
1091  /* return null if both geoms are null */
1092  if ( PG_ARGISNULL(0) && PG_ARGISNULL(1) )
1093  PG_RETURN_NULL();
1094 
1095  /* Return the second geom if the first geom is null */
1096  if (PG_ARGISNULL(0))
1097  PG_RETURN_DATUM(PG_GETARG_DATUM(1));
1098 
1099  /* Return the first geom if the second geom is null */
1100  if (PG_ARGISNULL(1))
1101  PG_RETURN_DATUM(PG_GETARG_DATUM(0));
1102 
1103  gser1 = PG_GETARG_GSERIALIZED_P(0);
1104  gser2 = PG_GETARG_GSERIALIZED_P(1);
1105 
1106  POSTGIS_DEBUGF(3, "LWGEOM_collect(%s, %s): call", lwtype_name(gserialized_get_type(gser1)), lwtype_name(gserialized_get_type(gser2)));
1107 
1108  if ( FLAGS_GET_ZM(gser1->flags) != FLAGS_GET_ZM(gser2->flags) )
1109  {
1110  elog(ERROR,"Cannot ST_Collect geometries with differing dimensionality.");
1111  PG_RETURN_NULL();
1112  }
1113 
1114  srid = gserialized_get_srid(gser1);
1116 
1117  lwgeoms[0] = lwgeom_from_gserialized(gser1);
1118  lwgeoms[1] = lwgeom_from_gserialized(gser2);
1119 
1120  type1 = lwgeoms[0]->type;
1121  type2 = lwgeoms[1]->type;
1122 
1123  if ( (type1 == type2) && (!lwgeom_is_collection(lwgeoms[0])) )
1124  outtype = lwtype_get_collectiontype(type1);
1125  else
1126  outtype = COLLECTIONTYPE;
1127 
1128  POSTGIS_DEBUGF(3, " outtype = %d", outtype);
1129 
1130  /* Drop input geometries bbox and SRID */
1131  lwgeom_drop_bbox(lwgeoms[0]);
1132  lwgeom_drop_srid(lwgeoms[0]);
1133  lwgeom_drop_bbox(lwgeoms[1]);
1134  lwgeom_drop_srid(lwgeoms[1]);
1135 
1136  outlwg = (LWGEOM *)lwcollection_construct(outtype, srid, NULL, 2, lwgeoms);
1137  result = geometry_serialize(outlwg);
1138 
1139  lwgeom_free(lwgeoms[0]);
1140  lwgeom_free(lwgeoms[1]);
1141 
1142  PG_FREE_IF_COPY(gser1, 0);
1143  PG_FREE_IF_COPY(gser2, 1);
1144 
1145  PG_RETURN_POINTER(result);
1146 }
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:55
int lwtype_get_collectiontype(uint8_t type)
Given an lwtype number, what homogeneous collection can hold it?
Definition: lwgeom.c:1026
int lwgeom_is_collection(const LWGEOM *lwgeom)
Determine whether a LWGEOM can contain sub-geometries or not.
Definition: lwgeom.c:991
LWCOLLECTION * lwcollection_construct(uint8_t type, int srid, GBOX *bbox, uint32_t ngeoms, LWGEOM **geoms)
Definition: lwcollection.c:30
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1050
#define FLAGS_GET_ZM(flags)
Definition: liblwgeom.h:137
void error_if_srid_mismatch(int srid1, int srid2)
Definition: lwutil.c:341
void lwgeom_drop_bbox(LWGEOM *lwgeom)
Call this function to drop BBOX and SRID from LWGEOM.
Definition: lwgeom.c:586
const char * lwtype_name(uint8_t type)
Return the type name string associated with a type number (e.g.
Definition: lwutil.c:188
void lwgeom_drop_srid(LWGEOM *lwgeom)
Definition: lwgeom.c:662
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
uint8_t type
Definition: liblwgeom.h:380
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:69
#define COLLECTIONTYPE
Definition: liblwgeom.h:76
uint8_t flags
Definition: liblwgeom.h:367

Here is the call graph for this function: