function works only with hardcoded values

1.1k views Asked by At

I am using a function with hardcoded values to assign colors to polygons on a leaflet map. the styling functions are called from inside a $.getJSON. i want to rewrite my code so that that the values are pulled directly from the data instead of hardcoded - in theory, this is so that i can reuse the code more easily in the future

The (truncated) geojson data called by $.getJSON is:

{"type": "FeatureCollection","crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [{"type": "Feature", "properties": {"MAJORCOLOR": "ZONE A"}, "geometry": { "type": "Polygon", "coordinates": [ [ [ -77.946264290220981, 42.574112051418197 ], [ -77.954525714402251, 42.569801987122105 ], [ -77.964847297117899, 42.562124252064194 ]]]}},  
"features": [{"type": "Feature", "properties": {"MAJORCOLOR": "ZONE B"}, "geometry": { "type": "Polygon", "coordinates": [ [ [ -77.946264290220981, 42.574112051418197 ], [ -77.954525714402251, 42.569801987122105 ], [ -77.964847297117899, 42.562124252064194 ]]]}},
"features": [{"type": "Feature", "properties": {"MAJORCOLOR": "ZONE A"}, "geometry": { "type": "Polygon", "coordinates": [ [ [ -77.946264290220981, 42.574112051418197 ], [ -77.954525714402251, 42.569801987122105 ], [ -77.964847297117899, 42.562124252064194 ]]]]}}]}

The $.getJSON is:

$.getJSON('data/ecozone_wgs84_multipart.geojson', function(data){
var geojsonLayer = L.geoJson(data.features, {  
    onEachFeature: makeMarkers,
    //this provides thematic styling to the layers
    style: style
})
.addTo(map);  

//call the function to create keys
getArray(data);
});

This is the function that assigns colors based on the data values

//get color depending on the Zone value
function getColor(z) {
return  z == 'ZONE A' ? '#a6cee3':
        z == 'ZONE B' ? '#1f78b4':
        z == 'ZONE C' ? '#b2df8a':
'#000000';}

Which in turn is called by the style function, from inside $.getJSON

function style(feature) {
return {
    fillColor: getColor(feature.properties.MAJORCOLOR),
    color: getColor(feature.properties.MAJORCOLOR),
    weight: 1.25,
    opacity: 0.95,
    fillOpacity: 0.5
};}

I want to rewrite the getColor function so that instead of having hardcoded values determining the color, instead the values are pulled from an array that i created from the geojson, which i have written as follows:

//this is a function that pulls the values from MAJORCOLOR to create the array
function getArray(data) {
for (var i=0; i< data.features.length; i++) {
keys.push(data.features[i].properties.MAJORCOLOR);
}}

//this is a function to collapse to unique values
function unique(keys) {
return keys.reduce(function(p, c) {
    if (p.indexOf(c) < 0) p.push(c);
    return p;
}, []);};

However when I rewrite the color function to use values from the array instead it doesn't work - the polygons are all assigned the "else" color #000000 instead of the colors I want.

function getColor(z) {
return  z == unique(keys)[0] ? '#a6cee3':
        z == unique(keys)[1] ? '#1f78b4':
        z == unique(keys)[2] ? '#b2df8a':
        '#000000';}

Why does this not work?

Looking at unique(keys) from the console, i get ["ZONE A", "ZONE B", "ZONE C"] so i know i am creating keys properly... i am baffled.

Thanks in advance for slogging through my question!

1

There are 1 answers

0
Oleksandr T. On BEST ANSWER
var keys = [];

function getArray(data) {
  for (var i = 0; i < data.features.length; i++) {
    keys.push(data.features[i].properties.MAJORCOLOR);
  }
}

function unique(keys) {
  return keys.reduce(function(p, c) {
    if (p.indexOf(c) < 0) p.push(c);
    return p;
  }, []);
};

function getColor(z) {
  return z == unique(keys)[0] ? '#a6cee3':
         z == unique(keys)[1] ? '#1f78b4':
         z == unique(keys)[2] ? '#b2df8a': '#000000';
} 

$.getJSON('data/ecozone_wgs84_multipart.geojson', function(data){
  getArray(data);

  var geojsonLayer = L.geoJson(data.features, {  
    onEachFeature: makeMarkers,
    style: function (feature) {


      // keys avaliable
      // getColor('xxxxxx'); keys availible
    }
  }).addTo(map);  
});