首页 > 解决方案 > 将 json 添加到与 id 匹配的 geojson 文件

问题描述

我有一个包含某些位置信息的 JSON 文件和一个包含这些位置多边形的 GeoJSON 文件。要将信息导入到 QGis 上的位置,我需要将 JSON 文件添加到 GeoJSON 文件中。

我有一个带有 ID 的 GeoJSON 文件kobuleti_precincts.geojson

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              4653793.42338882,
              5124889.5345974155
            ],
           ...
           [
              4653796.093490953,
              5124881.326846274
            ],
            [
              4653793.42338882,
              5124889.5345974155
            ]
          ]
        ]
      },
      "properties": {
        "id": 72757,
        "parent_id": 69911,
        "common_id": "61",
        "common_name": "81-61",
        "has_children": false,
        "shape_type_id": 4,
        "shape_type_name": "Precinct",
        "value": "No Data",
        "color": null,
        "title_location": "Precinct: 81-61"
      }
    }
  ]
}

和一个带有 id 的 JSON 文件kobuleti_results.json

{
  "indicator": {
    "is_summary": true,
    "name": null,
    "name_abbrv": "Overall Results",
    "description": "Overall Results",
    "number_format": null,
    "scales": [
      {
        "name": "No Data",
        "color": "#CCCCCC"
      }
    ],
    "scale_colors": [
      "#CCCCCC"
    ],
    "switcher_indicator_id": null
  },
  "view_type": "summary",
  "shape_data": [
    ...
    [
      {
        "shape_values": {
          "shape_id": 72757,
          "shape_name": "81-61",
          "parent_id": null,
          "value": "Margvelashvili (Georgian Dream)",
          "color": "#1b70af",
          "number_format": null,
          "precincts_completed_precent": null,
          "title": "Results",
          "title_abbrv": null,
          "title_location": null,
          "title_precincts_completed": null
        }
      },
      {
        "summary_data": {
          "data": [
            {
              "id": 5538710,
              "value": "54.46",
              "formatted_value": "54.46",
              "number_format": "%",
              "rank": 1,
              "color": "#1b70af",
              "indicator_type_id": 2,
              "indicator_type_name": "Results",
              "core_indicator_id": 119,
              "indicator_id": 2701,
              "indicator_name_unformatted": "Giorgi Margvelashvili",
              "indicator_name": "Giorgi Margvelashvili (Georgian Dream)",
              "indicator_name_abbrv": "Margvelashvili (Georgian Dream)",
              "has_openlayers_rule_value": false,
              "visible": true
            },
            {
              "id": 5538692,
              "value": "29.34",
              "formatted_value": "29.34",
              "number_format": "%",
              "rank": 2,
              "color": "#ca4748",
              "indicator_type_id": 2,
              "indicator_type_name": "Results",
              "core_indicator_id": 121,
              "indicator_id": 2719,
              "indicator_name_unformatted": "Davit Bakradze",
              "indicator_name": "Davit Bakradze (UNM)",
              "indicator_name_abbrv": "Bakradze (UNM)",
              "has_openlayers_rule_value": false,
              "visible": true
            },
            {
              "id": 5538701,
              "value": "9.15",
              "formatted_value": "9.15",
              "number_format": "%",
              "rank": 3,
              "color": "#079fe2",
              "indicator_type_id": 2,
              "indicator_type_name": "Results",
              "core_indicator_id": 125,
              "indicator_id": 2731,
              "indicator_name_unformatted": "Nino Burjanadze",
              "indicator_name": "Nino Burjanadze (United Geo)",
              "indicator_name_abbrv": "Burjanadze (United Geo)",
              "has_openlayers_rule_value": false,
              "visible": true
            },
            {
              "id": 5538697,
              "value": "4.93",
              "formatted_value": "4.93",
              "number_format": "%",
              "rank": 4,
              "color": "#ca9c6c",
              "indicator_type_id": 2,
              "indicator_type_name": "Results",
              "core_indicator_id": 117,
              "indicator_id": 2689,
              "indicator_name_unformatted": "Giorgi Targamadze",
              "indicator_name": "Giorgi Targamadze (CDM)",
              "indicator_name_abbrv": "Targamadze (CDM)",
              "has_openlayers_rule_value": false,
              "visible": true
            },
            {
              "id": 5538689,
              "value": "0.47",
              "formatted_value": "0.47",
              "number_format": "%",
              "rank": 5,
              "color": "#e0e4bf",
              "indicator_type_id": 2,
              "indicator_type_name": "Results",
              "core_indicator_id": 105,
              "indicator_id": 2617,
              "indicator_name_unformatted": "Giorgi Liluashvili",
              "indicator_name": "Giorgi Liluashvili (Init Grp)",
              "indicator_name_abbrv": "Liluashvili (Init Grp)",
              "has_openlayers_rule_value": false,
              "visible": true
            }
          ],
          "visible": true,
          "has_openlayers_rule_value": true,
          "total_ranks": 12,
          "has_duplicates": true
        }
      },
      {
        "data_item": {
          "id": 5538680,
          "value": "426",
          "formatted_value": "426",
          "number_format": null,
          "rank": null,
          "color": null,
          "indicator_type_id": 1,
          "indicator_type_name": null,
          "core_indicator_id": 15,
          "indicator_id": 2523,
          "indicator_name_unformatted": "Total Voter Turnout (#)",
          "indicator_name": "Total Voter Turnout (#)",
          "indicator_name_abbrv": "Total Turnout (#)",
          "has_openlayers_rule_value": false,
          "visible": true
        }
      },
      {
        "data_item": {
          "id": 5538681,
          "value": "38.38",
          "formatted_value": "38.38",
          "number_format": "%",
          "rank": null,
          "color": null,
          "indicator_type_id": 1,
          "indicator_type_name": null,
          "core_indicator_id": 16,
          "indicator_id": 2529,
          "indicator_name_unformatted": "Total Voter Turnout (%)",
          "indicator_name": "Total Voter Turnout (%)",
          "indicator_name_abbrv": "Total Turnout (%)",
          "has_openlayers_rule_value": false,
          "visible": false
        }
      }
    ]
  ]
}

