Reputation: 687
I have a geoJSON file as below:
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
]};
From this I want to create a variable filtered_geoJSON where only points >=9 are available like below:
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
]};
Because each time I update the points of each Line String like
geoJSON['features'][i]['properties']['points']=data[i].points;
So from this I want to create the filtered_geoJSON variable each time and pass it to the
L.geoJson(filtered_geoJSON, {
style: style,
onEachFeature: onEachFeature
}).addTo(map);
So that I only draw the points which are having points >=9.
So I tried with
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++;
}
The filtered_geoJSON is there but not plotting the lines in the map.
Any help is appreciated.
Upvotes: 1
Views: 1024
Reputation: 53215
There is a filter
option on the Leaflet L.geoJSON
factory, that you can use to feed your entire geoJSON
data and have Leaflet keep only the ones that meet your specified criteria (i.e. feature.properties.points >= 9
in your case).
When you re-build your filtered_geoJSON
variable after updating your points
, you have to build a new L.geoJSON
out of it before you get something on your map.
Furthermore, even by changing the geoJSON
data directly, Leaflet will not evaluate it again through the filter
, so if some features go below 9, or some new ones go above, this will not be automatically reflected on the map.
The most simple solution would be to remove your previously created L.geoJSON
layer group, and build a new one with your updated geoJSON
data (and the same filter), so that Leaflet re-evaluates your features again.
A slightly more complex solution would be to iterate through the L.geoJSON
layer group child layers directly (instead of your raw geoJSON
data), typically using eachLayer
, update their feature.properties.points
there and determine whether it should now be hidden or shown back. You would need to create your L.geoJSON
layer group initially without any filter, and manually add the child layers to the map / remove them from map. Of course you can also use an intermediate L.layerGroup
.
Upvotes: 2