PostGIS  2.5.1dev-r@@SVN_REVISION@@
liblwgeom/lwgeom_geos.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 2011-2014 Sandro Santilli <strk@kbt.io>
22  * Copyright 2015-2018 Daniel Baston <dbaston@gmail.com>
23  * Copyright 2017-2018 Darafei Praliaskouski <me@komzpa.net>
24  *
25  **********************************************************************/
26 
27 #include "lwgeom_geos.h"
28 #include "liblwgeom.h"
29 #include "liblwgeom_internal.h"
30 #include "lwgeom_log.h"
31 
32 #include <stdarg.h>
33 #include <stdlib.h>
34 #include <time.h>
35 
36 LWTIN* lwtin_from_geos(const GEOSGeometry* geom, uint8_t want3d);
37 
38 #define AUTOFIX LW_TRUE
39 #define LWGEOM_GEOS_ERRMSG_MAXSIZE 256
41 
42 extern void
43 lwgeom_geos_error(const char* fmt, ...)
44 {
45  va_list ap;
46  va_start(ap, fmt);
47 
48  /* Call the supplied function */
51 
52  va_end(ap);
53 }
54 
55 /* Destroy any non-null GEOSGeometry* pointers passed as arguments */
56 #define GEOS_FREE(...) \
57 do { \
58  geos_destroy((sizeof((void*[]){__VA_ARGS__})/sizeof(void*)), __VA_ARGS__); \
59 } while (0)
60 
61 /* Pass the latest GEOS error to lwerror, then return NULL */
62 #define GEOS_FAIL() \
63 do { \
64  lwerror("%s: GEOS Error: %s", __func__, lwgeom_geos_errmsg); \
65  return NULL; \
66 } while (0)
67 
68 #define GEOS_FREE_AND_FAIL(...) \
69 do { \
70  GEOS_FREE(__VA_ARGS__); \
71  GEOS_FAIL(); \
72 } while (0)
73 
74 /* Return the consistent SRID of all inputs, or call lwerror
75  * in case of SRID mismatch. */
76 #define RESULT_SRID(...) \
77  (get_result_srid((sizeof((const void*[]){__VA_ARGS__})/sizeof(void*)), __func__, __VA_ARGS__))
78 
79 /* Free any non-null GEOSGeometry* pointers passed as arguments *
80  * Called by GEOS_FREE, which populates 'count' */
81 static void geos_destroy(size_t count, ...) {
82  va_list ap;
83  va_start(ap, count);
84  while (count--)
85  {
86  GEOSGeometry* g = va_arg(ap, GEOSGeometry*);
87  if (g)
88  {
89  GEOSGeom_destroy(g);
90  }
91  }
92 }
93 
94 /*
95 ** GEOS <==> PostGIS conversion functions
96 **
97 ** Default conversion creates a GEOS point array, then iterates through the
98 ** PostGIS points, setting each value in the GEOS array one at a time.
99 **
100 */
101 
102 /* Return a POINTARRAY from a GEOSCoordSeq */
103 POINTARRAY*
104 ptarray_from_GEOSCoordSeq(const GEOSCoordSequence* cs, uint8_t want3d)
105 {
106  uint32_t dims = 2;
107  uint32_t size = 0, i;
108  POINTARRAY* pa;
109  POINT4D point = { 0.0, 0.0, 0.0, 0.0 };
110 
111  LWDEBUG(2, "ptarray_fromGEOSCoordSeq called");
112 
113  if (!GEOSCoordSeq_getSize(cs, &size)) lwerror("Exception thrown");
114 
115  LWDEBUGF(4, " GEOSCoordSeq size: %d", size);
116 
117  if (want3d)
118  {
119  if (!GEOSCoordSeq_getDimensions(cs, &dims)) lwerror("Exception thrown");
120 
121  LWDEBUGF(4, " GEOSCoordSeq dimensions: %d", dims);
122 
123  /* forget higher dimensions (if any) */
124  if (dims > 3) dims = 3;
125  }
126 
127  LWDEBUGF(4, " output dimensions: %d", dims);
128 
129  pa = ptarray_construct((dims == 3), 0, size);
130 
131  for (i = 0; i < size; i++)
132  {
133  GEOSCoordSeq_getX(cs, i, &(point.x));
134  GEOSCoordSeq_getY(cs, i, &(point.y));
135  if (dims >= 3) GEOSCoordSeq_getZ(cs, i, &(point.z));
136  ptarray_set_point4d(pa, i, &point);
137  }
138 
139  return pa;
140 }
141 
142 /* Return an LWGEOM from a Geometry */
143 LWGEOM*
144 GEOS2LWGEOM(const GEOSGeometry* geom, uint8_t want3d)
145 {
146  int type = GEOSGeomTypeId(geom);
147  int SRID = GEOSGetSRID(geom);
148 
149  /* GEOS's 0 is equivalent to our unknown as for SRID values */
150  if (SRID == 0) SRID = SRID_UNKNOWN;
151 
152  if (want3d && !GEOSHasZ(geom))
153  {
154  LWDEBUG(3, "Geometry has no Z, won't provide one");
155  want3d = 0;
156  }
157 
158  switch (type)
159  {
160  const GEOSCoordSequence* cs;
161  POINTARRAY *pa, **ppaa;
162  const GEOSGeometry* g;
163  LWGEOM** geoms;
164  uint32_t i, ngeoms;
165 
166  case GEOS_POINT:
167  LWDEBUG(4, "lwgeom_from_geometry: it's a Point");
168  cs = GEOSGeom_getCoordSeq(geom);
169  if (GEOSisEmpty(geom)) return (LWGEOM*)lwpoint_construct_empty(SRID, want3d, 0);
170  pa = ptarray_from_GEOSCoordSeq(cs, want3d);
171  return (LWGEOM*)lwpoint_construct(SRID, NULL, pa);
172 
173  case GEOS_LINESTRING:
174  case GEOS_LINEARRING:
175  LWDEBUG(4, "lwgeom_from_geometry: it's a LineString or LinearRing");
176  if (GEOSisEmpty(geom)) return (LWGEOM*)lwline_construct_empty(SRID, want3d, 0);
177 
178  cs = GEOSGeom_getCoordSeq(geom);
179  pa = ptarray_from_GEOSCoordSeq(cs, want3d);
180  return (LWGEOM*)lwline_construct(SRID, NULL, pa);
181 
182  case GEOS_POLYGON:
183  LWDEBUG(4, "lwgeom_from_geometry: it's a Polygon");
184  if (GEOSisEmpty(geom)) return (LWGEOM*)lwpoly_construct_empty(SRID, want3d, 0);
185  ngeoms = GEOSGetNumInteriorRings(geom);
186  ppaa = lwalloc(sizeof(POINTARRAY*) * (ngeoms + 1));
187  g = GEOSGetExteriorRing(geom);
188  cs = GEOSGeom_getCoordSeq(g);
189  ppaa[0] = ptarray_from_GEOSCoordSeq(cs, want3d);
190  for (i = 0; i < ngeoms; i++)
191  {
192  g = GEOSGetInteriorRingN(geom, i);
193  cs = GEOSGeom_getCoordSeq(g);
194  ppaa[i + 1] = ptarray_from_GEOSCoordSeq(cs, want3d);
195  }
196  return (LWGEOM*)lwpoly_construct(SRID, NULL, ngeoms + 1, ppaa);
197 
198  case GEOS_MULTIPOINT:
199  case GEOS_MULTILINESTRING:
200  case GEOS_MULTIPOLYGON:
201  case GEOS_GEOMETRYCOLLECTION:
202  LWDEBUG(4, "lwgeom_from_geometry: it's a Collection or Multi");
203 
204  ngeoms = GEOSGetNumGeometries(geom);
205  geoms = NULL;
206  if (ngeoms)
207  {
208  geoms = lwalloc(sizeof(LWGEOM*) * ngeoms);
209  for (i = 0; i < ngeoms; i++)
210  {
211  g = GEOSGetGeometryN(geom, i);
212  geoms[i] = GEOS2LWGEOM(g, want3d);
213  }
214  }
215  return (LWGEOM*)lwcollection_construct(type, SRID, NULL, ngeoms, geoms);
216 
217  default:
218  lwerror("GEOS2LWGEOM: unknown geometry type: %d", type);
219  return NULL;
220  }
221 }
222 
223 GEOSCoordSeq ptarray_to_GEOSCoordSeq(const POINTARRAY*, uint8_t fix_ring);
224 
225 GEOSCoordSeq
227 {
228  uint32_t dims = 2;
229  uint32_t i;
230  int append_points = 0;
231  const POINT3DZ* p3d;
232  const POINT2D* p2d;
233  GEOSCoordSeq sq;
234 
235  if (FLAGS_GET_Z(pa->flags)) dims = 3;
236 
237  if (fix_ring)
238  {
239  if (pa->npoints < 1)
240  {
241  lwerror("ptarray_to_GEOSCoordSeq called with fix_ring and 0 vertices in ring, cannot fix");
242  return NULL;
243  }
244  else
245  {
246  if (pa->npoints < 4) append_points = 4 - pa->npoints;
247  if (!ptarray_is_closed_2d(pa) && append_points == 0) append_points = 1;
248  }
249  }
250 
251  if (!(sq = GEOSCoordSeq_create(pa->npoints + append_points, dims)))
252  {
253  lwerror("Error creating GEOS Coordinate Sequence");
254  return NULL;
255  }
256 
257  for (i = 0; i < pa->npoints; i++)
258  {
259  if (dims == 3)
260  {
261  p3d = getPoint3dz_cp(pa, i);
262  p2d = (const POINT2D*)p3d;
263  LWDEBUGF(4, "Point: %g,%g,%g", p3d->x, p3d->y, p3d->z);
264  }
265  else
266  {
267  p2d = getPoint2d_cp(pa, i);
268  LWDEBUGF(4, "Point: %g,%g", p2d->x, p2d->y);
269  }
270 
271  GEOSCoordSeq_setX(sq, i, p2d->x);
272  GEOSCoordSeq_setY(sq, i, p2d->y);
273 
274  if (dims == 3) GEOSCoordSeq_setZ(sq, i, p3d->z);
275  }
276 
277  if (append_points)
278  {
279  if (dims == 3)
280  {
281  p3d = getPoint3dz_cp(pa, 0);
282  p2d = (const POINT2D*)p3d;
283  }
284  else
285  p2d = getPoint2d_cp(pa, 0);
286  for (i = pa->npoints; i < pa->npoints + append_points; i++)
287  {
288  GEOSCoordSeq_setX(sq, i, p2d->x);
289  GEOSCoordSeq_setY(sq, i, p2d->y);
290 
291  if (dims == 3) GEOSCoordSeq_setZ(sq, i, p3d->z);
292  }
293  }
294 
295  return sq;
296 }
297 
298 static inline GEOSGeometry*
300 {
301  GEOSCoordSeq sq;
302  GEOSGeom g;
303  sq = ptarray_to_GEOSCoordSeq(pa, autofix);
304  g = GEOSGeom_createLinearRing(sq);
305  return g;
306 }
307 
308 GEOSGeometry*
309 GBOX2GEOS(const GBOX* box)
310 {
311  GEOSGeometry* envelope;
312  GEOSGeometry* ring;
313  GEOSCoordSequence* seq = GEOSCoordSeq_create(5, 2);
314  if (!seq) return NULL;
315 
316  GEOSCoordSeq_setX(seq, 0, box->xmin);
317  GEOSCoordSeq_setY(seq, 0, box->ymin);
318 
319  GEOSCoordSeq_setX(seq, 1, box->xmax);
320  GEOSCoordSeq_setY(seq, 1, box->ymin);
321 
322  GEOSCoordSeq_setX(seq, 2, box->xmax);
323  GEOSCoordSeq_setY(seq, 2, box->ymax);
324 
325  GEOSCoordSeq_setX(seq, 3, box->xmin);
326  GEOSCoordSeq_setY(seq, 3, box->ymax);
327 
328  GEOSCoordSeq_setX(seq, 4, box->xmin);
329  GEOSCoordSeq_setY(seq, 4, box->ymin);
330 
331  ring = GEOSGeom_createLinearRing(seq);
332  if (!ring)
333  {
334  GEOSCoordSeq_destroy(seq);
335  return NULL;
336  }
337 
338  envelope = GEOSGeom_createPolygon(ring, NULL, 0);
339  if (!envelope)
340  {
341  GEOSGeom_destroy(ring);
342  return NULL;
343  }
344 
345  return envelope;
346 }
347 
348 GEOSGeometry*
349 LWGEOM2GEOS(const LWGEOM* lwgeom, uint8_t autofix)
350 {
351  GEOSCoordSeq sq;
352  GEOSGeom g, shell;
353  GEOSGeom* geoms = NULL;
354  uint32_t ngeoms, i, j;
355  int geostype;
356 #if LWDEBUG_LEVEL >= 4
357  char* wkt;
358 #endif
359 
360  if (autofix)
361  {
362  /* cross fingers and try without autofix, maybe it'll work? */
363  g = LWGEOM2GEOS(lwgeom, LW_FALSE);
364  if (g) return g;
365  }
366 
367  LWDEBUGF(4, "LWGEOM2GEOS got a %s", lwtype_name(lwgeom->type));
368 
369  if (lwgeom_has_arc(lwgeom))
370  {
371  LWGEOM* lwgeom_stroked = lwgeom_stroke(lwgeom, 32);
372  GEOSGeometry* g = LWGEOM2GEOS(lwgeom_stroked, autofix);
373  lwgeom_free(lwgeom_stroked);
374  return g;
375  }
376 
377  LWPOINT* lwp = NULL;
378  LWPOLY* lwpoly = NULL;
379  LWLINE* lwl = NULL;
380  LWCOLLECTION* lwc = NULL;
381 
382  switch (lwgeom->type)
383  {
384  case POINTTYPE:
385  lwp = (LWPOINT*)lwgeom;
386 
387  if (lwgeom_is_empty(lwgeom))
388  g = GEOSGeom_createEmptyPolygon();
389  else
390  {
391  sq = ptarray_to_GEOSCoordSeq(lwp->point, 0);
392  g = GEOSGeom_createPoint(sq);
393  }
394  if (!g) return NULL;
395  break;
396 
397  case LINETYPE:
398  lwl = (LWLINE*)lwgeom;
399  /* TODO: if (autofix) */
400  if (lwl->points->npoints == 1)
401  {
402  /* Duplicate point, to make geos-friendly */
403  lwl->points = ptarray_addPoint(lwl->points,
404  getPoint_internal(lwl->points, 0),
405  FLAGS_NDIMS(lwl->points->flags),
406  lwl->points->npoints);
407  }
408  sq = ptarray_to_GEOSCoordSeq(lwl->points, 0);
409  g = GEOSGeom_createLineString(sq);
410  if (!g) return NULL;
411  break;
412 
413  case POLYGONTYPE:
414  lwpoly = (LWPOLY*)lwgeom;
415  if (lwgeom_is_empty(lwgeom))
416  g = GEOSGeom_createEmptyPolygon();
417  else
418  {
419  shell = ptarray_to_GEOSLinearRing(lwpoly->rings[0], autofix);
420  if (!shell) return NULL;
421  ngeoms = lwpoly->nrings - 1;
422  if (ngeoms > 0) geoms = lwalloc(sizeof(GEOSGeom) * ngeoms);
423 
424  for (i = 1; i < lwpoly->nrings; i++)
425  {
426  geoms[i - 1] = ptarray_to_GEOSLinearRing(lwpoly->rings[i], autofix);
427  if (!geoms[i - 1])
428  {
429  uint32_t k;
430  for (k = 0; k < i - 1; k++)
431  GEOSGeom_destroy(geoms[k]);
432  lwfree(geoms);
433  GEOSGeom_destroy(shell);
434  return NULL;
435  }
436  }
437  g = GEOSGeom_createPolygon(shell, geoms, ngeoms);
438  if (geoms) lwfree(geoms);
439  }
440  if (!g) return NULL;
441  break;
442  case MULTIPOINTTYPE:
443  case MULTILINETYPE:
444  case MULTIPOLYGONTYPE:
445  case COLLECTIONTYPE:
446  if (lwgeom->type == MULTIPOINTTYPE)
447  geostype = GEOS_MULTIPOINT;
448  else if (lwgeom->type == MULTILINETYPE)
449  geostype = GEOS_MULTILINESTRING;
450  else if (lwgeom->type == MULTIPOLYGONTYPE)
451  geostype = GEOS_MULTIPOLYGON;
452  else
453  geostype = GEOS_GEOMETRYCOLLECTION;
454 
455  lwc = (LWCOLLECTION*)lwgeom;
456 
457  ngeoms = lwc->ngeoms;
458  if (ngeoms > 0) geoms = lwalloc(sizeof(GEOSGeom) * ngeoms);
459 
460  j = 0;
461  for (i = 0; i < ngeoms; ++i)
462  {
463  GEOSGeometry* g;
464 
465  if (lwgeom_is_empty(lwc->geoms[i])) continue;
466 
467  g = LWGEOM2GEOS(lwc->geoms[i], 0);
468  if (!g)
469  {
470  uint32_t k;
471  for (k = 0; k < j; k++)
472  GEOSGeom_destroy(geoms[k]);
473  lwfree(geoms);
474  return NULL;
475  }
476  geoms[j++] = g;
477  }
478  g = GEOSGeom_createCollection(geostype, geoms, j);
479  if (ngeoms > 0) lwfree(geoms);
480  if (!g) return NULL;
481  break;
482 
483  default:
484  lwerror("Unknown geometry type: %d - %s", lwgeom->type, lwtype_name(lwgeom->type));
485  return NULL;
486  }
487 
488  GEOSSetSRID(g, lwgeom->srid);
489 
490 #if LWDEBUG_LEVEL >= 4
491  wkt = GEOSGeomToWKT(g);
492  LWDEBUGF(4, "LWGEOM2GEOS: GEOSGeom: %s", wkt);
493  free(wkt);
494 #endif
495 
496  return g;
497 }
498 
499 GEOSGeometry*
500 make_geos_point(double x, double y)
501 {
502  GEOSCoordSequence* seq = GEOSCoordSeq_create(1, 2);
503  GEOSGeometry* geom = NULL;
504 
505  if (!seq) return NULL;
506 
507  GEOSCoordSeq_setX(seq, 0, x);
508  GEOSCoordSeq_setY(seq, 0, y);
509 
510  geom = GEOSGeom_createPoint(seq);
511  if (!geom) GEOSCoordSeq_destroy(seq);
512  return geom;
513 }
514 
515 GEOSGeometry*
516 make_geos_segment(double x1, double y1, double x2, double y2)
517 {
518  GEOSCoordSequence* seq = GEOSCoordSeq_create(2, 2);
519  GEOSGeometry* geom = NULL;
520 
521  if (!seq) return NULL;
522 
523  GEOSCoordSeq_setX(seq, 0, x1);
524  GEOSCoordSeq_setY(seq, 0, y1);
525  GEOSCoordSeq_setX(seq, 1, x2);
526  GEOSCoordSeq_setY(seq, 1, y2);
527 
528  geom = GEOSGeom_createLineString(seq);
529  if (!geom) GEOSCoordSeq_destroy(seq);
530  return geom;
531 }
532 
533 const char*
535 {
536  const char* ver = GEOSversion();
537  return ver;
538 }
539 
540 /* Return the consistent SRID of all input.
541  * Intended to be called from RESULT_SRID macro */
542 static int32_t
543 get_result_srid(size_t count, const char* funcname, ...)
544 {
545  va_list ap;
546  va_start(ap, funcname);
547  int32_t srid = SRID_INVALID;
548  size_t i;
549  for(i = 0; i < count; i++)
550  {
551  LWGEOM* g = va_arg(ap, LWGEOM*);
552  if (!g)
553  {
554  lwerror("%s: Geometry is null", funcname);
555  return SRID_INVALID;
556  }
557  if (i == 0)
558  {
559  srid = g->srid;
560  }
561  else
562  {
563  if (g->srid != srid)
564  {
565  lwerror("%s: Operation on mixed SRID geometries (%d != %d)", funcname, srid, g->srid);
566  return SRID_INVALID;
567  }
568  }
569  }
570  return srid;
571 }
572 
573 LWGEOM*
575 {
576  LWGEOM* result;
577  int32_t srid = RESULT_SRID(geom);
578  uint8_t is3d = FLAGS_GET_Z(geom->flags);
579  GEOSGeometry* g;
580 
581  if (srid == SRID_INVALID) return NULL;
582 
583  initGEOS(lwnotice, lwgeom_geos_error);
584 
585  if (!(g = LWGEOM2GEOS(geom, AUTOFIX))) GEOS_FAIL();
586 
587  if (GEOSNormalize(g) == -1) GEOS_FREE_AND_FAIL(g);
588  GEOSSetSRID(g, srid);
589 
590  if (!(result = GEOS2LWGEOM(g, is3d))) GEOS_FREE_AND_FAIL(g);
591 
592  GEOSGeom_destroy(g);
593  return result;
594 }
595 
596 LWGEOM*
597 lwgeom_intersection(const LWGEOM* geom1, const LWGEOM* geom2)
598 {
599  LWGEOM* result;
600  int32_t srid = RESULT_SRID(geom1, geom2);
601  uint8_t is3d = (FLAGS_GET_Z(geom1->flags) || FLAGS_GET_Z(geom2->flags));
602  GEOSGeometry* g1;
603  GEOSGeometry* g2;
604  GEOSGeometry* g3;
605 
606  if (srid == SRID_INVALID) return NULL;
607 
608  /* A.Intersection(Empty) == Empty */
609  if (lwgeom_is_empty(geom2)) return lwgeom_clone_deep(geom2); /* match empty type? */
610 
611  /* Empty.Intersection(A) == Empty */
612  if (lwgeom_is_empty(geom1)) return lwgeom_clone_deep(geom1); /* match empty type? */
613 
614  initGEOS(lwnotice, lwgeom_geos_error);
615 
616  if (!(g1 = LWGEOM2GEOS(geom1, AUTOFIX))) GEOS_FAIL();
617  if (!(g2 = LWGEOM2GEOS(geom2, AUTOFIX))) GEOS_FREE_AND_FAIL(g1);
618 
619  g3 = GEOSIntersection(g1, g2);
620 
621  if (!g3) GEOS_FREE_AND_FAIL(g1);
622  GEOSSetSRID(g3, srid);
623 
624  if (!(result = GEOS2LWGEOM(g3, is3d))) GEOS_FREE_AND_FAIL(g1, g2, g3);
625 
626  GEOS_FREE(g1, g2, g3);
627  return result;
628 }
629 
630 LWGEOM*
632 {
633  LWGEOM* result;
634  int32_t srid = RESULT_SRID(geom);
635  uint8_t is3d = FLAGS_GET_Z(geom->flags);
636  GEOSGeometry* g1;
637  GEOSGeometry* g3;
638 
639  if (srid == SRID_INVALID) return NULL;
640 
641  /* Empty.Linemerge() == Empty */
642  if (lwgeom_is_empty(geom)) return lwgeom_clone_deep(geom); /* match empty type to linestring? */
643 
644  initGEOS(lwnotice, lwgeom_geos_error);
645 
646  if (!(g1 = LWGEOM2GEOS(geom, AUTOFIX))) GEOS_FAIL();
647 
648  g3 = GEOSLineMerge(g1);
649 
650  if (!g3) GEOS_FREE_AND_FAIL(g1);
651  GEOSSetSRID(g3, srid);
652 
653  if (!(result = GEOS2LWGEOM(g3, is3d)))
654  GEOS_FREE_AND_FAIL(g1, g3);
655 
656  GEOS_FREE(g1, g3);
657 
658  return result;
659 }
660 
661 LWGEOM*
663 {
664  LWGEOM* result;
665  int32_t srid = RESULT_SRID(geom);
666  uint8_t is3d = FLAGS_GET_Z(geom->flags);
667  GEOSGeometry* g1;
668  GEOSGeometry* g3;
669 
670  if (srid == SRID_INVALID) return NULL;
671 
672  /* Empty.UnaryUnion() == Empty */
673  if (lwgeom_is_empty(geom)) return lwgeom_clone_deep(geom);
674 
675  initGEOS(lwnotice, lwgeom_geos_error);
676 
677  if (!(g1 = LWGEOM2GEOS(geom, AUTOFIX))) GEOS_FAIL();
678 
679  g3 = GEOSUnaryUnion(g1);
680 
681  if (!g3) GEOS_FREE_AND_FAIL(g1);
682  GEOSSetSRID(g3, srid);
683 
684  if (!(result = GEOS2LWGEOM(g3, is3d)))
685  GEOS_FREE_AND_FAIL(g1, g3);
686 
687  GEOS_FREE(g1, g3);
688 
689  return result;
690 }
691 
692 LWGEOM*
693 lwgeom_difference(const LWGEOM* geom1, const LWGEOM* geom2)
694 {
695  LWGEOM* result;
696  int32_t srid = RESULT_SRID(geom1, geom2);
697  uint8_t is3d = (FLAGS_GET_Z(geom1->flags) || FLAGS_GET_Z(geom2->flags));
698  GEOSGeometry *g1, *g2, *g3;
699 
700  if (srid == SRID_INVALID) return NULL;
701 
702  /* A.Intersection(Empty) == Empty */
703  if (lwgeom_is_empty(geom2)) return lwgeom_clone_deep(geom1); /* match empty type? */
704 
705  /* Empty.Intersection(A) == Empty */
706  if (lwgeom_is_empty(geom1)) return lwgeom_clone_deep(geom1); /* match empty type? */
707 
708  initGEOS(lwnotice, lwgeom_geos_error);
709 
710  if (!(g1 = LWGEOM2GEOS(geom1, AUTOFIX))) GEOS_FAIL();
711  if (!(g2 = LWGEOM2GEOS(geom2, AUTOFIX))) GEOS_FREE_AND_FAIL(g1);
712 
713  g3 = GEOSDifference(g1, g2);
714 
715  if (!g3) GEOS_FREE_AND_FAIL(g1, g2);
716  GEOSSetSRID(g3, srid);
717 
718  if (!(result = GEOS2LWGEOM(g3, is3d)))
719  GEOS_FREE_AND_FAIL(g1, g2, g3);
720 
721  GEOS_FREE(g1, g2, g3);
722  return result;
723 }
724 
725 LWGEOM*
726 lwgeom_symdifference(const LWGEOM* geom1, const LWGEOM* geom2)
727 {
728  LWGEOM* result;
729  int32_t srid = RESULT_SRID(geom1, geom2);
730  uint8_t is3d = (FLAGS_GET_Z(geom1->flags) || FLAGS_GET_Z(geom2->flags));
731  GEOSGeometry *g1, *g2, *g3;
732 
733  if (srid == SRID_INVALID) return NULL;
734 
735  /* A.SymDifference(Empty) == A */
736  if (lwgeom_is_empty(geom2)) return lwgeom_clone_deep(geom1);
737 
738  /* Empty.DymDifference(B) == B */
739  if (lwgeom_is_empty(geom1)) return lwgeom_clone_deep(geom2);
740 
741  initGEOS(lwnotice, lwgeom_geos_error);
742 
743  if (!(g1 = LWGEOM2GEOS(geom1, AUTOFIX))) GEOS_FAIL();
744  if (!(g2 = LWGEOM2GEOS(geom2, AUTOFIX))) GEOS_FREE_AND_FAIL(g1);
745 
746  g3 = GEOSSymDifference(g1, g2);
747 
748  if (!g3) GEOS_FREE_AND_FAIL(g1, g2);
749  GEOSSetSRID(g3, srid);
750 
751  if (!(result = GEOS2LWGEOM(g3, is3d)))
752  GEOS_FREE_AND_FAIL(g1, g2, g3);
753 
754  GEOS_FREE(g1, g2, g3);
755  return result;
756 }
757 
758 LWGEOM*
760 {
761  LWGEOM* result;
762  int32_t srid = RESULT_SRID(geom);
763  uint8_t is3d = FLAGS_GET_Z(geom->flags);
764  GEOSGeometry *g1, *g3;
765 
766  if (srid == SRID_INVALID) return NULL;
767 
768  if (lwgeom_is_empty(geom))
769  {
770  LWPOINT* lwp = lwpoint_construct_empty(srid, is3d, lwgeom_has_m(geom));
771  return lwpoint_as_lwgeom(lwp);
772  }
773 
774  initGEOS(lwnotice, lwgeom_geos_error);
775 
776  if (!(g1 = LWGEOM2GEOS(geom, AUTOFIX))) GEOS_FAIL();
777 
778  g3 = GEOSGetCentroid(g1);
779 
780  if (!g3) GEOS_FREE_AND_FAIL(g1);
781  GEOSSetSRID(g3, srid);
782 
783  if (!(result = GEOS2LWGEOM(g3, is3d)))
784  GEOS_FREE_AND_FAIL(g1);
785 
786  GEOS_FREE(g1, g3);
787 
788  return result;
789 }
790 
791 LWGEOM *
793 {
794  LWGEOM *result;
795  int32_t srid = RESULT_SRID(geom);
796  uint8_t is3d = FLAGS_GET_Z(geom->flags);
797  GEOSGeometry *g1, *g3;
798 
799  if (srid == SRID_INVALID) return NULL;
800 
801  if (lwgeom_is_empty(geom))
802  {
803  LWPOINT *lwp = lwpoint_construct_empty(srid, is3d, lwgeom_has_m(geom));
804  return lwpoint_as_lwgeom(lwp);
805  }
806 
807  initGEOS(lwnotice, lwgeom_geos_error);
808 
809  if (!(g1 = LWGEOM2GEOS(geom, AUTOFIX))) GEOS_FAIL();
810 
811  g3 = GEOSPointOnSurface(g1);
812 
813  if (!g3) GEOS_FREE_AND_FAIL(g1);
814  GEOSSetSRID(g3, srid);
815 
816  if (!(result = GEOS2LWGEOM(g3, is3d)))
817  GEOS_FREE_AND_FAIL(g1, g3);
818 
819  GEOS_FREE(g1, g3);
820 
821  return result;
822 }
823 
824 LWGEOM*
825 lwgeom_union(const LWGEOM* geom1, const LWGEOM* geom2)
826 {
827  LWGEOM* result;
828  int32_t srid = RESULT_SRID(geom1, geom2);
829  uint8_t is3d = (FLAGS_GET_Z(geom1->flags) || FLAGS_GET_Z(geom2->flags));
830  GEOSGeometry *g1, *g2, *g3;
831 
832  if (srid == SRID_INVALID) return NULL;
833 
834  /* A.Union(empty) == A */
835  if (lwgeom_is_empty(geom1)) return lwgeom_clone_deep(geom2);
836 
837  /* B.Union(empty) == B */
838  if (lwgeom_is_empty(geom2)) return lwgeom_clone_deep(geom1);
839 
840  initGEOS(lwnotice, lwgeom_geos_error);
841 
842  if (!(g1 = LWGEOM2GEOS(geom1, AUTOFIX))) GEOS_FAIL();
843  if (!(g2 = LWGEOM2GEOS(geom2, AUTOFIX))) GEOS_FREE_AND_FAIL(g1);
844 
845  g3 = GEOSUnion(g1, g2);
846 
847  if (!g3) GEOS_FREE_AND_FAIL(g1, g2);
848  GEOSSetSRID(g3, srid);
849 
850  if (!(result = GEOS2LWGEOM(g3, is3d)))
851  GEOS_FREE_AND_FAIL(g1, g2, g3);
852 
853  GEOS_FREE(g1, g2, g3);
854  return result;
855 }
856 
857 LWGEOM *
858 lwgeom_clip_by_rect(const LWGEOM *geom1, double x1, double y1, double x2, double y2)
859 {
860  LWGEOM *result;
861  GEOSGeometry *g1, *g3;
862  int is3d;
863 
864  /* A.Intersection(Empty) == Empty */
865  if ( lwgeom_is_empty(geom1) )
866  return lwgeom_clone_deep(geom1);
867 
868  is3d = FLAGS_GET_Z(geom1->flags);
869 
870  initGEOS(lwnotice, lwgeom_geos_error);
871 
872  if (!(g1 = LWGEOM2GEOS(geom1, AUTOFIX)))
873  GEOS_FAIL();
874 
875  if (!(g3 = GEOSClipByRect(g1, x1, y1, x2, y2)))
876  GEOS_FREE_AND_FAIL(g1);
877 
878  GEOS_FREE(g1);
879  result = GEOS2LWGEOM(g3, is3d);
880  GEOS_FREE(g3);
881 
882  if (!result)
883  GEOS_FAIL();
884 
885  result->srid = geom1->srid;
886 
887  return result;
888 }
889 
890 /* ------------ BuildArea stuff ---------------------------------------------------------------------{ */
891 
892 typedef struct Face_t
893 {
894  const GEOSGeometry* geom;
895  GEOSGeometry* env;
896  double envarea;
897  struct Face_t* parent; /* if this face is an hole of another one, or NULL */
898 } Face;
899 
900 static Face* newFace(const GEOSGeometry* g);
901 static void delFace(Face* f);
902 static unsigned int countParens(const Face* f);
903 static void findFaceHoles(Face** faces, int nfaces);
904 
905 static Face*
906 newFace(const GEOSGeometry* g)
907 {
908  Face* f = lwalloc(sizeof(Face));
909  f->geom = g;
910  f->env = GEOSEnvelope(f->geom);
911  GEOSArea(f->env, &f->envarea);
912  f->parent = NULL;
913  return f;
914 }
915 
916 static unsigned int
917 countParens(const Face* f)
918 {
919  unsigned int pcount = 0;
920  while (f->parent)
921  {
922  ++pcount;
923  f = f->parent;
924  }
925  return pcount;
926 }
927 
928 /* Destroy the face and release memory associated with it */
929 static void
931 {
932  GEOSGeom_destroy(f->env);
933  lwfree(f);
934 }
935 
936 static int
937 compare_by_envarea(const void* g1, const void* g2)
938 {
939  Face* f1 = *(Face**)g1;
940  Face* f2 = *(Face**)g2;
941  double n1 = f1->envarea;
942  double n2 = f2->envarea;
943 
944  if (n1 < n2) return 1;
945  if (n1 > n2) return -1;
946  return 0;
947 }
948 
949 /* Find holes of each face */
950 static void
951 findFaceHoles(Face** faces, int nfaces)
952 {
953  int i, j, h;
954 
955  /* We sort by envelope area so that we know holes are only after their shells */
956  qsort(faces, nfaces, sizeof(Face*), compare_by_envarea);
957  for (i = 0; i < nfaces; ++i)
958  {
959  Face* f = faces[i];
960  int nholes = GEOSGetNumInteriorRings(f->geom);
961  LWDEBUGF(2, "Scanning face %d with env area %g and %d holes", i, f->envarea, nholes);
962  for (h = 0; h < nholes; ++h)
963  {
964  const GEOSGeometry* hole = GEOSGetInteriorRingN(f->geom, h);
965  LWDEBUGF(2,
966  "Looking for hole %d/%d of face %d among %d other faces",
967  h + 1,
968  nholes,
969  i,
970  nfaces - i - 1);
971  for (j = i + 1; j < nfaces; ++j)
972  {
973  const GEOSGeometry* f2er;
974  Face* f2 = faces[j];
975  if (f2->parent) continue; /* hole already assigned */
976  f2er = GEOSGetExteriorRing(f2->geom);
977  /* TODO: can be optimized as the ring would have the same vertices, possibly in
978  * different order. Maybe comparing number of points could already be useful. */
979  if (GEOSEquals(f2er, hole))
980  {
981  LWDEBUGF(2, "Hole %d/%d of face %d is face %d", h + 1, nholes, i, j);
982  f2->parent = f;
983  break;
984  }
985  }
986  }
987  }
988 }
989 
990 static GEOSGeometry*
992 {
993  GEOSGeometry** geoms = lwalloc(sizeof(GEOSGeometry*) * nfaces);
994  GEOSGeometry* ret;
995  unsigned int ngeoms = 0;
996  int i;
997 
998  for (i = 0; i < nfaces; ++i)
999  {
1000  Face* f = faces[i];
1001  if (countParens(f) % 2) continue; /* we skip odd parents geoms */
1002  geoms[ngeoms++] = GEOSGeom_clone(f->geom);
1003  }
1004 
1005  ret = GEOSGeom_createCollection(GEOS_MULTIPOLYGON, geoms, ngeoms);
1006  lwfree(geoms);
1007  return ret;
1008 }
1009 
1010 GEOSGeometry*
1011 LWGEOM_GEOS_buildArea(const GEOSGeometry* geom_in)
1012 {
1013  GEOSGeometry* tmp;
1014  GEOSGeometry *geos_result, *shp;
1015  GEOSGeometry const* vgeoms[1];
1016  uint32_t i, ngeoms;
1017  int srid = GEOSGetSRID(geom_in);
1018  Face** geoms;
1019 #if POSTGIS_DEBUG_LEVEL >= 3
1020  LWGEOM *geos_geom;
1021  char *geom_ewkt;
1022 #endif
1023 
1024  vgeoms[0] = geom_in;
1025  geos_result = GEOSPolygonize(vgeoms, 1);
1026 
1027  LWDEBUGF(3, "GEOSpolygonize returned @ %p", geos_result);
1028 
1029  /* Null return from GEOSpolygonize (an exception) */
1030  if (!geos_result) return 0;
1031 
1032  /* We should now have a collection */
1033 #if PARANOIA_LEVEL > 0
1034  if (GEOSGeomTypeId(geos_result) != COLLECTIONTYPE)
1035  {
1036  GEOSGeom_destroy(geos_result);
1037  lwerror("%s [%d] Unexpected return from GEOSpolygonize", __FILE__, __LINE__);
1038  return 0;
1039  }
1040 #endif
1041 
1042  ngeoms = GEOSGetNumGeometries(geos_result);
1043 
1044 #if POSTGIS_DEBUG_LEVEL >= 3
1045  LWDEBUGF(3, "GEOSpolygonize: ngeoms in polygonize output: %d", ngeoms);
1046  geos_geom = GEOS2LWGEOM(geos_result, 0);
1047  geom_ewkt = lwgeom_to_ewkt(geos_geom);
1048  LWDEBUGF(3, "GEOSpolygonize: polygonized:%s", geom_ewkt);
1049  lwgeom_free(geos_geom);
1050  lwfree(geom_ewkt);
1051 #endif
1052 
1053  /* No geometries in collection, early out */
1054  if (ngeoms == 0)
1055  {
1056  GEOSSetSRID(geos_result, srid);
1057  return geos_result;
1058  }
1059 
1060  /* Return first geometry if we only have one in collection, to avoid the unnecessary Geometry clone below. */
1061  if (ngeoms == 1)
1062  {
1063  tmp = (GEOSGeometry*)GEOSGetGeometryN(geos_result, 0);
1064  if (!tmp)
1065  {
1066  GEOSGeom_destroy(geos_result);
1067  return 0; /* exception */
1068  }
1069  shp = GEOSGeom_clone(tmp);
1070  GEOSGeom_destroy(geos_result); /* only safe after the clone above */
1071  GEOSSetSRID(shp, srid);
1072  return shp;
1073  }
1074 
1075  LWDEBUGF(2, "Polygonize returned %d geoms", ngeoms);
1076 
1077  /*
1078  * Polygonizer returns a polygon for each face in the built topology.
1079  *
1080  * This means that for any face with holes we'll have other faces representing each hole. We can imagine a
1081  * parent-child relationship between these faces.
1082  *
1083  * In order to maximize the number of visible rings in output we only use those faces which have an even number
1084  * of parents.
1085  *
1086  * Example:
1087  *
1088  * +---------------+
1089  * | L0 | L0 has no parents
1090  * | +---------+ |
1091  * | | L1 | | L1 is an hole of L0
1092  * | | +---+ | |
1093  * | | |L2 | | | L2 is an hole of L1 (which is an hole of L0)
1094  * | | | | | |
1095  * | | +---+ | |
1096  * | +---------+ |
1097  * | |
1098  * +---------------+
1099  *
1100  * See http://trac.osgeo.org/postgis/ticket/1806
1101  *
1102  */
1103 
1104  /* Prepare face structures for later analysis */
1105  geoms = lwalloc(sizeof(Face**) * ngeoms);
1106  for (i = 0; i < ngeoms; ++i)
1107  geoms[i] = newFace(GEOSGetGeometryN(geos_result, i));
1108 
1109  /* Find faces representing other faces holes */
1110  findFaceHoles(geoms, ngeoms);
1111 
1112  /* Build a MultiPolygon composed only by faces with an even number of ancestors */
1113  tmp = collectFacesWithEvenAncestors(geoms, ngeoms);
1114 
1115  /* Cleanup face structures */
1116  for (i = 0; i < ngeoms; ++i)
1117  delFace(geoms[i]);
1118  lwfree(geoms);
1119 
1120  /* Faces referenced memory owned by geos_result. It is safe to destroy geos_result after deleting them. */
1121  GEOSGeom_destroy(geos_result);
1122 
1123  /* Run a single overlay operation to dissolve shared edges */
1124  shp = GEOSUnionCascaded(tmp);
1125  if (!shp)
1126  {
1127  GEOSGeom_destroy(tmp);
1128  return 0; /* exception */
1129  }
1130 
1131  GEOSGeom_destroy(tmp);
1132 
1133  GEOSSetSRID(shp, srid);
1134 
1135  return shp;
1136 }
1137 
1138 LWGEOM*
1140 {
1141  LWGEOM* result;
1142  int32_t srid = RESULT_SRID(geom);
1143  uint8_t is3d = FLAGS_GET_Z(geom->flags);
1144  GEOSGeometry *g1, *g3;
1145 
1146  if (srid == SRID_INVALID) return NULL;
1147 
1148  /* Can't build an area from an empty! */
1149  if (lwgeom_is_empty(geom)) return (LWGEOM*)lwpoly_construct_empty(srid, is3d, 0);
1150 
1151  initGEOS(lwnotice, lwgeom_geos_error);
1152 
1153  if (!(g1 = LWGEOM2GEOS(geom, AUTOFIX))) GEOS_FAIL();
1154 
1155  g3 = LWGEOM_GEOS_buildArea(g1);
1156 
1157  if (!g3) GEOS_FREE_AND_FAIL(g1);
1158  GEOSSetSRID(g3, srid);
1159 
1160  /* If no geometries are in result collection, return NULL */
1161  if (GEOSGetNumGeometries(g3) == 0)
1162  {
1163  GEOS_FREE(g1);
1164  return NULL;
1165  }
1166 
1167  if (!(result = GEOS2LWGEOM(g3, is3d)))
1168  GEOS_FREE_AND_FAIL(g1, g3);
1169 
1170  GEOS_FREE(g1, g3);
1171 
1172  return result;
1173 }
1174 
1175 /* ------------ end of BuildArea stuff ---------------------------------------------------------------------} */
1176 
1177 int
1179 {
1180  GEOSGeometry* g;
1181  int simple;
1182 
1183  /* Empty is always simple */
1184  if (lwgeom_is_empty(geom)) return LW_TRUE;
1185 
1186  initGEOS(lwnotice, lwgeom_geos_error);
1187 
1188  if (!(g = LWGEOM2GEOS(geom, AUTOFIX))) return -1;
1189 
1190  simple = GEOSisSimple(g);
1191  GEOSGeom_destroy(g);
1192 
1193  if (simple == 2) /* exception thrown */
1194  {
1195  lwerror("lwgeom_is_simple: %s", lwgeom_geos_errmsg);
1196  return -1;
1197  }
1198 
1199  return simple ? LW_TRUE : LW_FALSE;
1200 }
1201 
1202 LWGEOM*
1204 {
1205  LWGEOM* result;
1206  int32_t srid = RESULT_SRID(geom);
1207  uint8_t is3d = FLAGS_GET_Z(geom->flags);
1208  GEOSGeometry* g;
1209 
1210  if (srid == SRID_INVALID) return NULL;
1211 
1212  initGEOS(lwnotice, lwgeom_geos_error);
1213 
1214  if (!(g = LWGEOM2GEOS(geom, AUTOFIX))) GEOS_FAIL();
1215 
1216  if (!g) GEOS_FREE_AND_FAIL(g);
1217  GEOSSetSRID(g, srid);
1218 
1219  if (!(result = GEOS2LWGEOM(g, is3d)))
1220  GEOS_FREE_AND_FAIL(g);
1221 
1222  GEOS_FREE(g);
1223 
1224  return result;
1225 }
1226 
1227 LWGEOM*
1228 lwgeom_snap(const LWGEOM* geom1, const LWGEOM* geom2, double tolerance)
1229 {
1230  LWGEOM* result;
1231  int32_t srid = RESULT_SRID(geom1, geom2);
1232  uint8_t is3d = (FLAGS_GET_Z(geom1->flags) || FLAGS_GET_Z(geom2->flags));
1233  GEOSGeometry *g1, *g2, *g3;
1234 
1235  if (srid == SRID_INVALID) return NULL;
1236 
1237  initGEOS(lwnotice, lwgeom_geos_error);
1238 
1239  if (!(g1 = LWGEOM2GEOS(geom1, AUTOFIX))) GEOS_FAIL();
1240  if (!(g2 = LWGEOM2GEOS(geom2, AUTOFIX))) GEOS_FREE_AND_FAIL(g1);
1241 
1242  g3 = GEOSSnap(g1, g2, tolerance);
1243 
1244  if (!g3) GEOS_FREE_AND_FAIL(g1, g2);
1245  GEOSSetSRID(g3, srid);
1246 
1247  if (!(result = GEOS2LWGEOM(g3, is3d)))
1248  GEOS_FREE_AND_FAIL(g1, g2, g3);
1249 
1250  GEOS_FREE(g1, g2, g3);
1251  return result;
1252 }
1253 
1254 LWGEOM*
1255 lwgeom_sharedpaths(const LWGEOM* geom1, const LWGEOM* geom2)
1256 {
1257  LWGEOM* result;
1258  int32_t srid = RESULT_SRID(geom1, geom2);
1259  uint8_t is3d = (FLAGS_GET_Z(geom1->flags) || FLAGS_GET_Z(geom2->flags));
1260  GEOSGeometry *g1, *g2, *g3;
1261 
1262  if (srid == SRID_INVALID) return NULL;
1263 
1264  initGEOS(lwnotice, lwgeom_geos_error);
1265 
1266  if (!(g1 = LWGEOM2GEOS(geom1, AUTOFIX))) GEOS_FAIL();
1267  if (!(g2 = LWGEOM2GEOS(geom2, AUTOFIX))) GEOS_FREE_AND_FAIL(g1);
1268 
1269  g3 = GEOSSharedPaths(g1, g2);
1270 
1271  if (!g3) GEOS_FREE_AND_FAIL(g1, g2);
1272  GEOSSetSRID(g3, srid);
1273 
1274  if (!(result = GEOS2LWGEOM(g3, is3d)))
1275  GEOS_FREE_AND_FAIL(g1, g2, g3);
1276 
1277  GEOS_FREE(g1, g2, g3);
1278  return result;
1279 }
1280 
1281 static LWGEOM *
1282 lwline_offsetcurve(const LWLINE *lwline, double size, int quadsegs, int joinStyle, double mitreLimit)
1283 {
1284  LWGEOM* result;
1285  LWGEOM* geom = lwline_as_lwgeom(lwline);
1286  int32_t srid = RESULT_SRID(geom);
1287  uint8_t is3d = FLAGS_GET_Z(geom->flags);
1288  GEOSGeometry *g1, *g3;
1289 
1290  if (srid == SRID_INVALID) return NULL;
1291 
1292  initGEOS(lwnotice, lwgeom_geos_error);
1293 
1294  if (!(g1 = LWGEOM2GEOS(geom, AUTOFIX))) GEOS_FAIL();
1295 
1296  g3 = GEOSOffsetCurve(g1, size, quadsegs, joinStyle, mitreLimit);
1297 
1298  if (!g3)
1299  {
1300  GEOS_FREE(g1);
1301  return NULL;
1302  }
1303 
1304  GEOSSetSRID(g3, srid);
1305 
1306  if (!(result = GEOS2LWGEOM(g3, is3d)))
1307  GEOS_FREE_AND_FAIL(g1, g3);
1308 
1309  GEOS_FREE(g1, g3);
1310  return result;
1311 }
1312 
1313 static LWGEOM *
1314 lwcollection_offsetcurve(const LWCOLLECTION *col, double size, int quadsegs, int joinStyle, double mitreLimit)
1315 {
1316  const LWGEOM *geom = lwcollection_as_lwgeom(col);
1317  int32_t srid = RESULT_SRID(geom);
1318  uint8_t is3d = FLAGS_GET_Z(col->flags);
1319  LWCOLLECTION *result;
1320  LWGEOM *tmp;
1321  uint32_t i;
1322  if (srid == SRID_INVALID) return NULL;
1323 
1324  result = lwcollection_construct_empty(MULTILINETYPE, srid, is3d, LW_FALSE);
1325 
1326  for (i = 0; i < col->ngeoms; i++)
1327  {
1328  tmp = lwgeom_offsetcurve(col->geoms[i], size, quadsegs, joinStyle, mitreLimit);
1329 
1330  if (!tmp)
1331  {
1332  lwcollection_free(result);
1333  return NULL;
1334  }
1335 
1336  if (!lwgeom_is_empty(tmp))
1337  {
1338  if (lwgeom_is_collection(tmp))
1340  else
1341  result = lwcollection_add_lwgeom(result, tmp);
1342 
1343  if (!result)
1344  {
1345  lwgeom_free(tmp);
1346  return NULL;
1347  }
1348  }
1349  }
1350 
1351  if (result->ngeoms == 1)
1352  {
1353  tmp = result->geoms[0];
1354  lwcollection_release(result);
1355  return tmp;
1356  }
1357  else
1358  return lwcollection_as_lwgeom(result);
1359 }
1360 
1361 LWGEOM*
1362 lwgeom_offsetcurve(const LWGEOM* geom, double size, int quadsegs, int joinStyle, double mitreLimit)
1363 {
1364  int32_t srid = RESULT_SRID(geom);
1365  LWGEOM *result = NULL;
1366  LWGEOM *noded = NULL;
1367  if (srid == SRID_INVALID) return NULL;
1368 
1369  if (lwgeom_dimension(geom) != 1)
1370  {
1371  lwerror("%s: input is not linear", __func__, lwtype_name(geom->type));
1372  return NULL;
1373  }
1374 
1375  while (!result)
1376  {
1377  switch (geom->type)
1378  {
1379  case LINETYPE:
1380  result = lwline_offsetcurve(lwgeom_as_lwline(geom), size, quadsegs, joinStyle, mitreLimit);
1381  break;
1382  case COLLECTIONTYPE:
1383  case MULTILINETYPE:
1384  result = lwcollection_offsetcurve(lwgeom_as_lwcollection(geom), size, quadsegs, joinStyle, mitreLimit);
1385  break;
1386  default:
1387  lwerror("%s: unsupported geometry type: %s", __func__, lwtype_name(geom->type));
1388  return NULL;
1389  }
1390 
1391  if (result)
1392  {
1393  if (noded) lwgeom_free(noded);
1394  return result;
1395  }
1396  else if (!noded)
1397  {
1398  noded = lwgeom_node(geom);
1399  if (!noded)
1400  {
1401  lwerror("lwgeom_offsetcurve: cannot node input");
1402  return NULL;
1403  }
1404  geom = noded;
1405  }
1406  else
1407  {
1408  lwgeom_free(noded);
1409  lwerror("lwgeom_offsetcurve: noded geometry cannot be offset");
1410  return NULL;
1411  }
1412  }
1413 
1414  return result;
1415 }
1416 
1417 LWMPOINT*
1418 lwpoly_to_points(const LWPOLY* lwpoly, uint32_t npoints)
1419 {
1420  double area, bbox_area, bbox_width, bbox_height;
1421  GBOX bbox;
1422  const LWGEOM* lwgeom = (LWGEOM*)lwpoly;
1423  uint32_t sample_npoints, sample_sqrt, sample_width, sample_height;
1424  double sample_cell_size;
1425  uint32_t i, j, n;
1426  uint32_t iterations = 0;
1427  uint32_t npoints_generated = 0;
1428  uint32_t npoints_tested = 0;
1429  GEOSGeometry* g;
1430  const GEOSPreparedGeometry* gprep;
1431  GEOSGeometry* gpt;
1432  GEOSCoordSequence* gseq;
1433  LWMPOINT* mpt;
1434  int srid = lwgeom_get_srid(lwgeom);
1435  int done = 0;
1436  int* cells;
1437  const size_t size = 2 * sizeof(int);
1438  char tmp[2 * sizeof(int)];
1439  const size_t stride = 2 * sizeof(int);
1440 
1441  if (lwgeom_get_type(lwgeom) != POLYGONTYPE)
1442  {
1443  lwerror("%s: only polygons supported", __func__);
1444  return NULL;
1445  }
1446 
1447  if (npoints == 0 || lwgeom_is_empty(lwgeom)) return NULL;
1448 
1449  if (!lwpoly->bbox)
1450  lwgeom_calculate_gbox(lwgeom, &bbox);
1451  else
1452  bbox = *(lwpoly->bbox);
1453 
1454  area = lwpoly_area(lwpoly);
1455  bbox_width = bbox.xmax - bbox.xmin;
1456  bbox_height = bbox.ymax - bbox.ymin;
1457  bbox_area = bbox_width * bbox_height;
1458 
1459  if (area == 0.0 || bbox_area == 0.0)
1460  {
1461  lwerror("%s: zero area input polygon, TBD", __func__);
1462  return NULL;
1463  }
1464 
1465  /* Gross up our test set a bit to increase odds of getting coverage in one pass */
1466  sample_npoints = npoints * bbox_area / area;
1467 
1468  /* We're going to generate points using a sample grid as described
1469  * http://lin-ear-th-inking.blogspot.ca/2010/05/more-random-points-in-jts.html to try and get a more uniform
1470  * "random" set of points. So we have to figure out how to stick a grid into our box */
1471  sample_sqrt = lround(sqrt(sample_npoints));
1472  if (sample_sqrt == 0) sample_sqrt = 1;
1473 
1474  /* Calculate the grids we're going to randomize within */
1475  if (bbox_width > bbox_height)
1476  {
1477  sample_width = sample_sqrt;
1478  sample_height = ceil((double)sample_npoints / (double)sample_width);
1479  sample_cell_size = bbox_width / sample_width;
1480  }
1481  else
1482  {
1483  sample_height = sample_sqrt;
1484  sample_width = ceil((double)sample_npoints / (double)sample_height);
1485  sample_cell_size = bbox_height / sample_height;
1486  }
1487 
1488  /* Prepare the polygon for fast true/false testing */
1489  initGEOS(lwnotice, lwgeom_geos_error);
1490  g = (GEOSGeometry*)LWGEOM2GEOS(lwgeom, 0);
1491  if (!g)
1492  {
1493  lwerror("%s: Geometry could not be converted to GEOS: %s", __func__, lwgeom_geos_errmsg);
1494  return NULL;
1495  }
1496  gprep = GEOSPrepare(g);
1497 
1498  /* Get an empty multi-point ready to return */
1499  mpt = lwmpoint_construct_empty(srid, 0, 0);
1500 
1501  /* Init random number generator */
1502  srand(time(NULL));
1503 
1504  /* Now we fill in an array of cells, and then shuffle that array, */
1505  /* so we can visit the cells in random order to avoid visual ugliness */
1506  /* caused by visiting them sequentially */
1507  cells = lwalloc(2 * sizeof(int) * sample_height * sample_width);
1508  for (i = 0; i < sample_width; i++)
1509  {
1510  for (j = 0; j < sample_height; j++)
1511  {
1512  cells[2 * (i * sample_height + j)] = i;
1513  cells[2 * (i * sample_height + j) + 1] = j;
1514  }
1515  }
1516 
1517  /* shuffle */
1518  n = sample_height * sample_width;
1519  if (n > 1)
1520  {
1521  for (i = 0; i < n - 1; ++i)
1522  {
1523  size_t rnd = (size_t)rand();
1524  size_t j = i + rnd / (RAND_MAX / (n - i) + 1);
1525 
1526  memcpy(tmp, (char *)cells + j * stride, size);
1527  memcpy((char *)cells + j * stride, (char *)cells + i * stride, size);
1528  memcpy((char *)cells + i * stride, tmp, size);
1529  }
1530  }
1531 
1532  /* Start testing points */
1533  while (npoints_generated < npoints)
1534  {
1535  iterations++;
1536  for (i = 0; i < sample_width * sample_height; i++)
1537  {
1538  int contains = 0;
1539  double y = bbox.ymin + cells[2 * i] * sample_cell_size;
1540  double x = bbox.xmin + cells[2 * i + 1] * sample_cell_size;
1541  x += rand() * sample_cell_size / RAND_MAX;
1542  y += rand() * sample_cell_size / RAND_MAX;
1543  if (x >= bbox.xmax || y >= bbox.ymax) continue;
1544 
1545  gseq = GEOSCoordSeq_create(1, 2);
1546  GEOSCoordSeq_setX(gseq, 0, x);
1547  GEOSCoordSeq_setY(gseq, 0, y);
1548  gpt = GEOSGeom_createPoint(gseq);
1549 
1550  contains = GEOSPreparedIntersects(gprep, gpt);
1551 
1552  GEOSGeom_destroy(gpt);
1553 
1554  if (contains == 2)
1555  {
1556  GEOSPreparedGeom_destroy(gprep);
1557  GEOSGeom_destroy(g);
1558  lwerror("%s: GEOS exception on PreparedContains: %s", __func__, lwgeom_geos_errmsg);
1559  return NULL;
1560  }
1561  if (contains)
1562  {
1563  npoints_generated++;
1564  mpt = lwmpoint_add_lwpoint(mpt, lwpoint_make2d(srid, x, y));
1565  if (npoints_generated == npoints)
1566  {
1567  done = 1;
1568  break;
1569  }
1570  }
1571 
1572  /* Short-circuit check for ctrl-c occasionally */
1573  npoints_tested++;
1574  if (npoints_tested % 10000 == 0)
1575  LW_ON_INTERRUPT(GEOSPreparedGeom_destroy(gprep); GEOSGeom_destroy(g); return NULL);
1576 
1577  if (done) break;
1578  }
1579  if (done || iterations > 100) break;
1580  }
1581 
1582  GEOSPreparedGeom_destroy(gprep);
1583  GEOSGeom_destroy(g);
1584  lwfree(cells);
1585 
1586  return mpt;
1587 }
1588 
1589 /* Allocate points to sub-geometries by area, then call lwgeom_poly_to_points and bundle up final result in a single
1590  * multipoint. */
1591 LWMPOINT*
1592 lwmpoly_to_points(const LWMPOLY* lwmpoly, uint32_t npoints)
1593 {
1594  const LWGEOM* lwgeom = (LWGEOM*)lwmpoly;
1595  double area;
1596  uint32_t i;
1597  LWMPOINT* mpt = NULL;
1598 
1599  if (lwgeom_get_type(lwgeom) != MULTIPOLYGONTYPE)
1600  {
1601  lwerror("%s: only multipolygons supported", __func__);
1602  return NULL;
1603  }
1604  if (npoints == 0 || lwgeom_is_empty(lwgeom)) return NULL;
1605 
1606  area = lwgeom_area(lwgeom);
1607 
1608  for (i = 0; i < lwmpoly->ngeoms; i++)
1609  {
1610  double sub_area = lwpoly_area(lwmpoly->geoms[i]);
1611  int sub_npoints = lround(npoints * sub_area / area);
1612  if (sub_npoints > 0)
1613  {
1614  LWMPOINT* sub_mpt = lwpoly_to_points(lwmpoly->geoms[i], sub_npoints);
1615  if (!mpt)
1616  mpt = sub_mpt;
1617  else
1618  {
1619  uint32_t j;
1620  for (j = 0; j < sub_mpt->ngeoms; j++)
1621  mpt = lwmpoint_add_lwpoint(mpt, sub_mpt->geoms[j]);
1622  /* Just free the shell, leave the underlying lwpoints alone, as they are now owned by
1623  * the returning multipoint */
1624  lwfree(sub_mpt->geoms);
1625  lwgeom_release((LWGEOM*)sub_mpt);
1626  }
1627  }
1628  }
1629  return mpt;
1630 }
1631 
1632 LWMPOINT*
1633 lwgeom_to_points(const LWGEOM* lwgeom, uint32_t npoints)
1634 {
1635  switch (lwgeom_get_type(lwgeom))
1636  {
1637  case MULTIPOLYGONTYPE:
1638  return lwmpoly_to_points((LWMPOLY*)lwgeom, npoints);
1639  case POLYGONTYPE:
1640  return lwpoly_to_points((LWPOLY*)lwgeom, npoints);
1641  default:
1642  lwerror("%s: unsupported geometry type '%s'", __func__, lwtype_name(lwgeom_get_type(lwgeom)));
1643  return NULL;
1644  }
1645 }
1646 
1647 LWTIN*
1648 lwtin_from_geos(const GEOSGeometry* geom, uint8_t want3d)
1649 {
1650  int type = GEOSGeomTypeId(geom);
1651  int SRID = GEOSGetSRID(geom);
1652 
1653  /* GEOS's 0 is equivalent to our unknown as for SRID values */
1654  if (SRID == 0) SRID = SRID_UNKNOWN;
1655 
1656  if (want3d && !GEOSHasZ(geom))
1657  {
1658  LWDEBUG(3, "Geometry has no Z, won't provide one");
1659  want3d = 0;
1660  }
1661 
1662  switch (type)
1663  {
1664  LWTRIANGLE** geoms;
1665  uint32_t i, ngeoms;
1666  case GEOS_GEOMETRYCOLLECTION:
1667  LWDEBUG(4, "lwgeom_from_geometry: it's a Collection or Multi");
1668 
1669  ngeoms = GEOSGetNumGeometries(geom);
1670  geoms = NULL;
1671  if (ngeoms)
1672  {
1673  geoms = lwalloc(ngeoms * sizeof *geoms);
1674  if (!geoms)
1675  {
1676  lwerror("lwtin_from_geos: can't allocate geoms");
1677  return NULL;
1678  }
1679  for (i = 0; i < ngeoms; i++)
1680  {
1681  const GEOSGeometry *poly, *ring;
1682  const GEOSCoordSequence* cs;
1683  POINTARRAY* pa;
1684 
1685  poly = GEOSGetGeometryN(geom, i);
1686  ring = GEOSGetExteriorRing(poly);
1687  cs = GEOSGeom_getCoordSeq(ring);
1688  pa = ptarray_from_GEOSCoordSeq(cs, want3d);
1689 
1690  geoms[i] = lwtriangle_construct(SRID, NULL, pa);
1691  }
1692  }
1693  return (LWTIN*)lwcollection_construct(TINTYPE, SRID, NULL, ngeoms, (LWGEOM**)geoms);
1694  case GEOS_POLYGON:
1695  case GEOS_MULTIPOINT:
1696  case GEOS_MULTILINESTRING:
1697  case GEOS_MULTIPOLYGON:
1698  case GEOS_LINESTRING:
1699  case GEOS_LINEARRING:
1700  case GEOS_POINT:
1701  lwerror("lwtin_from_geos: invalid geometry type for tin: %d", type);
1702  break;
1703 
1704  default:
1705  lwerror("GEOS2LWGEOM: unknown geometry type: %d", type);
1706  return NULL;
1707  }
1708 
1709  /* shouldn't get here */
1710  return NULL;
1711 }
1712 /*
1713  * output = 1 for edges, 2 for TIN, 0 for polygons
1714  */
1715 LWGEOM*
1716 lwgeom_delaunay_triangulation(const LWGEOM* geom, double tolerance, int32_t output)
1717 {
1718  LWGEOM* result;
1719  int32_t srid = RESULT_SRID(geom);
1720  uint8_t is3d = FLAGS_GET_Z(geom->flags);
1721  GEOSGeometry *g1, *g3;
1722 
1723  if (output < 0 || output > 2)
1724  {
1725  lwerror("%s: invalid output type specified %d", __func__, output);
1726  return NULL;
1727  }
1728 
1729  if (srid == SRID_INVALID) return NULL;
1730 
1731  initGEOS(lwnotice, lwgeom_geos_error);
1732 
1733  if (!(g1 = LWGEOM2GEOS(geom, AUTOFIX))) GEOS_FAIL();
1734 
1735  /* if output != 1 we want polys */
1736  g3 = GEOSDelaunayTriangulation(g1, tolerance, output == 1);
1737 
1738  if (!g3) GEOS_FREE_AND_FAIL(g1);
1739  GEOSSetSRID(g3, srid);
1740 
1741  if (output == 2)
1742  {
1743  result = (LWGEOM*)lwtin_from_geos(g3, is3d);
1744  if (!result)
1745  {
1746  GEOS_FREE(g1, g3);
1747  lwerror("%s: cannot convert output geometry", __func__);
1748  return NULL;
1749  }
1750  lwgeom_set_srid(result, srid);
1751  }
1752  else if (!(result = GEOS2LWGEOM(g3, is3d)))
1753  GEOS_FREE_AND_FAIL(g1, g3);
1754 
1755  GEOS_FREE(g1, g3);
1756  return result;
1757 }
1758 
1759 static GEOSCoordSequence*
1761 {
1762  uint32_t i = 0;
1763  uint8_t num_dims = 2;
1764  LWPOINTITERATOR* it;
1765  GEOSCoordSequence* coords;
1766  POINT4D tmp;
1767 
1768  coords = GEOSCoordSeq_create(num_points, num_dims);
1769  if (!coords) return NULL;
1770 
1771  it = lwpointiterator_create(g);
1772  while (lwpointiterator_next(it, &tmp))
1773  {
1774  if (i >= num_points)
1775  {
1776  lwerror("Incorrect num_points provided to lwgeom_get_geos_coordseq_2d");
1777  GEOSCoordSeq_destroy(coords);
1779  return NULL;
1780  }
1781 
1782  if (!GEOSCoordSeq_setX(coords, i, tmp.x) || !GEOSCoordSeq_setY(coords, i, tmp.y))
1783  {
1784  GEOSCoordSeq_destroy(coords);
1786  return NULL;
1787  }
1788  i++;
1789  }
1791 
1792  return coords;
1793 }
1794 
1795 LWGEOM*
1796 lwgeom_voronoi_diagram(const LWGEOM* g, const GBOX* env, double tolerance, int output_edges)
1797 {
1798  uint32_t num_points = lwgeom_count_vertices(g);
1799  LWGEOM* lwgeom_result;
1800  char is_3d = LW_FALSE;
1801  int srid = lwgeom_get_srid(g);
1802  GEOSCoordSequence* coords;
1803  GEOSGeometry* geos_geom;
1804  GEOSGeometry* geos_env = NULL;
1805  GEOSGeometry* geos_result;
1806 
1807  if (num_points < 2)
1808  {
1810  return lwcollection_as_lwgeom(empty);
1811  }
1812 
1813  initGEOS(lwnotice, lwgeom_geos_error);
1814 
1815  /* Instead of using the standard LWGEOM2GEOS transformer, we read the vertices of the LWGEOM directly and put
1816  * them into a single GEOS CoordinateSeq that can be used to define a LineString. This allows us to process
1817  * geometry types that may not be supported by GEOS, and reduces the memory requirements in cases of many
1818  * geometries with few points (such as LWMPOINT).*/
1819  coords = lwgeom_get_geos_coordseq_2d(g, num_points);
1820  if (!coords) return NULL;
1821 
1822  geos_geom = GEOSGeom_createLineString(coords);
1823  if (!geos_geom)
1824  {
1825  GEOSCoordSeq_destroy(coords);
1826  return NULL;
1827  }
1828 
1829  if (env) geos_env = GBOX2GEOS(env);
1830 
1831  geos_result = GEOSVoronoiDiagram(geos_geom, geos_env, tolerance, output_edges);
1832 
1833  GEOSGeom_destroy(geos_geom);
1834  if (env) GEOSGeom_destroy(geos_env);
1835 
1836  if (!geos_result)
1837  {
1838  lwerror("GEOSVoronoiDiagram: %s", lwgeom_geos_errmsg);
1839  return NULL;
1840  }
1841 
1842  lwgeom_result = GEOS2LWGEOM(geos_result, is_3d);
1843  GEOSGeom_destroy(geos_result);
1844 
1845  lwgeom_set_srid(lwgeom_result, srid);
1846 
1847  return lwgeom_result;
1848 }
LWGEOM * lwgeom_unaryunion(const LWGEOM *geom)
double x
Definition: liblwgeom.h:354
#define LINETYPE
Definition: liblwgeom.h:85
static void geos_destroy(size_t count,...)
POINTARRAY * ptarray_from_GEOSCoordSeq(const GEOSCoordSequence *cs, uint8_t want3d)
LWGEOM * lwgeom_stroke(const LWGEOM *geom, uint32_t perQuad)
Definition: lwstroke.c:760
LWGEOM * lwgeom_voronoi_diagram(const LWGEOM *g, const GBOX *env, double tolerance, int output_edges)
Take vertices of a geometry and build the Voronoi diagram.
double z
Definition: liblwgeom.h:336
double y
Definition: liblwgeom.h:336
POINTARRAY * ptarray_construct(char hasz, char hasm, uint32_t npoints)
Construct an empty pointarray, allocating storage and setting the npoints, but not filling in any inf...
Definition: ptarray.c:62
int lwgeom_is_simple(const LWGEOM *geom)
LWLINE * lwline_construct_empty(int srid, char hasz, char hasm)
Definition: lwline.c:64
LWGEOM * lwgeom_clip_by_rect(const LWGEOM *geom1, double x1, double y1, double x2, double y2)
GEOSGeometry * make_geos_segment(double x1, double y1, double x2, double y2)
#define GEOS_FAIL()
uint32_t ngeoms
Definition: liblwgeom.h:470
int lwgeom_is_collection(const LWGEOM *lwgeom)
Determine whether a LWGEOM can contain sub-geometries or not.
Definition: lwgeom.c:1085
LWGEOM * GEOS2LWGEOM(const GEOSGeometry *geom, uint8_t want3d)
int lwpointiterator_next(LWPOINTITERATOR *s, POINT4D *p)
Attempts to assign the next point in the iterator to p, and advances the iterator to the next point...
Definition: lwiterator.c:212
LWCOLLECTION * lwcollection_construct(uint8_t type, int srid, GBOX *bbox, uint32_t ngeoms, LWGEOM **geoms)
Definition: lwcollection.c:43
double x
Definition: liblwgeom.h:336
static void delFace(Face *f)
void lwnotice(const char *fmt,...)
Write a notice out to the notice handler.
Definition: lwutil.c:177
void lwfree(void *mem)
Definition: lwutil.c:244
uint32_t lwgeom_get_type(const LWGEOM *geom)
Return LWTYPE number.
Definition: lwgeom.c:923
uint32_t ngeoms
Definition: liblwgeom.h:496
LWGEOM * lwgeom_linemerge(const LWGEOM *geom)
static int compare_by_envarea(const void *g1, const void *g2)
#define POLYGONTYPE
Definition: liblwgeom.h:86
LWPOINT * lwpoint_make2d(int srid, double x, double y)
Definition: lwpoint.c:163
Datum area(PG_FUNCTION_ARGS)
char * lwgeom_to_ewkt(const LWGEOM *lwgeom)
Return an alloced string.
Definition: lwgeom.c:556
uint8_t flags
Definition: liblwgeom.h:399
double xmax
Definition: liblwgeom.h:295
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1144
#define MULTIPOINTTYPE
Definition: liblwgeom.h:87
def fmt
Definition: pixval.py:92
LWGEOM * lwgeom_symdifference(const LWGEOM *geom1, const LWGEOM *geom2)
Datum contains(PG_FUNCTION_ARGS)
char lwgeom_geos_errmsg[LWGEOM_GEOS_ERRMSG_MAXSIZE]
void ptarray_set_point4d(POINTARRAY *pa, uint32_t n, const POINT4D *p4d)
Definition: lwgeom_api.c:425
static GEOSGeometry * ptarray_to_GEOSLinearRing(const POINTARRAY *pa, uint8_t autofix)
static LWGEOM * lwline_offsetcurve(const LWLINE *lwline, double size, int quadsegs, int joinStyle, double mitreLimit)
GEOSGeometry * GBOX2GEOS(const GBOX *box)
#define LWDEBUG(level, msg)
Definition: lwgeom_log.h:83
int32_t lwgeom_get_srid(const LWGEOM *geom)
Return SRID number.
Definition: lwgeom.c:916
#define LW_ON_INTERRUPT(x)
#define GEOS_FREE_AND_FAIL(...)
struct Face_t Face
static void findFaceHoles(Face **faces, int nfaces)
#define GEOS_FREE(...)
LWGEOM * lwgeom_clone_deep(const LWGEOM *lwgeom)
Deep clone an LWGEOM, everything is copied.
Definition: lwgeom.c:520
GBOX * bbox
Definition: liblwgeom.h:455
LWPOINT * lwpoint_construct_empty(int srid, char hasz, char hasm)
Definition: lwpoint.c:151
LWGEOM * lwgeom_centroid(const LWGEOM *geom)
uint32_t ngeoms
Definition: liblwgeom.h:509
#define RESULT_SRID(...)
POINTARRAY * point
Definition: liblwgeom.h:413
int ptarray_is_closed_2d(const POINTARRAY *pa)
Definition: ptarray.c:695
static LWGEOM * lwcollection_offsetcurve(const LWCOLLECTION *col, double size, int quadsegs, int joinStyle, double mitreLimit)
LWPOINTITERATOR * lwpointiterator_create(const LWGEOM *g)
Create a new LWPOINTITERATOR over supplied LWGEOM*.
Definition: lwiterator.c:244
int32_t srid
Definition: liblwgeom.h:401
GEOSGeometry * make_geos_point(double x, double y)
LWGEOM * lwgeom_intersection(const LWGEOM *geom1, const LWGEOM *geom2)
static GEOSCoordSequence * lwgeom_get_geos_coordseq_2d(const LWGEOM *g, uint32_t num_points)
LWCOLLECTION * lwcollection_concat_in_place(LWCOLLECTION *col1, const LWCOLLECTION *col2)
Appends all geometries from col2 to col1 in place.
Definition: lwcollection.c:240
uint32_t nrings
Definition: liblwgeom.h:457
#define AUTOFIX
LWTIN * lwtin_from_geos(const GEOSGeometry *geom, uint8_t want3d)
unsigned int uint32_t
Definition: uthash.h:78
int lwgeom_calculate_gbox(const LWGEOM *lwgeom, GBOX *gbox)
Calculate bounding box of a geometry, automatically taking into account whether it is cartesian or ge...
Definition: lwgeom.c:746
double x
Definition: liblwgeom.h:330
uint8_t flags
Definition: liblwgeom.h:506
LWMPOINT * lwpoly_to_points(const LWPOLY *lwpoly, uint32_t npoints)
LWGEOM * lwgeom_buildarea(const LWGEOM *geom)
Take a geometry and return an areal geometry (Polygon or MultiPolygon).
POINTARRAY * ptarray_addPoint(const POINTARRAY *pa, uint8_t *p, size_t pdims, uint32_t where)
Add a point in a pointarray.
Definition: ptarray.c:503
const POINT3DZ * getPoint3dz_cp(const POINTARRAY *pa, uint32_t n)
Returns a POINT3DZ pointer into the POINTARRAY serialized_ptlist, suitable for reading from...
Definition: lwgeom_api.c:378
double ymin
Definition: liblwgeom.h:296
void lwpointiterator_destroy(LWPOINTITERATOR *s)
Free all memory associated with the iterator.
Definition: lwiterator.c:269
const char * lwtype_name(uint8_t type)
Return the type name string associated with a type number (e.g.
Definition: lwutil.c:218
LWGEOM * geom
LWGEOM * lwline_as_lwgeom(const LWLINE *obj)
Definition: lwgeom.c:330
double xmin
Definition: liblwgeom.h:294
void lwgeom_geos_error(const char *fmt,...)
LWGEOM * lwgeom_node(const LWGEOM *lwgeom_in)
#define LW_FALSE
Definition: liblwgeom.h:76
LWGEOM * lwgeom_sharedpaths(const LWGEOM *geom1, const LWGEOM *geom2)
uint8_t flags
Definition: liblwgeom.h:371
static int32_t get_result_srid(size_t count, const char *funcname,...)
LWPOLY * lwpoly_construct(int srid, GBOX *bbox, uint32_t nrings, POINTARRAY **points)
Definition: lwpoly.c:43
LWMPOINT * lwgeom_to_points(const LWGEOM *lwgeom, uint32_t npoints)
LWPOLY ** geoms
Definition: liblwgeom.h:498
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:75
LWLINE * lwline_construct(int srid, GBOX *bbox, POINTARRAY *points)
Definition: lwline.c:42
LWGEOM ** geoms
Definition: liblwgeom.h:511
static GEOSGeometry * collectFacesWithEvenAncestors(Face **faces, int nfaces)
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:187
#define TINTYPE
Definition: liblwgeom.h:98
const GEOSGeometry * geom
LWGEOM * lwgeom_union(const LWGEOM *geom1, const LWGEOM *geom2)
static unsigned int countParens(const Face *f)
LWGEOM * lwgeom_difference(const LWGEOM *geom1, const LWGEOM *geom2)
#define SRID_INVALID
Definition: liblwgeom.h:191
POINTARRAY ** rings
Definition: liblwgeom.h:459
int count
Definition: genraster.py:56
LWPOINT ** geoms
Definition: liblwgeom.h:472
double lwpoly_area(const LWPOLY *poly)
Find the area of the outer ring - sum (area of inner rings).
Definition: lwpoly.c:441
LWMPOINT * lwmpoint_construct_empty(int srid, char hasz, char hasm)
Definition: lwmpoint.c:39
LWGEOM * lwgeom_delaunay_triangulation(const LWGEOM *geom, double tolerance, int32_t output)
Take vertices of a geometry and build a delaunay triangulation on them.
double ymax
Definition: liblwgeom.h:297
int lwgeom_has_arc(const LWGEOM *geom)
Definition: lwstroke.c:55
static Face * newFace(const GEOSGeometry *g)
double y
Definition: liblwgeom.h:330
#define LWGEOM_GEOS_ERRMSG_MAXSIZE
LWGEOM * lwgeom_geos_noop(const LWGEOM *geom)
Convert an LWGEOM to a GEOS Geometry and convert back – for debug only.
#define FLAGS_GET_Z(flags)
Macros for manipulating the &#39;flags&#39; byte.
Definition: liblwgeom.h:139
double z
Definition: liblwgeom.h:354
LWTRIANGLE * lwtriangle_construct(int srid, GBOX *bbox, POINTARRAY *points)
Definition: lwtriangle.c:40
LWGEOM * lwgeom_pointonsurface(const LWGEOM *geom)
LWLINE * lwgeom_as_lwline(const LWGEOM *lwgeom)
Definition: lwgeom.c:170
GEOSGeometry * LWGEOM2GEOS(const LWGEOM *lwgeom, uint8_t autofix)
GEOSGeometry * env
uint32_t lwgeom_count_vertices(const LWGEOM *geom)
Count the total number of vertices in any LWGEOM.
Definition: lwgeom.c:1235
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:89
LWMPOINT * lwmpoly_to_points(const LWMPOLY *lwmpoly, uint32_t npoints)
void lwcollection_release(LWCOLLECTION *lwcollection)
Definition: lwcollection.c:36
LWGEOM * lwgeom_snap(const LWGEOM *geom1, const LWGEOM *geom2, double tolerance)
Snap vertices and segments of a geometry to another using a given tolerance.
LWCOLLECTION * lwgeom_as_lwcollection(const LWGEOM *lwgeom)
Definition: lwgeom.c:224
double lwgeom_area(const LWGEOM *geom)
Definition: lwgeom.c:1857
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:1287
uint8_t * getPoint_internal(const POINTARRAY *pa, uint32_t n)
Definition: ptarray.c:1750
LWMPOINT * lwmpoint_add_lwpoint(LWMPOINT *mobj, const LWPOINT *obj)
Definition: lwmpoint.c:45
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:84
LWPOLY * lwpoly_construct_empty(int srid, char hasz, char hasm)
Definition: lwpoly.c:161
void lwgeom_release(LWGEOM *lwgeom)
Free the containing LWGEOM and the associated BOX.
Definition: lwgeom.c:459
uint8_t type
Definition: liblwgeom.h:398
type
Definition: ovdump.py:41
struct Face_t * parent
void free(void *)
void lwcollection_free(LWCOLLECTION *col)
Definition: lwcollection.c:356
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
Definition: lwgeom.c:335
const char * lwgeom_geos_version()
Return GEOS version string (not to be freed)
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...
LWPOINT * lwpoint_construct(int srid, GBOX *bbox, POINTARRAY *point)
Definition: lwpoint.c:129
void * lwalloc(size_t size)
Definition: lwutil.c:229
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:1393
double y
Definition: liblwgeom.h:354
#define MULTILINETYPE
Definition: liblwgeom.h:88
LWCOLLECTION * lwcollection_construct_empty(uint8_t type, int srid, char hasz, char hasm)
Definition: lwcollection.c:94
LWGEOM * lwgeom_normalize(const LWGEOM *geom)
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:88
#define FLAGS_NDIMS(flags)
Definition: liblwgeom.h:151
unsigned char uint8_t
Definition: uthash.h:79
LWGEOM * lwgeom_offsetcurve(const LWGEOM *geom, double size, int quadsegs, int joinStyle, double mitreLimit)
int lwgeom_has_m(const LWGEOM *geom)
Return LW_TRUE if geometry has M ordinates.
Definition: lwgeom.c:937
LWCOLLECTION * lwcollection_add_lwgeom(LWCOLLECTION *col, const LWGEOM *geom)
Appends geom to the collection managed by col.
Definition: lwcollection.c:187
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:190
#define COLLECTIONTYPE
Definition: liblwgeom.h:90
This library is the generic geometry handling section of PostGIS.
GEOSGeometry * LWGEOM_GEOS_buildArea(const GEOSGeometry *geom_in)
GEOSCoordSeq ptarray_to_GEOSCoordSeq(const POINTARRAY *, uint8_t fix_ring)
POINTARRAY * points
Definition: liblwgeom.h:424
const POINT2D * getPoint2d_cp(const POINTARRAY *pa, uint32_t n)
Returns a POINT2D pointer into the POINTARRAY serialized_ptlist, suitable for reading from...
Definition: lwgeom_api.c:364
LWGEOM * lwcollection_as_lwgeom(const LWCOLLECTION *obj)
Definition: lwgeom.c:300
uint32_t npoints
Definition: liblwgeom.h:373