PostGIS  2.1.10dev-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 103 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().

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

Here is the call graph for this function:

Here is the caller graph for this function: