gpt4 book ai didi

javascript - 如何从现有文件创建 geoJson 文件的子集

转载 作者:行者123 更新时间:2023-11-30 15:21:46 26 4
gpt4 key购买 nike

我有一个 geoJSON 文件如下:

    var geoJSON=    
{

"type":"FeatureCollection",

"features": [
{"type": "Feature","geometry":{"type":"LineString", "coordinates":[[102.463303,36.447488],[102.514114,36.439755]]},"properties": {"id":"01","points":9}},
{"type": "Feature","geometry":{"type":"LineString", "coordinates":[[102.593765,36.414341],[102.634964,36.402183]]},"properties": {"id":"02","points":8}},
{"type": "Feature","geometry":{"type":"LineString", "coordinates":[[102.880783,36.485038],[102.882156,36.561187]]},"properties":{"id":"03","points":10}}

........
and so on
]};

由此我想创建一个变量 filtered_geoJSON,其中只有 >=9 的点可用,如下所示:

var filtered_geoJSON=    
{

"type":"FeatureCollection",

"features": [
{"type": "Feature","geometry":{"type":"LineString", "coordinates":[[102.463303,36.447488],[102.514114,36.439755]]},"properties": {"id":"01","points":9}},
{"type": "Feature","geometry":{"type":"LineString", "coordinates":[[102.880783,36.485038],[102.882156,36.561187]]},"properties":{"id":"03","points":10}}

........
and so on
]};

因为每次我都像这样更新每个线串的点

  geoJSON['features'][i]['properties']['points']=data[i].points;

因此我想每次都创建 filtered_geoJSON 变量并将其传递给

L.geoJson(filtered_geoJSON, {
style: style,
onEachFeature: onEachFeature
}).addTo(map);

所以我只绘制点数 >=9 的点。

所以我尝试了

var top_geoJSON={"type":"FeatureCollection","features": []};
var c=0;
for (i = 0; i<40000; i++) {


if(geoJSON['features'][i]['properties']['score']!=data[i].points){
links['features'][i]['properties']['score']=data[i].score;
}

if(geoJSON['features'][i]['properties']['points']>9){
filtered_geoJSON[c]=geoJSON['features'][i];
c++;
}

filtered_geoJSON 存在,但未在 map 中绘制线条。

感谢任何帮助。

最佳答案

有一个filter Leaflet L.geoJSON 工厂上的选项,您可以使用它来提供整个 geoJSON 数据,并让 Leaflet 仅保留满足您指定条件的数据(即 feature.properties.points >= 9 在你的情况下)。

当您在更新之后重新构建您的filtered_geoJSON变量时,您必须构建一个新的L.geoJSON 在你在 map 上找到一些东西之前退出它。

此外,即使直接更改 geoJSON 数据,Leaflet 也不会通过 filter 再次对其进行评估,因此如果某些功能低于 9,或者某些新功能上面,这将不会自动反射(reflect)在 map 上。

最简单的解决方案是删除您之前创建的 L.geoJSON 层组,并使用更新后的 geoJSON 数据(和相同的过滤器)构建一个新层组, 以便 Leaflet 再次重新评估您的功能。

稍微复杂一点的解决方案是直接遍历 L.geoJSON 层组子层(而不是原始的 geoJSON 数据),通常使用 eachLayer ,在那里更新它们的 feature.properties.points 并确定它现在是应该隐藏还是显示回来。您最初需要在不使用任何过滤器的情况下创建 L.geoJSON 图层组,然后手动将子图层添加到 map /从 map 中删除它们。当然你也可以使用中间的L.layerGroup

关于javascript - 如何从现有文件创建 geoJson 文件的子集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43626397/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com