预期产出

我期待一个可以被大多数 GIS 作为图层读取的 GeoJSON 文件。在我们的特殊情况下,我认为是这样的:

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              4653793.42338882,
              5124889.5345974155
            ],
           ...
           [
              4653796.093490953,
              5124881.326846274
            ],
            [
              4653793.42338882,
              5124889.5345974155
            ]
          ]
        ]
      },
      "properties": {
        "id": 72757,
        "parent_id": 69911,
        "common_id": "61",
        "common_name": "81-61",
        "has_children": false,
        "shape_type_id": 4,
        "shape_type_name": "Precinct",
        "value": "No Data",
        "color": null,
        "title_location": "Precinct: 81-61"
    "shape_data": [
          {
            "shape_values": {
              "shape_id": 72757,
              "shape_name": "81-61",
              "parent_id": null,
              "value": "Margvelashvili (Georgian Dream)",
              "color": "#1b70af",
              "number_format": null,
              "precincts_completed_precent": null,
              "title": "Results",
              "title_abbrv": null,
              "title_location": null,
              "title_precincts_completed": null
            }
          },
          {
            "summary_data": {
              "data": [
                {
                  "id": 5538710,
                  "value": "54.46",
                  "formatted_value": "54.46",
                  "number_format": "%",
                  "rank": 1,
                  "color": "#1b70af",
                  "indicator_type_id": 2,
                  "indicator_type_name": "Results",
                  "core_indicator_id": 119,
                  "indicator_id": 2701,
                  "indicator_name_unformatted": "Giorgi Margvelashvili",
                  "indicator_name": "Giorgi Margvelashvili (Georgian Dream)",
                  "indicator_name_abbrv": "Margvelashvili (Georgian Dream)",
                  "has_openlayers_rule_value": false,
                  "visible": true
                },
                {
                  "id": 5538692,
                  "value": "29.34",
                  "formatted_value": "29.34",
                  "number_format": "%",
                  "rank": 2,
                  "color": "#ca4748",
                  "indicator_type_id": 2,
                  "indicator_type_name": "Results",
                  "core_indicator_id": 121,
                  "indicator_id": 2719,
                  "indicator_name_unformatted": "Davit Bakradze",
                  "indicator_name": "Davit Bakradze (UNM)",
                  "indicator_name_abbrv": "Bakradze (UNM)",
                  "has_openlayers_rule_value": false,
                  "visible": true
                },
                {
                  "id": 5538701,
                  "value": "9.15",
                  "formatted_value": "9.15",
                  "number_format": "%",
                  "rank": 3,
                  "color": "#079fe2",
                  "indicator_type_id": 2,
                  "indicator_type_name": "Results",
                  "core_indicator_id": 125,
                  "indicator_id": 2731,
                  "indicator_name_unformatted": "Nino Burjanadze",
                  "indicator_name": "Nino Burjanadze (United Geo)",
                  "indicator_name_abbrv": "Burjanadze (United Geo)",
                  "has_openlayers_rule_value": false,
                  "visible": true
                },
                {
                  "id": 5538697,
                  "value": "4.93",
                  "formatted_value": "4.93",
                  "number_format": "%",
                  "rank": 4,
                  "color": "#ca9c6c",
                  "indicator_type_id": 2,
                  "indicator_type_name": "Results",
                  "core_indicator_id": 117,
                  "indicator_id": 2689,
                  "indicator_name_unformatted": "Giorgi Targamadze",
                  "indicator_name": "Giorgi Targamadze (CDM)",
                  "indicator_name_abbrv": "Targamadze (CDM)",
                  "has_openlayers_rule_value": false,
                  "visible": true
                },
                {
                  "id": 5538689,
                  "value": "0.47",
                  "formatted_value": "0.47",
                  "number_format": "%",
                  "rank": 5,
                  "color": "#e0e4bf",
                  "indicator_type_id": 2,
                  "indicator_type_name": "Results",
                  "core_indicator_id": 105,
                  "indicator_id": 2617,
                  "indicator_name_unformatted": "Giorgi Liluashvili",
                  "indicator_name": "Giorgi Liluashvili (Init Grp)",
                  "indicator_name_abbrv": "Liluashvili (Init Grp)",
                  "has_openlayers_rule_value": false,
                  "visible": true
                }
              ],
              "visible": true,
              "has_openlayers_rule_value": true,
              "total_ranks": 12,
              "has_duplicates": true
            }
          },
          {
            "data_item": {
              "id": 5538680,
              "value": "426",
              "formatted_value": "426",
              "number_format": null,
              "rank": null,
              "color": null,
              "indicator_type_id": 1,
              "indicator_type_name": null,
              "core_indicator_id": 15,
              "indicator_id": 2523,
              "indicator_name_unformatted": "Total Voter Turnout (#)",
              "indicator_name": "Total Voter Turnout (#)",
              "indicator_name_abbrv": "Total Turnout (#)",
              "has_openlayers_rule_value": false,
              "visible": true
            }
          },
          {
            "data_item": {
              "id": 5538681,
              "value": "38.38",
              "formatted_value": "38.38",
              "number_format": "%",
              "rank": null,
              "color": null,
              "indicator_type_id": 1,
              "indicator_type_name": null,
              "core_indicator_id": 16,
              "indicator_id": 2529,
              "indicator_name_unformatted": "Total Voter Turnout (%)",
              "indicator_name": "Total Voter Turnout (%)",
              "indicator_name_abbrv": "Total Turnout (%)",
              "has_openlayers_rule_value": false,
              "visible": false
            }
          }
        ]
      ...
      }
    }
  ]
}

