PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ LWGEOM_collect()

Datum LWGEOM_collect ( PG_FUNCTION_ARGS  )

Definition at line 1127 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().

1128 {
1129  GSERIALIZED *gser1, *gser2, *result;
1130  LWGEOM *lwgeoms[2], *outlwg;
1131  uint32 type1, type2;
1132  uint8_t outtype;
1133  int srid;
1134 
1135  POSTGIS_DEBUG(2, "LWGEOM_collect called.");
1136 
1137  /* return null if both geoms are null */
1138  if ( PG_ARGISNULL(0) && PG_ARGISNULL(1) )
1139  PG_RETURN_NULL();
1140 
1141  /* Return the second geom if the first geom is null */
1142  if (PG_ARGISNULL(0))
1143  PG_RETURN_DATUM(PG_GETARG_DATUM(1));
1144 
1145  /* Return the first geom if the second geom is null */
1146  if (PG_ARGISNULL(1))
1147  PG_RETURN_DATUM(PG_GETARG_DATUM(0));
1148 
1149  gser1 = PG_GETARG_GSERIALIZED_P(0);
1150  gser2 = PG_GETARG_GSERIALIZED_P(1);
1151 
1152  POSTGIS_DEBUGF(3, "LWGEOM_collect(%s, %s): call", lwtype_name(gserialized_get_type(gser1)), lwtype_name(gserialized_get_type(gser2)));
1153 
1154  if ( FLAGS_GET_ZM(gser1->flags) != FLAGS_GET_ZM(gser2->flags) )
1155  {
1156  elog(ERROR,"Cannot ST_Collect geometries with differing dimensionality.");
1157  PG_RETURN_NULL();
1158  }
1159 
1160  srid = gserialized_get_srid(gser1);
1162 
1163  lwgeoms[0] = lwgeom_from_gserialized(gser1);
1164  lwgeoms[1] = lwgeom_from_gserialized(gser2);
1165 
1166  type1 = lwgeoms[0]->type;
1167  type2 = lwgeoms[1]->type;
1168 
1169  if ( (type1 == type2) && (!lwgeom_is_collection(lwgeoms[0])) )
1170  outtype = lwtype_get_collectiontype(type1);
1171  else
1172  outtype = COLLECTIONTYPE;
1173 
1174  POSTGIS_DEBUGF(3, " outtype = %d", outtype);
1175 
1176  /* Drop input geometries bbox and SRID */
1177  lwgeom_drop_bbox(lwgeoms[0]);
1178  lwgeom_drop_srid(lwgeoms[0]);
1179  lwgeom_drop_bbox(lwgeoms[1]);
1180  lwgeom_drop_srid(lwgeoms[1]);
1181 
1182  outlwg = (LWGEOM *)lwcollection_construct(outtype, srid, NULL, 2, lwgeoms);
1183  result = geometry_serialize(outlwg);
1184 
1185  lwgeom_free(lwgeoms[0]);
1186  lwgeom_free(lwgeoms[1]);
1187 
1188  PG_FREE_IF_COPY(gser1, 0);
1189  PG_FREE_IF_COPY(gser2, 1);
1190 
1191  PG_RETURN_POINTER(result);
1192 }
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 lwtype_get_collectiontype(uint8_t type)
Given an lwtype number, what homogeneous collection can hold it?
Definition: lwgeom.c:1075
int lwgeom_is_collection(const LWGEOM *lwgeom)
Determine whether a LWGEOM can contain sub-geometries or not.
Definition: lwgeom.c:1040
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:1099
#define FLAGS_GET_ZM(flags)
Definition: liblwgeom.h:153
void error_if_srid_mismatch(int srid1, int srid2)
Definition: lwutil.c:371
void lwgeom_drop_bbox(LWGEOM *lwgeom)
Call this function to drop BBOX and SRID from LWGEOM.
Definition: lwgeom.c:635
const char * lwtype_name(uint8_t type)
Return the type name string associated with a type number (e.g.
Definition: lwutil.c:218
void lwgeom_drop_srid(LWGEOM *lwgeom)
Definition: lwgeom.c:711
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
uint8_t type
Definition: liblwgeom.h:396
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:100
#define COLLECTIONTYPE
Definition: liblwgeom.h:91
uint8_t flags
Definition: liblwgeom.h:383
Here is the call graph for this function:
Here is the caller graph for this function: