2017-03-28 63 views
0

我正在尝试查找多边形中的点。使用嵌套和拼接的Javascript

所以我有一个数组与点对象和一个数组与多边形对象。

我想迭代我的点阵,并遍历多边形数组,并查看点是否在该数组中。如果点在数组中,我想从点数组中删除点,因为一个点只能在一个多边形中。

于是,我就用这个功能:

function pointsInPolygons(features, points) { 
    var pointCopy = $.extend(true, [], points.features); 
    for (var i = 0; i < pointCopy.length; i++) { 
     var point = pointCopy[i]; 
     for (var j = 0; j < features.features.length; j++) { 
      var feature = features.features[j]; 
      if (isPoly(feature) && gju.pointInPolygon(point.geometry, feature.geometry)) { 
       feature.properties.ratings.push(point.properties.rating); 
       pointCopy.splice(i, 1); 
       i--; 
      } 
     } 
    } 
} 

但通过内部去为后,该功能将离开。我尝试没有拼接和减少我一样。但它仍然是一样的行为。

所以问题是我怎么能再次进入外部?

+0

在刚刚完成拼接的位置,是否要'bre​​ak'跳出内循环并继续外循环的下一次迭代? – nnnnnn

+0

是的,这就是我想要的 – dominic

+1

所以...我说的是在'i - ;'语句之后立即添加'break;'(在'if'块内)。 – nnnnnn

回答

0

实际上,你不需要从数组中删除项目,你当前的循环对points只会迭代一次。这是你正在做的事情的扁平方法。

function pointsInPolygons(features, points){ 
    let pointsCopy = points.features.slice(); 
    let check = (point,feature) => isPoly(feature) && gju.pointInPolygon(point.geometry, feature.geometry); 
    let isInPolygon = point => features.find(check.bind(null, point)); 

    pointsCopy.forEach(point => { 
    let feature = isInPolygon(point); 
    if (!!feature) { 
     feature.properties.ratings.push(point.properties.rating); 
    } 
    }); 
}