或者喜欢

{
    "type": "FeatureCollection",
    "features": [{
        "type": "Feature",
        "geometry": {
            "type": "Polygon",
            "coordinates": [
                [
                    [
                        4653793.42338882,
                        5124889.5345974155
                    ],
                     ...
                    [
                        4653796.093490953,
                        5124881.326846274
                    ],
                    [
                        4653793.42338882,
                        5124889.5345974155
                    ]
                ]
            ]
        },
        "properties": {
            "id": 72757,
            "parent_id": 69911,
            "common_id": "61",
            "common_name": "81-61",
            "has_children": false,
            "shape_type_id": 4,
            "shape_type_name": "Precinct",
            "value": "No Data",
            "color": null,
            "title_location": "Precinct: 81-61"
            "data": [{
                    "id": 5538710,
                    "value": "54.46",
                    "formatted_value": "54.46",
                    "number_format": "%",
                    "rank": 1,
                    "color": "#1b70af",
                    "indicator_type_id": 2,
                    "indicator_type_name": "Results",
                    "core_indicator_id": 119,
                    "indicator_id": 2701,
                    "indicator_name_unformatted": "Giorgi Margvelashvili",
                    "indicator_name": "Giorgi Margvelashvili (Georgian Dream)",
                    "indicator_name_abbrv": "Margvelashvili (Georgian Dream)",
                    "has_openlayers_rule_value": false,
                    "visible": true
                },
                {
                    "id": 5538692,
                    "value": "29.34",
                    "formatted_value": "29.34",
                    "number_format": "%",
                    "rank": 2,
                    "color": "#ca4748",
                    "indicator_type_id": 2,
                    "indicator_type_name": "Results",
                    "core_indicator_id": 121,
                    "indicator_id": 2719,
                    "indicator_name_unformatted": "Davit Bakradze",
                    "indicator_name": "Davit Bakradze (UNM)",
                    "indicator_name_abbrv": "Bakradze (UNM)",
                    "has_openlayers_rule_value": false,
                    "visible": true
                },
                {
                    "id": 5538701,
                    "value": "9.15",
                    "formatted_value": "9.15",
                    "number_format": "%",
                    "rank": 3,
                    "color": "#079fe2",
                    "indicator_type_id": 2,
                    "indicator_type_name": "Results",
                    "core_indicator_id": 125,
                    "indicator_id": 2731,
                    "indicator_name_unformatted": "Nino Burjanadze",
                    "indicator_name": "Nino Burjanadze (United Geo)",
                    "indicator_name_abbrv": "Burjanadze (United Geo)",
                    "has_openlayers_rule_value": false,
                    "visible": true
                },
                {
                    "id": 5538697,
                    "value": "4.93",
                    "formatted_value": "4.93",
                    "number_format": "%",
                    "rank": 4,
                    "color": "#ca9c6c",
                    "indicator_type_id": 2,
                    "indicator_type_name": "Results",
                    "core_indicator_id": 117,
                    "indicator_id": 2689,
                    "indicator_name_unformatted": "Giorgi Targamadze",
                    "indicator_name": "Giorgi Targamadze (CDM)",
                    "indicator_name_abbrv": "Targamadze (CDM)",
                    "has_openlayers_rule_value": false,
                    "visible": true
                },
                {
                    "id": 5538689,
                    "value": "0.47",
                    "formatted_value": "0.47",
                    "number_format": "%",
                    "rank": 5,
                    "color": "#e0e4bf",
                    "indicator_type_id": 2,
                    "indicator_type_name": "Results",
                    "core_indicator_id": 105,
                    "indicator_id": 2617,
                    "indicator_name_unformatted": "Giorgi Liluashvili",
                    "indicator_name": "Giorgi Liluashvili (Init Grp)",
                    "indicator_name_abbrv": "Liluashvili (Init Grp)",
                    "has_openlayers_rule_value": false,
                    "visible": true
                }
            ]
        }
    ...
    }]
}

