PostGIS  3.1.6dev-r@@SVN_REVISION@@
lwgeom_ogc.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 2001-2005 Refractions Research Inc.
22  *
23  **********************************************************************/
24 
25 
26 #include "postgres.h"
27 
28 #include <math.h>
29 #include <float.h>
30 #include <string.h>
31 #include <stdio.h>
32 
33 #include "access/gist.h"
34 #include "access/itup.h"
35 
36 #include "fmgr.h"
37 #include "utils/builtins.h"
38 #include "utils/elog.h"
39 
40 #include "../postgis_config.h"
41 
42 #include "liblwgeom.h"
43 #include "liblwgeom_internal.h"
44 #include "lwgeom_pg.h"
45 
46 
47 /* ---- SRID(geometry) */
48 Datum LWGEOM_get_srid(PG_FUNCTION_ARGS);
49 /* ---- SetSRID(geometry, integer) */
50 Datum LWGEOM_set_srid(PG_FUNCTION_ARGS);
51 /* ---- GeometryType(geometry) */
52 Datum LWGEOM_getTYPE(PG_FUNCTION_ARGS);
53 Datum geometry_geometrytype(PG_FUNCTION_ARGS);
54 /* ---- NumPoints(geometry) */
55 Datum LWGEOM_numpoints_linestring(PG_FUNCTION_ARGS);
56 /* ---- NumGeometries(geometry) */
57 Datum LWGEOM_numgeometries_collection(PG_FUNCTION_ARGS);
58 /* ---- GeometryN(geometry, integer) */
59 Datum LWGEOM_geometryn_collection(PG_FUNCTION_ARGS);
60 /* ---- Dimension(geometry) */
61 Datum LWGEOM_dimension(PG_FUNCTION_ARGS);
62 /* ---- ExteriorRing(geometry) */
63 Datum LWGEOM_exteriorring_polygon(PG_FUNCTION_ARGS);
64 /* ---- InteriorRingN(geometry, integer) */
65 Datum LWGEOM_interiorringn_polygon(PG_FUNCTION_ARGS);
66 /* ---- NumInteriorRings(geometry) */
67 Datum LWGEOM_numinteriorrings_polygon(PG_FUNCTION_ARGS);
68 /* ---- PointN(geometry, integer) */
69 Datum LWGEOM_pointn_linestring(PG_FUNCTION_ARGS);
70 /* ---- X(geometry) */
71 Datum LWGEOM_x_point(PG_FUNCTION_ARGS);
72 /* ---- Y(geometry) */
73 Datum LWGEOM_y_point(PG_FUNCTION_ARGS);
74 /* ---- Z(geometry) */
75 Datum LWGEOM_z_point(PG_FUNCTION_ARGS);
76 /* ---- M(geometry) */
77 Datum LWGEOM_m_point(PG_FUNCTION_ARGS);
78 /* ---- StartPoint(geometry) */
79 Datum LWGEOM_startpoint_linestring(PG_FUNCTION_ARGS);
80 /* ---- EndPoint(geometry) */
81 Datum LWGEOM_endpoint_linestring(PG_FUNCTION_ARGS);
82 /* ---- AsText(geometry) */
83 Datum LWGEOM_asText(PG_FUNCTION_ARGS);
84 /* ---- AsBinary(geometry, [XDR|NDR]) */
85 Datum LWGEOM_asBinary(PG_FUNCTION_ARGS);
86 /* ---- GeometryFromText(text, integer) */
87 Datum LWGEOM_from_text(PG_FUNCTION_ARGS);
88 /* ---- GeomFromWKB(bytea, integer) */
89 Datum LWGEOM_from_WKB(PG_FUNCTION_ARGS);
90 /* ---- IsClosed(geometry) */
91 Datum LWGEOM_isclosed(PG_FUNCTION_ARGS);
92 
93 /*------------------------------------------------------------------*/
94 
95 /* getSRID(lwgeom) :: int4 */
97 Datum LWGEOM_get_srid(PG_FUNCTION_ARGS)
98 {
99  GSERIALIZED *geom = PG_GETARG_GSERIALIZED_HEADER(0);
100  int32_t srid = gserialized_get_srid(geom);
101  PG_FREE_IF_COPY(geom,0);
102  PG_RETURN_INT32(srid);
103 }
104 
105 /* setSRID(lwgeom, int4) :: lwgeom */
107 Datum LWGEOM_set_srid(PG_FUNCTION_ARGS)
108 {
109  GSERIALIZED *g = (GSERIALIZED *)PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(0));
110  int32_t srid = PG_GETARG_INT32(1);
111  gserialized_set_srid(g, srid);
112  PG_RETURN_POINTER(g);
113 }
114 
115 /* returns a string representation of this geometry's type */
117 Datum LWGEOM_getTYPE(PG_FUNCTION_ARGS)
118 {
119  GSERIALIZED *gser;
120  text *text_ob;
121  char *result;
122  uint8_t type;
123  static int maxtyplen = 20;
124 
125  gser = PG_GETARG_GSERIALIZED_HEADER(0);
126  text_ob = palloc0(VARHDRSZ + maxtyplen);
127  result = VARDATA(text_ob);
128 
129  type = gserialized_get_type(gser);
130 
131  if (type == POINTTYPE)
132  strcpy(result,"POINT");
133  else if (type == MULTIPOINTTYPE)
134  strcpy(result,"MULTIPOINT");
135  else if (type == LINETYPE)
136  strcpy(result,"LINESTRING");
137  else if (type == CIRCSTRINGTYPE)
138  strcpy(result,"CIRCULARSTRING");
139  else if (type == COMPOUNDTYPE)
140  strcpy(result, "COMPOUNDCURVE");
141  else if (type == MULTILINETYPE)
142  strcpy(result,"MULTILINESTRING");
143  else if (type == MULTICURVETYPE)
144  strcpy(result, "MULTICURVE");
145  else if (type == POLYGONTYPE)
146  strcpy(result,"POLYGON");
147  else if (type == TRIANGLETYPE)
148  strcpy(result,"TRIANGLE");
149  else if (type == CURVEPOLYTYPE)
150  strcpy(result,"CURVEPOLYGON");
151  else if (type == MULTIPOLYGONTYPE)
152  strcpy(result,"MULTIPOLYGON");
153  else if (type == MULTISURFACETYPE)
154  strcpy(result, "MULTISURFACE");
155  else if (type == COLLECTIONTYPE)
156  strcpy(result,"GEOMETRYCOLLECTION");
157  else if (type == POLYHEDRALSURFACETYPE)
158  strcpy(result,"POLYHEDRALSURFACE");
159  else if (type == TINTYPE)
160  strcpy(result,"TIN");
161  else
162  strcpy(result,"UNKNOWN");
163 
164  if ( gserialized_has_m(gser) && ! gserialized_has_z(gser) )
165  strcat(result, "M");
166 
167  SET_VARSIZE(text_ob, strlen(result) + VARHDRSZ); /* size of string */
168 
169  PG_FREE_IF_COPY(gser, 0);
170 
171  PG_RETURN_TEXT_P(text_ob);
172 }
173 
174 /* Matches lwutil.c::lwgeomTypeName */
175 static char *stTypeName[] = {"Unknown",
176  "ST_Point",
177  "ST_LineString",
178  "ST_Polygon",
179  "ST_MultiPoint",
180  "ST_MultiLineString",
181  "ST_MultiPolygon",
182  "ST_GeometryCollection",
183  "ST_CircularString",
184  "ST_CompoundCurve",
185  "ST_CurvePolygon",
186  "ST_MultiCurve",
187  "ST_MultiSurface",
188  "ST_PolyhedralSurface",
189  "ST_Triangle",
190  "ST_Tin"};
191 
192 /* returns a string representation of this geometry's type */
194 Datum geometry_geometrytype(PG_FUNCTION_ARGS)
195 {
196  GSERIALIZED *gser;
197  text *type_text;
198 
199  /* Read just the header from the toasted tuple */
200  gser = PG_GETARG_GSERIALIZED_HEADER(0);
201 
202  /* Build a text type to store things in */
203  type_text = cstring_to_text(stTypeName[gserialized_get_type(gser)]);
204 
205  PG_FREE_IF_COPY(gser, 0);
206  PG_RETURN_TEXT_P(type_text);
207 }
208 
209 
210 
216 Datum LWGEOM_numpoints_linestring(PG_FUNCTION_ARGS)
217 {
218  GSERIALIZED *geom = PG_GETARG_GSERIALIZED_P(0);
219  LWGEOM *lwgeom = lwgeom_from_gserialized(geom);
220  int count = -1;
221  int type = lwgeom->type;
222 
223  if ( type == LINETYPE || type == CIRCSTRINGTYPE || type == COMPOUNDTYPE )
224  count = lwgeom_count_vertices(lwgeom);
225 
226  lwgeom_free(lwgeom);
227  PG_FREE_IF_COPY(geom, 0);
228 
229  /* OGC says this functions is only valid on LINESTRING */
230  if ( count < 0 )
231  PG_RETURN_NULL();
232 
233  PG_RETURN_INT32(count);
234 }
235 
237 Datum LWGEOM_numgeometries_collection(PG_FUNCTION_ARGS)
238 {
239  GSERIALIZED *geom = PG_GETARG_GSERIALIZED_P(0);
240  LWGEOM *lwgeom;
241  int32 ret = 1;
242 
243  lwgeom = lwgeom_from_gserialized(geom);
244  if ( lwgeom_is_empty(lwgeom) )
245  {
246  ret = 0;
247  }
248  else if ( lwgeom_is_collection(lwgeom) )
249  {
250  LWCOLLECTION *col = lwgeom_as_lwcollection(lwgeom);
251  ret = col->ngeoms;
252  }
253  lwgeom_free(lwgeom);
254  PG_FREE_IF_COPY(geom, 0);
255  PG_RETURN_INT32(ret);
256 }
257 
260 Datum LWGEOM_geometryn_collection(PG_FUNCTION_ARGS)
261 {
262  GSERIALIZED *geom = PG_GETARG_GSERIALIZED_P(0);
264  int type = gserialized_get_type(geom);
265  int32 idx;
266  LWCOLLECTION *coll;
267  LWGEOM *subgeom;
268 
269  POSTGIS_DEBUG(2, "LWGEOM_geometryn_collection called.");
270 
271  /* elog(NOTICE, "GeometryN called"); */
272 
273  idx = PG_GETARG_INT32(1);
274  idx -= 1; /* index is 1-based */
275 
276  /* call is valid on multi* geoms only */
280  {
281  if ( idx == 0 ) PG_RETURN_POINTER(geom);
282  PG_RETURN_NULL();
283  }
284 
286 
287  if ( idx < 0 ) PG_RETURN_NULL();
288  if ( idx >= (int32) coll->ngeoms ) PG_RETURN_NULL();
289 
290  subgeom = coll->geoms[idx];
291  subgeom->srid = coll->srid;
292 
293  /* COMPUTE_BBOX==TAINTING */
294  if ( coll->bbox ) lwgeom_add_bbox(subgeom);
295 
296  result = geometry_serialize(subgeom);
297 
298  lwcollection_free(coll);
299  PG_FREE_IF_COPY(geom, 0);
300 
301  PG_RETURN_POINTER(result);
302 
303 }
304 
305 
311 Datum LWGEOM_dimension(PG_FUNCTION_ARGS)
312 {
313  GSERIALIZED *geom = PG_GETARG_GSERIALIZED_P(0);
314  LWGEOM *lwgeom = lwgeom_from_gserialized(geom);
315  int dimension = -1;
316 
317  dimension = lwgeom_dimension(lwgeom);
318  lwgeom_free(lwgeom);
319  PG_FREE_IF_COPY(geom, 0);
320 
321  if ( dimension < 0 )
322  {
323  elog(NOTICE, "Could not compute geometry dimensions");
324  PG_RETURN_NULL();
325  }
326 
327  PG_RETURN_INT32(dimension);
328 }
329 
330 
337 Datum LWGEOM_exteriorring_polygon(PG_FUNCTION_ARGS)
338 {
339  GSERIALIZED *geom = PG_GETARG_GSERIALIZED_P(0);
341  POINTARRAY *extring;
342  LWGEOM *lwgeom;
343  LWLINE *line;
344  GBOX *bbox=NULL;
345  int type = gserialized_get_type(geom);
346 
347  POSTGIS_DEBUG(2, "LWGEOM_exteriorring_polygon called.");
348 
349  if ( (type != POLYGONTYPE) &&
350  (type != CURVEPOLYTYPE) &&
351  (type != TRIANGLETYPE))
352  {
353  PG_RETURN_NULL();
354  }
355 
356  lwgeom = lwgeom_from_gserialized(geom);
357 
358  if( lwgeom_is_empty(lwgeom) )
359  {
360  line = lwline_construct_empty(lwgeom->srid,
361  lwgeom_has_z(lwgeom),
362  lwgeom_has_m(lwgeom));
363  result = geometry_serialize(lwline_as_lwgeom(line));
364  }
365  else if ( type == POLYGONTYPE )
366  {
367  LWPOLY *poly = lwgeom_as_lwpoly(lwgeom);
368 
369  /* Ok, now we have a polygon. Here is its exterior ring. */
370  extring = poly->rings[0];
371 
372  /*
373  * This is a LWLINE constructed by exterior ring POINTARRAY
374  * If the input geom has a bbox, use it for
375  * the output geom, as exterior ring makes it up !
376  */
377  if ( poly->bbox )
378  bbox = gbox_copy(poly->bbox);
379 
380  line = lwline_construct(poly->srid, bbox, extring);
381  result = geometry_serialize((LWGEOM *)line);
382 
383  lwgeom_release((LWGEOM *)line);
384  }
385  else if ( type == TRIANGLETYPE )
386  {
387  LWTRIANGLE *triangle = lwgeom_as_lwtriangle(lwgeom);
388 
389  /*
390  * This is a LWLINE constructed by exterior ring POINTARRAY
391  * If the input geom has a bbox, use it for
392  * the output geom, as exterior ring makes it up !
393  */
394  if ( triangle->bbox )
395  bbox = gbox_copy(triangle->bbox);
396  line = lwline_construct(triangle->srid, bbox, triangle->points);
397 
398  result = geometry_serialize((LWGEOM *)line);
399 
400  lwgeom_release((LWGEOM *)line);
401  }
402  else
403  {
404  LWCURVEPOLY *curvepoly = lwgeom_as_lwcurvepoly(lwgeom);
405  result = geometry_serialize(curvepoly->rings[0]);
406  }
407 
408  lwgeom_free(lwgeom);
409  PG_FREE_IF_COPY(geom, 0);
410  PG_RETURN_POINTER(result);
411 }
412 
420 Datum LWGEOM_numinteriorrings_polygon(PG_FUNCTION_ARGS)
421 {
422  GSERIALIZED *geom = PG_GETARG_GSERIALIZED_P(0);
423  int type = gserialized_get_type(geom);
424  LWGEOM *lwgeom;
425  int result = -1;
426 
427  if ( (type != POLYGONTYPE) &&
428  (type != CURVEPOLYTYPE) &&
429  (type != TRIANGLETYPE))
430  {
431  PG_RETURN_NULL();
432  }
433 
434  lwgeom = lwgeom_from_gserialized(geom);
435  if ( lwgeom_is_empty(lwgeom) || type == TRIANGLETYPE )
436  {
437  result = 0;
438  }
439  else
440  {
441  const LWPOLY *poly = (LWPOLY*)lwgeom;
442  result = poly->nrings - 1;
443  }
444 
445  lwgeom_free(lwgeom);
446  PG_FREE_IF_COPY(geom, 0);
447 
448  if ( result < 0 )
449  PG_RETURN_NULL();
450 
451  PG_RETURN_INT32(result);
452 }
453 
461 Datum LWGEOM_interiorringn_polygon(PG_FUNCTION_ARGS)
462 {
463  GSERIALIZED *geom;
464  int32 wanted_index;
465  LWCURVEPOLY *curvepoly = NULL;
466  LWPOLY *poly = NULL;
467  POINTARRAY *ring;
468  LWLINE *line;
469  LWGEOM *lwgeom;
471  GBOX *bbox = NULL;
472  int type;
473 
474  POSTGIS_DEBUG(2, "LWGEOM_interiorringn_polygon called.");
475 
476  wanted_index = PG_GETARG_INT32(1);
477  if ( wanted_index < 1 )
478  {
479  PG_RETURN_NULL(); /* index out of range */
480  }
481 
482  geom = PG_GETARG_GSERIALIZED_P(0);
483  type = gserialized_get_type(geom);
484 
485  if ( (type != POLYGONTYPE) && (type != CURVEPOLYTYPE) )
486  {
487  PG_FREE_IF_COPY(geom, 0);
488  PG_RETURN_NULL();
489  }
490 
491  lwgeom = lwgeom_from_gserialized(geom);
492  if( lwgeom_is_empty(lwgeom) )
493  {
494  lwpoly_free(poly);
495  PG_FREE_IF_COPY(geom, 0);
496  PG_RETURN_NULL();
497  }
498 
499  if ( type == POLYGONTYPE)
500  {
502 
503  /* Ok, now we have a polygon. Let's see if it has enough holes */
504  if ( wanted_index >= (int32)poly->nrings )
505  {
506  lwpoly_free(poly);
507  PG_FREE_IF_COPY(geom, 0);
508  PG_RETURN_NULL();
509  }
510 
511  ring = poly->rings[wanted_index];
512 
513  /* COMPUTE_BBOX==TAINTING */
514  if ( poly->bbox )
515  {
516  bbox = lwalloc(sizeof(GBOX));
518  }
519 
520  /* This is a LWLINE constructed by interior ring POINTARRAY */
521  line = lwline_construct(poly->srid, bbox, ring);
522 
523 
524  result = geometry_serialize((LWGEOM *)line);
525  lwline_release(line);
526  lwpoly_free(poly);
527  }
528  else
529  {
531 
532  if (wanted_index >= (int32)curvepoly->nrings)
533  {
534  PG_FREE_IF_COPY(geom, 0);
535  lwgeom_release((LWGEOM *)curvepoly);
536  PG_RETURN_NULL();
537  }
538 
539  result = geometry_serialize(curvepoly->rings[wanted_index]);
540  lwgeom_free((LWGEOM*)curvepoly);
541  }
542 
543  PG_FREE_IF_COPY(geom, 0);
544  PG_RETURN_POINTER(result);
545 }
546 
553 Datum LWGEOM_pointn_linestring(PG_FUNCTION_ARGS)
554 {
555  GSERIALIZED *geom = PG_GETARG_GSERIALIZED_P(0);
556  int where = PG_GETARG_INT32(1);
557  LWGEOM *lwgeom = lwgeom_from_gserialized(geom);
558  LWPOINT *lwpoint = NULL;
559  int type = lwgeom->type;
560 
561  /* If index is negative, count backward */
562  if( where < 1 )
563  {
564  int count = -1;
565  if ( type == LINETYPE || type == CIRCSTRINGTYPE || type == COMPOUNDTYPE )
566  count = lwgeom_count_vertices(lwgeom);
567  if(count >0)
568  {
569  /* only work if we found the total point number */
570  /* converting where to positive backward indexing, +1 because 1 indexing */
571  where = where + count + 1;
572  }
573  if (where < 1)
574  PG_RETURN_NULL();
575  }
576 
577  if ( type == LINETYPE || type == CIRCSTRINGTYPE )
578  {
579  /* OGC index starts at one, so we substract first. */
580  lwpoint = lwline_get_lwpoint((LWLINE*)lwgeom, where - 1);
581  }
582  else if ( type == COMPOUNDTYPE )
583  {
584  lwpoint = lwcompound_get_lwpoint((LWCOMPOUND*)lwgeom, where - 1);
585  }
586 
587  lwgeom_free(lwgeom);
588  PG_FREE_IF_COPY(geom, 0);
589 
590  if ( ! lwpoint )
591  PG_RETURN_NULL();
592 
593  PG_RETURN_POINTER(geometry_serialize(lwpoint_as_lwgeom(lwpoint)));
594 }
595 
601 Datum LWGEOM_x_point(PG_FUNCTION_ARGS)
602 {
603  GSERIALIZED *geom = PG_GETARG_GSERIALIZED_P(0);
604  POINT4D pt;
605 
606  if (gserialized_get_type(geom) != POINTTYPE)
607  lwpgerror("Argument to ST_X() must have type POINT");
608 
609  if (gserialized_peek_first_point(geom, &pt) == LW_FAILURE)
610  {
611  PG_RETURN_NULL();
612  }
613  PG_RETURN_FLOAT8(pt.x);
614 }
615 
621 Datum LWGEOM_y_point(PG_FUNCTION_ARGS)
622 {
623  GSERIALIZED *geom = PG_GETARG_GSERIALIZED_P(0);
624  POINT4D pt;
625 
626  if (gserialized_get_type(geom) != POINTTYPE)
627  lwpgerror("Argument to ST_Y() must have type POINT");
628 
629  if (gserialized_peek_first_point(geom, &pt) == LW_FAILURE)
630  {
631  PG_RETURN_NULL();
632  }
633  PG_RETURN_FLOAT8(pt.y);
634 }
635 
642 Datum LWGEOM_z_point(PG_FUNCTION_ARGS)
643 {
644  GSERIALIZED *geom = PG_GETARG_GSERIALIZED_P(0);
645  POINT4D pt;
646 
647  if (gserialized_get_type(geom) != POINTTYPE)
648  lwpgerror("Argument to ST_Z() must have type POINT");
649 
650  if (!gserialized_has_z(geom) || (gserialized_peek_first_point(geom, &pt) == LW_FAILURE))
651  {
652  PG_RETURN_NULL();
653  }
654  PG_RETURN_FLOAT8(pt.z);
655 }
656 
662 Datum LWGEOM_m_point(PG_FUNCTION_ARGS)
663 {
664  GSERIALIZED *geom = PG_GETARG_GSERIALIZED_P(0);
665  POINT4D pt;
666 
667  if (gserialized_get_type(geom) != POINTTYPE)
668  lwpgerror("Argument to ST_M() must have type POINT");
669 
670  if (!gserialized_has_m(geom) || (gserialized_peek_first_point(geom, &pt) == LW_FAILURE))
671  {
672  PG_RETURN_NULL();
673  }
674  PG_RETURN_FLOAT8(pt.m);
675 }
676 
683 Datum LWGEOM_startpoint_linestring(PG_FUNCTION_ARGS)
684 {
685  GSERIALIZED *geom = PG_GETARG_GSERIALIZED_P(0);
686  LWGEOM *lwgeom = lwgeom_from_gserialized(geom);
687  LWPOINT *lwpoint = NULL;
688  int type = lwgeom->type;
689 
690  if ( type == LINETYPE || type == CIRCSTRINGTYPE )
691  {
692  lwpoint = lwline_get_lwpoint((LWLINE*)lwgeom, 0);
693  }
694  else if ( type == COMPOUNDTYPE )
695  {
696  lwpoint = lwcompound_get_startpoint((LWCOMPOUND*)lwgeom);
697  }
698 
699  lwgeom_free(lwgeom);
700  PG_FREE_IF_COPY(geom, 0);
701 
702  if ( ! lwpoint )
703  PG_RETURN_NULL();
704 
705  PG_RETURN_POINTER(geometry_serialize(lwpoint_as_lwgeom(lwpoint)));
706 }
707 
713 Datum LWGEOM_endpoint_linestring(PG_FUNCTION_ARGS)
714 {
715  GSERIALIZED *geom = PG_GETARG_GSERIALIZED_P(0);
716  LWGEOM *lwgeom = lwgeom_from_gserialized(geom);
717  LWPOINT *lwpoint = NULL;
718  int type = lwgeom->type;
719 
720  if ( type == LINETYPE || type == CIRCSTRINGTYPE )
721  {
722  LWLINE *line = (LWLINE*)lwgeom;
723  if ( line->points )
724  lwpoint = lwline_get_lwpoint((LWLINE*)lwgeom, line->points->npoints - 1);
725  }
726  else if ( type == COMPOUNDTYPE )
727  {
728  lwpoint = lwcompound_get_endpoint((LWCOMPOUND*)lwgeom);
729  }
730 
731  lwgeom_free(lwgeom);
732  PG_FREE_IF_COPY(geom, 0);
733 
734  if ( ! lwpoint )
735  PG_RETURN_NULL();
736 
737  PG_RETURN_POINTER(geometry_serialize(lwpoint_as_lwgeom(lwpoint)));
738 }
739 
748 Datum LWGEOM_from_text(PG_FUNCTION_ARGS)
749 {
750  text *wkttext = PG_GETARG_TEXT_P(0);
751  char *wkt = text_to_cstring(wkttext);
752  LWGEOM_PARSER_RESULT lwg_parser_result;
753  GSERIALIZED *geom_result = NULL;
754  LWGEOM *lwgeom;
755 
756  POSTGIS_DEBUG(2, "LWGEOM_from_text");
757  POSTGIS_DEBUGF(3, "wkt: [%s]", wkt);
758 
759  if (lwgeom_parse_wkt(&lwg_parser_result, wkt, LW_PARSER_CHECK_ALL) == LW_FAILURE )
760  PG_PARSER_ERROR(lwg_parser_result);
761 
762  lwgeom = lwg_parser_result.geom;
763 
764  if ( lwgeom->srid != SRID_UNKNOWN )
765  {
766  elog(WARNING, "OGC WKT expected, EWKT provided - use GeomFromEWKT() for this");
767  }
768 
769  /* read user-requested SRID if any */
770  if ( PG_NARGS() > 1 )
771  lwgeom_set_srid(lwgeom, PG_GETARG_INT32(1));
772 
773  geom_result = geometry_serialize(lwgeom);
774  lwgeom_parser_result_free(&lwg_parser_result);
775 
776  PG_RETURN_POINTER(geom_result);
777 }
778 
789 Datum LWGEOM_from_WKB(PG_FUNCTION_ARGS)
790 {
791  bytea *bytea_wkb = PG_GETARG_BYTEA_P(0);
792  int32 srid = 0;
793  GSERIALIZED *geom;
794  LWGEOM *lwgeom;
795  uint8_t *wkb = (uint8_t*)VARDATA(bytea_wkb);
796 
797  lwgeom = lwgeom_from_wkb(wkb, VARSIZE_ANY_EXHDR(bytea_wkb), LW_PARSER_CHECK_ALL);
798  if (!lwgeom)
799  lwpgerror("Unable to parse WKB");
800 
801  geom = geometry_serialize(lwgeom);
802  lwgeom_free(lwgeom);
803  PG_FREE_IF_COPY(bytea_wkb, 0);
804 
805  if ( gserialized_get_srid(geom) != SRID_UNKNOWN )
806  {
807  elog(WARNING, "OGC WKB expected, EWKB provided - use GeometryFromEWKB() for this");
808  }
809 
810  if ( PG_NARGS() > 1 )
811  {
812  srid = PG_GETARG_INT32(1);
813  if ( srid != gserialized_get_srid(geom) )
814  gserialized_set_srid(geom, srid);
815  }
816 
817  PG_RETURN_POINTER(geom);
818 }
819 
822 Datum LWGEOM_asText(PG_FUNCTION_ARGS)
823 {
824  GSERIALIZED *geom = PG_GETARG_GSERIALIZED_P(0);
825  LWGEOM *lwgeom = lwgeom_from_gserialized(geom);
826 
827  int dbl_dig_for_wkt = OUT_DEFAULT_DECIMAL_DIGITS;
828  if (PG_NARGS() > 1) dbl_dig_for_wkt = PG_GETARG_INT32(1);
829 
830  PG_RETURN_TEXT_P(lwgeom_to_wkt_varlena(lwgeom, WKT_ISO, dbl_dig_for_wkt));
831 }
832 
833 
836 Datum LWGEOM_asBinary(PG_FUNCTION_ARGS)
837 {
838  GSERIALIZED *geom;
839  LWGEOM *lwgeom;
840  uint8_t variant = WKB_ISO;
841 
842  if (PG_ARGISNULL(0))
843  PG_RETURN_NULL();
844 
845  /* Get a 2D version of the geometry */
846  geom = PG_GETARG_GSERIALIZED_P(0);
847  lwgeom = lwgeom_from_gserialized(geom);
848 
849 
850  /* If user specified endianness, respect it */
851  if ( (PG_NARGS()>1) && (!PG_ARGISNULL(1)) )
852  {
853  text *wkb_endian = PG_GETARG_TEXT_P(1);
854 
855  if ( ! strncmp(VARDATA(wkb_endian), "xdr", 3) ||
856  ! strncmp(VARDATA(wkb_endian), "XDR", 3) )
857  {
858  variant = variant | WKB_XDR;
859  }
860  else
861  {
862  variant = variant | WKB_NDR;
863  }
864  }
865 
866  /* Write to WKB and free the geometry */
867  PG_RETURN_BYTEA_P(lwgeom_to_wkb_varlena(lwgeom, variant));
868 }
869 
870 
871 
879 Datum LWGEOM_isclosed(PG_FUNCTION_ARGS)
880 {
881  GSERIALIZED *geom = PG_GETARG_GSERIALIZED_P(0);
882  LWGEOM *lwgeom = lwgeom_from_gserialized(geom);
883  int closed = lwgeom_is_closed(lwgeom);
884 
885  lwgeom_free(lwgeom);
886  PG_FREE_IF_COPY(geom, 0);
887  PG_RETURN_BOOL(closed);
888 }
static uint8_t variant
Definition: cu_in_twkb.c:26
char result[OUT_DOUBLE_BUFFER_SIZE]
Definition: cu_print.c:267
GBOX * gbox_copy(const GBOX *box)
Return a copy of the GBOX, based on dimensionality of flags.
Definition: gbox.c:426
int ptarray_calculate_gbox_cartesian(const POINTARRAY *pa, GBOX *gbox)
Calculate box (x/y) and add values to gbox.
Definition: gbox.c:601
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_has_m(const GSERIALIZED *g)
Check if a GSERIALIZED has an M ordinate.
Definition: gserialized.c:185
int gserialized_peek_first_point(const GSERIALIZED *g, POINT4D *out_point)
Pull the first point values of a GSERIALIZED.
Definition: gserialized.c:257
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
Definition: gserialized.c:239
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
int lwgeom_is_closed(const LWGEOM *geom)
Return true or false depending on whether a geometry is a linear feature that closes on itself.
Definition: lwgeom.c:1036
#define LW_PARSER_CHECK_ALL
Definition: liblwgeom.h:2086
LWGEOM * lwline_as_lwgeom(const LWLINE *obj)
Definition: lwgeom.c:322
#define COLLECTIONTYPE
Definition: liblwgeom.h:122
#define COMPOUNDTYPE
Definition: liblwgeom.h:124
#define WKB_ISO
Definition: liblwgeom.h:2146
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 CURVEPOLYTYPE
Definition: liblwgeom.h:125
#define MULTILINETYPE
Definition: liblwgeom.h:120
LWPOINT * lwcompound_get_lwpoint(const LWCOMPOUND *lwcmp, uint32_t where)
Definition: lwcompound.c:213
#define MULTISURFACETYPE
Definition: liblwgeom.h:127
#define LINETYPE
Definition: liblwgeom.h:117
void lwline_release(LWLINE *lwline)
Definition: lwline.c:125
#define MULTIPOINTTYPE
Definition: liblwgeom.h:119
int lwgeom_parse_wkt(LWGEOM_PARSER_RESULT *parser_result, char *wktstr, int parse_flags)
Parse a WKT geometry string into an LWGEOM structure.
int lwgeom_has_z(const LWGEOM *geom)
Return LW_TRUE if geometry has Z ordinates.
Definition: lwgeom.c:917
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:116
LWPOINT * lwcompound_get_startpoint(const LWCOMPOUND *lwcmp)
Definition: lwcompound.c:248
LWLINE * lwline_construct(int32_t srid, GBOX *bbox, POINTARRAY *points)
Definition: lwline.c:42
LWTRIANGLE * lwgeom_as_lwtriangle(const LWGEOM *lwgeom)
Definition: lwgeom.c:207
#define TINTYPE
Definition: liblwgeom.h:130
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:121
uint32_t lwgeom_count_vertices(const LWGEOM *geom)
Count the total number of vertices in any LWGEOM.
Definition: lwgeom.c:1229
int lwgeom_dimension(const LWGEOM *geom)
For an LWGEOM, returns 0 for points, 1 for lines, 2 for polygons, 3 for volume, and the max dimension...
Definition: lwgeom.c:1281
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
Definition: lwgeom.c:327
int lwgeom_is_collection(const LWGEOM *lwgeom)
Determine whether a LWGEOM can contain sub-geometries or not.
Definition: lwgeom.c:1080
#define POLYGONTYPE
Definition: liblwgeom.h:118
#define POLYHEDRALSURFACETYPE
Definition: liblwgeom.h:128
LWCURVEPOLY * lwgeom_as_lwcurvepoly(const LWGEOM *lwgeom)
Definition: lwgeom.c:189
#define CIRCSTRINGTYPE
Definition: liblwgeom.h:123
#define WKT_ISO
Definition: liblwgeom.h:2155
#define WKB_NDR
Definition: liblwgeom.h:2149
void lwcollection_free(LWCOLLECTION *col)
Definition: lwcollection.c:357
#define MULTICURVETYPE
Definition: liblwgeom.h:126
#define TRIANGLETYPE
Definition: liblwgeom.h:129
void * lwalloc(size_t size)
Definition: lwutil.c:227
LWCOLLECTION * lwgeom_as_lwcollection(const LWGEOM *lwgeom)
Definition: lwgeom.c:216
void lwpoly_free(LWPOLY *poly)
Definition: lwpoly.c:175
LWPOLY * lwgeom_as_lwpoly(const LWGEOM *lwgeom)
Definition: lwgeom.c:198
void lwgeom_release(LWGEOM *lwgeom)
Free the containing LWGEOM and the associated BOX.
Definition: lwgeom.c:451
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:229
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:834
int lwgeom_has_m(const LWGEOM *geom)
Return LW_TRUE if geometry has M ordinates.
Definition: lwgeom.c:924
lwvarlena_t * lwgeom_to_wkb_varlena(const LWGEOM *geom, uint8_t variant)
Definition: lwout_wkb.c:851
void lwgeom_add_bbox(LWGEOM *lwgeom)
Compute a bbox if not already computed.
Definition: lwgeom.c:678
LWLINE * lwline_construct_empty(int32_t srid, char hasz, char hasm)
Definition: lwline.c:55
void lwgeom_parser_result_free(LWGEOM_PARSER_RESULT *parser_result)
Definition: lwin_wkt.c:886
LWPOINT * lwcompound_get_endpoint(const LWCOMPOUND *lwcmp)
Definition: lwcompound.c:254
#define WKB_XDR
Definition: liblwgeom.h:2150
lwvarlena_t * lwgeom_to_wkt_varlena(const LWGEOM *geom, uint8_t variant, int precision)
Definition: lwout_wkt.c:717
LWPOINT * lwline_get_lwpoint(const LWLINE *line, uint32_t where)
Returns freshly allocated LWPOINT that corresponds to the index where.
Definition: lwline.c:309
This library is the generic geometry handling section of PostGIS.
#define OUT_DEFAULT_DECIMAL_DIGITS
Datum LWGEOM_asBinary(PG_FUNCTION_ARGS)
Definition: lwgeom_ogc.c:836
Datum LWGEOM_exteriorring_polygon(PG_FUNCTION_ARGS)
Definition: lwgeom_ogc.c:337
Datum LWGEOM_from_WKB(PG_FUNCTION_ARGS)
Definition: lwgeom_ogc.c:789
Datum LWGEOM_dimension(PG_FUNCTION_ARGS)
Definition: lwgeom_ogc.c:311
PG_FUNCTION_INFO_V1(LWGEOM_get_srid)
Datum LWGEOM_get_srid(PG_FUNCTION_ARGS)
Definition: lwgeom_ogc.c:97
Datum LWGEOM_isclosed(PG_FUNCTION_ARGS)
Definition: lwgeom_ogc.c:879
Datum LWGEOM_asText(PG_FUNCTION_ARGS)
Definition: lwgeom_ogc.c:822
static char * stTypeName[]
Definition: lwgeom_ogc.c:175
Datum LWGEOM_endpoint_linestring(PG_FUNCTION_ARGS)
Definition: lwgeom_ogc.c:713
Datum LWGEOM_startpoint_linestring(PG_FUNCTION_ARGS)
Definition: lwgeom_ogc.c:683
Datum LWGEOM_from_text(PG_FUNCTION_ARGS)
Definition: lwgeom_ogc.c:748
Datum LWGEOM_pointn_linestring(PG_FUNCTION_ARGS)
Definition: lwgeom_ogc.c:553
Datum LWGEOM_numpoints_linestring(PG_FUNCTION_ARGS)
Definition: lwgeom_ogc.c:216
Datum LWGEOM_geometryn_collection(PG_FUNCTION_ARGS)
Definition: lwgeom_ogc.c:260
Datum geometry_geometrytype(PG_FUNCTION_ARGS)
Definition: lwgeom_ogc.c:194
Datum LWGEOM_x_point(PG_FUNCTION_ARGS)
Definition: lwgeom_ogc.c:601
Datum LWGEOM_interiorringn_polygon(PG_FUNCTION_ARGS)
Definition: lwgeom_ogc.c:461
Datum LWGEOM_y_point(PG_FUNCTION_ARGS)
Definition: lwgeom_ogc.c:621
Datum LWGEOM_m_point(PG_FUNCTION_ARGS)
Definition: lwgeom_ogc.c:662
Datum LWGEOM_numinteriorrings_polygon(PG_FUNCTION_ARGS)
Definition: lwgeom_ogc.c:420
Datum LWGEOM_numgeometries_collection(PG_FUNCTION_ARGS)
Definition: lwgeom_ogc.c:237
Datum LWGEOM_getTYPE(PG_FUNCTION_ARGS)
Definition: lwgeom_ogc.c:117
Datum LWGEOM_set_srid(PG_FUNCTION_ARGS)
Definition: lwgeom_ogc.c:107
Datum LWGEOM_z_point(PG_FUNCTION_ARGS)
Definition: lwgeom_ogc.c:642
static int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members)
Definition: lwinline.h:203
int count
Definition: genraster.py:57
type
Definition: ovdump.py:42
unsigned int int32
Definition: shpopen.c:54
uint32_t ngeoms
Definition: liblwgeom.h:594
GBOX * bbox
Definition: liblwgeom.h:588
LWGEOM ** geoms
Definition: liblwgeom.h:589
int32_t srid
Definition: liblwgeom.h:590
LWGEOM ** rings
Definition: liblwgeom.h:617
uint32_t nrings
Definition: liblwgeom.h:622
uint8_t type
Definition: liblwgeom.h:476
int32_t srid
Definition: liblwgeom.h:474
POINTARRAY * points
Definition: liblwgeom.h:497
POINTARRAY ** rings
Definition: liblwgeom.h:533
uint32_t nrings
Definition: liblwgeom.h:538
GBOX * bbox
Definition: liblwgeom.h:532
int32_t srid
Definition: liblwgeom.h:534
int32_t srid
Definition: liblwgeom.h:510
GBOX * bbox
Definition: liblwgeom.h:508
POINTARRAY * points
Definition: liblwgeom.h:509
double m
Definition: liblwgeom.h:428
double x
Definition: liblwgeom.h:428
double z
Definition: liblwgeom.h:428
double y
Definition: liblwgeom.h:428
uint32_t npoints
Definition: liblwgeom.h:441
Parser result structure: returns the result of attempting to convert (E)WKT/(E)WKB to LWGEOM.
Definition: liblwgeom.h:2093