I have a task at hand where I am aiming to merge a GeoJSON map file with pertinent key values from a JSON file in order to create a choropleth map.
Let's take a look at the structure of these files:
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,
}
This is what I envision for the final combined object:
{
"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": [...]
}
}
]
}
Presently, my code appears as follows:
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);
}
})
}
})
})
While this code gets me near to the desired outcome, my aim is to preserve the GeoJSON map format showcased in data1.json
.