gpt4 book ai didi

javascript - 使用 JSTS 缓冲区识别自相交多边形

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:32:13 25 4
gpt4 key购买 nike

我期望能够通过 JSTS 未能构建自相交多边形或通过添加缓冲区并在缓冲后测试它们是否为多边形来测试自相交多边形,但对于某种形状,这不起作用,这已经超出了我的范围几何理解能力

//a self-intersecting shape
var poly = [[0, 3], [1, 5], [3, 1], [5, 5], [6, 3], [0, 3]];

var geomFactory = new jsts.geom.GeometryFactory();

var jstsCoordinates = poly.map(function(pt) {
return new jsts.geom.Coordinate(pt[0], pt[1]);
});

var linearRing = geomFactory.createLinearRing(jstsCoordinates);

var jstsPolygon = geomFactory.createPolygon(linearRing).buffer(1);

console.log(jstsPolygon.getGeometryType()); //this will be polygon but I thought should be MultiPolygon

var bufferedPoly = (jstsPolygon.shell.points.coordinates.map(function(pr) {
return [pr.x, pr.y]
}))

var svg = d3.select('svg');

//add the first shape (maginified for display)
svg.selectAll('.original').data([poly]).enter().append("polygon")
.attr("points",function(d) {
return d.map(function(d) {
return [d[0] * 10 + 10, d[1]*10].join(",");
}).join(" ");
})
.attr("fill", "yellow");

//add the buffered shape below it
svg.selectAll('.buffered').data([bufferedPoly]).enter().append("polygon")
.attr("points",function(d) {
return d.map(function(d) {
return [d[0] * 10 + 10, d[1]*10 + 40].join(",");
}).join(" ");
})
.attr("fill", "yellow");
svg {background-color:blue}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<script src="https://cdn.rawgit.com/bjornharrtell/jsts/gh-pages/1.0.2/jsts.min.js"></script>
<svg width=200 height=200>
<svg>

最佳答案

基于使用 JTS ( Is there a way to convert a self intersecting polygon to a multipolygon in JTS? ) 的链接答案,我使用 JSTS 重新实现了它

只是想为寻求相同答案/功能的其他人发布代码。

通过运行代码,使用 JSTS Geometry,您将返回一个有效的 Geometry,可以是 Polygon 或 Multipolygon

/**
* Get / create a valid version of the geometry given. If the geometry is a polygon or multi polygon, self intersections /
* inconsistencies are fixed. Otherwise the geometry is returned.
*
* @param geom
* @return a geometry
*/
var jsts_validate = function(geom) {
if (geom instanceof jsts.geom.Polygon) {
if (geom.isValid()) {
geom.normalize(); // validate does not pick up rings in the wrong order - this will fix that
return geom; // If the polygon is valid just return it
}
var polygonizer = new jsts.operation.polygonize.Polygonizer();
jsts_addPolygon(geom, polygonizer);
return jsts_toPolygonGeometry(polygonizer.getPolygons(), geom.getFactory());
} else if (geom instanceof jsts.geom.MultiPolygon) {
if (geom.isValid()) {
geom.normalize(); // validate does not pick up rings in the wrong order - this will fix that
return geom; // If the multipolygon is valid just return it
}
var polygonizer = new jsts.operation.polygonize.Polygonizer();

for (var n = geom.getNumGeometries(); n > 0; n--) {
jsts_addPolygon(geom.getGeometryN(n - 1), polygonizer);
}
return jsts_toPolygonGeometry(polygonizer.getPolygons(), geom.getFactory());
} else {
return geom; // In my case, I only care about polygon / multipolygon geometries
}
};

/**
* Add all line strings from the polygon given to the polygonizer given
*
* @param polygon polygon from which to extract line strings
* @param polygonizer polygonizer
*/
var jsts_addPolygon = function(polygon, polygonizer) {
jsts_addLineString(polygon.getExteriorRing(), polygonizer);

for (var n = polygon.getNumInteriorRing(); n > 0; n--) {
jsts_addLineString(polygon.getInteriorRingN(n), polygonizer);
}
};

/**
* Add the linestring given to the polygonizer
*
* @param linestring line string
* @param polygonizer polygonizer
*/
var jsts_addLineString = function(lineString, polygonizer) {

if (lineString instanceof jsts.geom.LinearRing) {
// LinearRings are treated differently to line strings : we need a LineString NOT a LinearRing
lineString = lineString.getFactory().createLineString(lineString.getCoordinateSequence());
}

// unioning the linestring with the point makes any self intersections explicit.
var point = lineString.getFactory().createPoint(lineString.getCoordinateN(0));
var toAdd = lineString.union(point); //geometry

//Add result to polygonizer
polygonizer.add(toAdd);
}

/**
* Get a geometry from a collection of polygons.
*
* @param polygons collection
* @param factory factory to generate MultiPolygon if required
* @return null if there were no polygons, the polygon if there was only one, or a MultiPolygon containing all polygons otherwise
*/
var jsts_toPolygonGeometry = function(polygons, factory) {
switch (polygons.size()) {
case 0:
return null; // No valid polygons!
case 1:
return polygons.iterator().next(); // single polygon - no need to wrap
default:
//polygons may still overlap! Need to sym difference them
var iter = polygons.iterator();
var ret = iter.next();
while (iter.hasNext()) {
ret = ret.symDifference(iter.next());
}
return ret;
}
}

关于javascript - 使用 JSTS 缓冲区识别自相交多边形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36118883/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com