PostGIS  2.2.7dev-r@@SVN_REVISION@@
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 104 of file gserialized_typmod.c.

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().

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

Here is the call graph for this function:

Here is the caller graph for this function: