38 #define AUTOFIX LW_TRUE
39 #define LWGEOM_GEOS_ERRMSG_MAXSIZE 256
73 "%s requires a build against GEOS-%s or higher,"
74 " this version of PostGIS was built against version %s",
80 #define GEOS_FREE(...) \
82 geos_destroy((sizeof((void*[]){__VA_ARGS__})/sizeof(void*)), __VA_ARGS__); \
88 lwerror("%s: GEOS Error: %s", __func__, lwgeom_geos_errmsg); \
93 #define GEOS_FAIL_DEBUG() \
96 lwdebug(1, "%s: GEOS Error: %s", __func__, lwgeom_geos_errmsg); \
100 #define GEOS_FREE_AND_FAIL(...) \
102 GEOS_FREE(__VA_ARGS__); \
106 #define GEOS_FREE_AND_FAIL_DEBUG(...) \
109 GEOS_FREE(__VA_ARGS__); \
115 #define RESULT_SRID(...) \
116 (get_result_srid((sizeof((const void*[]){__VA_ARGS__})/sizeof(void*)), __func__, __VA_ARGS__))
125 GEOSGeometry* g = va_arg(ap, GEOSGeometry*);
149 #if POSTGIS_GEOS_VERSION < 31000
151 POINT4D point = { 0.0, 0.0, 0.0, 0.0 };
154 LWDEBUG(2,
"ptarray_fromGEOSCoordSeq called");
156 if (!GEOSCoordSeq_getSize(cs, &size))
lwerror(
"Exception thrown");
158 LWDEBUGF(4,
" GEOSCoordSeq size: %d", size);
162 if (!GEOSCoordSeq_getDimensions(cs, &dims))
lwerror(
"Exception thrown");
164 LWDEBUGF(4,
" GEOSCoordSeq dimensions: %d", dims);
167 if (dims > 3) dims = 3;
170 LWDEBUGF(4,
" output dimensions: %d", dims);
173 #if POSTGIS_GEOS_VERSION >= 31000
177 for (i = 0; i < size; i++)
179 #if POSTGIS_GEOS_VERSION < 30800
180 GEOSCoordSeq_getX(cs, i, &(point.
x));
181 GEOSCoordSeq_getY(cs, i, &(point.
y));
182 if (dims >= 3) GEOSCoordSeq_getZ(cs, i, &(point.
z));
185 GEOSCoordSeq_getXYZ(cs, i, &(point.
x), &(point.
y), &(point.
z));
187 GEOSCoordSeq_getXY(cs, i, &(point.
x), &(point.
y));
200 int type = GEOSGeomTypeId(geom);
201 int SRID = GEOSGetSRID(geom);
206 if (want3d && !GEOSHasZ(geom))
208 LWDEBUG(3,
"Geometry has no Z, won't provide one");
214 const GEOSCoordSequence* cs;
216 const GEOSGeometry* g;
221 LWDEBUG(4,
"lwgeom_from_geometry: it's a Point");
222 cs = GEOSGeom_getCoordSeq(geom);
227 case GEOS_LINESTRING:
228 case GEOS_LINEARRING:
229 LWDEBUG(4,
"lwgeom_from_geometry: it's a LineString or LinearRing");
232 cs = GEOSGeom_getCoordSeq(geom);
237 LWDEBUG(4,
"lwgeom_from_geometry: it's a Polygon");
239 ngeoms = GEOSGetNumInteriorRings(geom);
241 g = GEOSGetExteriorRing(geom);
242 cs = GEOSGeom_getCoordSeq(g);
244 for (i = 0; i < ngeoms; i++)
246 g = GEOSGetInteriorRingN(geom, i);
247 cs = GEOSGeom_getCoordSeq(g);
252 case GEOS_MULTIPOINT:
253 case GEOS_MULTILINESTRING:
254 case GEOS_MULTIPOLYGON:
255 case GEOS_GEOMETRYCOLLECTION:
256 LWDEBUG(4,
"lwgeom_from_geometry: it's a Collection or Multi");
258 ngeoms = GEOSGetNumGeometries(geom);
263 for (i = 0; i < ngeoms; i++)
265 g = GEOSGetGeometryN(geom, i);
272 lwerror(
"GEOS2LWGEOM: unknown geometry type: %d",
type);
284 int append_points = 0;
295 lwerror(
"ptarray_to_GEOSCoordSeq called with fix_ring and 0 vertices in ring, cannot fix");
305 #if POSTGIS_GEOS_VERSION >= 31000
306 if (append_points == 0) {
315 if (!(sq = GEOSCoordSeq_create(pa->
npoints + append_points, dims)))
320 for (i = 0; i < pa->
npoints; i++)
326 LWDEBUGF(4,
"Point: %g,%g,%g", p3d->
x, p3d->
y, p3d->
z);
334 #if POSTGIS_GEOS_VERSION < 30800
335 GEOSCoordSeq_setX(sq, i, p2d->
x);
336 GEOSCoordSeq_setY(sq, i, p2d->
y);
337 if (dims == 3) GEOSCoordSeq_setZ(sq, i, p3d->
z);
340 GEOSCoordSeq_setXYZ(sq, i, p2d->
x, p2d->
y, p3d->
z);
342 GEOSCoordSeq_setXY(sq, i, p2d->
x, p2d->
y);
356 for (i = pa->
npoints; i < pa->npoints + append_points; i++)
358 #if POSTGIS_GEOS_VERSION < 30800
359 GEOSCoordSeq_setX(sq, i, p2d->
x);
360 GEOSCoordSeq_setY(sq, i, p2d->
y);
362 GEOSCoordSeq_setXY(sq, i, p2d->
x, p2d->
y);
365 if (dims == 3) GEOSCoordSeq_setZ(sq, i, p3d->
z);
373 static inline GEOSGeometry*
379 g = GEOSGeom_createLinearRing(sq);
386 GEOSGeometry* envelope;
388 GEOSCoordSequence* seq = GEOSCoordSeq_create(5, 2);
389 if (!seq)
return NULL;
391 #if POSTGIS_GEOS_VERSION < 30800
392 GEOSCoordSeq_setX(seq, 0, box->
xmin);
393 GEOSCoordSeq_setY(seq, 0, box->
ymin);
395 GEOSCoordSeq_setX(seq, 1, box->
xmax);
396 GEOSCoordSeq_setY(seq, 1, box->
ymin);
398 GEOSCoordSeq_setX(seq, 2, box->
xmax);
399 GEOSCoordSeq_setY(seq, 2, box->
ymax);
401 GEOSCoordSeq_setX(seq, 3, box->
xmin);
402 GEOSCoordSeq_setY(seq, 3, box->
ymax);
404 GEOSCoordSeq_setX(seq, 4, box->
xmin);
405 GEOSCoordSeq_setY(seq, 4, box->
ymin);
407 GEOSCoordSeq_setXY(seq, 0, box->
xmin, box->
ymin);
408 GEOSCoordSeq_setXY(seq, 1, box->
xmax, box->
ymin);
409 GEOSCoordSeq_setXY(seq, 2, box->
xmax, box->
ymax);
410 GEOSCoordSeq_setXY(seq, 3, box->
xmin, box->
ymax);
411 GEOSCoordSeq_setXY(seq, 4, box->
xmin, box->
ymin);
414 ring = GEOSGeom_createLinearRing(seq);
417 GEOSCoordSeq_destroy(seq);
421 envelope = GEOSGeom_createPolygon(ring, NULL, 0);
424 GEOSGeom_destroy(ring);
436 GEOSGeom* geoms = NULL;
437 uint32_t ngeoms, i, j;
439 #if LWDEBUG_LEVEL >= 4
455 GEOSGeometry* g =
LWGEOM2GEOS(lwgeom_stroked, autofix);
462 switch (lwgeom->
type)
467 g = GEOSGeom_createEmptyPoint();
471 #if POSTGIS_GEOS_VERSION < 30800
473 g = GEOSGeom_createPoint(sq);
478 g = GEOSGeom_createPoint(sq);
483 g = GEOSGeom_createPointFromXY(p->
x, p->
y);
494 g = GEOSGeom_createEmptyLineString();
508 g = GEOSGeom_createLineString(sq);
518 g = GEOSGeom_createEmptyPolygon();
522 if (!shell)
return NULL;
523 ngeoms = lwpoly->
nrings - 1;
524 if (ngeoms > 0) geoms =
lwalloc(
sizeof(GEOSGeom) * ngeoms);
526 for (i = 1; i < lwpoly->
nrings; i++)
532 for (k = 0; k < i - 1; k++)
533 GEOSGeom_destroy(geoms[k]);
535 GEOSGeom_destroy(shell);
539 g = GEOSGeom_createPolygon(shell, geoms, ngeoms);
549 g = GEOSGeom_createEmptyPolygon();
554 if (!shell)
return NULL;
555 g = GEOSGeom_createPolygon(shell, NULL, 0);
568 geostype = GEOS_MULTIPOINT;
570 geostype = GEOS_MULTILINESTRING;
572 geostype = GEOS_MULTIPOLYGON;
574 geostype = GEOS_GEOMETRYCOLLECTION;
579 if (ngeoms > 0) geoms =
lwalloc(
sizeof(GEOSGeom) * ngeoms);
582 for (i = 0; i < ngeoms; ++i)
592 for (k = 0; k < j; k++)
593 GEOSGeom_destroy(geoms[k]);
599 g = GEOSGeom_createCollection(geostype, geoms, j);
600 if (ngeoms > 0)
lwfree(geoms);
612 GEOSSetSRID(g, lwgeom->
srid);
614 #if LWDEBUG_LEVEL >= 4
615 wkt = GEOSGeomToWKT(g);
616 LWDEBUGF(4,
"LWGEOM2GEOS: GEOSGeom: %s", wkt);
626 GEOSCoordSequence* seq = GEOSCoordSeq_create(1, 2);
627 GEOSGeometry* geom = NULL;
629 if (!seq)
return NULL;
631 #if POSTGIS_GEOS_VERSION < 30800
632 GEOSCoordSeq_setX(seq, 0,
x);
633 GEOSCoordSeq_setY(seq, 0,
y);
635 GEOSCoordSeq_setXY(seq, 0,
x,
y);
638 geom = GEOSGeom_createPoint(seq);
639 if (!geom) GEOSCoordSeq_destroy(seq);
646 GEOSCoordSequence* seq = GEOSCoordSeq_create(2, 2);
647 GEOSGeometry* geom = NULL;
649 if (!seq)
return NULL;
651 #if POSTGIS_GEOS_VERSION < 30800
652 GEOSCoordSeq_setX(seq, 0, x1);
653 GEOSCoordSeq_setY(seq, 0, y1);
654 GEOSCoordSeq_setX(seq, 1, x2);
655 GEOSCoordSeq_setY(seq, 1, y2);
657 GEOSCoordSeq_setXY(seq, 0, x1, y1);
658 GEOSCoordSeq_setXY(seq, 1, x2, y2);
661 geom = GEOSGeom_createLineString(seq);
662 if (!geom) GEOSCoordSeq_destroy(seq);
669 const char* ver = GEOSversion();
679 va_start(ap, funcname);
682 for(i = 0; i <
count; i++)
687 lwerror(
"%s: Geometry is null", funcname);
699 lwerror(
"%s: Operation on mixed SRID geometries (%d != %d)", funcname, srid, g->
srid);
724 GEOSSetSRID(g, srid);
762 #if POSTGIS_GEOS_VERSION < 30900
767 g3 = GEOSIntersectionPrec(g1, g2, prec);
772 g3 = GEOSIntersection(g1, g2);
776 GEOSSetSRID(g3, srid);
810 #if POSTGIS_GEOS_VERSION < 31100
815 g3 = GEOSLineMergeDirected(g1);
820 g3 = GEOSLineMerge(g1);
824 GEOSSetSRID(g3, srid);
859 #if POSTGIS_GEOS_VERSION < 30900
864 g3 = GEOSUnaryUnionPrec(g1, prec);
869 g3 = GEOSUnaryUnion(g1);
873 GEOSSetSRID(g3, srid);
895 GEOSGeometry *g1, *g2, *g3;
911 #if POSTGIS_GEOS_VERSION < 30900
916 g3 = GEOSDifferencePrec(g1, g2, prec);
921 g3 = GEOSDifference(g1, g2);
925 GEOSSetSRID(g3, srid);
946 GEOSGeometry *g1, *g2, *g3;
962 #if POSTGIS_GEOS_VERSION < 30900
967 g3 = GEOSSymDifferencePrec(g1, g2, prec);
972 g3 = GEOSSymDifference(g1, g2);
976 GEOSSetSRID(g3, srid);
991 GEOSGeometry *g1, *g3;
1005 g3 = GEOSGetCentroid(g1);
1008 GEOSSetSRID(g3, srid);
1021 #if POSTGIS_GEOS_VERSION < 30900
1029 GEOSGeometry *g1, *g3;
1040 g3 = GEOSGeom_setPrecision(g1, gridSize, 0);
1043 GEOSSetSRID(g3, srid);
1060 GEOSGeometry *g1, *g3;
1074 g3 = GEOSPointOnSurface(g1);
1077 GEOSSetSRID(g3, srid);
1099 GEOSGeometry *g1, *g2, *g3;
1114 if ( gridSize >= 0) {
1115 #if POSTGIS_GEOS_VERSION < 30900
1120 g3 = GEOSUnionPrec(g1, g2, gridSize);
1125 g3 = GEOSUnion(g1, g2);
1129 GEOSSetSRID(g3, srid);
1142 GEOSGeometry *g1, *g3;
1156 if (!(g3 = GEOSClipByRect(g1, x1, y1, x2, y2)))
1172 #if POSTGIS_GEOS_VERSION < 30800
1173 typedef struct Face_t
1175 const GEOSGeometry* geom;
1178 struct Face_t* parent;
1181 static Face* newFace(
const GEOSGeometry* g);
1182 static void delFace(Face* f);
1183 static unsigned int countParens(
const Face* f);
1184 static void findFaceHoles(Face** faces,
int nfaces);
1187 newFace(
const GEOSGeometry* g)
1189 Face* f =
lwalloc(
sizeof(Face));
1191 f->env = GEOSEnvelope(f->geom);
1192 GEOSArea(f->env, &f->envarea);
1198 countParens(
const Face* f)
1200 unsigned int pcount = 0;
1213 GEOSGeom_destroy(f->env);
1218 compare_by_envarea(
const void* g1,
const void* g2)
1220 Face* f1 = *(Face**)g1;
1221 Face* f2 = *(Face**)g2;
1222 double n1 = f1->envarea;
1223 double n2 = f2->envarea;
1225 if (n1 < n2)
return 1;
1226 if (n1 > n2)
return -1;
1232 findFaceHoles(Face** faces,
int nfaces)
1237 qsort(faces, nfaces,
sizeof(Face*), compare_by_envarea);
1238 for (i = 0; i < nfaces; ++i)
1241 int nholes = GEOSGetNumInteriorRings(f->geom);
1242 LWDEBUGF(2,
"Scanning face %d with env area %g and %d holes", i, f->envarea, nholes);
1243 for (h = 0; h < nholes; ++h)
1245 const GEOSGeometry* hole = GEOSGetInteriorRingN(f->geom, h);
1247 "Looking for hole %d/%d of face %d among %d other faces",
1252 for (j = i + 1; j < nfaces; ++j)
1254 const GEOSGeometry* f2er;
1255 Face* f2 = faces[j];
1256 if (f2->parent)
continue;
1257 f2er = GEOSGetExteriorRing(f2->geom);
1260 if (GEOSEquals(f2er, hole))
1262 LWDEBUGF(2,
"Hole %d/%d of face %d is face %d", h + 1, nholes, i, j);
1271 static GEOSGeometry*
1272 collectFacesWithEvenAncestors(Face** faces,
int nfaces)
1274 GEOSGeometry** geoms =
lwalloc(
sizeof(GEOSGeometry*) * nfaces);
1276 unsigned int ngeoms = 0;
1279 for (i = 0; i < nfaces; ++i)
1282 if (countParens(f) % 2)
continue;
1283 geoms[ngeoms++] = GEOSGeom_clone(f->geom);
1286 ret = GEOSGeom_createCollection(GEOS_MULTIPOLYGON, geoms, ngeoms);
1292 LWGEOM_GEOS_buildArea(
const GEOSGeometry* geom_in)
1295 GEOSGeometry *geos_result, *shp;
1296 GEOSGeometry
const* vgeoms[1];
1298 int srid = GEOSGetSRID(geom_in);
1300 #if POSTGIS_DEBUG_LEVEL >= 3
1305 vgeoms[0] = geom_in;
1306 geos_result = GEOSPolygonize(vgeoms, 1);
1308 LWDEBUGF(3,
"GEOSpolygonize returned @ %p", geos_result);
1311 if (!geos_result)
return 0;
1314 #if PARANOIA_LEVEL > 0
1317 GEOSGeom_destroy(geos_result);
1318 lwerror(
"%s [%d] Unexpected return from GEOSpolygonize", __FILE__, __LINE__);
1323 ngeoms = GEOSGetNumGeometries(geos_result);
1325 #if POSTGIS_DEBUG_LEVEL >= 3
1326 LWDEBUGF(3,
"GEOSpolygonize: ngeoms in polygonize output: %d", ngeoms);
1329 LWDEBUGF(3,
"GEOSpolygonize: polygonized:%s", geom_ewkt);
1337 GEOSSetSRID(geos_result, srid);
1344 tmp = (GEOSGeometry*)GEOSGetGeometryN(geos_result, 0);
1347 GEOSGeom_destroy(geos_result);
1350 shp = GEOSGeom_clone(tmp);
1351 GEOSGeom_destroy(geos_result);
1352 GEOSSetSRID(shp, srid);
1356 LWDEBUGF(2,
"Polygonize returned %d geoms", ngeoms);
1386 geoms =
lwalloc(
sizeof(Face**) * ngeoms);
1387 for (i = 0; i < ngeoms; ++i)
1388 geoms[i] = newFace(GEOSGetGeometryN(geos_result, i));
1391 findFaceHoles(geoms, ngeoms);
1394 tmp = collectFacesWithEvenAncestors(geoms, ngeoms);
1397 for (i = 0; i < ngeoms; ++i)
1402 GEOSGeom_destroy(geos_result);
1405 shp = GEOSUnionCascaded(tmp);
1408 GEOSGeom_destroy(tmp);
1412 GEOSGeom_destroy(tmp);
1414 GEOSSetSRID(shp, srid);
1426 GEOSGeometry *g1, *g3;
1437 #if POSTGIS_GEOS_VERSION < 30800
1438 g3 = LWGEOM_GEOS_buildArea(g1);
1440 g3 = GEOSBuildArea(g1);
1444 GEOSSetSRID(g3, srid);
1447 if (GEOSGetNumGeometries(g3) == 0)
1476 simple = GEOSisSimple(g);
1477 GEOSGeom_destroy(g);
1503 GEOSSetSRID(g, srid);
1519 GEOSGeometry *g1, *g2, *g3;
1528 g3 = GEOSSnap(g1, g2, tolerance);
1531 GEOSSetSRID(g3, srid);
1546 GEOSGeometry *g1, *g2, *g3;
1555 g3 = GEOSSharedPaths(g1, g2);
1558 GEOSSetSRID(g3, srid);
1574 GEOSGeometry *g1, *g3;
1582 g3 = GEOSOffsetCurve(g1, size, quadsegs, joinStyle, mitreLimit);
1590 GEOSSetSRID(g3, srid);
1612 for (i = 0; i < col->
ngeoms; i++)
1687 lwerror(
"lwgeom_offsetcurve: cannot node input");
1695 lwerror(
"lwgeom_offsetcurve: noded geometry cannot be offset");
1706 double area, bbox_area, bbox_width, bbox_height;
1709 uint32_t sample_npoints, sample_sqrt, sample_width, sample_height;
1710 double sample_cell_size;
1712 uint32_t iterations = 0;
1713 uint32_t npoints_generated = 0;
1714 uint32_t npoints_tested = 0;
1716 const GEOSPreparedGeometry* gprep;
1718 GEOSCoordSequence* gseq;
1723 const size_t size = 2 *
sizeof(int);
1724 char tmp[2 *
sizeof(int)];
1725 const size_t stride = 2 *
sizeof(int);
1729 lwerror(
"%s: only polygons supported", __func__);
1738 bbox = *(lwpoly->
bbox);
1741 bbox_width = bbox.
xmax - bbox.
xmin;
1742 bbox_height = bbox.
ymax - bbox.
ymin;
1743 bbox_area = bbox_width * bbox_height;
1745 if (area == 0.0 || bbox_area == 0.0)
1747 lwerror(
"%s: zero area input polygon, TBD", __func__);
1752 sample_npoints = npoints * bbox_area / area;
1757 sample_sqrt = lround(sqrt(sample_npoints));
1758 if (sample_sqrt == 0)
1762 if (bbox_width > bbox_height)
1764 sample_width = sample_sqrt;
1765 sample_height = ceil((
double)sample_npoints / (
double)sample_width);
1766 sample_cell_size = bbox_width / sample_width;
1770 sample_height = sample_sqrt;
1771 sample_width = ceil((
double)sample_npoints / (
double)sample_height);
1772 sample_cell_size = bbox_height / sample_height;
1783 gprep = GEOSPrepare(g);
1797 cells =
lwalloc(2 *
sizeof(
int) * sample_height * sample_width);
1798 for (i = 0; i < sample_width; i++)
1800 for (j = 0; j < sample_height; j++)
1802 cells[2 * (i * sample_height + j)] = i;
1803 cells[2 * (i * sample_height + j) + 1] = j;
1808 n = sample_height * sample_width;
1811 for (i = n - 1; i > 0; i--)
1815 memcpy(tmp, (
char *)cells + j * stride, size);
1816 memcpy((
char *)cells + j * stride, (
char *)cells + i * stride, size);
1817 memcpy((
char *)cells + i * stride, tmp, size);
1822 while (npoints_generated < npoints)
1825 for (i = 0; i < sample_width * sample_height; i++)
1828 double y = bbox.
ymin + cells[2 * i] * sample_cell_size;
1829 double x = bbox.
xmin + cells[2 * i + 1] * sample_cell_size;
1832 if (
x >= bbox.
xmax ||
y >= bbox.
ymax)
continue;
1834 gseq = GEOSCoordSeq_create(1, 2);
1835 #if POSTGIS_GEOS_VERSION < 30800
1836 GEOSCoordSeq_setX(gseq, 0,
x);
1837 GEOSCoordSeq_setY(gseq, 0,
y);
1839 GEOSCoordSeq_setXY(gseq, 0,
x,
y);
1841 gpt = GEOSGeom_createPoint(gseq);
1843 contains = GEOSPreparedIntersects(gprep, gpt);
1845 GEOSGeom_destroy(gpt);
1849 GEOSPreparedGeom_destroy(gprep);
1850 GEOSGeom_destroy(g);
1856 npoints_generated++;
1858 if (npoints_generated == npoints)
1867 if (npoints_tested % 10000 == 0)
1868 LW_ON_INTERRUPT(GEOSPreparedGeom_destroy(gprep); GEOSGeom_destroy(g);
return NULL);
1872 if (done || iterations > 100)
break;
1875 GEOSPreparedGeom_destroy(gprep);
1876 GEOSGeom_destroy(g);
1894 lwerror(
"%s: only multipolygons supported", __func__);
1901 for (i = 0; i < lwmpoly->
ngeoms; i++)
1904 int sub_npoints = lround(npoints * sub_area / area);
1905 if (sub_npoints > 0)
1913 for (j = 0; j < sub_mpt->
ngeoms; j++)
1943 int type = GEOSGeomTypeId(geom);
1944 int SRID = GEOSGetSRID(geom);
1949 if (want3d && !GEOSHasZ(geom))
1951 LWDEBUG(3,
"Geometry has no Z, won't provide one");
1959 case GEOS_GEOMETRYCOLLECTION:
1960 LWDEBUG(4,
"lwgeom_from_geometry: it's a Collection or Multi");
1962 ngeoms = GEOSGetNumGeometries(geom);
1966 geoms =
lwalloc(ngeoms *
sizeof *geoms);
1969 lwerror(
"lwtin_from_geos: can't allocate geoms");
1972 for (i = 0; i < ngeoms; i++)
1974 const GEOSGeometry *poly, *ring;
1975 const GEOSCoordSequence* cs;
1978 poly = GEOSGetGeometryN(geom, i);
1979 ring = GEOSGetExteriorRing(poly);
1980 cs = GEOSGeom_getCoordSeq(ring);
1988 case GEOS_MULTIPOINT:
1989 case GEOS_MULTILINESTRING:
1990 case GEOS_MULTIPOLYGON:
1991 case GEOS_LINESTRING:
1992 case GEOS_LINEARRING:
1994 lwerror(
"lwtin_from_geos: invalid geometry type for tin: %d",
type);
1998 lwerror(
"GEOS2LWGEOM: unknown geometry type: %d",
type);
2014 GEOSGeometry *g1, *g3;
2016 if (output < 0 || output > 2)
2018 lwerror(
"%s: invalid output type specified %d", __func__, output);
2029 g3 = GEOSDelaunayTriangulation(g1, tolerance, output == 1);
2032 GEOSSetSRID(g3, srid);
2040 lwerror(
"%s: cannot convert output geometry", __func__);
2053 static GEOSCoordSequence*
2057 uint8_t num_dims = 2;
2059 GEOSCoordSequence* coords;
2062 coords = GEOSCoordSeq_create(num_points, num_dims);
2063 if (!coords)
return NULL;
2068 if (i >= num_points)
2070 lwerror(
"Incorrect num_points provided to lwgeom_get_geos_coordseq_2d");
2071 GEOSCoordSeq_destroy(coords);
2076 #if POSTGIS_GEOS_VERSION < 30800
2077 if (!GEOSCoordSeq_setX(coords, i, tmp.
x) || !GEOSCoordSeq_setY(coords, i, tmp.
y))
2079 if (!GEOSCoordSeq_setXY(coords, i, tmp.
x, tmp.
y))
2082 GEOSCoordSeq_destroy(coords);
2100 GEOSCoordSequence* coords;
2101 GEOSGeometry* geos_geom;
2102 GEOSGeometry* geos_env = NULL;
2103 GEOSGeometry* geos_result;
2118 if (!coords)
return NULL;
2120 geos_geom = GEOSGeom_createLineString(coords);
2123 GEOSCoordSeq_destroy(coords);
2129 geos_result = GEOSVoronoiDiagram(geos_geom, geos_env, tolerance, output_edges);
2131 GEOSGeom_destroy(geos_geom);
2132 if (env) GEOSGeom_destroy(geos_env);
2141 GEOSGeom_destroy(geos_result);
2145 return lwgeom_result;
2149 #if POSTGIS_GEOS_VERSION >= 31100
2156 GEOSGeometry *g1, *g3;
2163 geosGeomType = GEOSGeomTypeId(g1);
2164 if (geosGeomType == GEOS_POLYGON || geosGeomType == GEOS_MULTIPOLYGON) {
2166 g3 = GEOSConcaveHullOfPolygons(g1, ratio, is_tight, allow_holes);
2169 g3 = GEOSConcaveHull(g1, ratio, allow_holes);
2175 GEOSSetSRID(g3, srid);
2190 GEOSGeometry *g1, *g3;
2196 g3 = GEOSPolygonHullSimplify(g1, is_outer, vertex_fraction);
2201 GEOSSetSRID(g3, srid);
2216 GEOSGeometry *g1, *g3;
2225 g3 = GEOSConstrainedDelaunayTriangulation(g1);
2228 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_concavehull(const LWGEOM *geom, double ratio, uint32_t allow_holes)
Take a geometry and build the concave hull.
LWGEOM * lwgeom_unaryunion(const LWGEOM *geom)
void lwgeom_geos_error_minversion(const char *functionality, const char *minver)
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]
const char * lwgeom_geos_compiled_version()
LWGEOM * lwgeom_simplify_polygonal(const LWGEOM *geom, double vertex_fraction, uint32_t is_outer)
Computes a boundary-respecting hull of a polygonal geometry, with hull shape determined by a target p...
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_triangulate_polygon(const LWGEOM *geom)
Take vertices of a polygon and build a constrained triangulation that respects the boundary of the po...
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)
#define POSTGIS_GEOS_VERSION
uint8_t * serialized_pointlist