如何将键值从json文件组合到geojson映射文件? [英] How to combine key values from a json file to a geojson map file?

查看:145
本文介绍了如何将键值从json文件组合到geojson映射文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将GeoJSON映射文件与JSON文件中的键值组合在一起,以用于合计映射.

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

这是文件的样子:

{
    "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": [...]
            }
        }
    ]
}

当前,我的代码如下:

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);
                }
            })
        }
    })
})

这使我接近想要的东西,但我想保留data1.json中显示的GeoJSON地图格式.

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

推荐答案

您可以在features数组上循环并检查data2上的zipcode值(如果存在),将其添加到各个元素的属性中

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)

这篇关于如何将键值从json文件组合到geojson映射文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