1

I'm trying to combine a GeoJSON map file with key values from a JSON file to use for a choropleth map.

Here is what the files look like:

data1.json

{
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "properties": {
                "STATE": "06",
                "ZIPCODE": "94601"
            },
            "geometry": {
                "type": "Polygon",
                "coordinates": [...]
            }
        },
        {
            "type": "Feature",
            "properties": {
                "STATE": "06",
                "ZIPCODE": "94501"
            },
            "geometry": {
                "type": "Polygon",
                "coordinates": [...]
            }
        }
    ]
}

data2.json

{
    "94501": {
    "crime": 172,
    "income": 9456,
    },
    "94601": {
    "crime": 118,
    "income": 28097,
}

Here's what I'd like the combined object to look like:

{
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "properties": {
                "STATE": "06",
                "ZIPCODE": "94601",
                "crime": 118,
                "income": 28097
            },
            "geometry": {
                "type": "Polygon",
                "coordinates": [...]
            }
        },
        {
            "type": "Feature",
            "properties": {
                "STATE": "06",
                "ZIPCODE": "94501",
                "crime": 172,
                "income": 9456
            },
            "geometry": {
                "type": "Polygon",
                "coordinates": [...]
            }
        }
    ]
}

Currently, my code looks like:

d3.json("data1.json", function (geoData) {
    d3.json("data2.json", function (zipdata) {

        var geoFeatures = geoData.features;

        for (var i = 0; i < geoFeatures.length; i++) {
            Object.keys(zipdata).forEach(function (key) {
                if (geoFeatures[i].properties.ZIPCODE == key) {
                    var combined = Object.assign({}, geoFeatures[i], zipdata[key]);
                    console.log(combined);
                }
            })
        }
    })
})

This gets me close to what I want, but I'd like to retain the GeoJSON map format shown in data1.json.

2 Answers2

2

You can loop on features array and check for zipcode values on data2 if it's there add it's to respective element's properties

let obj = {"type": "FeatureCollection","features": [{"type": "Feature","properties": {"STATE": "06","ZIPCODE": "94601"},"geometry": {"type": "Polygon","coordinates": "[...]"}},{"type": "Feature","properties": {"STATE": "06","ZIPCODE": "94501"},"geometry": {"type": "Polygon","coordinates": "[...]"}}]}

let data2 = {"94501": {"crime": 172,"income": 9456,},"94601": {"crime": 118,"income": 28097,}}

obj.features.forEach(val => {
  let { properties } = val
  let newProps = data2[properties.ZIPCODE]
  val.properties = { ...properties, ...newProps }
})

console.log(obj)
Code Maniac
  • 37,143
  • 5
  • 39
  • 60
1

Try this:

let data1 = {"type": "FeatureCollection","features": [{"type": "Feature","properties": {"STATE": "06","ZIPCODE": "94601"},"geometry": {"type": "Polygon","coordinates": "[...]"}},{"type": "Feature","properties": {"STATE": "06","ZIPCODE": "94501"},"geometry": {"type": "Polygon","coordinates": "[...]"}}]}

let data2 = {"94501": {"crime": 172,"income": 9456,},"94601": {"crime": 118,"income": 28097,}}

data1.features.map(res => Object.assign(res, {
properties: {
    ...res.properties,
    ...data2[res.properties.ZIPCODE]
}
}))

console.log(data1)
Ghoul Ahmed
  • 4,446
  • 1
  • 14
  • 23