javascript - 如何将新数据添加到 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)
如何添加类型和几何?我知道我只是缺乏一个小逻辑。我无法抓住。
解决方案
尝试这个
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),
}
}
推荐阅读
- spring - hibernate.globally_quoted_identifiers=true 打破与 H2 内存数据库的集成测试。找不到表
- angular - 在Angular中绑定图标时字体真棒不起作用
- android - 如何使用 pendingIntent Kotlin 从 Activity 传递到 BroadcastReceiver()
- python - Discord 嵌入标题和描述不起作用
- android - 如何在另一个应用程序上显示自定义对话
- vue.js - Vue组合api - 组件在开始使用reactive()时不呈现
- c# - 为什么 CMake 不能成功设置 COMMON_LANGUAGE_RUNTIME
- ios - ios 14,webp 动画在 safari 和 chrome 中以白色背景闪烁
- java - Dropwizard DropwizardAppRule 测试分段文件上传
- mysql - 在 SQL 表中查找重复值并在列中添加唯一值