我也在尝试在多边形中寻找点。
所以我有一个带有点对象的数组和一个带有多边形对象的数组。
我想遍历我的点数组,而不是迭代多边形数组并查看点是否在该数组中。如果点在数组中,我想从点数组中删除该点,因为一个点只能在一个多边形中。
所以我尝试使用这个函数:
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--;
}
}
}
}
但是在经历了内部之后,该功能正在离开。我尝试了相同的方法,没有拼接和减少 i。但它仍然是相同的行为。
所以问题是我怎样才能再次进入外部?
实际上,您不必从数组中删除该项目,针对points
的当前 for 循环只会遍历一次。这是您正在做的事情的扁平化方法。
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);
}
});
}