簡體   English   中英

如何將新數據添加到 geoJson 文件?

[英]How to add new data to geoJson file?

我想從“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)

如何添加類型和幾何圖形? 我知道我只是缺乏一個小邏輯。 我無法抓住。

嘗試這個

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

另外-如果你想編輯你的功能,試試這個

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM