如何在 GEOS 中生成多边形的负缓冲区?
How do I produce a Negative Buffer of a polygon in GEOS?
我正在尝试使用 GEOS(使用 c api)生成多边形的负缓冲区(偏移)。
到目前为止,我已经完成了正偏移 (see image),但是当我尝试将宽度设置为“-”时,它什么也没有产生。我怀疑 'offset' 在取回外环之前需要以某种方式进行剪裁。
仔细查看了文档但无法解决,任何帮助将不胜感激!!
如果有帮助,我的代码如下:
#include <geos_c.h>
// Define coordinate sequence
int noPoints = 6 + 1; // +1 because it is a loop
GEOSCoordSequence* points = GEOSCoordSeq_create(noPoints, 2 /*# ordinates*/);
GEOSCoordSeq_setX(points, 0, 0);
GEOSCoordSeq_setY(points, 0, 0);
GEOSCoordSeq_setX(points, 1, 0);
GEOSCoordSeq_setY(points, 1, 50);
GEOSCoordSeq_setX(points, 2, 50);
GEOSCoordSeq_setY(points, 2, 50);
GEOSCoordSeq_setX(points, 3, 50);
GEOSCoordSeq_setY(points, 3, 0);
GEOSCoordSeq_setX(points, 4, 30);
GEOSCoordSeq_setY(points, 4, 15);
GEOSCoordSeq_setX(points, 5, 20);
GEOSCoordSeq_setY(points, 5, 15);
GEOSCoordSeq_setX(points, noPoints-1, 0);
GEOSCoordSeq_setY(points, noPoints-1, 0);
// Define linear ring
GEOSGeometry* ploop = GEOSGeom_createLinearRing(points);
// Define offset
double width = 15;
int quadsegs = 100;
int endCapStyle = 1;
int joinStyle = 1;
double mitreLimit = 1.0;
const GEOSGeometry* offset = GEOSBufferWithStyle(ploop, width, quadsegs, endCapStyle, joinStyle, mitreLimit);
// Get exterior ring
const GEOSGeometry* exteriorRing = GEOSGetExteriorRing(offset);
// Convert to coord sequence and draw points
const GEOSCoordSequence *coordSeq = GEOSGeom_getCoordSeq(exteriorRing);
uint numPoints = GEOSGeomGetNumPoints(exteriorRing);
double xCoord, yCoord;
for (uint p = 0; p < numPoints; p++) {
GEOSCoordSeq_getX(coordSeq, p, &xCoord);
GEOSCoordSeq_getY(coordSeq, p, &yCoord);
printf("x: %g\ty:%g\n", xCoord, yCoord);
drawPoint(xCoord, yCoord);
}
提前致谢!
终于解决了!我输入的几何图形是线性环,而不是多边形。对于以后遇到类似问题的任何人:
替换:
GEOSGeometry* ploop = GEOSGeom_createLinearRing(points);
与:
GEOSGeometry* rloop = GEOSGeom_createLinearRing(points);
const GEOSGeometry* ploop = GEOSGeom_createPolygon(rloop, NULL, 0);
我正在尝试使用 GEOS(使用 c api)生成多边形的负缓冲区(偏移)。
到目前为止,我已经完成了正偏移 (see image),但是当我尝试将宽度设置为“-”时,它什么也没有产生。我怀疑 'offset' 在取回外环之前需要以某种方式进行剪裁。
仔细查看了文档但无法解决,任何帮助将不胜感激!!
如果有帮助,我的代码如下:
#include <geos_c.h>
// Define coordinate sequence
int noPoints = 6 + 1; // +1 because it is a loop
GEOSCoordSequence* points = GEOSCoordSeq_create(noPoints, 2 /*# ordinates*/);
GEOSCoordSeq_setX(points, 0, 0);
GEOSCoordSeq_setY(points, 0, 0);
GEOSCoordSeq_setX(points, 1, 0);
GEOSCoordSeq_setY(points, 1, 50);
GEOSCoordSeq_setX(points, 2, 50);
GEOSCoordSeq_setY(points, 2, 50);
GEOSCoordSeq_setX(points, 3, 50);
GEOSCoordSeq_setY(points, 3, 0);
GEOSCoordSeq_setX(points, 4, 30);
GEOSCoordSeq_setY(points, 4, 15);
GEOSCoordSeq_setX(points, 5, 20);
GEOSCoordSeq_setY(points, 5, 15);
GEOSCoordSeq_setX(points, noPoints-1, 0);
GEOSCoordSeq_setY(points, noPoints-1, 0);
// Define linear ring
GEOSGeometry* ploop = GEOSGeom_createLinearRing(points);
// Define offset
double width = 15;
int quadsegs = 100;
int endCapStyle = 1;
int joinStyle = 1;
double mitreLimit = 1.0;
const GEOSGeometry* offset = GEOSBufferWithStyle(ploop, width, quadsegs, endCapStyle, joinStyle, mitreLimit);
// Get exterior ring
const GEOSGeometry* exteriorRing = GEOSGetExteriorRing(offset);
// Convert to coord sequence and draw points
const GEOSCoordSequence *coordSeq = GEOSGeom_getCoordSeq(exteriorRing);
uint numPoints = GEOSGeomGetNumPoints(exteriorRing);
double xCoord, yCoord;
for (uint p = 0; p < numPoints; p++) {
GEOSCoordSeq_getX(coordSeq, p, &xCoord);
GEOSCoordSeq_getY(coordSeq, p, &yCoord);
printf("x: %g\ty:%g\n", xCoord, yCoord);
drawPoint(xCoord, yCoord);
}
提前致谢!
终于解决了!我输入的几何图形是线性环,而不是多边形。对于以后遇到类似问题的任何人:
替换:
GEOSGeometry* ploop = GEOSGeom_createLinearRing(points);
与:
GEOSGeometry* rloop = GEOSGeom_createLinearRing(points);
const GEOSGeometry* ploop = GEOSGeom_createPolygon(rloop, NULL, 0);