turf.js与OpenLayers3 Draw中的自相交多边形的相交错误 [英] turf.js Intersect Error for Self-intersecting Polygons from OpenLayers3 Draw

查看:1093
本文介绍了turf.js与OpenLayers3 Draw中的自相交多边形的相交错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用OpenLayers3 ol.interaction.Draw让用户在地图上绘制形状,方法是单击顶点或通过Shift + Drag绘制自由格式的多边形(这对我的应用程序很重要).绘制形状后,我将使用turf.js将绘制的形状与客户端中的WFS图层进行比较,并运行intersect()来查看WFS功能是否与绘制的形状相交.但是,如果手绘形状甚至具有最小的自交,则turf.js intersect()函数将失败,并出现以下错误(在326行中,我将其称为intersect()).

I am using OpenLayers3 ol.interaction.Draw to let the user draw a shape on the map, either by clicking vertices or by Shift+Drag to draw a freeform polygon (this is important to my application). Once a shape is drawn, I use turf.js to compare the drawn shape to a WFS layer in the client, running intersect() to see if the WFS features intersect the drawn shape. However, if the hand drawn shape has even the slightest self-intersection, the turf.js intersect() function fails with the following error (Line 326 is where I call intersect()).

turf.min.js:9个未捕获的[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

turf.min.js:9 Uncaught [object Object]
getResultGeometry @ turf.min.js:9
si.overlayOp @ turf.min.js:9
intersection @ turf.min.js:15
e.exports @ turf.min.js:16
(anonymous function) @ main.js:326

以下是我的代码的草图.

Follows is a sketch of my code.

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()处理手绘的自相交多边形?还是在运行相交之前删除自相交的方法?

I have tried to use both turf.js simplify() and ol.geom.Geometry.simplify() to no avail. Does anyone have any suggestions for getting turf.js intersect() to handle the hand-drawn self-intersecting polygons? Or a way to remove the self-intersections before running the intersection?

推荐答案

灵感来自

Inspired by the answer to Using JSTS buffer to identify a self-intersecting polygon (thanks for the lead, @ahocevar), I ported the solution to turf.js. Buffering the drawn feature with self-intersections by 0 removes the smaller, self-intersected polygons and leaves you with a clean feature for running through 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);
      }
    });

这篇关于turf.js与OpenLayers3 Draw中的自相交多边形的相交错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