首页 > 解决方案 > 如何将新数据添加到 geoJson 文件?

问题描述

我想从“dataToAdd”在“gj”中添加新属性。"gj 的格式为:

const gj = {
    "type": "FeatureCollection", "features" : [
        {
            "type": "Feature",
            "geometry": {
                "type": "Ploygon",
                "coordinates": ["coordinates"]
            },
            "properties": {
                "OBJECTID": 1,
                "DS_ID" : 1
            }
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "Ploygon",
                "coordinates": ["coordinates"]
            },
            "properties": {
                "OBJECTID": 2,
                "DS_ID" : 3
            }
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "Ploygon",
                "coordinates": ["coordinates"]
            },
            "properties": {
                "OBJECTID": 3,
                "DS_ID" : 2
            }
        },
    ]
}

& dataToAdd 的格式为:

const dataToAdd = [
    {
        "ds_id": 3,
        "value": 10
    },
    {
        "ds_id": 1,
        "value": 20
    },
    {
        "ds_id": 2,
        "value": 30
    },
]

我想要以下格式的要求输出:

requireOutput = {
    "type": "FeatureCollection", "features" : [
        {
            "type": "Feature",
            "geometry": {
                "type": "Ploygon",
                "coordinates": ["coordinates"]
            },
            "properties": {
                "OBJECTID": 1,
                "DS_ID" : 1,
                "value": 20
            }
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "Ploygon",
                "coordinates": ["coordinates"]
            },
            "properties": {
                "OBJECTID": 2,
                "DS_ID" : 3,
                "value": 10
            }
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "Ploygon",
                "coordinates": ["coordinates"]
            },
            "properties": {
                "OBJECTID": 3,
                "DS_ID" : 2,
                "value": 30
            }
        },
    ]
}

我可以在属性中添加数据,但我很难实现我想要的:

let requireOutput = [];
for(let i =0; i<gj.features.length; i++) {
    const properties = gj.features[i].properties
    requireOutput.push({
        ...properties,
        ...dataToAdd.find((item) => item.ds_id === properties.DS_ID)
    })
}
console.log(requireOutput)

如何添加类型和几何?我知道我只是缺乏一个小逻辑。我无法抓住。

标签: javascriptarraysgeojson

解决方案


尝试这个

let requireOutput = JSON.parse(JSON.stringify(gj));// For Deep Cloning so that gj does not get changed
for (i of requireOutput.features) 
    i.properties.value = dataToAdd.find((item) => item.ds_id === i.properties.DS_ID).value

const gj = {
    "type": "FeatureCollection",
    "features": [{
            "type": "Feature",
            "geometry": {
                "type": "Ploygon",
                "coordinates": ["coordinates"]
            },
            "properties": {
                "OBJECTID": 1,
                "DS_ID": 1
            }
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "Ploygon",
                "coordinates": ["coordinates"]
            },
            "properties": {
                "OBJECTID": 2,
                "DS_ID": 3
            }
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "Ploygon",
                "coordinates": ["coordinates"]
            },
            "properties": {
                "OBJECTID": 3,
                "DS_ID": 2
            }
        },
    ]
}
const dataToAdd = [{
        "ds_id": 3,
        "value": 10
    },
    {
        "ds_id": 1,
        "value": 20
    },
    {
        "ds_id": 2,
        "value": 30
    },
]
let requireOutput = JSON.parse(JSON.stringify(gj)); // For Deep Cloning so that gj does not get changed
for (i of requireOutput.features) 
    i.properties.value = dataToAdd.find((item) => item.ds_id === i.properties.DS_ID).value
console.log(requireOutput);

Plus-如果你想编辑你的函数,试试这个

let requireOutput = JSON.parse(JSON.stringify(gj))
for (let i = 0; i < requireOutput.features.length; i++) {
    const properties = requireOutput.features[i].properties
    requireOutput.features[i].properties = {
        ...properties,
        ...dataToAdd.find((item) => item.ds_id === properties.DS_ID),
    }
}

推荐阅读