我有一个大型的geojson文件,其结构如下:
{
"features": [{
"geometry": {
"coordinates": [
[
[-12.345, 26.006],
[-78.56, 24.944],
[-76.44, 24.99],
[-76.456, 26.567],
[-78.345, 26.23456]
]
],
"type": "Polygon"
},
"id": "Some_ID_01",
"properties": {
"parameters": "elevation"
},
"type": "Feature"
},
{
"geometry": {
"coordinates": [
[
[139.345, 39.2345],
[139.23456, 37.3465],
[141.678, 37.7896],
[141.2345, 39.6543],
[139.7856, 39.2345]
]
],
"type": "Polygon"
},
"id": "Some_OtherID_01",
"properties": {
"parameters": "elevation"
},
"type": "Feature"
}, {
"geometry": {
"coordinates": [
[
[143.8796, -30.243],
[143.456, -32.764],
[145.3452, -32.76],
[145.134, -30.87],
[143.123, -30.765]
]
],
"type": "Polygon"
},
"id": "Some_ID_02",
"properties": {
"parameters": "elevation"
},
"type": "Feature"
}
],
"type": "FeatureCollection"
}

我试图删除重复的ID,并保留最新的版本(即Some_ID_01和Some_ID_02被认为是我的目的重复,我想保留Some_ID_02)。这些"重复的内容"没有任何顺序(虽然如果我可以按照字母顺序在这个过程中对它们进行排序会很好),这些副本也不一定包含相同的坐标值(它们是同一点的较新版本)
到目前为止,我已经阅读了几个删除重复的json条目(特别尝试修改this guide here中的代码),但我不知道足够的JS来根据我的特定需求修改它。我正在阅读underscore.js以查看是否有帮助(基于其他线程中的建议),并且还将查看python或excel(作为CSV文件)以查看是否有任何简化。
是否有可能将geojson输入到程序中并获得一个文件作为回报(即使它是一个文本文件),还是更简单地将其输入内联?
答案 0 :(得分:0)
我选择使用python,因为我在那种语言中表现得更强。我将在下面发布我的代码以供参考,但您也可以找到我发布的另一篇文章here,其中详细介绍了使用列表从字典中删除密钥时遇到的问题
import json
json_file = open('features.json')
json_str = json_file.read()
json_data = json.loads(json_str)
dictionaryOfJsonId = {}
removalCounter = 0
keyToRemove = []
valueToRemoveFromList = []
IDList = []
for values in json_data['features']: #This loop converts the values in the json parse into a dict of only ID
stringToSplit = values["id"] #the id values from the json file
IDList.append(stringToSplit) #list with all the ID
newKey = stringToSplit[:-2] #takes the initial substring up to the last 2 spaces (version)
newValue = stringToSplit[-2:] #grabs the last two characters of the string
if newKey in dictionaryOfJsonId:
dictionaryOfJsonId[newKey].append(newValue)
else:
dictionaryOfJsonId[newKey] = [newValue]
for key in dictionaryOfJsonId: #Remove entries that do not have duplicates
if len(dictionaryOfJsonId[key])<2:
valueToRemoveFromList.append(str(key + dictionaryOfJsonId[key][0]))
else:
valueToRemoveFromList.append(str(key +max(dictionaryOfJsonId[key])))
for string in valueToRemoveFromList: #Remove all values that don't have duplicates from the List of ID
IDList.remove(string)
removalCounter+=1
good_features = [i for i in json_data['features'] if i['id'] not in IDList] #Loops through the original and
#removes keys on list from original JSON
with open('features.geojson','w') as outfile: #create JSON file from list
json.dump(good_features,outfile)
print "Removed",len(json_data['features'])-removalCounter, "entries from JSON"