PostGIS  3.3.9dev-r@@SVN_REVISION@@
gserialized_typmod.c
Go to the documentation of this file.
1 /**********************************************************************
2  *
3  * PostGIS - Spatial Types for PostgreSQL
4  * http://postgis.net
5  *
6  * PostGIS is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * PostGIS is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with PostGIS. If not, see <http://www.gnu.org/licenses/>.
18  *
19  **********************************************************************
20  *
21  * Copyright 2009 Paul Ramsey <pramsey@cleverelephant.ca>
22  *
23  **********************************************************************/
24 
25 
26 #include "postgres.h"
27 
28 #include "../postgis_config.h"
29 
30 #include <math.h>
31 #include <float.h>
32 #include <string.h>
33 #include <stdio.h>
34 #include <errno.h>
35 
36 #include "utils/elog.h"
37 #include "utils/array.h"
38 #include "utils/builtins.h" /* for cstring_to_text */
39 #include "lib/stringinfo.h" /* For binary input */
40 #include "catalog/pg_type.h" /* for CSTRINGOID */
41 
42 #include "liblwgeom.h" /* For standard geometry types. */
43 #include "lwgeom_pg.h" /* For debugging macros. */
44 #include "geography.h" /* For utility functions. */
45 #include "lwgeom_transform.h" /* for srid_is_latlon */
46 
47 
48 Datum geography_typmod_in(PG_FUNCTION_ARGS);
49 Datum geometry_typmod_in(PG_FUNCTION_ARGS);
50 Datum postgis_typmod_out(PG_FUNCTION_ARGS);
51 Datum postgis_typmod_dims(PG_FUNCTION_ARGS);
52 Datum postgis_typmod_srid(PG_FUNCTION_ARGS);
53 Datum postgis_typmod_type(PG_FUNCTION_ARGS);
54 Datum geography_enforce_typmod(PG_FUNCTION_ARGS);
55 Datum geometry_enforce_typmod(PG_FUNCTION_ARGS);
56 
57 
58 /*
59 ** postgis_typmod_out(int) returns cstring
60 */
62 Datum postgis_typmod_out(PG_FUNCTION_ARGS)
63 {
64  StringInfoData si;
65  // char *s = (char*)palloc(64);
66  int32 typmod = PG_GETARG_INT32(0);
67  int32 srid = TYPMOD_GET_SRID(typmod);
68  int32 type = TYPMOD_GET_TYPE(typmod);
69  int32 hasz = TYPMOD_GET_Z(typmod);
70  int32 hasm = TYPMOD_GET_M(typmod);
71 
72  POSTGIS_DEBUGF(3, "Got typmod(srid = %d, type = %d, hasz = %d, hasm = %d)", srid, type, hasz, hasm);
73 
74  /* No SRID or type or dimensionality? Then no typmod at all. Return empty string. */
75  if (!(srid || type || hasz || hasm) || typmod < 0)
76  {
77  PG_RETURN_CSTRING(pstrdup(""));
78  }
79 
80  /* Opening bracket. */
81  initStringInfo(&si);
82  appendStringInfoChar(&si, '(');
83 
84  /* Has type? */
85  if (type)
86  appendStringInfo(&si, "%s", lwtype_name(type));
87  else if (srid || hasz || hasm)
88  appendStringInfoString(&si, "Geometry");
89 
90  /* Has Z? */
91  if (hasz) appendStringInfoString(&si, "Z");
92 
93  /* Has M? */
94  if (hasm) appendStringInfoString(&si, "M");
95 
96  /* Has SRID? */
97  if (srid) appendStringInfo(&si, ",%d", srid);
98 
99  /* Closing bracket. */
100  appendStringInfoChar(&si, ')');
101 
102  PG_RETURN_CSTRING(si.data);
103 }
104 
105 
111 {
112  int32 geom_srid = gserialized_get_srid(gser);
113  int32 geom_type = gserialized_get_type(gser);
114  int32 geom_z = gserialized_has_z(gser);
115  int32 geom_m = gserialized_has_m(gser);
116  int32 typmod_srid = TYPMOD_GET_SRID(typmod);
117  int32 typmod_type = TYPMOD_GET_TYPE(typmod);
118  int32 typmod_z = TYPMOD_GET_Z(typmod);
119  int32 typmod_m = TYPMOD_GET_M(typmod);
120 
121  POSTGIS_DEBUG(2, "Entered function");
122 
123  /* No typmod (-1) => no preferences */
124  if (typmod < 0) return gser;
125 
126  POSTGIS_DEBUGF(3, "Got geom(type = %d, srid = %d, hasz = %d, hasm = %d)", geom_type, geom_srid, geom_z, geom_m);
127  POSTGIS_DEBUGF(3, "Got typmod(type = %d, srid = %d, hasz = %d, hasm = %d)", typmod_type, typmod_srid, typmod_z, typmod_m);
128 
129  /*
130  * #3031: If a user is handing us a MULTIPOINT EMPTY but trying to fit it into
131  * a POINT geometry column, there's a strong chance the reason she has
132  * a MULTIPOINT EMPTY because we gave it to her during data dump,
133  * converting the internal POINT EMPTY into a EWKB MULTIPOINT EMPTY
134  * (because EWKB doesn't have a clean way to represent POINT EMPTY).
135  * In such a case, it makes sense to turn the MULTIPOINT EMPTY back into a
136  * point EMPTY, rather than throwing an error.
137  */
138  if ( typmod_type == POINTTYPE && geom_type == MULTIPOINTTYPE &&
139  gserialized_is_empty(gser) )
140  {
141  LWPOINT *empty_point = lwpoint_construct_empty(geom_srid, geom_z, geom_m);
142  geom_type = POINTTYPE;
143  pfree(gser);
144  if ( gserialized_is_geodetic(gser) )
145  gser = geography_serialize(lwpoint_as_lwgeom(empty_point));
146  else
147  gser = geometry_serialize(lwpoint_as_lwgeom(empty_point));
148  }
149 
150  /* Typmod has a preference for SRID, but geometry does not? Harmonize the geometry SRID. */
151  if ( typmod_srid > 0 && geom_srid == 0 )
152  {
153  gserialized_set_srid(gser, typmod_srid);
154  geom_srid = typmod_srid;
155  }
156 
157  /* Typmod has a preference for SRID? Geometry SRID had better match. */
158  if ( typmod_srid > 0 && typmod_srid != geom_srid )
159  {
160  ereport(ERROR, (
161  errcode(ERRCODE_INVALID_PARAMETER_VALUE),
162  errmsg("Geometry SRID (%d) does not match column SRID (%d)", geom_srid, typmod_srid) ));
163  }
164 
165  /* Typmod has a preference for geometry type. */
166  if ( typmod_type > 0 &&
167  /* GEOMETRYCOLLECTION column can hold any kind of collection */
168  ((typmod_type == COLLECTIONTYPE && ! (geom_type == COLLECTIONTYPE ||
169  geom_type == MULTIPOLYGONTYPE ||
170  geom_type == MULTIPOINTTYPE ||
171  geom_type == MULTILINETYPE )) ||
172  /* Other types must be strictly equal. */
173  (typmod_type != geom_type)) )
174  {
175  ereport(ERROR, (
176  errcode(ERRCODE_INVALID_PARAMETER_VALUE),
177  errmsg("Geometry type (%s) does not match column type (%s)", lwtype_name(geom_type), lwtype_name(typmod_type)) ));
178  }
179 
180  /* Mismatched Z dimensionality. */
181  if ( typmod_z && ! geom_z )
182  {
183  ereport(ERROR, (
184  errcode(ERRCODE_INVALID_PARAMETER_VALUE),
185  errmsg("Column has Z dimension but geometry does not" )));
186  }
187 
188  /* Mismatched Z dimensionality (other way). */
189  if ( geom_z && ! typmod_z )
190  {
191  ereport(ERROR, (
192  errcode(ERRCODE_INVALID_PARAMETER_VALUE),
193  errmsg("Geometry has Z dimension but column does not" )));
194  }
195 
196  /* Mismatched M dimensionality. */
197  if ( typmod_m && ! geom_m )
198  {
199  ereport(ERROR, (
200  errcode(ERRCODE_INVALID_PARAMETER_VALUE),
201  errmsg("Column has M dimension but geometry does not" )));
202  }
203 
204  /* Mismatched M dimensionality (other way). */
205  if ( geom_m && ! typmod_m )
206  {
207  ereport(ERROR, (
208  errcode(ERRCODE_INVALID_PARAMETER_VALUE),
209  errmsg("Geometry has M dimension but column does not" )));
210  }
211 
212  return gser;
213 
214 }
215 
216 
217 static uint32 gserialized_typmod_in(ArrayType *arr, int is_geography)
218 {
219  int32 typmod = 0;
220  Datum *elem_values;
221  int n = 0;
222  int i = 0;
223 
224  if (ARR_ELEMTYPE(arr) != CSTRINGOID)
225  ereport(ERROR,
226  (errcode(ERRCODE_ARRAY_ELEMENT_ERROR),
227  errmsg("typmod array must be type cstring[]")));
228 
229  if (ARR_NDIM(arr) != 1)
230  ereport(ERROR,
231  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
232  errmsg("typmod array must be one-dimensional")));
233 
234  if (ARR_HASNULL(arr))
235  ereport(ERROR,
236  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
237  errmsg("typmod array must not contain nulls")));
238 
239  deconstruct_array(arr,
240  CSTRINGOID, -2, false, 'c', /* hardwire cstring representation details */
241  &elem_values, NULL, &n);
242 
243  /* Set the SRID to the default value first */
244  if (is_geography)
245  TYPMOD_SET_SRID(typmod, SRID_DEFAULT);
246  else
247  TYPMOD_SET_SRID(typmod, SRID_UNKNOWN);
248 
249  for (i = 0; i < n; i++)
250  {
251  if ( i == 0 ) /* TYPE */
252  {
253  char *s = DatumGetCString(elem_values[i]);
254  uint8_t type = 0;
255  int z = 0;
256  int m = 0;
257 
258  if ( geometry_type_from_string(s, &type, &z, &m) == LW_FAILURE )
259  {
260  ereport(ERROR,
261  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
262  errmsg("Invalid geometry type modifier: %s", s)));
263  }
264  else
265  {
266  TYPMOD_SET_TYPE(typmod, type);
267  if ( z )
268  TYPMOD_SET_Z(typmod);
269  if ( m )
270  TYPMOD_SET_M(typmod);
271  }
272  }
273  if ( i == 1 ) /* SRID */
274  {
275  char *int_string = DatumGetCString(elem_values[i]);
276  char *endp;
277  long l;
278  int32_t srid;
279 
280  errno = 0;
281  l = strtol(int_string, &endp, 10);
282 
283  if (int_string == endp)
284  ereport(ERROR,
285  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
286  errmsg("invalid input syntax for type %s: \"%s\"",
287  "integer", int_string)));
288 
289  if (errno == ERANGE || l < INT_MIN || l > INT_MAX)
290  ereport(ERROR,
291  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
292  errmsg("value \"%s\" is out of range for type %s", int_string,
293  "integer")));
294 
295  if (*endp != '\0')
296  ereport(ERROR,
297  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
298  errmsg("invalid input syntax for type %s: \"%s\"",
299  "integer", int_string)));
300 
301  srid = clamp_srid(l);
302  POSTGIS_DEBUGF(3, "srid: %d", srid);
303  if ( srid != SRID_UNKNOWN )
304  {
305  TYPMOD_SET_SRID(typmod, srid);
306  }
307  }
308  }
309 
310  pfree(elem_values);
311 
312  return typmod;
313 }
314 
315 /*
316 ** geography_typmod_in(cstring[]) returns int32
317 **
318 ** Modified from ArrayGetIntegerTypmods in PostgreSQL 8.3
319 */
321 Datum geography_typmod_in(PG_FUNCTION_ARGS)
322 {
323  ArrayType *arr = (ArrayType *) DatumGetPointer(PG_GETARG_DATUM(0));
324  int32 typmod = gserialized_typmod_in(arr, LW_TRUE);
325  int32_t srid = TYPMOD_GET_SRID(typmod);
326  /* Check the SRID is legal (geographic coordinates) */
327  srid_check_latlong(srid);
328 
329  PG_RETURN_INT32(typmod);
330 }
331 
332 /*
333 ** geometry_typmod_in(cstring[]) returns int32
334 **
335 ** Modified from ArrayGetIntegerTypmods in PostgreSQL 8.3
336 */
338 Datum geometry_typmod_in(PG_FUNCTION_ARGS)
339 {
340  ArrayType *arr = (ArrayType *) DatumGetPointer(PG_GETARG_DATUM(0));
341  uint32 typmod = gserialized_typmod_in(arr, LW_FALSE); /* Not a geography */;
342  PG_RETURN_INT32(typmod);
343 }
344 
345 /*
346 ** geography_enforce_typmod(*GSERIALIZED, uint32) returns *GSERIALIZED
347 ** Ensure that an incoming geometry conforms to typmod restrictions on
348 ** type, dims and srid.
349 */
351 Datum geography_enforce_typmod(PG_FUNCTION_ARGS)
352 {
353  GSERIALIZED *arg = PG_GETARG_GSERIALIZED_P(0);
354  int32 typmod = PG_GETARG_INT32(1);
355  /* We don't need to have different behavior based on explicitness. */
356  /* bool isExplicit = PG_GETARG_BOOL(2); */
357 
358  /* Check if geometry typmod is consistent with the supplied one. */
359  arg = postgis_valid_typmod(arg, typmod);
360 
361  PG_RETURN_POINTER(arg);
362 }
363 
364 /*
365 ** geometry_enforce_typmod(*GSERIALIZED, uint32) returns *GSERIALIZED
366 ** Ensure that an incoming geometry conforms to typmod restrictions on
367 ** type, dims and srid.
368 */
370 Datum geometry_enforce_typmod(PG_FUNCTION_ARGS)
371 {
372  GSERIALIZED *arg = PG_GETARG_GSERIALIZED_P(0);
373  int32 typmod = PG_GETARG_INT32(1);
374  /* We don't need to have different behavior based on explicitness. */
375  /* bool isExplicit = PG_GETARG_BOOL(2); */
376 
377  /* Check if geometry typmod is consistent with the supplied one. */
378  arg = postgis_valid_typmod(arg, typmod);
379 
380  PG_RETURN_POINTER(arg);
381 }
382 
383 
384 /*
385 ** postgis_typmod_type(uint32) returns cstring
386 ** Used for geometry_columns and other views on system tables
387 */
389 Datum postgis_typmod_type(PG_FUNCTION_ARGS)
390 {
391  int32 typmod = PG_GETARG_INT32(0);
392  int32 type = TYPMOD_GET_TYPE(typmod);
393  char *s = (char*)palloc(64);
394  char *ptr = s;
395  text *stext;
396 
397  /* Has type? */
398  if ( typmod < 0 || type == 0 )
399  ptr += sprintf(ptr, "Geometry");
400  else
401  ptr += sprintf(ptr, "%s", lwtype_name(type));
402 
403  /* Has Z? */
404  if ( typmod >= 0 && TYPMOD_GET_Z(typmod) )
405  ptr += sprintf(ptr, "%s", "Z");
406 
407  /* Has M? */
408  if ( typmod >= 0 && TYPMOD_GET_M(typmod) )
409  ptr += sprintf(ptr, "%s", "M");
410 
411  stext = cstring_to_text(s);
412  pfree(s);
413  PG_RETURN_TEXT_P(stext);
414 }
415 
416 /*
417 ** postgis_typmod_dims(uint32) returns int
418 ** Used for geometry_columns and other views on system tables
419 */
421 Datum postgis_typmod_dims(PG_FUNCTION_ARGS)
422 {
423  int32 typmod = PG_GETARG_INT32(0);
424  int32 dims = 2;
425  if ( typmod < 0 )
426  PG_RETURN_NULL(); /* unconstrained */
427  if ( TYPMOD_GET_Z(typmod) )
428  dims++;
429  if ( TYPMOD_GET_M(typmod) )
430  dims++;
431  PG_RETURN_INT32(dims);
432 }
433 
434 /*
435 ** postgis_typmod_srid(uint32) returns int
436 ** Used for geometry_columns and other views on system tables
437 */
439 Datum postgis_typmod_srid(PG_FUNCTION_ARGS)
440 {
441  int32 typmod = PG_GETARG_INT32(0);
442  if ( typmod < 0 )
443  PG_RETURN_INT32(0);
444  PG_RETURN_INT32(TYPMOD_GET_SRID(typmod));
445 }
446 
char * s
Definition: cu_in_wkt.c:23
int32_t gserialized_get_srid(const GSERIALIZED *g)
Extract the SRID from the serialized form (it is packed into three bytes so this is a handy function)...
Definition: gserialized.c:126
int gserialized_is_geodetic(const GSERIALIZED *g)
Check if a GSERIALIZED is a geography.
Definition: gserialized.c:196
int gserialized_has_m(const GSERIALIZED *g)
Check if a GSERIALIZED has an M ordinate.
Definition: gserialized.c:185
int gserialized_is_empty(const GSERIALIZED *g)
Check if a GSERIALIZED is empty without deserializing first.
Definition: gserialized.c:152
void gserialized_set_srid(GSERIALIZED *g, int32_t srid)
Write the SRID into the serialized form (it is packed into three bytes so this is a handy function).
Definition: gserialized.c:138
int gserialized_has_z(const GSERIALIZED *g)
Check if a GSERIALIZED has a Z ordinate.
Definition: gserialized.c:174
uint32_t gserialized_get_type(const GSERIALIZED *g)
Extract the geometry type from the serialized form (it hides in the anonymous data area,...
Definition: gserialized.c:89
Datum geometry_enforce_typmod(PG_FUNCTION_ARGS)
Datum postgis_typmod_type(PG_FUNCTION_ARGS)
Datum geography_typmod_in(PG_FUNCTION_ARGS)
Datum geometry_typmod_in(PG_FUNCTION_ARGS)
Datum postgis_typmod_dims(PG_FUNCTION_ARGS)
Datum geography_enforce_typmod(PG_FUNCTION_ARGS)
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...
static uint32 gserialized_typmod_in(ArrayType *arr, int is_geography)
Datum postgis_typmod_srid(PG_FUNCTION_ARGS)
Datum postgis_typmod_out(PG_FUNCTION_ARGS)
PG_FUNCTION_INFO_V1(postgis_typmod_out)
LWPOINT * lwpoint_construct_empty(int32_t srid, char hasz, char hasm)
Definition: lwpoint.c:151
#define TYPMOD_GET_SRID(typmod)
Macros for manipulating the 'typemod' int.
Definition: liblwgeom.h:207
#define LW_FALSE
Definition: liblwgeom.h:109
#define COLLECTIONTYPE
Definition: liblwgeom.h:123
#define TYPMOD_SET_SRID(typmod, srid)
Definition: liblwgeom.h:208
#define LW_FAILURE
Definition: liblwgeom.h:111
#define MULTILINETYPE
Definition: liblwgeom.h:121
#define MULTIPOINTTYPE
Definition: liblwgeom.h:120
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:117
int geometry_type_from_string(const char *str, uint8_t *type, int *z, int *m)
Utility function to get type number from string.
Definition: lwutil.c:489
#define TYPMOD_GET_M(typmod)
Definition: liblwgeom.h:213
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:122
#define TYPMOD_SET_TYPE(typmod, type)
Definition: liblwgeom.h:210
#define SRID_DEFAULT
Definition: liblwgeom.h:240
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
Definition: lwgeom.c:344
const char * lwtype_name(uint8_t type)
Return the type name string associated with a type number (e.g.
Definition: lwutil.c:216
#define TYPMOD_GET_TYPE(typmod)
Definition: liblwgeom.h:209
#define TYPMOD_SET_M(typmod)
Definition: liblwgeom.h:214
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:108
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:230
#define TYPMOD_SET_Z(typmod)
Definition: liblwgeom.h:212
#define TYPMOD_GET_Z(typmod)
Definition: liblwgeom.h:211
int32_t clamp_srid(int32_t srid)
Return a valid SRID from an arbitrary integer Raises a notice if what comes out is different from wha...
Definition: lwutil.c:333
This library is the generic geometry handling section of PostGIS.
type
Definition: ovdump.py:42
unsigned int int32
Definition: shpopen.c:54