PostGIS  2.3.7dev-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 #include <errno.h>
33 
34 #include "access/gist.h"
35 #include "access/itup.h"
36 
37 #include "fmgr.h"
38 #include "utils/elog.h"
39 
40 #include "../postgis_config.h"
41 
42 #include "liblwgeom.h"
43 #include "lwgeom_pg.h"
44 
45 
46 /* ---- SRID(geometry) */
47 Datum LWGEOM_get_srid(PG_FUNCTION_ARGS);
48 /* ---- SetSRID(geometry, integer) */
49 Datum LWGEOM_set_srid(PG_FUNCTION_ARGS);
50 /* ---- GeometryType(geometry) */
51 Datum LWGEOM_getTYPE(PG_FUNCTION_ARGS);
52 Datum geometry_geometrytype(PG_FUNCTION_ARGS);
53 /* ---- NumPoints(geometry) */
54 Datum LWGEOM_numpoints_linestring(PG_FUNCTION_ARGS);
55 /* ---- NumGeometries(geometry) */
56 Datum LWGEOM_numgeometries_collection(PG_FUNCTION_ARGS);
57 /* ---- GeometryN(geometry, integer) */
58 Datum LWGEOM_geometryn_collection(PG_FUNCTION_ARGS);
59 /* ---- Dimension(geometry) */
60 Datum LWGEOM_dimension(PG_FUNCTION_ARGS);
61 /* ---- ExteriorRing(geometry) */
62 Datum LWGEOM_exteriorring_polygon(PG_FUNCTION_ARGS);
63 /* ---- InteriorRingN(geometry, integer) */
64 Datum LWGEOM_interiorringn_polygon(PG_FUNCTION_ARGS);
65 /* ---- NumInteriorRings(geometry) */
66 Datum LWGEOM_numinteriorrings_polygon(PG_FUNCTION_ARGS);
67 /* ---- PointN(geometry, integer) */
68 Datum LWGEOM_pointn_linestring(PG_FUNCTION_ARGS);
69 /* ---- X(geometry) */
70 Datum LWGEOM_x_point(PG_FUNCTION_ARGS);
71 /* ---- Y(geometry) */
72 Datum LWGEOM_y_point(PG_FUNCTION_ARGS);
73 /* ---- Z(geometry) */
74 Datum LWGEOM_z_point(PG_FUNCTION_ARGS);
75 /* ---- M(geometry) */
76 Datum LWGEOM_m_point(PG_FUNCTION_ARGS);
77 /* ---- StartPoint(geometry) */
78 Datum LWGEOM_startpoint_linestring(PG_FUNCTION_ARGS);
79 /* ---- EndPoint(geometry) */
80 Datum LWGEOM_endpoint_linestring(PG_FUNCTION_ARGS);
81 /* ---- AsText(geometry) */
82 Datum LWGEOM_asText(PG_FUNCTION_ARGS);
83 /* ---- AsBinary(geometry, [XDR|NDR]) */
84 Datum LWGEOM_asBinary(PG_FUNCTION_ARGS);
85 /* ---- GeometryFromText(text, integer) */
86 Datum LWGEOM_from_text(PG_FUNCTION_ARGS);
87 /* ---- GeomFromWKB(bytea, integer) */
88 Datum LWGEOM_from_WKB(PG_FUNCTION_ARGS);
89 /* ---- IsClosed(geometry) */
90 Datum LWGEOM_isclosed(PG_FUNCTION_ARGS);
91 
92 /*------------------------------------------------------------------*/
93 
94 /* getSRID(lwgeom) :: int4 */
96 Datum LWGEOM_get_srid(PG_FUNCTION_ARGS)
97 {
98  GSERIALIZED *geom=PG_GETARG_GSERIALIZED_P(0);
99  int srid = gserialized_get_srid (geom);
100  PG_FREE_IF_COPY(geom,0);
101  PG_RETURN_INT32(srid);
102 }
103 
104 /* setSRID(lwgeom, int4) :: lwgeom */
106 Datum LWGEOM_set_srid(PG_FUNCTION_ARGS)
107 {
108  GSERIALIZED *g = (GSERIALIZED *)PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(0));
109  int srid = PG_GETARG_INT32(1);
110  gserialized_set_srid(g, srid);
111  PG_RETURN_POINTER(g);
112 }
113 
114 /* returns a string representation of this geometry's type */
116 Datum LWGEOM_getTYPE(PG_FUNCTION_ARGS)
117 {
118  GSERIALIZED *gser;
119  text *text_ob;
120  char *result;
121  uint8_t type;
122  static int maxtyplen = 20;
123 
124  gser = PG_GETARG_GSERIALIZED_P_SLICE(0, 0, gserialized_max_header_size());
125  text_ob = palloc0(VARHDRSZ + maxtyplen);
126  result = VARDATA(text_ob);
127 
128  type = gserialized_get_type(gser);
129 
130  if (type == POINTTYPE)
131  strcpy(result,"POINT");
132  else if (type == MULTIPOINTTYPE)
133  strcpy(result,"MULTIPOINT");
134  else if (type == LINETYPE)
135  strcpy(result,"LINESTRING");
136  else if (type == CIRCSTRINGTYPE)
137  strcpy(result,"CIRCULARSTRING");
138  else if (type == COMPOUNDTYPE)
139  strcpy(result, "COMPOUNDCURVE");
140  else if (type == MULTILINETYPE)
141  strcpy(result,"MULTILINESTRING");
142  else if (type == MULTICURVETYPE)
143  strcpy(result, "MULTICURVE");
144  else if (type == POLYGONTYPE)
145  strcpy(result,"POLYGON");
146  else if (type == TRIANGLETYPE)
147  strcpy(result,"TRIANGLE");
148  else if (type == CURVEPOLYTYPE)
149  strcpy(result,"CURVEPOLYGON");
150  else if (type == MULTIPOLYGONTYPE)
151  strcpy(result,"MULTIPOLYGON");
152  else if (type == MULTISURFACETYPE)
153  strcpy(result, "MULTISURFACE");
154  else if (type == COLLECTIONTYPE)
155  strcpy(result,"GEOMETRYCOLLECTION");
156  else if (type == POLYHEDRALSURFACETYPE)
157  strcpy(result,"POLYHEDRALSURFACE");
158  else if (type == TINTYPE)
159  strcpy(result,"TIN");
160  else
161  strcpy(result,"UNKNOWN");
162 
163  if ( gserialized_has_m(gser) && ! gserialized_has_z(gser) )
164  strcat(result, "M");
165 
166  SET_VARSIZE(text_ob, strlen(result) + VARHDRSZ); /* size of string */
167 
168  PG_FREE_IF_COPY(gser, 0);
169 
170  PG_RETURN_TEXT_P(text_ob);
171 }
172 
173 
174 /* returns a string representation of this geometry's type */
176 Datum geometry_geometrytype(PG_FUNCTION_ARGS)
177 {
178  GSERIALIZED *gser;
179  text *type_text;
180  static int type_str_len = 31;
181  char type_str[type_str_len + 1];
182 
183  /* Read just the header from the toasted tuple */
184  gser = PG_GETARG_GSERIALIZED_P_SLICE(0, 0, gserialized_max_header_size());
185 
186  /* Make it empty string to start */
187  type_str[0] = 0;
188 
189  /* Build up the output string */
190  strncat(type_str, "ST_", type_str_len);
191  strncat(type_str, lwtype_name(gserialized_get_type(gser)), type_str_len - 3);
192 
193  /* Build a text type to store things in */
194  type_text = cstring2text(type_str);
195 
196  PG_FREE_IF_COPY(gser, 0);
197  PG_RETURN_TEXT_P(type_text);
198 }
199 
200 
201 
207 Datum LWGEOM_numpoints_linestring(PG_FUNCTION_ARGS)
208 {
209  GSERIALIZED *geom = PG_GETARG_GSERIALIZED_P(0);
210  LWGEOM *lwgeom = lwgeom_from_gserialized(geom);
211  int count = -1;
212  int type = lwgeom->type;
213 
214  if ( type == LINETYPE || type == CIRCSTRINGTYPE || type == COMPOUNDTYPE )
215  count = lwgeom_count_vertices(lwgeom);
216 
217  lwgeom_free(lwgeom);
218  PG_FREE_IF_COPY(geom, 0);
219 
220  /* OGC says this functions is only valid on LINESTRING */
221  if ( count < 0 )
222  PG_RETURN_NULL();
223 
224  PG_RETURN_INT32(count);
225 }
226 
228 Datum LWGEOM_numgeometries_collection(PG_FUNCTION_ARGS)
229 {
230  GSERIALIZED *geom = PG_GETARG_GSERIALIZED_P(0);
231  LWGEOM *lwgeom;
232  int32 ret = 1;
233 
234  lwgeom = lwgeom_from_gserialized(geom);
235  if ( lwgeom_is_empty(lwgeom) )
236  {
237  ret = 0;
238  }
239  else if ( lwgeom_is_collection(lwgeom) )
240  {
241  LWCOLLECTION *col = lwgeom_as_lwcollection(lwgeom);
242  ret = col->ngeoms;
243  }
244  lwgeom_free(lwgeom);
245  PG_FREE_IF_COPY(geom, 0);
246  PG_RETURN_INT32(ret);
247 }
248 
251 Datum LWGEOM_geometryn_collection(PG_FUNCTION_ARGS)
252 {
253  GSERIALIZED *geom = PG_GETARG_GSERIALIZED_P(0);
254  GSERIALIZED *result;
255  int type = gserialized_get_type(geom);
256  int32 idx;
257  LWCOLLECTION *coll;
258  LWGEOM *subgeom;
259 
260  POSTGIS_DEBUG(2, "LWGEOM_geometryn_collection called.");
261 
262  /* elog(NOTICE, "GeometryN called"); */
263 
264  idx = PG_GETARG_INT32(1);
265  idx -= 1; /* index is 1-based */
266 
267  /* call is valid on multi* geoms only */
268  if (type==POINTTYPE || type==LINETYPE || type==CIRCSTRINGTYPE ||
269  type==COMPOUNDTYPE || type==POLYGONTYPE ||
270  type==CURVEPOLYTYPE || type==TRIANGLETYPE)
271  {
272  if ( idx == 0 ) PG_RETURN_POINTER(geom);
273  PG_RETURN_NULL();
274  }
275 
277 
278  if ( idx < 0 ) PG_RETURN_NULL();
279  if ( idx >= coll->ngeoms ) PG_RETURN_NULL();
280 
281  subgeom = coll->geoms[idx];
282  subgeom->srid = coll->srid;
283 
284  /* COMPUTE_BBOX==TAINTING */
285  if ( coll->bbox ) lwgeom_add_bbox(subgeom);
286 
287  result = geometry_serialize(subgeom);
288 
289  lwcollection_free(coll);
290  PG_FREE_IF_COPY(geom, 0);
291 
292  PG_RETURN_POINTER(result);
293 
294 }
295 
296 
302 Datum LWGEOM_dimension(PG_FUNCTION_ARGS)
303 {
304  GSERIALIZED *geom = PG_GETARG_GSERIALIZED_P(0);
305  LWGEOM *lwgeom = lwgeom_from_gserialized(geom);
306  int dimension = -1;
307 
308  dimension = lwgeom_dimension(lwgeom);
309  lwgeom_free(lwgeom);
310  PG_FREE_IF_COPY(geom, 0);
311 
312  if ( dimension < 0 )
313  {
314  elog(NOTICE, "Could not compute geometry dimensions");
315  PG_RETURN_NULL();
316  }
317 
318  PG_RETURN_INT32(dimension);
319 }
320 
321 
328 Datum LWGEOM_exteriorring_polygon(PG_FUNCTION_ARGS)
329 {
330  GSERIALIZED *geom = PG_GETARG_GSERIALIZED_P(0);
331  GSERIALIZED *result;
332  POINTARRAY *extring;
333  LWGEOM *lwgeom;
334  LWLINE *line;
335  GBOX *bbox=NULL;
336  int type = gserialized_get_type(geom);
337 
338  POSTGIS_DEBUG(2, "LWGEOM_exteriorring_polygon called.");
339 
340  if ( (type != POLYGONTYPE) &&
341  (type != CURVEPOLYTYPE) &&
342  (type != TRIANGLETYPE))
343  {
344  elog(ERROR, "ExteriorRing: geom is not a polygon");
345  PG_RETURN_NULL();
346  }
347 
348  lwgeom = lwgeom_from_gserialized(geom);
349 
350  if( lwgeom_is_empty(lwgeom) )
351  {
352  line = lwline_construct_empty(lwgeom->srid,
353  lwgeom_has_z(lwgeom),
354  lwgeom_has_m(lwgeom));
355  result = geometry_serialize(lwline_as_lwgeom(line));
356  }
357  else if ( lwgeom->type == POLYGONTYPE )
358  {
359  LWPOLY *poly = lwgeom_as_lwpoly(lwgeom);
360 
361  /* Ok, now we have a polygon. Here is its exterior ring. */
362  extring = poly->rings[0];
363 
364  /*
365  * This is a LWLINE constructed by exterior ring POINTARRAY
366  * If the input geom has a bbox, use it for
367  * the output geom, as exterior ring makes it up !
368  */
369  if ( poly->bbox )
370  bbox = gbox_copy(poly->bbox);
371 
372  line = lwline_construct(poly->srid, bbox, extring);
373  result = geometry_serialize((LWGEOM *)line);
374 
375  lwgeom_release((LWGEOM *)line);
376  }
377  else if ( lwgeom->type == TRIANGLETYPE )
378  {
379  LWTRIANGLE *triangle = lwgeom_as_lwtriangle(lwgeom);
380 
381  /*
382  * This is a LWLINE constructed by exterior ring POINTARRAY
383  * If the input geom has a bbox, use it for
384  * the output geom, as exterior ring makes it up !
385  */
386  if ( triangle->bbox )
387  bbox = gbox_copy(triangle->bbox);
388  line = lwline_construct(triangle->srid, bbox, triangle->points);
389 
390  result = geometry_serialize((LWGEOM *)line);
391 
392  lwgeom_release((LWGEOM *)line);
393  }
394  else
395  {
396  LWCURVEPOLY *curvepoly = lwgeom_as_lwcurvepoly(lwgeom);
397  result = geometry_serialize(curvepoly->rings[0]);
398  }
399 
400  lwgeom_free(lwgeom);
401  PG_FREE_IF_COPY(geom, 0);
402  PG_RETURN_POINTER(result);
403 }
404 
412 Datum LWGEOM_numinteriorrings_polygon(PG_FUNCTION_ARGS)
413 {
414  GSERIALIZED *geom = PG_GETARG_GSERIALIZED_P(0);
415  LWGEOM *lwgeom = lwgeom_from_gserialized(geom);
416  LWPOLY *poly = NULL;
417  LWCURVEPOLY *curvepoly = NULL;
418  int result = -1;
419 
420  if ( lwgeom->type == POLYGONTYPE )
421  {
422  poly = lwgeom_as_lwpoly(lwgeom);
423  result = poly->nrings - 1;
424  }
425  else if ( lwgeom->type == CURVEPOLYTYPE )
426  {
427  curvepoly = lwgeom_as_lwcurvepoly(lwgeom);
428  result = curvepoly->nrings - 1;
429  }
430 
431  lwgeom_free(lwgeom);
432  PG_FREE_IF_COPY(geom, 0);
433 
434  if ( result < 0 )
435  PG_RETURN_NULL();
436 
437  PG_RETURN_INT32(result);
438 }
439 
447 Datum LWGEOM_interiorringn_polygon(PG_FUNCTION_ARGS)
448 {
449  GSERIALIZED *geom;
450  int32 wanted_index;
451  LWCURVEPOLY *curvepoly = NULL;
452  LWPOLY *poly = NULL;
453  POINTARRAY *ring;
454  LWLINE *line;
455  LWGEOM *lwgeom;
456  GSERIALIZED *result;
457  GBOX *bbox = NULL;
458  int type;
459 
460  POSTGIS_DEBUG(2, "LWGEOM_interierringn_polygon called.");
461 
462  wanted_index = PG_GETARG_INT32(1);
463  if ( wanted_index < 1 )
464  {
465  /* elog(ERROR, "InteriorRingN: ring number is 1-based"); */
466  PG_RETURN_NULL(); /* index out of range */
467  }
468 
469  geom = PG_GETARG_GSERIALIZED_P(0);
470  type = gserialized_get_type(geom);
471 
472  if ( (type != POLYGONTYPE) && (type != CURVEPOLYTYPE) )
473  {
474  elog(ERROR, "InteriorRingN: geom is not a polygon");
475  PG_FREE_IF_COPY(geom, 0);
476  PG_RETURN_NULL();
477  }
478 
479  lwgeom = lwgeom_from_gserialized(geom);
480  if( lwgeom_is_empty(lwgeom) )
481  {
482  lwpoly_free(poly);
483  PG_FREE_IF_COPY(geom, 0);
484  PG_RETURN_NULL();
485  }
486 
487  if ( type == POLYGONTYPE)
488  {
490 
491  /* Ok, now we have a polygon. Let's see if it has enough holes */
492  if ( wanted_index >= poly->nrings )
493  {
494  lwpoly_free(poly);
495  PG_FREE_IF_COPY(geom, 0);
496  PG_RETURN_NULL();
497  }
498 
499  ring = poly->rings[wanted_index];
500 
501  /* COMPUTE_BBOX==TAINTING */
502  if ( poly->bbox )
503  {
504  bbox = lwalloc(sizeof(GBOX));
506  }
507 
508  /* This is a LWLINE constructed by interior ring POINTARRAY */
509  line = lwline_construct(poly->srid, bbox, ring);
510 
511 
512  result = geometry_serialize((LWGEOM *)line);
513  lwline_release(line);
514  lwpoly_free(poly);
515  }
516  else
517  {
519 
520  if (wanted_index >= curvepoly->nrings)
521  {
522  PG_FREE_IF_COPY(geom, 0);
523  lwgeom_release((LWGEOM *)curvepoly);
524  PG_RETURN_NULL();
525  }
526 
527  result = geometry_serialize(curvepoly->rings[wanted_index]);
528  lwgeom_free((LWGEOM*)curvepoly);
529  }
530 
531  PG_FREE_IF_COPY(geom, 0);
532  PG_RETURN_POINTER(result);
533 }
534 
541 Datum LWGEOM_pointn_linestring(PG_FUNCTION_ARGS)
542 {
543  GSERIALIZED *geom = PG_GETARG_GSERIALIZED_P(0);
544  int where = PG_GETARG_INT32(1);
545  LWGEOM *lwgeom = lwgeom_from_gserialized(geom);
546  LWPOINT *lwpoint = NULL;
547  int type = lwgeom->type;
548 
549  /* If index is negative, count backward */
550  if( where < 1 )
551  {
552  int count = -1;
553  if ( type == LINETYPE || type == CIRCSTRINGTYPE || type == COMPOUNDTYPE )
554  count = lwgeom_count_vertices(lwgeom);
555  if(count >0)
556  {
557  /* only work if we found the total point number */
558  /* converting where to positive backward indexing, +1 because 1 indexing */
559  where = where + count + 1;
560  }
561  if (where < 1)
562  PG_RETURN_NULL();
563  }
564 
565  if ( type == LINETYPE || type == CIRCSTRINGTYPE )
566  {
567  /* OGC index starts at one, so we substract first. */
568  lwpoint = lwline_get_lwpoint((LWLINE*)lwgeom, where - 1);
569  }
570  else if ( type == COMPOUNDTYPE )
571  {
572  lwpoint = lwcompound_get_lwpoint((LWCOMPOUND*)lwgeom, where - 1);
573  }
574 
575  lwgeom_free(lwgeom);
576  PG_FREE_IF_COPY(geom, 0);
577 
578  if ( ! lwpoint )
579  PG_RETURN_NULL();
580 
581  PG_RETURN_POINTER(geometry_serialize(lwpoint_as_lwgeom(lwpoint)));
582 }
583 
589 Datum LWGEOM_x_point(PG_FUNCTION_ARGS)
590 {
591  GSERIALIZED *geom;
592  LWGEOM *lwgeom;
593  LWPOINT *point = NULL;
594  POINT2D p;
595 
596  geom = PG_GETARG_GSERIALIZED_P(0);
597 
598  if ( gserialized_get_type(geom) != POINTTYPE )
599  lwpgerror("Argument to ST_X() must be a point");
600 
601  lwgeom = lwgeom_from_gserialized(geom);
602  point = lwgeom_as_lwpoint(lwgeom);
603 
604  if ( lwgeom_is_empty(lwgeom) )
605  PG_RETURN_NULL();
606 
607  getPoint2d_p(point->point, 0, &p);
608 
609  PG_FREE_IF_COPY(geom, 0);
610  PG_RETURN_FLOAT8(p.x);
611 }
612 
618 Datum LWGEOM_y_point(PG_FUNCTION_ARGS)
619 {
620  GSERIALIZED *geom;
621  LWPOINT *point = NULL;
622  LWGEOM *lwgeom;
623  POINT2D p;
624 
625  geom = PG_GETARG_GSERIALIZED_P(0);
626 
627  if ( gserialized_get_type(geom) != POINTTYPE )
628  lwpgerror("Argument to ST_Y() must be a point");
629 
630  lwgeom = lwgeom_from_gserialized(geom);
631  point = lwgeom_as_lwpoint(lwgeom);
632 
633  if ( lwgeom_is_empty(lwgeom) )
634  PG_RETURN_NULL();
635 
636  getPoint2d_p(point->point, 0, &p);
637 
638  PG_FREE_IF_COPY(geom, 0);
639 
640  PG_RETURN_FLOAT8(p.y);
641 }
642 
649 Datum LWGEOM_z_point(PG_FUNCTION_ARGS)
650 {
651  GSERIALIZED *geom;
652  LWPOINT *point = NULL;
653  LWGEOM *lwgeom;
654  POINT3DZ p;
655 
656  geom = PG_GETARG_GSERIALIZED_P(0);
657 
658  if ( gserialized_get_type(geom) != POINTTYPE )
659  lwpgerror("Argument to ST_Z() must be a point");
660 
661  lwgeom = lwgeom_from_gserialized(geom);
662  point = lwgeom_as_lwpoint(lwgeom);
663 
664  if ( lwgeom_is_empty(lwgeom) )
665  PG_RETURN_NULL();
666 
667  /* no Z in input */
668  if ( ! gserialized_has_z(geom) ) PG_RETURN_NULL();
669 
670  getPoint3dz_p(point->point, 0, &p);
671 
672  PG_FREE_IF_COPY(geom, 0);
673 
674  PG_RETURN_FLOAT8(p.z);
675 }
676 
682 Datum LWGEOM_m_point(PG_FUNCTION_ARGS)
683 {
684  GSERIALIZED *geom;
685  LWPOINT *point = NULL;
686  LWGEOM *lwgeom;
687  POINT3DM p;
688 
689  geom = PG_GETARG_GSERIALIZED_P(0);
690 
691  if ( gserialized_get_type(geom) != POINTTYPE )
692  lwpgerror("Argument to ST_M() must be a point");
693 
694  lwgeom = lwgeom_from_gserialized(geom);
695  point = lwgeom_as_lwpoint(lwgeom);
696 
697  if ( lwgeom_is_empty(lwgeom) )
698  PG_RETURN_NULL();
699 
700  /* no M in input */
701  if ( ! FLAGS_GET_M(point->flags) ) PG_RETURN_NULL();
702 
703  getPoint3dm_p(point->point, 0, &p);
704 
705  PG_FREE_IF_COPY(geom, 0);
706 
707  PG_RETURN_FLOAT8(p.m);
708 }
709 
716 Datum LWGEOM_startpoint_linestring(PG_FUNCTION_ARGS)
717 {
718  GSERIALIZED *geom = PG_GETARG_GSERIALIZED_P(0);
719  LWGEOM *lwgeom = lwgeom_from_gserialized(geom);
720  LWPOINT *lwpoint = NULL;
721  int type = lwgeom->type;
722 
723  if ( type == LINETYPE || type == CIRCSTRINGTYPE )
724  {
725  lwpoint = lwline_get_lwpoint((LWLINE*)lwgeom, 0);
726  }
727  else if ( type == COMPOUNDTYPE )
728  {
729  lwpoint = lwcompound_get_startpoint((LWCOMPOUND*)lwgeom);
730  }
731 
732  lwgeom_free(lwgeom);
733  PG_FREE_IF_COPY(geom, 0);
734 
735  if ( ! lwpoint )
736  PG_RETURN_NULL();
737 
738  PG_RETURN_POINTER(geometry_serialize(lwpoint_as_lwgeom(lwpoint)));
739 }
740 
746 Datum LWGEOM_endpoint_linestring(PG_FUNCTION_ARGS)
747 {
748  GSERIALIZED *geom = PG_GETARG_GSERIALIZED_P(0);
749  LWGEOM *lwgeom = lwgeom_from_gserialized(geom);
750  LWPOINT *lwpoint = NULL;
751  int type = lwgeom->type;
752 
753  if ( type == LINETYPE || type == CIRCSTRINGTYPE )
754  {
755  LWLINE *line = (LWLINE*)lwgeom;
756  if ( line->points )
757  lwpoint = lwline_get_lwpoint((LWLINE*)lwgeom, line->points->npoints - 1);
758  }
759  else if ( type == COMPOUNDTYPE )
760  {
761  lwpoint = lwcompound_get_endpoint((LWCOMPOUND*)lwgeom);
762  }
763 
764  lwgeom_free(lwgeom);
765  PG_FREE_IF_COPY(geom, 0);
766 
767  if ( ! lwpoint )
768  PG_RETURN_NULL();
769 
770  PG_RETURN_POINTER(geometry_serialize(lwpoint_as_lwgeom(lwpoint)));
771 }
772 
781 Datum LWGEOM_from_text(PG_FUNCTION_ARGS)
782 {
783  text *wkttext = PG_GETARG_TEXT_P(0);
784  char *wkt = text2cstring(wkttext);
785  LWGEOM_PARSER_RESULT lwg_parser_result;
786  GSERIALIZED *geom_result = NULL;
787  LWGEOM *lwgeom;
788 
789  POSTGIS_DEBUG(2, "LWGEOM_from_text");
790  POSTGIS_DEBUGF(3, "wkt: [%s]", wkt);
791 
792  if (lwgeom_parse_wkt(&lwg_parser_result, wkt, LW_PARSER_CHECK_ALL) == LW_FAILURE)
793  PG_PARSER_ERROR(lwg_parser_result);
794 
795  lwgeom = lwg_parser_result.geom;
796 
797  if ( lwgeom->srid != SRID_UNKNOWN )
798  {
799  elog(WARNING, "OGC WKT expected, EWKT provided - use GeomFromEWKT() for this");
800  }
801 
802  /* read user-requested SRID if any */
803  if ( PG_NARGS() > 1 )
804  lwgeom_set_srid(lwgeom, PG_GETARG_INT32(1));
805 
806  geom_result = geometry_serialize(lwgeom);
807  lwgeom_parser_result_free(&lwg_parser_result);
808 
809  PG_RETURN_POINTER(geom_result);
810 }
811 
822 Datum LWGEOM_from_WKB(PG_FUNCTION_ARGS)
823 {
824  bytea *bytea_wkb = (bytea*)PG_GETARG_BYTEA_P(0);
825  int32 srid = 0;
826  GSERIALIZED *geom;
827  LWGEOM *lwgeom;
828  uint8_t *wkb = (uint8_t*)VARDATA(bytea_wkb);
829 
830  lwgeom = lwgeom_from_wkb(wkb, VARSIZE(bytea_wkb)-VARHDRSZ, LW_PARSER_CHECK_ALL);
831 
832  if ( lwgeom_needs_bbox(lwgeom) )
833  lwgeom_add_bbox(lwgeom);
834 
835  geom = geometry_serialize(lwgeom);
836  lwgeom_free(lwgeom);
837  PG_FREE_IF_COPY(bytea_wkb, 0);
838 
839  if ( gserialized_get_srid(geom) != SRID_UNKNOWN )
840  {
841  elog(WARNING, "OGC WKB expected, EWKB provided - use GeometryFromEWKB() for this");
842  }
843 
844  if ( PG_NARGS() > 1 )
845  {
846  srid = PG_GETARG_INT32(1);
847  if ( srid != gserialized_get_srid(geom) )
848  gserialized_set_srid(geom, srid);
849  }
850 
851  PG_RETURN_POINTER(geom);
852 }
853 
856 Datum LWGEOM_asText(PG_FUNCTION_ARGS)
857 {
858  GSERIALIZED *geom;
859  LWGEOM *lwgeom;
860  char *wkt;
861  size_t wkt_size;
862  text *result;
863 
864  POSTGIS_DEBUG(2, "Called.");
865 
866  geom = PG_GETARG_GSERIALIZED_P(0);
867  lwgeom = lwgeom_from_gserialized(geom);
868 
869  /* Write to WKT and free the geometry */
870  wkt = lwgeom_to_wkt(lwgeom, WKT_ISO, DBL_DIG, &wkt_size);
871  lwgeom_free(lwgeom);
872  POSTGIS_DEBUGF(3, "WKT size = %u, WKT length = %u", (unsigned int)wkt_size, (unsigned int)strlen(wkt));
873 
874  /* Write to text and free the WKT */
875  result = cstring2text(wkt);
876  pfree(wkt);
877 
878  /* Return the text */
879  PG_FREE_IF_COPY(geom, 0);
880  PG_RETURN_TEXT_P(result);
881 }
882 
883 
886 Datum LWGEOM_asBinary(PG_FUNCTION_ARGS)
887 {
888  GSERIALIZED *geom;
889  LWGEOM *lwgeom;
890  uint8_t *wkb;
891  size_t wkb_size;
892  bytea *result;
893  uint8_t variant = WKB_ISO;
894 
895  /* Get a 2D version of the geometry */
896  geom = PG_GETARG_GSERIALIZED_P(0);
897  lwgeom = lwgeom_from_gserialized(geom);
898 
899  /* If user specified endianness, respect it */
900  if ( (PG_NARGS()>1) && (!PG_ARGISNULL(1)) )
901  {
902  text *wkb_endian = PG_GETARG_TEXT_P(1);
903 
904  if ( ! strncmp(VARDATA(wkb_endian), "xdr", 3) ||
905  ! strncmp(VARDATA(wkb_endian), "XDR", 3) )
906  {
907  variant = variant | WKB_XDR;
908  }
909  else
910  {
911  variant = variant | WKB_NDR;
912  }
913  }
914 
915  /* Write to WKB and free the geometry */
916  wkb = lwgeom_to_wkb(lwgeom, variant, &wkb_size);
917  lwgeom_free(lwgeom);
918 
919  /* Write to text and free the WKT */
920  result = palloc(wkb_size + VARHDRSZ);
921  memcpy(VARDATA(result), wkb, wkb_size);
922  SET_VARSIZE(result, wkb_size + VARHDRSZ);
923  pfree(wkb);
924 
925  /* Return the text */
926  PG_FREE_IF_COPY(geom, 0);
927  PG_RETURN_BYTEA_P(result);
928 }
929 
930 
931 
939 Datum LWGEOM_isclosed(PG_FUNCTION_ARGS)
940 {
941  GSERIALIZED *geom = PG_GETARG_GSERIALIZED_P(0);
942  LWGEOM *lwgeom = lwgeom_from_gserialized(geom);
943  int closed = lwgeom_is_closed(lwgeom);
944 
945  lwgeom_free(lwgeom);
946  PG_FREE_IF_COPY(geom, 0);
947  PG_RETURN_BOOL(closed);
948 }
#define LINETYPE
Definition: liblwgeom.h:85
int ptarray_calculate_gbox_cartesian(const POINTARRAY *pa, GBOX *gbox)
Calculate box (x/y) and add values to gbox.
Definition: g_box.c:547
GBOX * gbox_copy(const GBOX *box)
Return a copy of the GBOX, based on dimensionality of flags.
Definition: g_box.c:438
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:69
double z
Definition: liblwgeom.h:333
unsigned int int32
Definition: shpopen.c:273
Datum LWGEOM_pointn_linestring(PG_FUNCTION_ARGS)
Definition: lwgeom_ogc.c:541
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:960
POINTARRAY * points
Definition: liblwgeom.h:432
LWLINE * lwline_construct_empty(int srid, char hasz, char hasm)
Definition: lwline.c:64
#define MULTICURVETYPE
Definition: liblwgeom.h:94
uint8_t variant
Definition: cu_in_twkb.c:26
#define WKB_NDR
Definition: liblwgeom.h:2049
int lwgeom_is_collection(const LWGEOM *lwgeom)
Determine whether a LWGEOM can contain sub-geometries or not.
Definition: lwgeom.c:1004
char * lwgeom_to_wkt(const LWGEOM *geom, uint8_t variant, int precision, size_t *size_out)
WKT emitter function.
Definition: lwout_wkt.c:669
Datum LWGEOM_asBinary(PG_FUNCTION_ARGS)
Definition: lwgeom_ogc.c:886
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
LWGEOM ** rings
Definition: liblwgeom.h:534
int npoints
Definition: liblwgeom.h:370
int gserialized_has_m(const GSERIALIZED *gser)
Check if a GSERIALIZED has an M ordinate.
Definition: g_serialized.c:43
#define POLYGONTYPE
Definition: liblwgeom.h:86
#define CURVEPOLYTYPE
Definition: liblwgeom.h:93
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1063
#define COMPOUNDTYPE
Definition: liblwgeom.h:92
#define MULTIPOINTTYPE
Definition: liblwgeom.h:87
Datum LWGEOM_y_point(PG_FUNCTION_ARGS)
Definition: lwgeom_ogc.c:618
GBOX * bbox
Definition: liblwgeom.h:504
LWPOINT * lwcompound_get_startpoint(const LWCOMPOUND *lwcmp)
Definition: lwcompound.c:248
LWCURVEPOLY * lwgeom_as_lwcurvepoly(const LWGEOM *lwgeom)
Definition: lwgeom.c:129
void gserialized_set_srid(GSERIALIZED *s, int32_t srid)
Write the SRID into the serialized form (it is packed into three bytes so this is a handy function)...
Definition: g_serialized.c:100
#define TRIANGLETYPE
Definition: liblwgeom.h:97
#define POLYHEDRALSURFACETYPE
Definition: liblwgeom.h:96
GBOX * bbox
Definition: liblwgeom.h:430
void lwgeom_parser_result_free(LWGEOM_PARSER_RESULT *parser_result)
Definition: lwin_wkt.c:885
int getPoint3dm_p(const POINTARRAY *pa, int n, POINT3DM *point)
Definition: lwgeom_api.c:382
Datum LWGEOM_z_point(PG_FUNCTION_ARGS)
Definition: lwgeom_ogc.c:649
GBOX * bbox
Definition: liblwgeom.h:452
LWPOLY * lwgeom_as_lwpoly(const LWGEOM *lwgeom)
Definition: lwgeom.c:138
Datum LWGEOM_numgeometries_collection(PG_FUNCTION_ARGS)
Definition: lwgeom_ogc.c:228
LWPOINT * lwgeom_as_lwpoint(const LWGEOM *lwgeom)
Definition: lwgeom.c:93
POINTARRAY * point
Definition: liblwgeom.h:410
Datum LWGEOM_numpoints_linestring(PG_FUNCTION_ARGS)
Definition: lwgeom_ogc.c:207
int gserialized_has_z(const GSERIALIZED *gser)
Check if a GSERIALIZED has a Z ordinate.
Definition: g_serialized.c:38
int lwgeom_has_z(const LWGEOM *geom)
Return LW_TRUE if geometry has Z ordinates.
Definition: lwgeom.c:849
int32_t srid
Definition: liblwgeom.h:398
PG_FUNCTION_INFO_V1(LWGEOM_get_srid)
Datum LWGEOM_get_srid(PG_FUNCTION_ARGS)
Definition: lwgeom_ogc.c:96
#define LW_FAILURE
Definition: liblwgeom.h:78
double x
Definition: liblwgeom.h:327
Datum LWGEOM_m_point(PG_FUNCTION_ARGS)
Definition: lwgeom_ogc.c:682
Datum LWGEOM_asText(PG_FUNCTION_ARGS)
Definition: lwgeom_ogc.c:856
Datum LWGEOM_interiorringn_polygon(PG_FUNCTION_ARGS)
Definition: lwgeom_ogc.c:447
const char * lwtype_name(uint8_t type)
Return the type name string associated with a type number (e.g.
Definition: lwutil.c:216
#define WKT_ISO
Definition: liblwgeom.h:2055
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:764
int getPoint3dz_p(const POINTARRAY *pa, int n, POINT3DZ *point)
Definition: lwgeom_api.c:332
LWGEOM * lwline_as_lwgeom(const LWLINE *obj)
Definition: lwgeom.c:262
double m
Definition: liblwgeom.h:345
int lwgeom_parse_wkt(LWGEOM_PARSER_RESULT *parser_result, char *wktstr, int parse_flags)
Parse a WKT geometry string into an LWGEOM structure.
void lwpoly_free(LWPOLY *poly)
Definition: lwpoly.c:150
Parser result structure: returns the result of attempting to convert (E)WKT/(E)WKB to LWGEOM...
Definition: liblwgeom.h:1992
LWLINE * lwline_construct(int srid, GBOX *bbox, POINTARRAY *points)
Definition: lwline.c:42
LWGEOM ** geoms
Definition: liblwgeom.h:508
LWPOINT * lwcompound_get_endpoint(const LWCOMPOUND *lwcmp)
Definition: lwcompound.c:254
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:187
#define TINTYPE
Definition: liblwgeom.h:98
Datum LWGEOM_exteriorring_polygon(PG_FUNCTION_ARGS)
Definition: lwgeom_ogc.c:328
Datum LWGEOM_dimension(PG_FUNCTION_ARGS)
Definition: lwgeom_ogc.c:302
char * text2cstring(const text *textptr)
POINTARRAY ** rings
Definition: liblwgeom.h:456
int count
Definition: genraster.py:56
Datum LWGEOM_x_point(PG_FUNCTION_ARGS)
Definition: lwgeom_ogc.c:589
int nrings
Definition: liblwgeom.h:454
int32_t srid
Definition: liblwgeom.h:505
double y
Definition: liblwgeom.h:327
Datum LWGEOM_set_srid(PG_FUNCTION_ARGS)
Definition: lwgeom_ogc.c:106
int getPoint2d_p(const POINTARRAY *pa, int n, POINT2D *point)
Definition: lwgeom_api.c:461
#define WKB_ISO
Definition: liblwgeom.h:2046
Datum geometry_geometrytype(PG_FUNCTION_ARGS)
Definition: lwgeom_ogc.c:176
Datum LWGEOM_from_WKB(PG_FUNCTION_ARGS)
Definition: lwgeom_ogc.c:822
Datum LWGEOM_isclosed(PG_FUNCTION_ARGS)
Definition: lwgeom_ogc.c:939
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:89
Datum LWGEOM_from_text(PG_FUNCTION_ARGS)
Definition: lwgeom_ogc.c:781
uint8_t flags
Definition: liblwgeom.h:407
LWTRIANGLE * lwgeom_as_lwtriangle(const LWGEOM *lwgeom)
Definition: lwgeom.c:147
Datum LWGEOM_startpoint_linestring(PG_FUNCTION_ARGS)
Definition: lwgeom_ogc.c:716
#define MULTISURFACETYPE
Definition: liblwgeom.h:95
int32_t srid
Definition: liblwgeom.h:453
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
LWCOLLECTION * lwgeom_as_lwcollection(const LWGEOM *lwgeom)
Definition: lwgeom.c:156
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:1205
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:84
void lwgeom_add_bbox(LWGEOM *lwgeom)
Compute a bbox if not already computed.
Definition: lwgeom.c:612
#define FLAGS_GET_M(flags)
Definition: liblwgeom.h:140
#define WKB_XDR
Definition: liblwgeom.h:2050
void lwgeom_release(LWGEOM *lwgeom)
Free the containing LWGEOM and the associated BOX.
Definition: lwgeom.c:385
LWPOINT * lwline_get_lwpoint(const LWLINE *line, int where)
Returns freshly allocated LWPOINT that corresponds to the index where.
Definition: lwline.c:324
uint8_t type
Definition: liblwgeom.h:395
void lwcollection_free(LWCOLLECTION *col)
Definition: lwcollection.c:339
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
Definition: lwgeom.c:267
void lwgeom_set_srid(LWGEOM *geom, int srid)
Set the SRID on an LWGEOM For collections, only the parent gets an SRID, all the children get SRID_UN...
#define CIRCSTRINGTYPE
Definition: liblwgeom.h:91
void * lwalloc(size_t size)
Definition: lwutil.c:227
int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members) ...
Definition: lwgeom.c:1310
#define LW_PARSER_CHECK_ALL
Definition: liblwgeom.h:1986
Datum LWGEOM_geometryn_collection(PG_FUNCTION_ARGS)
Definition: lwgeom_ogc.c:251
int lwgeom_count_vertices(const LWGEOM *geom)
Count the total number of vertices in any LWGEOM.
Definition: lwgeom.c:1153
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:765
LWPOINT * lwcompound_get_lwpoint(const LWCOMPOUND *lwcmp, int where)
Definition: lwcompound.c:213
#define MULTILINETYPE
Definition: liblwgeom.h:88
Datum LWGEOM_getTYPE(PG_FUNCTION_ARGS)
Definition: lwgeom_ogc.c:116
Datum LWGEOM_numinteriorrings_polygon(PG_FUNCTION_ARGS)
Definition: lwgeom_ogc.c:412
void lwline_release(LWLINE *lwline)
Definition: lwline.c:134
int lwgeom_has_m(const LWGEOM *geom)
Return LW_TRUE if geometry has M ordinates.
Definition: lwgeom.c:856
Datum LWGEOM_endpoint_linestring(PG_FUNCTION_ARGS)
Definition: lwgeom_ogc.c:746
int lwgeom_needs_bbox(const LWGEOM *geom)
Check whether or not a lwgeom is big enough to warrant a bounding box.
Definition: lwgeom.c:1116
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:83
int32_t srid
Definition: liblwgeom.h:431
#define COLLECTIONTYPE
Definition: liblwgeom.h:90
This library is the generic geometry handling section of PostGIS.
uint32_t gserialized_max_header_size(void)
Returns the size in bytes to read from toast to get the basic information from a geometry: GSERIALIZE...
Definition: g_serialized.c:63
POINTARRAY * points
Definition: liblwgeom.h:421