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

Definition at line 1102 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(), result, and LWGEOM::type.

1103 {
1104  GSERIALIZED *gser1, *gser2, *result;
1105  LWGEOM *lwgeoms[2], *outlwg;
1106  uint32 type1, type2;
1107  uint8_t outtype;
1108  int srid;
1109 
1110  POSTGIS_DEBUG(2, "LWGEOM_collect called.");
1111 
1112  /* return null if both geoms are null */
1113  if ( PG_ARGISNULL(0) && PG_ARGISNULL(1) )
1114  PG_RETURN_NULL();
1115 
1116  /* Return the second geom if the first geom is null */
1117  if (PG_ARGISNULL(0))
1118  PG_RETURN_DATUM(PG_GETARG_DATUM(1));
1119 
1120  /* Return the first geom if the second geom is null */
1121  if (PG_ARGISNULL(1))
1122  PG_RETURN_DATUM(PG_GETARG_DATUM(0));
1123 
1124  gser1 = (GSERIALIZED *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
1125  gser2 = (GSERIALIZED *)PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
1126 
1127  POSTGIS_DEBUGF(3, "LWGEOM_collect(%s, %s): call", lwtype_name(gserialized_get_type(gser1)), lwtype_name(gserialized_get_type(gser2)));
1128 
1129  if ( FLAGS_GET_ZM(gser1->flags) != FLAGS_GET_ZM(gser2->flags) )
1130  {
1131  elog(ERROR,"Cannot ST_Collect geometries with differing dimensionality.");
1132  PG_RETURN_NULL();
1133  }
1134 
1135  srid = gserialized_get_srid(gser1);
1137 
1138  lwgeoms[0] = lwgeom_from_gserialized(gser1);
1139  lwgeoms[1] = lwgeom_from_gserialized(gser2);
1140 
1141  type1 = lwgeoms[0]->type;
1142  type2 = lwgeoms[1]->type;
1143 
1144  if ( (type1 == type2) && (!lwgeom_is_collection(lwgeoms[0])) )
1145  outtype = lwtype_get_collectiontype(type1);
1146  else
1147  outtype = COLLECTIONTYPE;
1148 
1149  POSTGIS_DEBUGF(3, " outtype = %d", outtype);
1150 
1151  /* Drop input geometries bbox and SRID */
1152  lwgeom_drop_bbox(lwgeoms[0]);
1153  lwgeom_drop_srid(lwgeoms[0]);
1154  lwgeom_drop_bbox(lwgeoms[1]);
1155  lwgeom_drop_srid(lwgeoms[1]);
1156 
1157  outlwg = (LWGEOM *)lwcollection_construct(outtype, srid, NULL, 2, lwgeoms);
1158  result = geometry_serialize(outlwg);
1159 
1160  lwgeom_free(lwgeoms[0]);
1161  lwgeom_free(lwgeoms[1]);
1162 
1163  PG_FREE_IF_COPY(gser1, 0);
1164  PG_FREE_IF_COPY(gser2, 1);
1165 
1166  PG_RETURN_POINTER(result);
1167 }
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:56
int lwtype_get_collectiontype(uint8_t type)
Given an lwtype number, what homogeneous collection can hold it?
Definition: lwgeom.c:982
int lwgeom_is_collection(const LWGEOM *lwgeom)
Determine whether a LWGEOM can contain sub-geometries or not.
Definition: lwgeom.c:947
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 error_if_srid_mismatch(int srid1, int srid2)
Definition: lwutil.c:317
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1006
#define FLAGS_GET_ZM(flags)
Definition: liblwgeom.h:119
char ** result
Definition: liblwgeom.h:218
void lwgeom_drop_bbox(LWGEOM *lwgeom)
Call this function to drop BBOX and SRID from LWGEOM.
Definition: lwgeom.c:542
unsigned int uint32
Definition: shpopen.c:274
const char * lwtype_name(uint8_t type)
Return the type name string associated with a type number (e.g.
Definition: lwutil.c:164
void lwgeom_drop_srid(LWGEOM *lwgeom)
Definition: lwgeom.c:618
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
uint8_t type
Definition: liblwgeom.h:352
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:70
#define COLLECTIONTYPE
Definition: liblwgeom.h:66
uint8_t flags
Definition: liblwgeom.h:339

Here is the call graph for this function: