将特定的GeoJSON文件格式化为正确的格式


9

我想使用这个json文件,它还不是GeoJSON文件,但是我注意到它包含多个功能,而没有一个令我感到困惑的功能。我想问一下您是否知道可以将所有Feature / FeatureCollections合并到一个有效的GeoJSON文件中的工具,以便可以将其用于D3.js?原始文件在这里,我已经摆脱了geojson不需要的东西。

这是GeoJson的摘录,它很大,所以我不只是片段

{"points": [{
        "type": "FeatureCollection",
        "features": [{
            "type": "Feature",
            "geometry": {
                "coordinates": [41.9773865, 36.3372536],
                "type": "Point"
            },
            "properties": {
                "attacks": 1,
                "location": "Sinjar",
                "date": "2015-10-16"
            }
        }, {
            "type": "Feature",
            "geometry": {
                "coordinates": [43.4873886, 34.9301605],
                "type": "Point"
            },
            "properties": {
                "attacks": 2,
                "location": "Baiji",
                "date": "2015-10-16"
            }
        }, {
            "type": "Feature",
            "geometry": {
                "coordinates": [42.4509315, 36.3707008],
                "type": "Point"
            },
            "properties": {
                "attacks": 3,
                "location": "Tal Afar",
                "date": "2015-10-16"
            }
        }, {
            "type": "Feature",
            "geometry": {
                "coordinates": [43.76667, 35.31667],
                "type": "Point"
            },
            "properties": {
                "attacks": 1,
                "location": "Hawija",
                "date": "2015-10-16"
            }
        }]
    }, {
        "type": "FeatureCollection",
        "features": [{
            "type": "Feature",
            "geometry": {
                "coordinates": [43.7820587, 33.3516083],
                "type": "Point"
            },
            "properties": {
                "attacks": 4,
                "location": "Fallujah",
                "date": "2015-04-24"
            }
        }, {
            "type": "Feature",
            "geometry": {
                "coordinates": [43.2637405, 33.4324112],
                "type": "Point"
            },
            "properties": {
                "attacks": 1,
                "location": "Ramadi",
                "date": "2015-04-24"
            }
        }, {
            "type": "Feature",
            "geometry": {
                "coordinates": [43.1170998, 36.3246002],
                "type": "Point"
            },
            "properties": {
                "attacks": 5,
                "location": "Mosul",
                "date": "2015-04-24"
            }
        }, {
            "type": "Feature",
            "geometry": {
                "coordinates": [38.3535004, 36.8908997],
                "type": "Point"
            },
            "properties": {
                "attacks": 4,
                "location": "Kobane",
                "date": "2015-04-24"
            }
        }, {
            "type": "Feature",
            "geometry": {
                "coordinates": [42.4509315, 36.3707008],
                "type": "Point"
            },
            "properties": {
                "attacks": 1,
                "location": "Tal Afar",
                "date": "2015-04-24"
            }
        }]
    }, {
        "type": "FeatureCollection",
        "features": [{
            "type": "Feature",
            "geometry": {
                "coordinates": [43.7820587, 33.3516083],
                "type": "Point"
            },
            "properties": {
                "attacks": 1,
                "location": "Fallujah",
                "date": "2015-09-09"
            }
        }, {
            "type": "Feature",
            "geometry": {
                "coordinates": [43.2637405, 33.4324112],
                "type": "Point"
            },
            "properties": {
                "attacks": 3,
                "location": "Ramadi",
                "date": "2015-09-09"
            }
        }, {
            "type": "Feature",
            "geometry": {
                "coordinates": [41.9773865, 36.3372536],
                "type": "Point"
            },
            "properties": {
                "attacks": 1,
                "location": "Sinjar",
                "date": "2015-09-09"
            }
        }, {
            "type": "Feature",
            "geometry": {
                "coordinates": [43.4873886, 34.9301605],
                "type": "Point"
            },
            "properties": {
                "attacks": 1,
                "location": "Baiji",
                "date": "2015-09-09"
            }
        }, {
            "type": "Feature",
            "geometry": {
                "coordinates": [42.4509315, 36.3707008],
                "type": "Point"
            },
            "properties": {
                "attacks": 2,
                "location": "Tal Afar",
                "date": "2015-09-09"
            }
        }, 

您对如何解决此问题并获得正确的GeoJSON文件有一些想法吗?

Answers:


10

您可以使用(例如)Python编写一个简单的脚本来为您处理数据。

import json
from itertools import chain

打开文件并将数据读入Python字典:

isil = json.load(open('isil.en.json'))

points对象只是要素集合的列表,因此您可以使用python itertools库来帮助将这些集合中的要素链接在一起:

features = list(chain.from_iterable(fc['feature'] for fc in isil['points']))

最后,将包含所有2818个功能的新功能集写入文件。

feature_collection = {
    "type": "FeatureCollection":,
    "features": features
}

with open("isil_points.geojson", "w") as f:
    json.dump(feature_collection, f)

并且应该可以将其加载到您选择的系统中。查看数据您可能还必须进行一些手动清理(一些“总计”位置和一些没有位置的点),但这应该是一个开始。

来自每个要素集合的点已合并。


7

因为它是“ oneshot”,所以您可以手动进行(也可以通过Node运行)

在浏览器中打开一个JavaScript控制台。

您需要循环获取的数组Feature(因为每个数组FeatureCollection都有一个或多个Feature

然后,您将使用flatten函数将数组数组转换为数组(从https://stackoverflow.com/a/15030117借用的递归函数)

完整的代码在下面(文件内容除外,为了保持可读性,不完整)

// Copy/paste the text from you source https://raw.githubusercontent.com/RitterLean/Geojson/master/geofile.json 
content = {
"points": [{
    "type": "FeatureCollection",
    "features": [{
        "type": "Feature",
        "geometry": {
            "coordinates": [41.9773865, 36.3372536],
            "type": "Point"
        },
        "properties": {
            "attacks": 1,
            "location": "Sinjar",
            "date": "2015-10-16"
        }
    }, {
        "type": "Feature",
        "geometry": {
            "coordinates": [43.4873886, 34.9301605],
            "type": "Point"
        },
        "properties": {
            "attacks": 2,
            "location": "Baiji",
            "date": "2015-10-16"
        }
    }, {
    ...
    // Be careful, incomplete because shortened for illustration 

intermediate_result = content['points'].map(function(el){
    return el.features;
});

function flatten(arr) {
  return arr.reduce(function (flat, toFlatten) {
    return flat.concat(Array.isArray(toFlatten) ? flatten(toFlatten) : toFlatten);
  }, []);
};

geojson_output = {
        "type": "FeatureCollection",
        "features": flatten(intermediate_result)
}
// Transform the object to a string you can paste into a file
console.log(JSON.stringify(geojson_output));

结果可以在http://geojson.io/#id=gist:anonymous/da10ab9afc9a5941ba66&map=4/19.48/22.32上看到

您会看到一些结果的坐标错误(0,0)。这是由于原始内容。

通过此演示,您还可以导出到GeoJSON。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.