turf.js来自OpenLayers3 Draw的自相交多边形的相交误差

时间:2016-07-26 21:05:05

标签: javascript openlayers-3 turfjs

我正在使用OpenLayers3 ol.interaction.Draw让用户在地图上绘制一个形状,可以通过单击顶点或通过Shift +拖动来绘制自由形状多边形(这对我的应用程序很重要)。绘制一个形状后,我使用turf.js将绘制的形状与客户端中的WFS图层进行比较,运行intersect()以查看WFS要素是否与绘制的形状相交。但是,如果手绘形状甚至有最轻微的自相交,则turf.js intersect()函数会失败并出现以下错误(第326行是我调用intersect()的位置)。

  

turf.min.js:9 Uncaught [object Object]
  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()来处理手绘的自相交多边形?或者在运行交叉路口之前移除自交叉点的方法?

2 个答案:

答案 0 :(得分:1)

受到Using JSTS buffer to identify a self-intersecting polygon答案的启发(感谢领导@hocevar),我将解决方案移植到了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);
      }
    });

答案 1 :(得分:0)

您至少可以警告用户自我交叉。这些可以通过JSTS检测到。见Google Maps Polygons self intersecting detection。删除自交叉更难,但JSTS也应该可以实现:Using JSTS buffer to identify a self-intersecting polygon