[英]How can I combine polygons and remove the overlap?
I made polygons editable in Google Maps and now I can change the shape, make holes in it, combine two of more polygons to multipolygons and disaggregate them again. 我在谷歌地图中制作了多边形可编辑,现在我可以改变形状,在其中打洞,将两个多边形组合成多边形并再次分解它们。
See http://maps.amsterdam.nl/testshape/beheer and read the Instructions in the Legenda to try it yourself. 请参阅http://maps.amsterdam.nl/testshape/beheer并阅读Legenda中的说明以自行尝试。
One question I can't figure out is how to combine two overlapping polygons to one polygon with no overlap. 我无法弄清楚的一个问题是如何将两个重叠的多边形组合成一个没有重叠的多边形。 Something like this:
像这样的东西:
function(path1, path2) {
algorithm...
return newPath;
}
Thank you. 谢谢。
Thanks a lot for the lib suggestion. 非常感谢lib的建议。 I used https://github.com/bjornharrtell/jsts/blob/master/examples/overlay.html with:
我使用https://github.com/bjornharrtell/jsts/blob/master/examples/overlay.html :
var union = a.union(b);
var difference = a.difference(b);
to combine shapes and to make holes in shapes or to clip shapes. 组合形状,在形状上打孔或剪裁形状。 Therefor I had to convert the Google Maps paths to WKT and I wrote this Javascript:
因此,我不得不将谷歌地图路径转换为WKT,我写了这个Javascript:
function doeWKT(dePaths) {
var deWKTarray = [];
for (var i = 0; i < dePaths.length; i++) {
dePath = dePaths.getArray()[i].getArray();
var deKomma = "";
var deCoords = "";
for (var j = 0; j < dePath.length; j++) {
deLatLng = dePath[j];
if (j == 0) var deCoords0 = deKomma + deLatLng.lng().toFixed(6) + " " + deLatLng.lat().toFixed(6);
deCoords += deKomma + deLatLng.lng().toFixed(6) + " " + deLatLng.lat().toFixed(6);
deKomma = ",";
}
deWKTarray.push("(" + deCoords + "," + deCoords0 + ")");
}
var deHoles = [];
var deReader = new jsts.io.WKTReader();
for (var i = 0; i < deWKTarray.length; i++) {
var deHole = deReader.read("POLYGON("+deWKTarray[i]+")");
if (!deHoles[i]) deHoles[i] = -1;
for (var j =0; j < deWKTarray.length; j++) {
if ( i != j) {
var deContainer = deReader.read("POLYGON(" + deWKTarray[j] + ")");
if (deHole.within(deContainer)) deHoles[i] = j;
}
}
}
var deKomma = "";
var deWKTstring = "";
var deMulti = false;
for (var i = 0; i < deWKTarray.length; i++) {
if (deHoles[i] == -1) {
deWKTstring += deKomma + "(" + deWKTarray[i] + "";
if (i > 0) var deMulti = true;
}
for (var j = 0; j < deHoles.length; j++) {
if (deHoles[j] == i) deWKTstring += "," + deWKTarray[j] + "";
}
if (deHoles[i] == -1) deWKTstring += ")";
deKomma = ",";
}
if (deMulti) deWKTstring = "MULTIPOLYGON(" + deWKTstring +")";
else deWKTstring = "POLYGON" + deWKTstring;
return deWKTstring;
}
You can try/see everthing working in http://maps.amsterdam.nl/testshape/beheer (read Instructions in the Legend) 您可以在http://maps.amsterdam.nl/testshape/beheer中尝试/查看工作(阅读图例中的说明)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.