2016-07-26 278 views
0

我使用OpenLayers3 ol.interaction.Draw來讓用戶在地圖上繪製一個形狀,可以通過點擊頂點或通過Shift +拖動來繪製自由形狀的多邊形(這很重要到我的申請)。一旦形狀繪製,我用turf.js來繪製形狀比較在客戶端WFS層,運行intersect(),看看是否WFS功能相交繪製的圖形。但是,如果手工繪製形狀有哪怕是一丁點的自我路口,turf.js intersect()功能失敗,出現以下錯誤(326行是我叫intersect())。turf.js來自OpenLayers3的自相交多邊形的相交錯誤Draw

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()處理手繪自相交多邊形有什麼建議?或者在運行交叉點之前刪除自相交的方法?

回答

0

通過回答啓發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); 
     } 
    });