PostGIS  2.5.0dev-r@@SVN_REVISION@@

◆ LWGEOM_collect()

Datum LWGEOM_collect ( PG_FUNCTION_ARGS  )

Definition at line 1131 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_collect_garray(), lwgeom_drop_bbox(), lwgeom_drop_srid(), lwgeom_free(), lwgeom_from_gserialized(), lwgeom_is_collection(), lwtype_get_collectiontype(), lwtype_name(), PG_FUNCTION_INFO_V1(), and LWGEOM::type.

Referenced by LWGEOM_inside_circle_point().

1132 {
1133  GSERIALIZED *gser1, *gser2, *result;
1134  LWGEOM *lwgeoms[2], *outlwg;
1135  uint32 type1, type2;
1136  uint8_t outtype;
1137  int srid;
1138 
1139  POSTGIS_DEBUG(2, "LWGEOM_collect called.");
1140 
1141  /* return null if both geoms are null */
1142  if ( PG_ARGISNULL(0) && PG_ARGISNULL(1) )
1143  PG_RETURN_NULL();
1144 
1145  /* Return the second geom if the first geom is null */
1146  if (PG_ARGISNULL(0))
1147  PG_RETURN_DATUM(PG_GETARG_DATUM(1));
1148 
1149  /* Return the first geom if the second geom is null */
1150  if (PG_ARGISNULL(1))
1151  PG_RETURN_DATUM(PG_GETARG_DATUM(0));
1152 
1153  gser1 = PG_GETARG_GSERIALIZED_P(0);
1154  gser2 = PG_GETARG_GSERIALIZED_P(1);
1155 
1156  POSTGIS_DEBUGF(3, "LWGEOM_collect(%s, %s): call", lwtype_name(gserialized_get_type(gser1)), lwtype_name(gserialized_get_type(gser2)));
1157 
1158  if ( FLAGS_GET_ZM(gser1->flags) != FLAGS_GET_ZM(gser2->flags) )
1159  {
1160  elog(ERROR,"Cannot ST_Collect geometries with differing dimensionality.");
1161  PG_RETURN_NULL();
1162  }
1163 
1164  srid = gserialized_get_srid(gser1);
1166 
1167  lwgeoms[0] = lwgeom_from_gserialized(gser1);
1168  lwgeoms[1] = lwgeom_from_gserialized(gser2);
1169 
1170  type1 = lwgeoms[0]->type;
1171  type2 = lwgeoms[1]->type;
1172 
1173  if ( (type1 == type2) && (!lwgeom_is_collection(lwgeoms[0])) )
1174  outtype = lwtype_get_collectiontype(type1);
1175  else
1176  outtype = COLLECTIONTYPE;
1177 
1178  POSTGIS_DEBUGF(3, " outtype = %d", outtype);
1179 
1180  /* Drop input geometries bbox and SRID */
1181  lwgeom_drop_bbox(lwgeoms[0]);
1182  lwgeom_drop_srid(lwgeoms[0]);
1183  lwgeom_drop_bbox(lwgeoms[1]);
1184  lwgeom_drop_srid(lwgeoms[1]);
1185 
1186  outlwg = (LWGEOM *)lwcollection_construct(outtype, srid, NULL, 2, lwgeoms);
1187  result = geometry_serialize(outlwg);
1188 
1189  lwgeom_free(lwgeoms[0]);
1190  lwgeom_free(lwgeoms[1]);
1191 
1192  PG_FREE_IF_COPY(gser1, 0);
1193  PG_FREE_IF_COPY(gser2, 1);
1194 
1195  PG_RETURN_POINTER(result);
1196 }
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:1085
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:1144
#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:673
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:1120
void lwgeom_drop_srid(LWGEOM *lwgeom)
Definition: lwgeom.c:756
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
uint8_t type
Definition: liblwgeom.h:398
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:385
Here is the call graph for this function:
Here is the caller graph for this function: