gpt4 book ai didi

javascript - 简化多边形 - Clipper Library Angus Johnson

转载 作者:行者123 更新时间:2023-11-30 06:35:04 26 4
gpt4 key购买 nike

我正在尝试简化在 Google map (API v3 Javascript)上绘制的复杂多边形。我的目标是接收一组简单的多边形。问题是我画的图有的可以分析,有的不能分析。

例如:

1) 图 8-垂直 - 对于具有坐标的多边形 (var areaPolygon):

{42.15093256154524, 24.746017456054687}
{42.149087174176515, 24.740352630615234}
{42.14049586965896, 24.754257202148437}
{42.1333673840616, 24.747648239135742}
{42.13795007405907, 24.73846435546875}
{42.145841707270215, 24.74867820739746}
{42.15093256154524, 24.749107360839843}
{42.15093256154524, 24.746017456054687}

它工作正常 - SimplifyPolygon 返回 2 个简单的多边形

2) 图 8-水平 - 对于具有坐标的多边形 (var areaPolygon):

{42.145523515284395, 24.744129180908203}
{42.15016895950386, 24.749279022216797}
{42.14603262169405, 24.7646427154541}
{42.14978715502878, 24.769277572631836}
{42.15055076167604, 24.758892059326172}
{42.14393253136682, 24.75193977355957}
{42.141705086714666, 24.747648239135742}
{42.14348704870535, 24.74472999572754}
{42.145523515284395, 24.744129180908203}
  • 它不起作用 - SimplifyPolygon 返回相同的复杂多边形

这是我的代码:

function analyzePolygon(areaPolygon) {

//areaPolygon is google.maps.Polygon
var subj_polygon = new ClipperLib.Polygon();
var result_polygons = new ClipperLib.Polygons();

//LatLng coordinates to integer coordinates
for(var i = 0; i < areaPolygon.getPath().getArray().length ; i++) {
subj_polygon.push(new ClipperLib.IntPoint(Math.round(areaPolygon.getPath().getArray()[i].lat()*100000000000000), Math.round(areaPolygon.getPath().getArray()[i].lng()*100000000000000)));
}

var cpr = new ClipperLib.Clipper();

result_polygons = cpr.SimplifyPolygon(subj_polygon, ClipperLib.PolyFillType.pftNonZero);

//for each simple polygon - make a request and populate markers of the objects which are in these bounds
for(var j=0; j < result_polygons.length; j++) {

var rpStringify = JSON.stringify(eval(result_polygons[j]));
var rpJSON = JSON.parse(rpStringify);
var arrayPolygon = [];
for (var i = 0; i < rpJSON.length; i++) {

var tempObject = JSON.stringify(eval(rpJSON[i]));
var objectJSON = JSON.parse(tempObject);
var tempArr = [2];
tempArr[0] = objectJSON.X/100000000000000;
tempArr[1] = objectJSON.Y/100000000000000;
arrayPolygon[i]= tempArr;
}

loadPinsPolygon(arrayPolygon);
}

最佳答案

埃琳娜。

it's not working - the SimplifyPolygon returns the same complex polygon

我不确定您所说的“复杂多边形”是什么意思,但如果您指的是自相交多边形(请参阅 http://en.wikipedia.org/wiki/Complex_polygon ),那么 SimplifyPolygon 函数确实会按预期删除自相交。您得到的是一个简单的多边形,尽管在先前的交点处有 2 个顶点位于相同的坐标。

simple polygon

但是,如果您真的想要两个离散的多边形,那么您将不得不修改库中的底层代码(参见讨论 here。)

关于javascript - 简化多边形 - Clipper Library Angus Johnson,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15130305/

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