它们在属性中都具有相同的 id:"id": 72757forkobuleti_precincts"shape_id": 72757for kobuleti_results。至于我不确定我们是否可以在 QGis 中加载 JSON 数据以将它们与 GeoJSON 映射我想将 JSON 添加到 GEOJson 文件以便能够将其加载到 GIS 上。

我是 javascript 的新手(显然它会死。我在 Python 中更好),到目前为止我尝试过:

precinct_json.features.map(function(feature){
    var matchedArray = precinct_results.shape_data.filter(function(feature2){
        return feature.properties.id === feature2[0].shape_values.shape_id;
    });
    if(matchedArray && matchedArray[0]){
        for(var attr in matchedArray[0].properties){
            feature.properties[attr] = matchedArray[0].properties[attr];
        }
    }
});

但我得到:

(64) [undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined]

重现示例的数据

所有数据都来自我使用网络工具获得的这张地图inspect element,就像在这里看到的另一张地图一样:

在此处输入图像描述

也许我可以从控制台合并?

到目前为止,我知道以下脚本允许我获取 JSON 文件所引用的 shape_id。:

for (var i = 0; i < precinct_results.shape_data.length ; i++) {
    console.log(precinct_results.shape_data[i][0].shape_values.shape_id);
    //Do something
}

下面的一个允许我得到idgeojson 的一个:

for (var i = 0; i < precinct_json.features.length ; i++) {
    console.log(precinct_json.features[i].properties.id);
    //Do something
}

标签: javascriptjsonmergegeojson

解决方案


如果我理解正确,您想在 GeoJson的特征和Json 文件中的形状数据之间进行JOIN 。你可以试试这个:

const geojdata = JSON.parse("your_geojson_string").features;
const jsondata = JSON.parse("your_json_string").shape_data;
// a function to find the unique shape corresponding to the feature f
// (note: valid if shape_value is always the first element)
const findS2F = (f,js) => js.find(s => s[0].shape_value.shape_id === f.properties.id);
// a function to perform the join: copy into properties.shape_data
const joinFS = (f,js) => {f.properties.shape_data = findS2F(f,js); return f;};
// a function to process the feature collection
const joinFeaturesShapes = (gf,js) => gf.map(f => joinFS(f,js));
// then use that function to get your new geojson string (to save in a file)
const newstring = `{
  "type": "FeatureCollection",
  "features":${JSON.stringify(joinFeaturesShapes(geojdata, jsondata))}
 }`;

推荐阅读