PostGIS  3.0.6dev-r@@SVN_REVISION@@
lwgeom_inout.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^
22  *
23  **********************************************************************/
24 
25 #include "postgres.h"
26 
27 #include "../postgis_config.h"
28 
29 #include <math.h>
30 #include <float.h>
31 #include <string.h>
32 #include <stdio.h>
33 #include <assert.h>
34 
35 #include "access/gist.h"
36 #include "access/itup.h"
37 
38 #include "fmgr.h"
39 #include "utils/elog.h"
40 #include "mb/pg_wchar.h"
41 #include "lib/stringinfo.h" /* for binary input */
42 #include "utils/array.h"
43 #include "utils/builtins.h"
44 #include "utils/lsyscache.h"
45 #include "funcapi.h"
46 
47 #include "liblwgeom.h"
48 #include "lwgeom_pg.h"
49 #include "geography.h" /* for lwgeom_valid_typmod */
50 #include "lwgeom_transform.h"
51 
52 
53 #include "access/htup_details.h"
54 
55 
56 void elog_ERROR(const char* string);
57 
58 Datum LWGEOM_in(PG_FUNCTION_ARGS);
59 Datum LWGEOM_out(PG_FUNCTION_ARGS);
60 Datum LWGEOM_to_text(PG_FUNCTION_ARGS);
61 Datum LWGEOM_to_bytea(PG_FUNCTION_ARGS);
62 Datum LWGEOM_from_bytea(PG_FUNCTION_ARGS);
63 Datum LWGEOM_asHEXEWKB(PG_FUNCTION_ARGS);
64 Datum parse_WKT_lwgeom(PG_FUNCTION_ARGS);
65 Datum LWGEOM_recv(PG_FUNCTION_ARGS);
66 Datum LWGEOM_send(PG_FUNCTION_ARGS);
67 Datum LWGEOM_to_latlon(PG_FUNCTION_ARGS);
68 Datum WKBFromLWGEOM(PG_FUNCTION_ARGS);
69 Datum TWKBFromLWGEOM(PG_FUNCTION_ARGS);
70 Datum TWKBFromLWGEOMArray(PG_FUNCTION_ARGS);
71 Datum LWGEOMFromTWKB(PG_FUNCTION_ARGS);
72 
73 
74 /*
75  * LWGEOM_in(cstring)
76  * format is '[SRID=#;]wkt|wkb'
77  * LWGEOM_in( 'SRID=99;POINT(0 0)')
78  * LWGEOM_in( 'POINT(0 0)') --> assumes SRID=SRID_UNKNOWN
79  * LWGEOM_in( 'SRID=99;0101000000000000000000F03F000000000000004')
80  * LWGEOM_in( '0101000000000000000000F03F000000000000004')
81  * returns a GSERIALIZED object
82  */
84 Datum LWGEOM_in(PG_FUNCTION_ARGS)
85 {
86  char *input = PG_GETARG_CSTRING(0);
87  int32 geom_typmod = -1;
88  char *str = input;
89  LWGEOM_PARSER_RESULT lwg_parser_result;
90  LWGEOM *lwgeom;
91  GSERIALIZED *ret;
92  int32_t srid = 0;
93 
94  if ( (PG_NARGS()>2) && (!PG_ARGISNULL(2)) ) {
95  geom_typmod = PG_GETARG_INT32(2);
96  }
97 
98  lwgeom_parser_result_init(&lwg_parser_result);
99 
100  /* Empty string. */
101  if ( str[0] == '\0' ) {
102  ereport(ERROR,(errmsg("parse error - invalid geometry")));
103  PG_RETURN_NULL();
104  }
105 
106  /* Starts with "SRID=" */
107  if( strncasecmp(str,"SRID=",5) == 0 )
108  {
109  /* Roll forward to semi-colon */
110  char *tmp = str;
111  while ( tmp && *tmp != ';' )
112  tmp++;
113 
114  /* Check next character to see if we have WKB */
115  if ( tmp && *(tmp+1) == '0' )
116  {
117  /* Null terminate the SRID= string */
118  *tmp = '\0';
119  /* Set str to the start of the real WKB */
120  str = tmp + 1;
121  /* Move tmp to the start of the numeric part */
122  tmp = input + 5;
123  /* Parse out the SRID number */
124  srid = atoi(tmp);
125  }
126  }
127 
128  /* WKB? Let's find out. */
129  if ( str[0] == '0' )
130  {
131  size_t hexsize = strlen(str);
132  unsigned char *wkb = bytes_from_hexbytes(str, hexsize);
133  /* TODO: 20101206: No parser checks! This is inline with current 1.5 behavior, but needs discussion */
134  lwgeom = lwgeom_from_wkb(wkb, hexsize/2, LW_PARSER_CHECK_NONE);
135  /* If we picked up an SRID at the head of the WKB set it manually */
136  if ( srid ) lwgeom_set_srid(lwgeom, srid);
137  /* Add a bbox if necessary */
138  if ( lwgeom_needs_bbox(lwgeom) ) lwgeom_add_bbox(lwgeom);
139  lwfree(wkb);
140  ret = geometry_serialize(lwgeom);
141  lwgeom_free(lwgeom);
142  }
143  /* WKT then. */
144  else
145  {
146  if ( lwgeom_parse_wkt(&lwg_parser_result, str, LW_PARSER_CHECK_ALL) == LW_FAILURE )
147  {
148  PG_PARSER_ERROR(lwg_parser_result);
149  PG_RETURN_NULL();
150  }
151  lwgeom = lwg_parser_result.geom;
152  if ( lwgeom_needs_bbox(lwgeom) )
153  lwgeom_add_bbox(lwgeom);
154  ret = geometry_serialize(lwgeom);
155  lwgeom_parser_result_free(&lwg_parser_result);
156  }
157 
158  if ( geom_typmod >= 0 )
159  {
160  ret = postgis_valid_typmod(ret, geom_typmod);
161  POSTGIS_DEBUG(3, "typmod and geometry were consistent");
162  }
163  else
164  {
165  POSTGIS_DEBUG(3, "typmod was -1");
166  }
167 
168  /* Don't free the parser result (and hence lwgeom) until we have done */
169  /* the typemod check with lwgeom */
170 
171  PG_RETURN_POINTER(ret);
172 
173 }
174 
175 /*
176  * LWGEOM_to_latlon(GEOMETRY, text)
177  * NOTE: Geometry must be a point. It is assumed that the coordinates
178  * of the point are in a lat/lon projection, and they will be
179  * normalized in the output to -90-90 and -180-180.
180  *
181  * The text parameter is a format string containing the format for the
182  * resulting text, similar to a date format string. Valid tokens
183  * are "D" for degrees, "M" for minutes, "S" for seconds, and "C" for
184  * cardinal direction (NSEW). DMS tokens may be repeated to indicate
185  * desired width and precision ("SSS.SSSS" means " 1.0023").
186  * "M", "S", and "C" are optional. If "C" is omitted, degrees are
187  * shown with a "-" sign if south or west. If "S" is omitted,
188  * minutes will be shown as decimal with as many digits of precision
189  * as you specify. If "M" is omitted, degrees are shown as decimal
190  * with as many digits precision as you specify.
191  *
192  * If the format string is omitted (null or 0-length) a default
193  * format will be used.
194  *
195  * returns text
196  */
198 Datum LWGEOM_to_latlon(PG_FUNCTION_ARGS)
199 {
200  /* Get the parameters */
201  GSERIALIZED *pg_lwgeom = PG_GETARG_GSERIALIZED_P(0);
202  text *format_text = PG_GETARG_TEXT_P(1);
203 
204  LWGEOM *lwgeom;
205  char *format_str = NULL;
206 
207  char * formatted_str;
208  text * formatted_text;
209  char * tmp;
210 
211  /* Only supports points. */
212  uint8_t geom_type = gserialized_get_type(pg_lwgeom);
213  if (POINTTYPE != geom_type)
214  {
215  lwpgerror("Only points are supported, you tried type %s.", lwtype_name(geom_type));
216  }
217  /* Convert to LWGEOM type */
218  lwgeom = lwgeom_from_gserialized(pg_lwgeom);
219 
220  if (format_text == NULL) {
221  lwpgerror("ST_AsLatLonText: invalid format string (null");
222  PG_RETURN_NULL();
223  }
224 
225  if (!lwgeom_isfinite(lwgeom)) {
226  lwpgerror("ST_AsLatLonText: invalid coordinate");
227  PG_RETURN_NULL();
228  }
229 
230  format_str = text_to_cstring(format_text);
231  assert(format_str != NULL);
232 
233  /* The input string supposedly will be in the database encoding,
234  so convert to UTF-8. */
235  tmp = (char *)pg_do_encoding_conversion(
236  (uint8_t *)format_str, strlen(format_str), GetDatabaseEncoding(), PG_UTF8);
237  assert(tmp != NULL);
238  if ( tmp != format_str ) {
239  pfree(format_str);
240  format_str = tmp;
241  }
242 
243  /* Produce the formatted string. */
244  formatted_str = lwpoint_to_latlon((LWPOINT *)lwgeom, format_str);
245  assert(formatted_str != NULL);
246  pfree(format_str);
247 
248  /* Convert the formatted string from UTF-8 back to database encoding. */
249  tmp = (char *)pg_do_encoding_conversion(
250  (uint8_t *)formatted_str, strlen(formatted_str),
251  PG_UTF8, GetDatabaseEncoding());
252  assert(tmp != NULL);
253  if ( tmp != formatted_str) {
254  pfree(formatted_str);
255  formatted_str = tmp;
256  }
257 
258  /* Convert to the postgres output string type. */
259  formatted_text = cstring_to_text(formatted_str);
260  pfree(formatted_str);
261 
262  PG_RETURN_POINTER(formatted_text);
263 }
264 
265 /*
266  * LWGEOM_out(lwgeom) --> cstring
267  * output is 'SRID=#;<wkb in hex form>'
268  * ie. 'SRID=-99;0101000000000000000000F03F0000000000000040'
269  * WKB is machine endian
270  * if SRID=-1, the 'SRID=-1;' will probably not be present.
271  */
273 Datum LWGEOM_out(PG_FUNCTION_ARGS)
274 {
275  GSERIALIZED *geom = PG_GETARG_GSERIALIZED_P(0);
276  LWGEOM *lwgeom;
277  char *hexwkb;
278  size_t hexwkb_size;
279 
280  lwgeom = lwgeom_from_gserialized(geom);
281  hexwkb = lwgeom_to_hexwkb(lwgeom, WKB_EXTENDED, &hexwkb_size);
282  lwgeom_free(lwgeom);
283 
284  PG_RETURN_CSTRING(hexwkb);
285 }
286 
287 /*
288  * AsHEXEWKB(geom, string)
289  */
291 Datum LWGEOM_asHEXEWKB(PG_FUNCTION_ARGS)
292 {
293  GSERIALIZED *geom = PG_GETARG_GSERIALIZED_P(0);
294  LWGEOM *lwgeom;
295  char *hexwkb;
296  size_t hexwkb_size;
297  uint8_t variant = 0;
298  text *result;
299  text *type;
300  size_t text_size;
301 
302  /* If user specified endianness, respect it */
303  if ( (PG_NARGS()>1) && (!PG_ARGISNULL(1)) )
304  {
305  type = PG_GETARG_TEXT_P(1);
306 
307  if ( ! strncmp(VARDATA(type), "xdr", 3) ||
308  ! strncmp(VARDATA(type), "XDR", 3) )
309  {
310  variant = variant | WKB_XDR;
311  }
312  else
313  {
314  variant = variant | WKB_NDR;
315  }
316  }
317 
318  /* Create WKB hex string */
319  lwgeom = lwgeom_from_gserialized(geom);
320  hexwkb = lwgeom_to_hexwkb(lwgeom, variant | WKB_EXTENDED, &hexwkb_size);
321  lwgeom_free(lwgeom);
322 
323  /* Prepare the PgSQL text return type */
324  text_size = hexwkb_size - 1 + VARHDRSZ;
325  result = palloc(text_size);
326  memcpy(VARDATA(result), hexwkb, hexwkb_size - 1);
327  SET_VARSIZE(result, text_size);
328 
329  /* Clean up and return */
330  lwfree(hexwkb);
331  PG_FREE_IF_COPY(geom, 0);
332  PG_RETURN_TEXT_P(result);
333 }
334 
335 
336 /*
337  * LWGEOM_to_text(lwgeom) --> text
338  * output is 'SRID=#;<wkb in hex form>'
339  * ie. 'SRID=-99;0101000000000000000000F03F0000000000000040'
340  * WKB is machine endian
341  * if SRID=-1, the 'SRID=-1;' will probably not be present.
342  */
344 Datum LWGEOM_to_text(PG_FUNCTION_ARGS)
345 {
346  GSERIALIZED *geom = PG_GETARG_GSERIALIZED_P(0);
347  LWGEOM *lwgeom;
348  char *hexwkb;
349  size_t hexwkb_size;
350  text *result;
351 
352  /* Generate WKB hex text */
353  lwgeom = lwgeom_from_gserialized(geom);
354  hexwkb = lwgeom_to_hexwkb(lwgeom, WKB_EXTENDED, &hexwkb_size);
355  lwgeom_free(lwgeom);
356 
357  /* Copy into text obect */
358  result = cstring_to_text(hexwkb);
359  lwfree(hexwkb);
360 
361  /* Clean up and return */
362  PG_FREE_IF_COPY(geom, 0);
363  PG_RETURN_TEXT_P(result);
364 }
365 
366 /*
367  * LWGEOMFromEWKB(wkb, [SRID] )
368  * NOTE: wkb is in *binary* not hex form.
369  *
370  * NOTE: this function parses EWKB (extended form)
371  * which also contains SRID info.
372  */
374 Datum LWGEOMFromEWKB(PG_FUNCTION_ARGS)
375 {
376  bytea *bytea_wkb = PG_GETARG_BYTEA_P(0);
377  GSERIALIZED *geom;
378  LWGEOM *lwgeom;
379  uint8_t *wkb = (uint8_t*)VARDATA(bytea_wkb);
380 
381  lwgeom = lwgeom_from_wkb(wkb, VARSIZE_ANY_EXHDR(bytea_wkb), LW_PARSER_CHECK_ALL);
382  if (!lwgeom)
383  lwpgerror("Unable to parse WKB");
384 
385  if ((PG_NARGS() > 1) && (!PG_ARGISNULL(1)))
386  {
387  int32 srid = PG_GETARG_INT32(1);
388  lwgeom_set_srid(lwgeom, srid);
389  }
390 
391  if ( lwgeom_needs_bbox(lwgeom) )
392  lwgeom_add_bbox(lwgeom);
393 
394  geom = geometry_serialize(lwgeom);
395  lwgeom_free(lwgeom);
396  PG_FREE_IF_COPY(bytea_wkb, 0);
397  PG_RETURN_POINTER(geom);
398 }
399 /*
400  * LWGEOMFromTWKB(wkb)
401  * NOTE: twkb is in *binary* not hex form.
402  *
403  */
405 Datum LWGEOMFromTWKB(PG_FUNCTION_ARGS)
406 {
407  bytea *bytea_twkb = PG_GETARG_BYTEA_P(0);
408  GSERIALIZED *geom;
409  LWGEOM *lwgeom;
410  uint8_t *twkb = (uint8_t*)VARDATA(bytea_twkb);
411 
412  lwgeom = lwgeom_from_twkb(twkb, VARSIZE_ANY_EXHDR(bytea_twkb), LW_PARSER_CHECK_ALL);
413 
414  if (lwgeom_needs_bbox(lwgeom))
415  lwgeom_add_bbox(lwgeom);
416 
417  geom = geometry_serialize(lwgeom);
418  lwgeom_free(lwgeom);
419  PG_FREE_IF_COPY(bytea_twkb, 0);
420  PG_RETURN_POINTER(geom);
421 }
422 
423 /*
424  * WKBFromLWGEOM(lwgeom) --> wkb
425  * this will have no 'SRID=#;'
426  */
428 Datum WKBFromLWGEOM(PG_FUNCTION_ARGS)
429 {
430  GSERIALIZED *geom = PG_GETARG_GSERIALIZED_P(0);
431  LWGEOM *lwgeom;
432  uint8_t *wkb;
433  size_t wkb_size;
434  uint8_t variant = 0;
435  bytea *result;
436  text *type;
437  /* If user specified endianness, respect it */
438  if ( (PG_NARGS()>1) && (!PG_ARGISNULL(1)) )
439  {
440  type = PG_GETARG_TEXT_P(1);
441 
442  if ( ! strncmp(VARDATA(type), "xdr", 3) ||
443  ! strncmp(VARDATA(type), "XDR", 3) )
444  {
445  variant = variant | WKB_XDR;
446  }
447  else
448  {
449  variant = variant | WKB_NDR;
450  }
451  }
452  wkb_size= VARSIZE_ANY_EXHDR(geom);
453  /* Create WKB hex string */
454  lwgeom = lwgeom_from_gserialized(geom);
455 
456  wkb = lwgeom_to_wkb(lwgeom, variant | WKB_EXTENDED , &wkb_size);
457  lwgeom_free(lwgeom);
458 
459  /* Prepare the PgSQL text return type */
460  result = palloc(wkb_size + VARHDRSZ);
461  memcpy(VARDATA(result), wkb, wkb_size);
462  SET_VARSIZE(result, wkb_size+VARHDRSZ);
463 
464  /* Clean up and return */
465  lwfree(wkb);
466  PG_FREE_IF_COPY(geom, 0);
467  PG_RETURN_BYTEA_P(result);
468 }
469 
471 Datum TWKBFromLWGEOM(PG_FUNCTION_ARGS)
472 {
473  GSERIALIZED *geom;
474  LWGEOM *lwgeom;
475  uint8_t *twkb;
476  size_t twkb_size;
477  uint8_t variant = 0;
478  bytea *result;
479  srs_precision sp;
480 
481  /*check for null input since we cannot have the sql-function as strict.
482  That is because we use null as default for optional ID*/
483  if ( PG_ARGISNULL(0) ) PG_RETURN_NULL();
484 
485  geom = PG_GETARG_GSERIALIZED_P(0);
486 
487  /* Read sensible precision defaults (about one meter) given the srs */
488  sp = srid_axis_precision(fcinfo, gserialized_get_srid(geom), TWKB_DEFAULT_PRECISION);
489 
490  /* If user specified XY precision, use it */
491  if ( PG_NARGS() > 1 && ! PG_ARGISNULL(1) )
492  sp.precision_xy = PG_GETARG_INT32(1);
493 
494  /* If user specified Z precision, use it */
495  if ( PG_NARGS() > 2 && ! PG_ARGISNULL(2) )
496  sp.precision_z = PG_GETARG_INT32(2);
497 
498  /* If user specified M precision, use it */
499  if ( PG_NARGS() > 3 && ! PG_ARGISNULL(3) )
500  sp.precision_m = PG_GETARG_INT32(3);
501 
502  /* We don't permit ids for single geoemtries */
503  variant = variant & ~TWKB_ID;
504 
505  /* If user wants registered twkb sizes */
506  if ( PG_NARGS() > 4 && ! PG_ARGISNULL(4) && PG_GETARG_BOOL(4) )
507  variant |= TWKB_SIZE;
508 
509  /* If user wants bounding boxes */
510  if ( PG_NARGS() > 5 && ! PG_ARGISNULL(5) && PG_GETARG_BOOL(5) )
511  variant |= TWKB_BBOX;
512 
513  /* Create TWKB binary string */
514  lwgeom = lwgeom_from_gserialized(geom);
515  twkb = lwgeom_to_twkb(lwgeom, variant, sp.precision_xy, sp.precision_z, sp.precision_m, &twkb_size);
516 
517  /* Prepare the PgSQL text return type */
518  result = palloc(twkb_size + VARHDRSZ);
519  memcpy(VARDATA(result), twkb, twkb_size);
520  SET_VARSIZE(result, twkb_size + VARHDRSZ);
521 
522  PG_RETURN_BYTEA_P(result);
523 }
524 
525 
527 Datum TWKBFromLWGEOMArray(PG_FUNCTION_ARGS)
528 {
529  ArrayType *arr_geoms = NULL;
530  ArrayType *arr_ids = NULL;
531  int num_geoms, num_ids, i = 0;
532 
533  ArrayIterator iter_geoms, iter_ids;
534  bool null_geom, null_id;
535  Datum val_geom, val_id;
536 
537  int is_homogeneous = true;
538  uint32_t subtype = 0;
539  int has_z = 0;
540  int has_m = 0;
541  LWCOLLECTION *col = NULL;
542  int64_t *idlist = NULL;
543  uint8_t variant = 0;
544 
545  srs_precision sp;
546  uint8_t *twkb;
547  size_t twkb_size;
548  bytea *result;
549 
550  /* The first two arguments are required */
551  if ( PG_NARGS() < 2 || PG_ARGISNULL(0) || PG_ARGISNULL(1) )
552  PG_RETURN_NULL();
553 
554  arr_geoms = PG_GETARG_ARRAYTYPE_P(0);
555  arr_ids = PG_GETARG_ARRAYTYPE_P(1);
556 
557  num_geoms = ArrayGetNItems(ARR_NDIM(arr_geoms), ARR_DIMS(arr_geoms));
558  num_ids = ArrayGetNItems(ARR_NDIM(arr_ids), ARR_DIMS(arr_ids));
559 
560  if ( num_geoms != num_ids )
561  {
562  elog(ERROR, "size of geometry[] and integer[] arrays must match");
563  PG_RETURN_NULL();
564  }
565 
566  /* Loop through array and build a collection of geometry and */
567  /* a simple array of ids. If either side is NULL, skip it */
568 
569  iter_geoms = array_create_iterator(arr_geoms, 0, NULL);
570  iter_ids = array_create_iterator(arr_ids, 0, NULL);
571 
572  while( array_iterate(iter_geoms, &val_geom, &null_geom) &&
573  array_iterate(iter_ids, &val_id, &null_id) )
574  {
575  LWGEOM *geom;
576  int32_t uid;
577 
578  if ( null_geom || null_id )
579  {
580  elog(NOTICE, "ST_AsTWKB skipping NULL entry at position %d", i);
581  continue;
582  }
583 
584  geom = lwgeom_from_gserialized((GSERIALIZED*)DatumGetPointer(val_geom));
585  uid = DatumGetInt64(val_id);
586 
587  /* Construct collection/idlist first time through */
588  if ( ! col )
589  {
590  has_z = lwgeom_has_z(geom);
591  has_m = lwgeom_has_m(geom);
592  col = lwcollection_construct_empty(COLLECTIONTYPE, lwgeom_get_srid(geom), has_z, has_m);
593  }
594  if ( ! idlist )
595  idlist = palloc0(num_geoms * sizeof(int64_t));
596 
597 
598  /* Check if there is differences in dimensionality*/
599  if( lwgeom_has_z(geom)!=has_z || lwgeom_has_m(geom)!=has_m)
600  {
601  elog(ERROR, "Geometries have different dimensionality");
602  PG_FREE_IF_COPY(arr_geoms, 0);
603  PG_FREE_IF_COPY(arr_ids, 1);
604  PG_RETURN_NULL();
605  }
606  /* Store the values */
607  lwcollection_add_lwgeom(col, geom);
608  idlist[i++] = uid;
609 
610  /* Grab the geometry type and note if all geometries share it */
611  /* If so, we can make this a homogeneous collection and save some space */
612  if ( lwgeom_get_type(geom) != subtype && subtype )
613  {
614  is_homogeneous = false;
615  }
616  else
617  {
618  subtype = lwgeom_get_type(geom);
619  }
620 
621  }
622  array_free_iterator(iter_geoms);
623  array_free_iterator(iter_ids);
624 
625  if(i==0)
626  {
627  elog(NOTICE, "No valid geometry - id pairs found");
628  PG_FREE_IF_COPY(arr_geoms, 0);
629  PG_FREE_IF_COPY(arr_ids, 1);
630  PG_RETURN_NULL();
631  }
632  if ( is_homogeneous )
633  {
634  col->type = lwtype_get_collectiontype(subtype);
635  }
636 
637  /* Read sensible precision defaults (about one meter) given the srs */
638  sp = srid_axis_precision(fcinfo, lwgeom_get_srid(lwcollection_as_lwgeom(col)), TWKB_DEFAULT_PRECISION);
639 
640  /* If user specified XY precision, use it */
641  if ( PG_NARGS() > 2 && ! PG_ARGISNULL(2) )
642  sp.precision_xy = PG_GETARG_INT32(2);
643 
644  /* If user specified Z precision, use it */
645  if ( PG_NARGS() > 3 && ! PG_ARGISNULL(3) )
646  sp.precision_z = PG_GETARG_INT32(3);
647 
648  /* If user specified M precision, use it */
649  if ( PG_NARGS() > 4 && ! PG_ARGISNULL(4) )
650  sp.precision_m = PG_GETARG_INT32(4);
651 
652  /* We are building an ID'ed output */
653  variant = TWKB_ID;
654 
655  /* If user wants registered twkb sizes */
656  if ( PG_NARGS() > 5 && ! PG_ARGISNULL(5) && PG_GETARG_BOOL(5) )
657  variant |= TWKB_SIZE;
658 
659  /* If user wants bounding boxes */
660  if ( PG_NARGS() > 6 && ! PG_ARGISNULL(6) && PG_GETARG_BOOL(6) )
661  variant |= TWKB_BBOX;
662 
663  /* Write out the TWKB */
665  idlist, variant,
666  sp.precision_xy, sp.precision_z, sp.precision_m,
667  &twkb_size);
668 
669  /* Convert to a bytea return type */
670  result = palloc(twkb_size + VARHDRSZ);
671  memcpy(VARDATA(result), twkb, twkb_size);
672  SET_VARSIZE(result, twkb_size + VARHDRSZ);
673 
674  /* Clean up */
675  pfree(twkb);
676  pfree(idlist);
677  lwcollection_free(col);
678  PG_FREE_IF_COPY(arr_geoms, 0);
679  PG_FREE_IF_COPY(arr_ids, 1);
680 
681  PG_RETURN_BYTEA_P(result);
682 }
683 
684 
685 /* puts a bbox inside the geometry */
687 Datum LWGEOM_addBBOX(PG_FUNCTION_ARGS)
688 {
689  GSERIALIZED *geom = PG_GETARG_GSERIALIZED_P(0);
690  GSERIALIZED *result;
691  LWGEOM *lwgeom;
692 
693  lwgeom = lwgeom_from_gserialized(geom);
694  lwgeom_add_bbox(lwgeom);
695  result = geometry_serialize(lwgeom);
696 
697  PG_FREE_IF_COPY(geom, 0);
698  PG_RETURN_POINTER(result);
699 }
700 
701 /* removes a bbox from a geometry */
703 Datum LWGEOM_dropBBOX(PG_FUNCTION_ARGS)
704 {
705  GSERIALIZED *geom = PG_GETARG_GSERIALIZED_P(0);
706 
707  /* No box? we're done already! */
708  if ( ! gserialized_has_bbox(geom) )
709  PG_RETURN_POINTER(geom);
710 
711  PG_RETURN_POINTER(gserialized_drop_gbox(geom));
712 }
713 
714 
715 /* for the wkt parser */
716 void elog_ERROR(const char* string)
717 {
718  elog(ERROR, "%s", string);
719 }
720 
721 /*
722 * This just does the same thing as the _in function,
723 * except it has to handle a 'text' input. First
724 * unwrap the text into a cstring, then call
725 * geometry_in
726 */
728 Datum parse_WKT_lwgeom(PG_FUNCTION_ARGS)
729 {
730  text *wkt_text = PG_GETARG_TEXT_P(0);
731  char *wkt;
732  Datum result;
733 
734  /* Unwrap the PgSQL text type into a cstring */
735  wkt = text_to_cstring(wkt_text);
736 
737  /* Now we call over to the geometry_in function */
738  result = DirectFunctionCall1(LWGEOM_in, CStringGetDatum(wkt));
739 
740  /* Return null on null */
741  if ( ! result )
742  PG_RETURN_NULL();
743 
744  PG_RETURN_DATUM(result);
745 }
746 
747 
748 /*
749  * This function must advance the StringInfo.cursor pointer
750  * and leave it at the end of StringInfo.buf. If it fails
751  * to do so the backend will raise an exception with message:
752  * ERROR: incorrect binary data format in bind parameter #
753  *
754  */
756 Datum LWGEOM_recv(PG_FUNCTION_ARGS)
757 {
758  StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
759  int32 geom_typmod = -1;
760  GSERIALIZED *geom;
761  LWGEOM *lwgeom;
762 
763  if ( (PG_NARGS()>2) && (!PG_ARGISNULL(2)) ) {
764  geom_typmod = PG_GETARG_INT32(2);
765  }
766 
767  lwgeom = lwgeom_from_wkb((uint8_t*)buf->data, buf->len, LW_PARSER_CHECK_ALL);
768 
769  if ( lwgeom_needs_bbox(lwgeom) )
770  lwgeom_add_bbox(lwgeom);
771 
772  /* Set cursor to the end of buffer (so the backend is happy) */
773  buf->cursor = buf->len;
774 
775  geom = geometry_serialize(lwgeom);
776  lwgeom_free(lwgeom);
777 
778  if ( geom_typmod >= 0 )
779  {
780  geom = postgis_valid_typmod(geom, geom_typmod);
781  POSTGIS_DEBUG(3, "typmod and geometry were consistent");
782  }
783  else
784  {
785  POSTGIS_DEBUG(3, "typmod was -1");
786  }
787 
788 
789  PG_RETURN_POINTER(geom);
790 }
791 
792 
793 
795 Datum LWGEOM_send(PG_FUNCTION_ARGS)
796 {
797  POSTGIS_DEBUG(2, "LWGEOM_send called");
798 
799  PG_RETURN_POINTER(
800  DatumGetPointer(
801  DirectFunctionCall1(
803  PG_GETARG_DATUM(0)
804  )));
805 }
806 
808 Datum LWGEOM_to_bytea(PG_FUNCTION_ARGS)
809 {
810  POSTGIS_DEBUG(2, "LWGEOM_to_bytea called");
811 
812  PG_RETURN_POINTER(
813  DatumGetPointer(
814  DirectFunctionCall1(
816  PG_GETARG_DATUM(0)
817  )));
818 }
819 
821 Datum LWGEOM_from_bytea(PG_FUNCTION_ARGS)
822 {
823  GSERIALIZED *result;
824 
825  POSTGIS_DEBUG(2, "LWGEOM_from_bytea start");
826 
827  result = (GSERIALIZED *)DatumGetPointer(DirectFunctionCall1(
828  LWGEOMFromEWKB, PG_GETARG_DATUM(0)));
829 
830  PG_RETURN_POINTER(result);
831 }
832 
static uint8_t variant
Definition: cu_in_twkb.c:26
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...
int gserialized_has_bbox(const GSERIALIZED *g)
Check if a GSERIALIZED has a bounding box without deserializing first.
Definition: gserialized.c:163
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
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
Definition: gserialized.c:239
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
GSERIALIZED * gserialized_drop_gbox(GSERIALIZED *g)
Remove the bounding box from a GSERIALIZED.
Definition: gserialized.c:52
#define LW_PARSER_CHECK_ALL
Definition: liblwgeom.h:2061
LWGEOM * lwcollection_as_lwgeom(const LWCOLLECTION *obj)
Definition: lwgeom.c:291
uint32_t lwtype_get_collectiontype(uint8_t type)
Given an lwtype number, what homogeneous collection can hold it?
Definition: lwgeom.c:1114
#define COLLECTIONTYPE
Definition: liblwgeom.h:122
int32_t lwgeom_get_srid(const LWGEOM *geom)
Return SRID number.
Definition: lwgeom.c:909
uint8_t * lwgeom_to_twkb_with_idlist(const LWGEOM *geom, int64_t *idlist, uint8_t variant, int8_t precision_xy, int8_t precision_z, int8_t precision_m, size_t *twkb_size)
Convert LWGEOM to a char* in TWKB format.
Definition: lwout_twkb.c:589
void lwgeom_set_srid(LWGEOM *geom, int32_t srid)
Set the SRID on an LWGEOM For collections, only the parent gets an SRID, all the children get SRID_UN...
Definition: lwgeom.c:1530
#define LW_FAILURE
Definition: liblwgeom.h:110
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1138
#define LW_PARSER_CHECK_NONE
Definition: liblwgeom.h:2060
LWGEOM * lwgeom_from_twkb(const uint8_t *twkb, size_t twkb_size, char check)
WKB inputs must have a declared size, to prevent malformed WKB from reading off the end of the memory...
Definition: lwin_twkb.c:654
void lwgeom_parser_result_init(LWGEOM_PARSER_RESULT *parser_result)
Definition: lwin_wkt.c:880
int lwgeom_parse_wkt(LWGEOM_PARSER_RESULT *parser_result, char *wktstr, int parse_flags)
Parse a WKT geometry string into an LWGEOM structure.
uint8_t * lwgeom_to_wkb(const LWGEOM *geom, uint8_t variant, size_t *size_out)
Convert LWGEOM to a char* in WKB format.
Definition: lwout_wkb.c:790
uint8_t * bytes_from_hexbytes(const char *hexbuf, size_t hexsize)
Definition: lwin_wkb.c:92
int lwgeom_needs_bbox(const LWGEOM *geom)
Check whether or not a lwgeom is big enough to warrant a bounding box.
Definition: lwgeom.c:1191
int lwgeom_has_z(const LWGEOM *geom)
Return LW_TRUE if geometry has Z ordinates.
Definition: lwgeom.c:916
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:116
int lwgeom_isfinite(const LWGEOM *lwgeom)
Check if a LWGEOM has any non-finite (NaN or Inf) coordinates.
Definition: lwgeom.c:2529
void lwfree(void *mem)
Definition: lwutil.c:242
#define TWKB_ID
Definition: liblwgeom.h:2140
#define WKB_EXTENDED
Definition: liblwgeom.h:2123
char * lwgeom_to_hexwkb(const LWGEOM *geom, uint8_t variant, size_t *size_out)
Definition: lwout_wkb.c:874
LWCOLLECTION * lwcollection_construct_empty(uint8_t type, int32_t srid, char hasz, char hasm)
Definition: lwcollection.c:92
char * lwpoint_to_latlon(const LWPOINT *p, const char *format)
Definition: lwprint.c:428
const char * lwtype_name(uint8_t type)
Return the type name string associated with a type number (e.g.
Definition: lwutil.c:216
#define WKB_NDR
Definition: liblwgeom.h:2124
void lwcollection_free(LWCOLLECTION *col)
Definition: lwcollection.c:357
#define TWKB_DEFAULT_PRECISION
Definition: liblwgeom.h:2143
#define TWKB_SIZE
Definition: liblwgeom.h:2139
LWCOLLECTION * lwcollection_add_lwgeom(LWCOLLECTION *col, const LWGEOM *geom)
Appends geom to the collection managed by col.
Definition: lwcollection.c:188
LWGEOM * lwgeom_from_wkb(const uint8_t *wkb, const size_t wkb_size, const char check)
WKB inputs must have a declared size, to prevent malformed WKB from reading off the end of the memory...
Definition: lwin_wkb.c:825
int lwgeom_has_m(const LWGEOM *geom)
Return LW_TRUE if geometry has M ordinates.
Definition: lwgeom.c:923
void lwgeom_add_bbox(LWGEOM *lwgeom)
Compute a bbox if not already computed.
Definition: lwgeom.c:677
void lwgeom_parser_result_free(LWGEOM_PARSER_RESULT *parser_result)
Definition: lwin_wkt.c:886
#define TWKB_BBOX
Definition: liblwgeom.h:2138
#define WKB_XDR
Definition: liblwgeom.h:2125
uint8_t * lwgeom_to_twkb(const LWGEOM *geom, uint8_t variant, int8_t precision_xy, int8_t precision_z, int8_t precision_m, size_t *twkb_size)
Definition: lwout_twkb.c:636
This library is the generic geometry handling section of PostGIS.
#define str(s)
Datum WKBFromLWGEOM(PG_FUNCTION_ARGS)
Definition: lwgeom_inout.c:428
Datum parse_WKT_lwgeom(PG_FUNCTION_ARGS)
Definition: lwgeom_inout.c:728
Datum TWKBFromLWGEOM(PG_FUNCTION_ARGS)
Definition: lwgeom_inout.c:471
PG_FUNCTION_INFO_V1(LWGEOM_in)
Datum LWGEOM_in(PG_FUNCTION_ARGS)
Definition: lwgeom_inout.c:84
Datum LWGEOM_to_latlon(PG_FUNCTION_ARGS)
Definition: lwgeom_inout.c:198
Datum LWGEOM_out(PG_FUNCTION_ARGS)
Definition: lwgeom_inout.c:273
Datum LWGEOM_to_bytea(PG_FUNCTION_ARGS)
Definition: lwgeom_inout.c:808
Datum LWGEOMFromTWKB(PG_FUNCTION_ARGS)
Definition: lwgeom_inout.c:405
Datum LWGEOM_from_bytea(PG_FUNCTION_ARGS)
Definition: lwgeom_inout.c:821
Datum TWKBFromLWGEOMArray(PG_FUNCTION_ARGS)
Definition: lwgeom_inout.c:527
Datum LWGEOM_send(PG_FUNCTION_ARGS)
Definition: lwgeom_inout.c:795
Datum LWGEOM_dropBBOX(PG_FUNCTION_ARGS)
Definition: lwgeom_inout.c:703
Datum LWGEOM_to_text(PG_FUNCTION_ARGS)
Definition: lwgeom_inout.c:344
Datum LWGEOMFromEWKB(PG_FUNCTION_ARGS)
Definition: lwgeom_inout.c:374
Datum LWGEOM_addBBOX(PG_FUNCTION_ARGS)
Definition: lwgeom_inout.c:687
void elog_ERROR(const char *string)
Definition: lwgeom_inout.c:716
Datum LWGEOM_asHEXEWKB(PG_FUNCTION_ARGS)
Definition: lwgeom_inout.c:291
Datum LWGEOM_recv(PG_FUNCTION_ARGS)
Definition: lwgeom_inout.c:756
static uint32_t lwgeom_get_type(const LWGEOM *geom)
Return LWTYPE number.
Definition: lwinline.h:135
type
Definition: ovdump.py:42
char * text_to_cstring(const text *textptr)
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
unsigned int int32
Definition: shpopen.c:273
uint8_t type
Definition: liblwgeom.h:564
Parser result structure: returns the result of attempting to convert (E)WKT/(E)WKB to LWGEOM.
Definition: liblwgeom.h:2068