I am trying to figure out why my polygons are not changing color based on a value I have in my GeoJSON. I've set up a hypothetical test here to replicate my issue here.
JSFIDDLE link here
My data snippet looks like so:
"properties": {
"Name": "Chahar Burjak",
"Province": "Nimroz",
"Ethnic1": 25,
"Ethnic2": 12,
"Ethnic3": 3,
"Ethnic4": 1,
"Ethnic5": 0,
"Ethnic6": 0
},
I have two polygons on a map that are a default blue. I want to style the polygons based on whether a value ('Ethnic1') is higher than a number like so:
Note: For brevity, this is just for "Ethnic Group 1". In my real world map, I have over 13 ethnic groups I am modeling
function setEthnic1Color(d) {
return d > 100 ? '#FC4E2A' :
d > 50 ? '#FD8D3C' :
d > 20 ? '#FEB24C' :
d > 10 ? '#FED976' :
'#FFEDA0';
}
Then, we define a styling function for our GeoJSON layer so that its fillColor depends on feature.properties.Ethnic1
property, also adjusting the appearance a bit and adding a nice touch with dashed stroke.
function Ethnic1Style(feature) {
return {
fillColor: setEthnic1Color(feature.properties.Ethnic1),
weight: 2,
opacity: 1,
color: 'white',
dashArray: '3',
fillOpacity: 0.7
};
}
Then, since we will have more than one ethnic group to style, I will use a switch statement to style it:
L.geoJson(myData, {
style: function(feature) {
switch (feature.properties) {
case 'Ethnic1': return { style: Ethnic1Style }
// ...and others
}
}
}).addTo(map);
As you can see in the fiddle, it doesn't style the polygons. What am I doing wrong?
Ethnic1
should be easy enough, but for multiple ethnic groups, it's a little unclear what your expected output would look like. Each of your features has properties for multiple ethnic groups,Ethnic1
throughEthnic6
. Are you intending to only display one ethnic group at a time? And if so, do you want to use a different color scale depending on which ethnic group is chosen, or do you want to use the same scale for all (i.e. a value of30
will produce the color#FEB24C
regardless of which ethnic group is being displayed)?feature.properties
is an object. It will never match any string of type'Ethnic1'
in yourswitch
statement.