Javascript 如何从现有文件创建geoJson文件的子集
我有一个geoJSON文件,如下所示:Javascript 如何从现有文件创建geoJson文件的子集,javascript,json,leaflet,mapbox,geojson,Javascript,Json,Leaflet,Mapbox,Geojson,我有一个geoJSON文件,如下所示: var geoJSON= { "type":"FeatureCollection", "features": [ {"type": "Feature","geometry":{"type":"LineString", "coordinates":[[102.463303,36.447488],[102.514
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;
因此,我希望每次都创建过滤的_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++;
}
var top_geoJSON={“type”:“FeatureCollection”,“features”:[]};
var c=0;
对于(i=0;i9){
过滤的_geoJSON[c]=geoJSON['features'][i];
C++;
}
过滤后的_geoJSON存在,但没有在地图中绘制线条
非常感谢您的帮助。传单L.geoJSON
工厂中有一个选项,您可以使用该选项提供整个geoJSON
数据,并让传单仅保留满足您指定标准的数据(即您的情况下feature.properties.points>=9
)
当您在更新点后重新构建过滤的\u geoJSON
变量时,您必须先从中构建一个新的L.geoJSON
,然后才能在地图上获得一些内容
此外,即使直接更改geoJSON
数据,传单也不会通过过滤器对其进行再次评估,因此,如果某些功能低于9,或者某些新功能高于9,则不会自动反映在地图上
最简单的解决方案是删除先前创建的L.geoJSON
图层组,并使用更新的geoJSON
数据(和相同的过滤器)构建一个新的图层组,以便传单再次评估您的功能
一个稍微复杂一点的解决方案是直接迭代L.geoJSON
层组子层(而不是原始geoJSON
数据),通常使用更新它们的feature.properties.points
并确定现在是隐藏还是显示。您首先需要创建L.geoJSON
图层组,而不使用任何过滤器,然后手动将子图层添加到地图中/从地图中删除它们。当然,您也可以使用中间的L.layerGroup
您的点
值是动态变化的吗,你想让地图相应地显示/隐藏你的特征吗?是的,没错。我的geoJSON变量会动态更新所有线串的点。因此,我只想显示点在9以上的线串。非常感谢过滤器选项。它工作得很好。