PostGIS  2.5.7dev-r@@SVN_REVISION@@

◆ postgis_valid_typmod()

GSERIALIZED* postgis_valid_typmod ( GSERIALIZED gser,
int32_t  typmod 
)

Check the consistency of the metadata we want to enforce in the typmod: srid, type and dimensionality.

If things are inconsistent, shut down the query.

Definition at line 112 of file gserialized_typmod.c.

113 {
114  int32 geom_srid = gserialized_get_srid(gser);
115  int32 geom_type = gserialized_get_type(gser);
116  int32 geom_z = gserialized_has_z(gser);
117  int32 geom_m = gserialized_has_m(gser);
118  int32 typmod_srid = TYPMOD_GET_SRID(typmod);
119  int32 typmod_type = TYPMOD_GET_TYPE(typmod);
120  int32 typmod_z = TYPMOD_GET_Z(typmod);
121  int32 typmod_m = TYPMOD_GET_M(typmod);
122 
123  POSTGIS_DEBUG(2, "Entered function");
124 
125  /* No typmod (-1) => no preferences */
126  if (typmod < 0) return gser;
127 
128  POSTGIS_DEBUGF(3, "Got geom(type = %d, srid = %d, hasz = %d, hasm = %d)", geom_type, geom_srid, geom_z, geom_m);
129  POSTGIS_DEBUGF(3, "Got typmod(type = %d, srid = %d, hasz = %d, hasm = %d)", typmod_type, typmod_srid, typmod_z, typmod_m);
130 
131  /*
132  * #3031: If a user is handing us a MULTIPOINT EMPTY but trying to fit it into
133  * a POINT geometry column, there's a strong chance the reason she has
134  * a MULTIPOINT EMPTY because we gave it to her during data dump,
135  * converting the internal POINT EMPTY into a EWKB MULTIPOINT EMPTY
136  * (because EWKB doesn't have a clean way to represent POINT EMPTY).
137  * In such a case, it makes sense to turn the MULTIPOINT EMPTY back into a
138  * point EMPTY, rather than throwing an error.
139  */
140  if ( typmod_type == POINTTYPE && geom_type == MULTIPOINTTYPE &&
141  gserialized_is_empty(gser) )
142  {
143  LWPOINT *empty_point = lwpoint_construct_empty(geom_srid, geom_z, geom_m);
144  geom_type = POINTTYPE;
145  pfree(gser);
146  if ( gserialized_is_geodetic(gser) )
147  gser = geography_serialize(lwpoint_as_lwgeom(empty_point));
148  else
149  gser = geometry_serialize(lwpoint_as_lwgeom(empty_point));
150  }
151 
152  /* Typmod has a preference for SRID? Geometry SRID had better match. */
153  if ( typmod_srid > 0 && typmod_srid != geom_srid )
154  {
155  ereport(ERROR, (
156  errcode(ERRCODE_INVALID_PARAMETER_VALUE),
157  errmsg("Geometry SRID (%d) does not match column SRID (%d)", geom_srid, typmod_srid) ));
158  }
159 
160  /* Typmod has a preference for geometry type. */
161  if ( typmod_type > 0 &&
162  /* GEOMETRYCOLLECTION column can hold any kind of collection */
163  ((typmod_type == COLLECTIONTYPE && ! (geom_type == COLLECTIONTYPE ||
164  geom_type == MULTIPOLYGONTYPE ||
165  geom_type == MULTIPOINTTYPE ||
166  geom_type == MULTILINETYPE )) ||
167  /* Other types must be strictly equal. */
168  (typmod_type != geom_type)) )
169  {
170  ereport(ERROR, (
171  errcode(ERRCODE_INVALID_PARAMETER_VALUE),
172  errmsg("Geometry type (%s) does not match column type (%s)", lwtype_name(geom_type), lwtype_name(typmod_type)) ));
173  }
174 
175  /* Mismatched Z dimensionality. */
176  if ( typmod_z && ! geom_z )
177  {
178  ereport(ERROR, (
179  errcode(ERRCODE_INVALID_PARAMETER_VALUE),
180  errmsg("Column has Z dimension but geometry does not" )));
181  }
182 
183  /* Mismatched Z dimensionality (other way). */
184  if ( geom_z && ! typmod_z )
185  {
186  ereport(ERROR, (
187  errcode(ERRCODE_INVALID_PARAMETER_VALUE),
188  errmsg("Geometry has Z dimension but column does not" )));
189  }
190 
191  /* Mismatched M dimensionality. */
192  if ( typmod_m && ! geom_m )
193  {
194  ereport(ERROR, (
195  errcode(ERRCODE_INVALID_PARAMETER_VALUE),
196  errmsg("Column has M dimension but geometry does not" )));
197  }
198 
199  /* Mismatched M dimensionality (other way). */
200  if ( geom_m && ! typmod_m )
201  {
202  ereport(ERROR, (
203  errcode(ERRCODE_INVALID_PARAMETER_VALUE),
204  errmsg("Geometry has M dimension but column does not" )));
205  }
206 
207  return gser;
208 
209 }
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
int gserialized_has_z(const GSERIALIZED *gser)
Check if a GSERIALIZED has a Z ordinate.
Definition: g_serialized.c:45
int gserialized_is_empty(const GSERIALIZED *g)
Check if a GSERIALIZED is empty without deserializing first.
Definition: g_serialized.c:179
int gserialized_is_geodetic(const GSERIALIZED *gser)
Check if a GSERIALIZED is a geography.
Definition: g_serialized.c:65
int gserialized_has_m(const GSERIALIZED *gser)
Check if a GSERIALIZED has an M ordinate.
Definition: g_serialized.c:50
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
#define TYPMOD_GET_SRID(typmod)
Macros for manipulating the 'typemod' int.
Definition: liblwgeom.h:165
#define COLLECTIONTYPE
Definition: liblwgeom.h:91
#define MULTILINETYPE
Definition: liblwgeom.h:89
#define MULTIPOINTTYPE
Definition: liblwgeom.h:88
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:85
#define TYPMOD_GET_M(typmod)
Definition: liblwgeom.h:171
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:90
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
Definition: lwgeom.c:335
const char * lwtype_name(uint8_t type)
Return the type name string associated with a type number (e.g.
Definition: lwutil.c:218
#define TYPMOD_GET_TYPE(typmod)
Definition: liblwgeom.h:167
LWPOINT * lwpoint_construct_empty(int srid, char hasz, char hasm)
Definition: lwpoint.c:151
#define TYPMOD_GET_Z(typmod)
Definition: liblwgeom.h:169
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
unsigned int int32
Definition: shpopen.c:273

References COLLECTIONTYPE, geometry_serialize(), gserialized_get_srid(), gserialized_get_type(), gserialized_has_m(), gserialized_has_z(), gserialized_is_empty(), gserialized_is_geodetic(), lwpoint_as_lwgeom(), lwpoint_construct_empty(), lwtype_name(), MULTILINETYPE, MULTIPOINTTYPE, MULTIPOLYGONTYPE, POINTTYPE, TYPMOD_GET_M, TYPMOD_GET_SRID, TYPMOD_GET_TYPE, and TYPMOD_GET_Z.

Referenced by geography_enforce_typmod(), geometry_enforce_typmod(), gserialized_geography_from_lwgeom(), LWGEOM_in(), and LWGEOM_recv().

Here is the call graph for this function:
Here is the caller graph for this function: