Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the style of each feature in a Leaflet GeoJSON layer

I have been studying the Leaflet Chloropleth example.

In my Leaflet application, I have a jQuery dropdown that, when selected, fires a function that takes the name of a state as an argument. I want to use that state name to update the Chloropleth map.

What is the pattern for changing the style of a Leaflet GeoJSON layer? Should I recreate the layer I created with L.geoJson() a second time? It seems as though the layers are drawing on top of each other with that approach.

I can provide a Fiddle if needed.

like image 948
Union find Avatar asked Sep 10 '14 19:09

Union find


People also ask

How do you add a GeoJSON layer to a Leaflet?

The GeoJSON layer To create it and add it to a map, we can use the following code: L. geoJSON(geojsonFeature). addTo(map);

What are layers in Leaflet?

In Leaflet, a “layer” is anything that moves around when the map is moved around. Before seeing how to create them from scratch, it's easier to explain how to do simple extensions.

How do you add a polygon to a Leaflet?

Create a polygon using the L. Pass the locations/points as variable to draw the polygon, and an option to specify the color of the polygon. var polygon = L. polygon(latlngs, {color: 'red'}); Add the polygon to the map using the addTo() method of the Polygon class.


3 Answers

To expand on the answer by @tmcw, the strategy is to pass a function to geojsonLayer.eachLayer() that changes the style for each feature instance within geojsonLayer.

Here is some code that demonstrates this strategy that I lifted and simplified from the code posted on the Mapbox example page referenced by @tmcw. My code example changes the style of each of the feature instances within geojsonLayer based on the value of a specified property on each feature instance.

var geojsonLayer = L.geoJson(...); // a GeoJSON layer declared in the outer scope

function restyleLayer(propertyName) {

    geojsonLayer.eachLayer(function(featureInstanceLayer) {
        propertyValue = featureInstanceLayer.feature.properties[propertyName];

        // Your function that determines a fill color for a particular
        // property name and value.
        var myFillColor = getColor(propertyName, propertyValue);

        featureInstanceLayer.setStyle({
            fillColor: myFillColor,
            fillOpacity: 0.8,
            weight: 0.5
        });
    });
}

restyleLayer('myProperty');
like image 87
DavidRR Avatar answered Nov 13 '22 05:11

DavidRR


Here's an example of changing a choropleth to classify on different properties - the trick is to call setStyle again with different values.

like image 41
tmcw Avatar answered Nov 13 '22 05:11

tmcw


The official documentation of Leaflet explains that:

https://leafletjs.com/examples/geojson/

The style option can be used to style features two different ways. First, we can pass a simple object that styles all paths (polylines and polygons) the same way ...

Alternatively, we can pass a function that styles individual features based on their properties. In the example below we check the "party" property and style our polygons accordingly ...

L.geoJSON(states, {
    style: function(feature) {
        switch (feature.properties.party) {
            case 'Republican': return {color: "#ff0000"};
            case 'Democrat':   return {color: "#0000ff"};
        }
    }
}).addTo(map);

Unfortunately, the names of the styles are not equal to css style names. For example, instead of 'stroke' use 'color' and instead of 'stroke-width' use 'weight':

https://leafletjs.com/reference-1.6.0.html#path-option

like image 5
Stefan Avatar answered Nov 13 '22 07:11

Stefan