我使用OpenLayers3 ol.interaction.Draw让用户在地图上绘制一个形状,或者单击顶点,或者通过Shift+Drag绘制一个自由形状的多边形(这对我的应用程序很重要)。绘制完形状后,我使用turf.js将绘制的形状与客户端中的WFS层进行比较,运行intersect()以查看WFS功能是否与绘制的形状相交。但是,如果手绘形状具有最小的自交,则turf.js intersect()函数将发生以下错误(第326行是我调用intersect()的地方)。
turf.min.js:9隐身物体 getResultGeometry @ turf.min.js:9 si.overlayOp @ turf.min.js:9 交集@ turf.min.js:15 e.exports @ turf.min.js:16 (匿名函数)@ main.js:326
下面是我的代码草图。
var features = new ol.Collection();
var vs = new ol.source.Vector({
format: new ol.format.GeoJSON(),
url: function(extent) {
return XXXXXX;
},
strategy: ol.loadingstrategy.bbox
});
features.on('add', function() {
vs.forEachFeatureIntersectingExtent(extent, function(feature) {
// use to turf.js to intersect each feature with drawn feature
var bt = gjformat.writeFeatureObject(feature, {rightHanded: false});
var dt = gjformat.writeFeatureObject(features.item(0), {rightHanded: false} );
var intersection = turf.intersect(bt, dt);
}
});我试图同时使用turf.js simplify()和ol.geom.Geometry.simplify(),但都没有效果。有没有人建议让turf.js intersect()来处理手绘的自交多边形?还是一种在运行交叉口之前删除自交点的方法?
发布于 2016-07-28 19:03:45
受Using JSTS buffer to identify a self-intersecting polygon答案的启发(感谢领导,@ahocevar),我将解决方案移植到了turf.js上。使用自交0缓冲绘制的特性,移除较小的、自相交的多边形,并为您留下一个干净的特性,以便在intersect()中运行。
features.on('add', function() {
vs.forEachFeatureIntersectingExtent(extent, function(feature) {
// create geojson of wfs features and drawn feature
var bt = gjformat.writeFeatureObject(feature, {rightHanded: false});
var dt = gjformat.writeFeatureObject(features.item(0), {rightHanded: false} );
// check for kinks in the drawn feature
var kinks = turf.kinks(dt);
var dtf;
if(kinks.features.length > 0) {
// if there are self-intersections, buffer by 0 to get rid of them
dtf = turf.buffer(dt, 0, 'meters');
} else {
// if there are no self-intersection, intersect by unbuffered features
dtf = dt;
}
var intersection = turf.intersect(bt, dtf);
}
});发布于 2016-07-28 10:26:49
您至少可以提醒用户注意自交。这些都可以用JSTS检测到。见Google Maps Polygons self intersecting detection。删除自交比较困难,但是使用JSTS:Using JSTS buffer to identify a self-intersecting polygon也应该是可能的。
https://stackoverflow.com/questions/38599863
复制相似问题