【发布时间】:2020-02-27 17:56:15
【问题描述】:
我有一个包含某些位置信息的 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
}
}
]
]
}
预期输出
我期待一个 GeoJSON 文件可以被大多数 GIS 作为一个图层读取。在我们的特殊情况下,我认为是这样的:
{
"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": 72757 对应于 kobuleti_precincts,"shape_id": 72757 对应于 kobuleti_results。至于我不确定我们是否可以在 QGis 中加载 JSON 数据以将它们与 GeoJSON 映射我想将 JSON 添加到 GEOJson 文件以便能够将其加载到 GIS 上。
我是 javascript 新手(显然是 it's going to die。我更擅长 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]
重现示例的数据
所有数据都来自this map,这是我使用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
}
下面的一个可以让我得到geojson的id:
for (var i = 0; i < precinct_json.features.length ; i++) {
console.log(precinct_json.features[i].properties.id);
//Do something
}
【问题讨论】:
-
请指定输出的方式。
-
@adnanmuttaleb 当然可以。
标签: javascript json merge geojson