[英]How to create a subset of geoJson file from existing file
我有一個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
]};
由此,我想創建一個變量filterd_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
]};
因為每次我更新每個Line String的點
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,但未在地圖上繪制線條。
任何幫助表示贊賞。
Leaflet L.geoJSON
工廠上有一個filter
選項,您可以用來提供整個geoJSON
數據,並讓Leaflet僅保留滿足指定條件的數據(例如,您的情況下feature.properties.points >= 9
)。
在更新points
后重新構建您的filtered_geoJSON
變量時,必須在其上構建一個新的L.geoJSON
,然后才能在地圖上得到一些東西。
此外,即使直接更改geoJSON
數據,Leaflet也不會通過filter
再次對其進行評估,因此,如果某些功能低於9,或某些新功能高於9,則這些功能不會自動反映在地圖上。
最簡單的解決方案是刪除先前創建的L.geoJSON
圖層組,並使用更新的geoJSON
數據(和相同的過濾器)構建一個新的圖層組,以便Leaflet再次重新評估您的功能。
稍微復雜一點的解決方案是直接遍歷L.geoJSON
圖層組子圖層(而不是原始的geoJSON
數據),通常使用eachLayer
,在其上更新其feature.properties.points
並確定是否應將其隱藏或顯示背部。 您最初需要創建不帶任何過濾器的L.geoJSON
圖層組,然后將子圖層手動添加到地圖/從地圖中將其刪除。 當然,您也可以使用中間的L.layerGroup
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.