我希望通过提供一个I数组来过滤像这样的geojson,该数组对应于我想要过滤的特定属性。我正在使用React和leaflet,本质上是想过滤掉我的地图。
因此,假设在上面链接的我的geojson中有一系列多多边形。我想使用以下索引let ids = [0,1,5,6]进行过滤,以便只显示具有该id的多多边形(在本例中为object_id)。我不知道该怎么做。我尝试使用leaflet提供的geojson过滤器功能,但不确定如何实现。下面是我到目前为止基于这个post的内容
import L from 'react-leaflet';
geoFilter = (feature, id) => {
if (feature.properties.index_right === id) return true;
};
//FILTER GEOJSON
sliceGeo = async () => {
let ids = [0, 1, 5, 6];
ids.map(id => {
//FOR EACH INDEX ITEM FILTER
let data = L.geoJson(myJson, { filter: sliceGeo((id = id)) }).addTo(map);
return data;
});
};我想我可能是以一种非常低效和不正确的方式来做这件事,所以希望有人能把我推向正确的方向。
发布于 2020-12-19 12:23:07
这是一个很好的函数,它返回过滤器函数。如下所示:
const filterByIds = (ids) => {
return futures => ids.every(id => feature.properties.index_right === id)
}此函数接受ID数组,并返回专用于ID的过滤器函数。例如,给定[1, 2, 3],返回features => [1, 2, 3].every(id => feature.properties.index_right === id。此筛选器函数检查[1, 2, 3]中是否存在includes index_right。
添加:
我检查并发现了更多有用的函数。
const filterByIds = (ids) => {
return futures => ids.includes(feature.properties.index_right)
}用法:
const ids = [0, 1, 5, 6];
const data = L.geoJson(myJson, { filter: filterByIds(ids) }).addTo(map);过滤器接受函数,所以倾向于认为应该给它去掉括号,但如果函数返回函数,则通常使用括号调用函数。以上就是这样。
发布于 2020-12-21 02:02:59
试试这个:
let ids = [0, 1, 5, 6];
geoFilter = (feature) => {
return ids.includes(feature.properties.index_right)
};
L.geoJson(myJson, { filter: geoFilter }).addTo(map);包含过滤器选项需要一个带有一个参数的函数( geoJson ),我用Array.prototype includes()函数更改了您的回调,如果ids数组中包含feature.properties.index_right,该函数将返回true。
https://stackoverflow.com/questions/65366124
复制相似问题