Description
It would be awesome to have some way to interactively change the styles of individual features of GeoJSON
objects (and possibly other vector layers) without having to remove and re-add the entire layer. FWIW it appears this works for TopoJSON
as well: http://gis.stackexchange.com/questions/120012/leaflet-omnivore-style-function. The final answer on this question (http://gis.stackexchange.com/questions/75590/setstyle-function-for-geojson-features-leaflet) included this:
geojson_layer.eachLayer(function (layer) {
if(layer.feature.properties.NAME == 'feature 1') {
layer.setStyle({fillColor :'blue'})
}
});
As this is my first exposure to this method, I don't have detailed recommendations on how to implement this yet. I can describe my use case - I'm dynamically calculating some values in R that are used to color GeoJSON
features, so right now I'm removing the GeoJSON
and adding back a GeoJSON
with the new styles upon each calculation. BTW, the jsonlite
and RJSONIO
toJSON
functions are very slow for converting back to GeoJSON
after manipulating the data as an R
list (takes around 15 seconds for a 1MB GeoJSON
with only id's and simplified shapes), so I am actually parsing the GeoJSON
as text and inserting the styles (0.3 seconds).