38 #define AUTOFIX LW_TRUE
39 #define LWGEOM_GEOS_ERRMSG_MAXSIZE 256
56 #define GEOS_FREE(...) \
58 geos_destroy((sizeof((void*[]){__VA_ARGS__})/sizeof(void*)), __VA_ARGS__); \
64 lwerror("%s: GEOS Error: %s", __func__, lwgeom_geos_errmsg); \
69 #define GEOS_FAIL_DEBUG() \
72 lwdebug(1, "%s: GEOS Error: %s", __func__, lwgeom_geos_errmsg); \
76 #define GEOS_FREE_AND_FAIL(...) \
78 GEOS_FREE(__VA_ARGS__); \
82 #define GEOS_FREE_AND_FAIL_DEBUG(...) \
85 GEOS_FREE(__VA_ARGS__); \
91 #define RESULT_SRID(...) \
92 (get_result_srid((sizeof((const void*[]){__VA_ARGS__})/sizeof(void*)), __func__, __VA_ARGS__))
101 GEOSGeometry* g = va_arg(ap, GEOSGeometry*);
125 #if POSTGIS_GEOS_VERSION < 31000
127 POINT4D point = { 0.0, 0.0, 0.0, 0.0 };
130 LWDEBUG(2,
"ptarray_fromGEOSCoordSeq called");
132 if (!GEOSCoordSeq_getSize(cs, &size))
lwerror(
"Exception thrown");
134 LWDEBUGF(4,
" GEOSCoordSeq size: %d", size);
138 if (!GEOSCoordSeq_getDimensions(cs, &dims))
lwerror(
"Exception thrown");
140 LWDEBUGF(4,
" GEOSCoordSeq dimensions: %d", dims);
143 if (dims > 3) dims = 3;
146 LWDEBUGF(4,
" output dimensions: %d", dims);
149 #if POSTGIS_GEOS_VERSION >= 31000
153 for (i = 0; i < size; i++)
155 #if POSTGIS_GEOS_VERSION < 30800
156 GEOSCoordSeq_getX(cs, i, &(point.
x));
157 GEOSCoordSeq_getY(cs, i, &(point.
y));
158 if (dims >= 3) GEOSCoordSeq_getZ(cs, i, &(point.
z));
161 GEOSCoordSeq_getXYZ(cs, i, &(point.
x), &(point.
y), &(point.
z));
163 GEOSCoordSeq_getXY(cs, i, &(point.
x), &(point.
y));
176 int type = GEOSGeomTypeId(geom);
177 int SRID = GEOSGetSRID(geom);
182 if (want3d && !GEOSHasZ(geom))
184 LWDEBUG(3,
"Geometry has no Z, won't provide one");
190 const GEOSCoordSequence* cs;
192 const GEOSGeometry* g;
197 LWDEBUG(4,
"lwgeom_from_geometry: it's a Point");
198 cs = GEOSGeom_getCoordSeq(geom);
203 case GEOS_LINESTRING:
204 case GEOS_LINEARRING:
205 LWDEBUG(4,
"lwgeom_from_geometry: it's a LineString or LinearRing");
208 cs = GEOSGeom_getCoordSeq(geom);
213 LWDEBUG(4,
"lwgeom_from_geometry: it's a Polygon");
215 ngeoms = GEOSGetNumInteriorRings(geom);
217 g = GEOSGetExteriorRing(geom);
218 cs = GEOSGeom_getCoordSeq(g);
220 for (i = 0; i < ngeoms; i++)
222 g = GEOSGetInteriorRingN(geom, i);
223 cs = GEOSGeom_getCoordSeq(g);
228 case GEOS_MULTIPOINT:
229 case GEOS_MULTILINESTRING:
230 case GEOS_MULTIPOLYGON:
231 case GEOS_GEOMETRYCOLLECTION:
232 LWDEBUG(4,
"lwgeom_from_geometry: it's a Collection or Multi");
234 ngeoms = GEOSGetNumGeometries(geom);
239 for (i = 0; i < ngeoms; i++)
241 g = GEOSGetGeometryN(geom, i);
248 lwerror(
"GEOS2LWGEOM: unknown geometry type: %d",
type);
260 int append_points = 0;
271 lwerror(
"ptarray_to_GEOSCoordSeq called with fix_ring and 0 vertices in ring, cannot fix");
281 #if POSTGIS_GEOS_VERSION >= 31000
282 if (append_points == 0) {
286 lwerror(
"Error creating GEOS Coordinate Sequence");
292 if (!(sq = GEOSCoordSeq_create(pa->
npoints + append_points, dims)))
294 lwerror(
"Error creating GEOS Coordinate Sequence");
298 for (i = 0; i < pa->
npoints; i++)
304 LWDEBUGF(4,
"Point: %g,%g,%g", p3d->
x, p3d->
y, p3d->
z);
312 #if POSTGIS_GEOS_VERSION < 30800
313 GEOSCoordSeq_setX(sq, i, p2d->
x);
314 GEOSCoordSeq_setY(sq, i, p2d->
y);
315 if (dims == 3) GEOSCoordSeq_setZ(sq, i, p3d->
z);
318 GEOSCoordSeq_setXYZ(sq, i, p2d->
x, p2d->
y, p3d->
z);
320 GEOSCoordSeq_setXY(sq, i, p2d->
x, p2d->
y);
334 for (i = pa->
npoints; i < pa->npoints + append_points; i++)
336 #if POSTGIS_GEOS_VERSION < 30800
337 GEOSCoordSeq_setX(sq, i, p2d->
x);
338 GEOSCoordSeq_setY(sq, i, p2d->
y);
340 GEOSCoordSeq_setXY(sq, i, p2d->
x, p2d->
y);
343 if (dims == 3) GEOSCoordSeq_setZ(sq, i, p3d->
z);
351 static inline GEOSGeometry*
357 g = GEOSGeom_createLinearRing(sq);
364 GEOSGeometry* envelope;
366 GEOSCoordSequence* seq = GEOSCoordSeq_create(5, 2);
367 if (!seq)
return NULL;
369 #if POSTGIS_GEOS_VERSION < 30800
370 GEOSCoordSeq_setX(seq, 0, box->
xmin);
371 GEOSCoordSeq_setY(seq, 0, box->
ymin);
373 GEOSCoordSeq_setX(seq, 1, box->
xmax);
374 GEOSCoordSeq_setY(seq, 1, box->
ymin);
376 GEOSCoordSeq_setX(seq, 2, box->
xmax);
377 GEOSCoordSeq_setY(seq, 2, box->
ymax);
379 GEOSCoordSeq_setX(seq, 3, box->
xmin);
380 GEOSCoordSeq_setY(seq, 3, box->
ymax);
382 GEOSCoordSeq_setX(seq, 4, box->
xmin);
383 GEOSCoordSeq_setY(seq, 4, box->
ymin);
385 GEOSCoordSeq_setXY(seq, 0, box->
xmin, box->
ymin);
386 GEOSCoordSeq_setXY(seq, 1, box->
xmax, box->
ymin);
387 GEOSCoordSeq_setXY(seq, 2, box->
xmax, box->
ymax);
388 GEOSCoordSeq_setXY(seq, 3, box->
xmin, box->
ymax);
389 GEOSCoordSeq_setXY(seq, 4, box->
xmin, box->
ymin);
392 ring = GEOSGeom_createLinearRing(seq);
395 GEOSCoordSeq_destroy(seq);
399 envelope = GEOSGeom_createPolygon(ring, NULL, 0);
402 GEOSGeom_destroy(ring);
414 GEOSGeom* geoms = NULL;
415 uint32_t ngeoms, i, j;
417 #if LWDEBUG_LEVEL >= 4
433 GEOSGeometry* g =
LWGEOM2GEOS(lwgeom_stroked, autofix);
443 switch (lwgeom->
type)
449 g = GEOSGeom_createEmptyPoint();
452 #if POSTGIS_GEOS_VERSION < 30800
454 g = GEOSGeom_createPoint(sq);
459 g = GEOSGeom_createPoint(sq);
464 g = GEOSGeom_createPointFromXY(p->
x, p->
y);
483 g = GEOSGeom_createLineString(sq);
490 g = GEOSGeom_createEmptyPolygon();
494 if (!shell)
return NULL;
495 ngeoms = lwpoly->
nrings - 1;
496 if (ngeoms > 0) geoms =
lwalloc(
sizeof(GEOSGeom) * ngeoms);
498 for (i = 1; i < lwpoly->
nrings; i++)
504 for (k = 0; k < i - 1; k++)
505 GEOSGeom_destroy(geoms[k]);
507 GEOSGeom_destroy(shell);
511 g = GEOSGeom_createPolygon(shell, geoms, ngeoms);
519 g = GEOSGeom_createEmptyPolygon();
526 g = GEOSGeom_createPolygon(shell, NULL, 0);
537 geostype = GEOS_MULTIPOINT;
539 geostype = GEOS_MULTILINESTRING;
541 geostype = GEOS_MULTIPOLYGON;
543 geostype = GEOS_GEOMETRYCOLLECTION;
548 if (ngeoms > 0) geoms =
lwalloc(
sizeof(GEOSGeom) * ngeoms);
551 for (i = 0; i < ngeoms; ++i)
561 for (k = 0; k < j; k++)
562 GEOSGeom_destroy(geoms[k]);
568 g = GEOSGeom_createCollection(geostype, geoms, j);
569 if (ngeoms > 0)
lwfree(geoms);
578 GEOSSetSRID(g, lwgeom->
srid);
580 #if LWDEBUG_LEVEL >= 4
581 wkt = GEOSGeomToWKT(g);
582 LWDEBUGF(4,
"LWGEOM2GEOS: GEOSGeom: %s", wkt);
592 GEOSCoordSequence* seq = GEOSCoordSeq_create(1, 2);
593 GEOSGeometry* geom = NULL;
595 if (!seq)
return NULL;
597 #if POSTGIS_GEOS_VERSION < 30800
598 GEOSCoordSeq_setX(seq, 0,
x);
599 GEOSCoordSeq_setY(seq, 0,
y);
601 GEOSCoordSeq_setXY(seq, 0,
x,
y);
604 geom = GEOSGeom_createPoint(seq);
605 if (!geom) GEOSCoordSeq_destroy(seq);
612 GEOSCoordSequence* seq = GEOSCoordSeq_create(2, 2);
613 GEOSGeometry* geom = NULL;
615 if (!seq)
return NULL;
617 #if POSTGIS_GEOS_VERSION < 30800
618 GEOSCoordSeq_setX(seq, 0, x1);
619 GEOSCoordSeq_setY(seq, 0, y1);
620 GEOSCoordSeq_setX(seq, 1, x2);
621 GEOSCoordSeq_setY(seq, 1, y2);
623 GEOSCoordSeq_setXY(seq, 0, x1, y1);
624 GEOSCoordSeq_setXY(seq, 1, x2, y2);
627 geom = GEOSGeom_createLineString(seq);
628 if (!geom) GEOSCoordSeq_destroy(seq);
635 const char* ver = GEOSversion();
645 va_start(ap, funcname);
648 for(i = 0; i <
count; i++)
653 lwerror(
"%s: Geometry is null", funcname);
665 lwerror(
"%s: Operation on mixed SRID geometries (%d != %d)", funcname, srid, g->
srid);
690 GEOSSetSRID(g, srid);
728 #if POSTGIS_GEOS_VERSION < 30900
729 lwerror(
"Fixed-precision intersection requires GEOS-3.9 or higher");
733 g3 = GEOSIntersectionPrec(g1, g2, prec);
738 g3 = GEOSIntersection(g1, g2);
742 GEOSSetSRID(g3, srid);
776 #if POSTGIS_GEOS_VERSION < 31100
777 lwerror(
"Directed line merging requires GEOS-3.11 or higher");
781 g3 = GEOSLineMergeDirected(g1);
786 g3 = GEOSLineMerge(g1);
790 GEOSSetSRID(g3, srid);
825 #if POSTGIS_GEOS_VERSION < 30900
826 lwerror(
"Fixed-precision union requires GEOS-3.9 or higher");
830 g3 = GEOSUnaryUnionPrec(g1, prec);
835 g3 = GEOSUnaryUnion(g1);
839 GEOSSetSRID(g3, srid);
861 GEOSGeometry *g1, *g2, *g3;
877 #if POSTGIS_GEOS_VERSION < 30900
878 lwerror(
"Fixed-precision difference requires GEOS-3.9 or higher");
882 g3 = GEOSDifferencePrec(g1, g2, prec);
887 g3 = GEOSDifference(g1, g2);
891 GEOSSetSRID(g3, srid);
912 GEOSGeometry *g1, *g2, *g3;
928 #if POSTGIS_GEOS_VERSION < 30900
929 lwerror(
"Fixed-precision difference requires GEOS-3.9 or higher");
933 g3 = GEOSSymDifferencePrec(g1, g2, prec);
938 g3 = GEOSSymDifference(g1, g2);
942 GEOSSetSRID(g3, srid);
957 GEOSGeometry *g1, *g3;
971 g3 = GEOSGetCentroid(g1);
974 GEOSSetSRID(g3, srid);
987 #if POSTGIS_GEOS_VERSION < 30900
988 lwerror(
"Precision reduction requires GEOS-3.9 or higher");
995 GEOSGeometry *g1, *g3;
1006 g3 = GEOSGeom_setPrecision(g1, gridSize, 0);
1009 GEOSSetSRID(g3, srid);
1026 GEOSGeometry *g1, *g3;
1040 g3 = GEOSPointOnSurface(g1);
1043 GEOSSetSRID(g3, srid);
1065 GEOSGeometry *g1, *g2, *g3;
1080 if ( gridSize >= 0) {
1081 #if POSTGIS_GEOS_VERSION < 30900
1082 lwerror(
"Fixed-precision union requires GEOS-3.9 or higher");
1086 g3 = GEOSUnionPrec(g1, g2, gridSize);
1091 g3 = GEOSUnion(g1, g2);
1095 GEOSSetSRID(g3, srid);
1108 GEOSGeometry *g1, *g3;
1122 if (!(g3 = GEOSClipByRect(g1, x1, y1, x2, y2)))
1138 #if POSTGIS_GEOS_VERSION < 30800
1139 typedef struct Face_t
1141 const GEOSGeometry* geom;
1144 struct Face_t* parent;
1147 static Face* newFace(
const GEOSGeometry* g);
1148 static void delFace(Face* f);
1149 static unsigned int countParens(
const Face* f);
1150 static void findFaceHoles(Face** faces,
int nfaces);
1153 newFace(
const GEOSGeometry* g)
1155 Face* f =
lwalloc(
sizeof(Face));
1157 f->env = GEOSEnvelope(f->geom);
1158 GEOSArea(f->env, &f->envarea);
1164 countParens(
const Face* f)
1166 unsigned int pcount = 0;
1179 GEOSGeom_destroy(f->env);
1184 compare_by_envarea(
const void* g1,
const void* g2)
1186 Face* f1 = *(Face**)g1;
1187 Face* f2 = *(Face**)g2;
1188 double n1 = f1->envarea;
1189 double n2 = f2->envarea;
1191 if (n1 < n2)
return 1;
1192 if (n1 > n2)
return -1;
1198 findFaceHoles(Face** faces,
int nfaces)
1203 qsort(faces, nfaces,
sizeof(Face*), compare_by_envarea);
1204 for (i = 0; i < nfaces; ++i)
1207 int nholes = GEOSGetNumInteriorRings(f->geom);
1208 LWDEBUGF(2,
"Scanning face %d with env area %g and %d holes", i, f->envarea, nholes);
1209 for (h = 0; h < nholes; ++h)
1211 const GEOSGeometry* hole = GEOSGetInteriorRingN(f->geom, h);
1213 "Looking for hole %d/%d of face %d among %d other faces",
1218 for (j = i + 1; j < nfaces; ++j)
1220 const GEOSGeometry* f2er;
1221 Face* f2 = faces[j];
1222 if (f2->parent)
continue;
1223 f2er = GEOSGetExteriorRing(f2->geom);
1226 if (GEOSEquals(f2er, hole))
1228 LWDEBUGF(2,
"Hole %d/%d of face %d is face %d", h + 1, nholes, i, j);
1237 static GEOSGeometry*
1238 collectFacesWithEvenAncestors(Face** faces,
int nfaces)
1240 GEOSGeometry** geoms =
lwalloc(
sizeof(GEOSGeometry*) * nfaces);
1242 unsigned int ngeoms = 0;
1245 for (i = 0; i < nfaces; ++i)
1248 if (countParens(f) % 2)
continue;
1249 geoms[ngeoms++] = GEOSGeom_clone(f->geom);
1252 ret = GEOSGeom_createCollection(GEOS_MULTIPOLYGON, geoms, ngeoms);
1258 LWGEOM_GEOS_buildArea(
const GEOSGeometry* geom_in)
1261 GEOSGeometry *geos_result, *shp;
1262 GEOSGeometry
const* vgeoms[1];
1264 int srid = GEOSGetSRID(geom_in);
1266 #if POSTGIS_DEBUG_LEVEL >= 3
1271 vgeoms[0] = geom_in;
1272 geos_result = GEOSPolygonize(vgeoms, 1);
1274 LWDEBUGF(3,
"GEOSpolygonize returned @ %p", geos_result);
1277 if (!geos_result)
return 0;
1280 #if PARANOIA_LEVEL > 0
1283 GEOSGeom_destroy(geos_result);
1284 lwerror(
"%s [%d] Unexpected return from GEOSpolygonize", __FILE__, __LINE__);
1289 ngeoms = GEOSGetNumGeometries(geos_result);
1291 #if POSTGIS_DEBUG_LEVEL >= 3
1292 LWDEBUGF(3,
"GEOSpolygonize: ngeoms in polygonize output: %d", ngeoms);
1295 LWDEBUGF(3,
"GEOSpolygonize: polygonized:%s", geom_ewkt);
1303 GEOSSetSRID(geos_result, srid);
1310 tmp = (GEOSGeometry*)GEOSGetGeometryN(geos_result, 0);
1313 GEOSGeom_destroy(geos_result);
1316 shp = GEOSGeom_clone(tmp);
1317 GEOSGeom_destroy(geos_result);
1318 GEOSSetSRID(shp, srid);
1322 LWDEBUGF(2,
"Polygonize returned %d geoms", ngeoms);
1352 geoms =
lwalloc(
sizeof(Face**) * ngeoms);
1353 for (i = 0; i < ngeoms; ++i)
1354 geoms[i] = newFace(GEOSGetGeometryN(geos_result, i));
1357 findFaceHoles(geoms, ngeoms);
1360 tmp = collectFacesWithEvenAncestors(geoms, ngeoms);
1363 for (i = 0; i < ngeoms; ++i)
1368 GEOSGeom_destroy(geos_result);
1371 shp = GEOSUnionCascaded(tmp);
1374 GEOSGeom_destroy(tmp);
1378 GEOSGeom_destroy(tmp);
1380 GEOSSetSRID(shp, srid);
1392 GEOSGeometry *g1, *g3;
1403 #if POSTGIS_GEOS_VERSION < 30800
1404 g3 = LWGEOM_GEOS_buildArea(g1);
1406 g3 = GEOSBuildArea(g1);
1410 GEOSSetSRID(g3, srid);
1413 if (GEOSGetNumGeometries(g3) == 0)
1442 simple = GEOSisSimple(g);
1443 GEOSGeom_destroy(g);
1469 GEOSSetSRID(g, srid);
1485 GEOSGeometry *g1, *g2, *g3;
1494 g3 = GEOSSnap(g1, g2, tolerance);
1497 GEOSSetSRID(g3, srid);
1512 GEOSGeometry *g1, *g2, *g3;
1521 g3 = GEOSSharedPaths(g1, g2);
1524 GEOSSetSRID(g3, srid);
1540 GEOSGeometry *g1, *g3;
1548 g3 = GEOSOffsetCurve(g1, size, quadsegs, joinStyle, mitreLimit);
1556 GEOSSetSRID(g3, srid);
1578 for (i = 0; i < col->
ngeoms; i++)
1653 lwerror(
"lwgeom_offsetcurve: cannot node input");
1661 lwerror(
"lwgeom_offsetcurve: noded geometry cannot be offset");
1672 double area, bbox_area, bbox_width, bbox_height;
1675 uint32_t sample_npoints, sample_sqrt, sample_width, sample_height;
1676 double sample_cell_size;
1678 uint32_t iterations = 0;
1679 uint32_t npoints_generated = 0;
1680 uint32_t npoints_tested = 0;
1682 const GEOSPreparedGeometry* gprep;
1684 GEOSCoordSequence* gseq;
1689 const size_t size = 2 *
sizeof(int);
1690 char tmp[2 *
sizeof(int)];
1691 const size_t stride = 2 *
sizeof(int);
1695 lwerror(
"%s: only polygons supported", __func__);
1704 bbox = *(lwpoly->
bbox);
1707 bbox_width = bbox.
xmax - bbox.
xmin;
1708 bbox_height = bbox.
ymax - bbox.
ymin;
1709 bbox_area = bbox_width * bbox_height;
1711 if (area == 0.0 || bbox_area == 0.0)
1713 lwerror(
"%s: zero area input polygon, TBD", __func__);
1719 sample_npoints = npoints *
FP_MIN(bbox_area / area, 10000.0);
1724 sample_sqrt = lround(sqrt(sample_npoints));
1725 if (sample_sqrt == 0)
1729 if (bbox_width > bbox_height)
1731 sample_width = sample_sqrt;
1732 sample_height = ceil((
double)sample_npoints / (
double)sample_width);
1733 sample_cell_size = bbox_width / sample_width;
1737 sample_height = sample_sqrt;
1738 sample_width = ceil((
double)sample_npoints / (
double)sample_height);
1739 sample_cell_size = bbox_height / sample_height;
1750 gprep = GEOSPrepare(g);
1764 cells =
lwalloc(2 *
sizeof(
int) * sample_height * sample_width);
1765 for (i = 0; i < sample_width; i++)
1767 for (j = 0; j < sample_height; j++)
1769 cells[2 * (i * sample_height + j)] = i;
1770 cells[2 * (i * sample_height + j) + 1] = j;
1775 n = sample_height * sample_width;
1778 for (i = n - 1; i > 0; i--)
1782 memcpy(tmp, (
char *)cells + j * stride, size);
1783 memcpy((
char *)cells + j * stride, (
char *)cells + i * stride, size);
1784 memcpy((
char *)cells + i * stride, tmp, size);
1789 while (npoints_generated < npoints)
1792 for (i = 0; i < sample_width * sample_height; i++)
1795 double y = bbox.
ymin + cells[2 * i] * sample_cell_size;
1796 double x = bbox.
xmin + cells[2 * i + 1] * sample_cell_size;
1799 if (
x >= bbox.
xmax ||
y >= bbox.
ymax)
continue;
1801 gseq = GEOSCoordSeq_create(1, 2);
1802 #if POSTGIS_GEOS_VERSION < 30800
1803 GEOSCoordSeq_setX(gseq, 0,
x);
1804 GEOSCoordSeq_setY(gseq, 0,
y);
1806 GEOSCoordSeq_setXY(gseq, 0,
x,
y);
1808 gpt = GEOSGeom_createPoint(gseq);
1810 contains = GEOSPreparedIntersects(gprep, gpt);
1812 GEOSGeom_destroy(gpt);
1816 GEOSPreparedGeom_destroy(gprep);
1817 GEOSGeom_destroy(g);
1823 npoints_generated++;
1825 if (npoints_generated == npoints)
1834 if (npoints_tested % 10000 == 0)
1835 LW_ON_INTERRUPT(GEOSPreparedGeom_destroy(gprep); GEOSGeom_destroy(g);
return NULL);
1839 if (done || iterations > 100)
break;
1842 GEOSPreparedGeom_destroy(gprep);
1843 GEOSGeom_destroy(g);
1861 lwerror(
"%s: only multipolygons supported", __func__);
1868 for (i = 0; i < lwmpoly->
ngeoms; i++)
1871 int sub_npoints = lround(npoints * sub_area / area);
1872 if (sub_npoints > 0)
1880 for (j = 0; j < sub_mpt->
ngeoms; j++)
1910 int type = GEOSGeomTypeId(geom);
1911 int SRID = GEOSGetSRID(geom);
1916 if (want3d && !GEOSHasZ(geom))
1918 LWDEBUG(3,
"Geometry has no Z, won't provide one");
1926 case GEOS_GEOMETRYCOLLECTION:
1927 LWDEBUG(4,
"lwgeom_from_geometry: it's a Collection or Multi");
1929 ngeoms = GEOSGetNumGeometries(geom);
1933 geoms =
lwalloc(ngeoms *
sizeof *geoms);
1936 lwerror(
"lwtin_from_geos: can't allocate geoms");
1939 for (i = 0; i < ngeoms; i++)
1941 const GEOSGeometry *poly, *ring;
1942 const GEOSCoordSequence* cs;
1945 poly = GEOSGetGeometryN(geom, i);
1946 ring = GEOSGetExteriorRing(poly);
1947 cs = GEOSGeom_getCoordSeq(ring);
1955 case GEOS_MULTIPOINT:
1956 case GEOS_MULTILINESTRING:
1957 case GEOS_MULTIPOLYGON:
1958 case GEOS_LINESTRING:
1959 case GEOS_LINEARRING:
1961 lwerror(
"lwtin_from_geos: invalid geometry type for tin: %d",
type);
1965 lwerror(
"GEOS2LWGEOM: unknown geometry type: %d",
type);
1981 GEOSGeometry *g1, *g3;
1983 if (output < 0 || output > 2)
1985 lwerror(
"%s: invalid output type specified %d", __func__, output);
1996 g3 = GEOSDelaunayTriangulation(g1, tolerance, output == 1);
1999 GEOSSetSRID(g3, srid);
2007 lwerror(
"%s: cannot convert output geometry", __func__);
2020 static GEOSCoordSequence*
2024 uint8_t num_dims = 2;
2026 GEOSCoordSequence* coords;
2029 coords = GEOSCoordSeq_create(num_points, num_dims);
2030 if (!coords)
return NULL;
2035 if (i >= num_points)
2037 lwerror(
"Incorrect num_points provided to lwgeom_get_geos_coordseq_2d");
2038 GEOSCoordSeq_destroy(coords);
2043 #if POSTGIS_GEOS_VERSION < 30800
2044 if (!GEOSCoordSeq_setX(coords, i, tmp.
x) || !GEOSCoordSeq_setY(coords, i, tmp.
y))
2046 if (!GEOSCoordSeq_setXY(coords, i, tmp.
x, tmp.
y))
2049 GEOSCoordSeq_destroy(coords);
2067 GEOSCoordSequence* coords;
2068 GEOSGeometry* geos_geom;
2069 GEOSGeometry* geos_env = NULL;
2070 GEOSGeometry* geos_result;
2085 if (!coords)
return NULL;
2087 geos_geom = GEOSGeom_createLineString(coords);
2090 GEOSCoordSeq_destroy(coords);
2096 geos_result = GEOSVoronoiDiagram(geos_geom, geos_env, tolerance, output_edges);
2098 GEOSGeom_destroy(geos_geom);
2099 if (env) GEOSGeom_destroy(geos_env);
2108 GEOSGeom_destroy(geos_result);
2112 return lwgeom_result;
2116 #if POSTGIS_GEOS_VERSION >= 31100
2118 lwgeom_concavehull(
const LWGEOM* geom,
double ratio, uint32_t allow_holes)
2123 GEOSGeometry *g1, *g3;
2130 geosGeomType = GEOSGeomTypeId(g1);
2131 if (geosGeomType == GEOS_POLYGON || geosGeomType == GEOS_MULTIPOLYGON) {
2133 g3 = GEOSConcaveHullOfPolygons(g1, ratio, is_tight, allow_holes);
2136 g3 = GEOSConcaveHull(g1, ratio, allow_holes);
2142 GEOSSetSRID(g3, srid);
2152 lwgeom_simplify_polygonal(
const LWGEOM* geom,
double vertex_fraction, uint32_t is_outer)
2157 GEOSGeometry *g1, *g3;
2163 g3 = GEOSPolygonHullSimplify(g1, is_outer, vertex_fraction);
2168 GEOSSetSRID(g3, srid);
2178 lwgeom_triangulate_polygon(
const LWGEOM* geom)
2183 GEOSGeometry *g1, *g3;
2192 g3 = GEOSConstrainedDelaunayTriangulation(g1);
2195 GEOSSetSRID(g3, srid);
char result[OUT_DOUBLE_BUFFER_SIZE]
#define LWGEOM_GEOS_ERRMSG_MAXSIZE
LWGEOM * lwgeom_centroid(const LWGEOM *geom)
LWGEOM * lwgeom_intersection_prec(const LWGEOM *geom1, const LWGEOM *geom2, double prec)
LWGEOM * lwgeom_geos_noop(const LWGEOM *geom)
Convert an LWGEOM to a GEOS Geometry and convert back – for debug only.
static LWGEOM * lwcollection_offsetcurve(const LWCOLLECTION *col, double size, int quadsegs, int joinStyle, double mitreLimit)
LWGEOM * lwgeom_linemerge_directed(const LWGEOM *geom, int directed)
static GEOSGeometry * ptarray_to_GEOSLinearRing(const POINTARRAY *pa, uint8_t autofix)
LWGEOM * lwgeom_symdifference(const LWGEOM *geom1, const LWGEOM *geom2)
LWGEOM * lwgeom_unaryunion(const LWGEOM *geom)
LWGEOM * lwgeom_union(const LWGEOM *g1, const LWGEOM *g2)
GEOSGeometry * make_geos_point(double x, double y)
static LWGEOM * lwline_offsetcurve(const LWLINE *lwline, double size, int quadsegs, int joinStyle, double mitreLimit)
LWGEOM * lwgeom_offsetcurve(const LWGEOM *geom, double size, int quadsegs, int joinStyle, double mitreLimit)
LWGEOM * lwgeom_delaunay_triangulation(const LWGEOM *geom, double tolerance, int32_t output)
Take vertices of a geometry and build a delaunay triangulation on them.
LWGEOM * lwgeom_difference_prec(const LWGEOM *geom1, const LWGEOM *geom2, double prec)
LWGEOM * lwgeom_snap(const LWGEOM *geom1, const LWGEOM *geom2, double tolerance)
Snap vertices and segments of a geometry to another using a given tolerance.
LWGEOM * lwgeom_difference(const LWGEOM *geom1, const LWGEOM *geom2)
int lwgeom_is_simple(const LWGEOM *geom)
LWGEOM * lwgeom_pointonsurface(const LWGEOM *geom)
LWGEOM * lwgeom_sharedpaths(const LWGEOM *geom1, const LWGEOM *geom2)
LWGEOM * lwgeom_symdifference_prec(const LWGEOM *geom1, const LWGEOM *geom2, double prec)
char lwgeom_geos_errmsg[LWGEOM_GEOS_ERRMSG_MAXSIZE]
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.
LWGEOM * lwgeom_union_prec(const LWGEOM *geom1, const LWGEOM *geom2, double gridSize)
LWGEOM * lwgeom_normalize(const LWGEOM *geom)
static int32_t get_result_srid(size_t count, const char *funcname,...)
#define GEOS_FREE_AND_FAIL_DEBUG(...)
LWGEOM * lwgeom_buildarea(const LWGEOM *geom)
Take a geometry and return an areal geometry (Polygon or MultiPolygon).
#define GEOS_FAIL_DEBUG()
LWGEOM * lwgeom_linemerge(const LWGEOM *geom)
#define GEOS_FREE_AND_FAIL(...)
GEOSGeometry * make_geos_segment(double x1, double y1, double x2, double y2)
static void geos_destroy(size_t count,...)
GEOSGeometry * GBOX2GEOS(const GBOX *box)
const char * lwgeom_geos_version()
Return GEOS version string (not to be freed)
GEOSGeometry * LWGEOM2GEOS(const LWGEOM *lwgeom, uint8_t autofix)
LWGEOM * lwgeom_reduceprecision(const LWGEOM *geom, double gridSize)
LWMPOINT * lwgeom_to_points(const LWGEOM *lwgeom, uint32_t npoints, int32_t seed)
LWGEOM * GEOS2LWGEOM(const GEOSGeometry *geom, uint8_t want3d)
GEOSCoordSeq ptarray_to_GEOSCoordSeq(const POINTARRAY *, uint8_t fix_ring)
LWMPOINT * lwmpoly_to_points(const LWMPOLY *lwmpoly, uint32_t npoints, int32_t seed)
LWGEOM * lwgeom_unaryunion_prec(const LWGEOM *geom, double prec)
POINTARRAY * ptarray_from_GEOSCoordSeq(const GEOSCoordSequence *cs, uint8_t want3d)
LWMPOINT * lwpoly_to_points(const LWPOLY *lwpoly, uint32_t npoints, int32_t seed)
LWGEOM * lwgeom_intersection(const LWGEOM *g1, const LWGEOM *g2)
LWGEOM * lwgeom_clip_by_rect(const LWGEOM *geom1, double x1, double y1, double x2, double y2)
void lwgeom_geos_error(const char *fmt,...)
static GEOSCoordSequence * lwgeom_get_geos_coordseq_2d(const LWGEOM *g, uint32_t num_points)
LWTIN * lwtin_from_geos(const GEOSGeometry *geom, uint8_t want3d)
LWPOINT * lwpoint_construct_empty(int32_t srid, char hasz, char hasm)
LWLINE * lwgeom_as_lwline(const LWGEOM *lwgeom)
LWGEOM * lwline_as_lwgeom(const LWLINE *obj)
LWGEOM * lwcollection_as_lwgeom(const LWCOLLECTION *obj)
int32_t lwgeom_get_srid(const LWGEOM *geom)
Return SRID number.
LWGEOM * lwgeom_node(const LWGEOM *lwgeom_in)
LWPOINT * lwpoint_make2d(int32_t srid, double x, double y)
LWPOINTITERATOR * lwpointiterator_create(const LWGEOM *g)
Create a new LWPOINTITERATOR over supplied LWGEOM*.
void lwgeom_set_srid(LWGEOM *geom, int32_t srid)
Set the SRID on an LWGEOM For collections, only the parent gets an SRID, all the children get SRID_UN...
void lwgeom_free(LWGEOM *geom)
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.
POINTARRAY * ptarray_addPoint(const POINTARRAY *pa, uint8_t *p, size_t pdims, uint32_t where)
Add a point in a pointarray.
LWGEOM * lwgeom_stroke(const LWGEOM *geom, uint32_t perQuad)
Convert type with arcs into equivalent linearized type.
LWGEOM * lwgeom_clone_deep(const LWGEOM *lwgeom)
Deep clone an LWGEOM, everything is copied.
void lwpointiterator_destroy(LWPOINTITERATOR *s)
Free all memory associated with the iterator.
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...
int lwgeom_has_z(const LWGEOM *geom)
Return LW_TRUE if geometry has Z ordinates.
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
char * lwgeom_to_ewkt(const LWGEOM *lwgeom)
Return an alloced string.
#define FLAGS_GET_Z(flags)
LWLINE * lwline_construct(int32_t srid, GBOX *bbox, POINTARRAY *points)
LWMPOINT * lwmpoint_add_lwpoint(LWMPOINT *mobj, const LWPOINT *obj)
double lwgeom_area(const LWGEOM *geom)
int lwgeom_type_arc(const LWGEOM *geom)
Geometry type is one of the potentially "arc containing" types (circstring, multicurve,...
uint32_t lwgeom_count_vertices(const LWGEOM *geom)
Count the total number of vertices in any LWGEOM.
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...
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
#define FLAGS_NDIMS(flags)
int lwgeom_is_collection(const LWGEOM *lwgeom)
Determine whether a LWGEOM can contain sub-geometries or not.
LWMPOINT * lwmpoint_construct_empty(int32_t srid, char hasz, char hasm)
LWPOINT * lwpoint_construct(int32_t srid, GBOX *bbox, POINTARRAY *point)
void lwcollection_release(LWCOLLECTION *lwcollection)
LWCOLLECTION * lwcollection_construct_empty(uint8_t type, int32_t srid, char hasz, char hasm)
const char * lwtype_name(uint8_t type)
Return the type name string associated with a type number (e.g.
#define FLAGS_GET_M(flags)
void lwcollection_free(LWCOLLECTION *col)
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...
LWCOLLECTION * lwcollection_concat_in_place(LWCOLLECTION *col1, const LWCOLLECTION *col2)
Appends all geometries from col2 to col1 in place.
LWCOLLECTION * lwcollection_add_lwgeom(LWCOLLECTION *col, const LWGEOM *geom)
Appends geom to the collection managed by col.
void * lwalloc(size_t size)
LWCOLLECTION * lwcollection_construct(uint8_t type, int32_t srid, GBOX *bbox, uint32_t ngeoms, LWGEOM **geoms)
int ptarray_is_closed_2d(const POINTARRAY *pa)
LWCOLLECTION * lwgeom_as_lwcollection(const LWGEOM *lwgeom)
#define LW_TRUE
Return types for functions with status returns.
void lwgeom_release(LWGEOM *lwgeom)
Free the containing LWGEOM and the associated BOX.
#define SRID_UNKNOWN
Unknown SRID value.
int lwgeom_has_m(const LWGEOM *geom)
Return LW_TRUE if geometry has M ordinates.
LWPOLY * lwpoly_construct_empty(int32_t srid, char hasz, char hasm)
LWPOLY * lwpoly_construct(int32_t srid, GBOX *bbox, uint32_t nrings, POINTARRAY **points)
void ptarray_set_point4d(POINTARRAY *pa, uint32_t n, const POINT4D *p4d)
LWLINE * lwline_construct_empty(int32_t srid, char hasz, char hasm)
LWTRIANGLE * lwtriangle_construct(int32_t srid, GBOX *bbox, POINTARRAY *points)
This library is the generic geometry handling section of PostGIS.
#define LW_ON_INTERRUPT(x)
double lwpoly_area(const LWPOLY *poly)
Find the area of the outer ring - sum (area of inner rings).
#define LWDEBUG(level, msg)
#define LWDEBUGF(level, msg,...)
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
void lwnotice(const char *fmt,...)
Write a notice out to the notice handler.
static const POINT2D * getPoint2d_cp(const POINTARRAY *pa, uint32_t n)
Returns a POINT2D pointer into the POINTARRAY serialized_ptlist, suitable for reading from.
static const POINT3D * getPoint3d_cp(const POINTARRAY *pa, uint32_t n)
Returns a POINT3D pointer into the POINTARRAY serialized_ptlist, suitable for reading from.
static uint32_t lwgeom_get_type(const LWGEOM *geom)
Return LWTYPE number.
static uint8_t * getPoint_internal(const POINTARRAY *pa, uint32_t n)
static int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members)
void lwrandom_set_seed(int32_t seed)
double lwrandom_uniform(void)
Datum contains(PG_FUNCTION_ARGS)
uint8_t * serialized_pointlist