【问题标题】:Remove older versions from a Geojson array从 Geojson 数组中删除旧版本
【发布时间】:2017-11-16 17:36:42
【问题描述】:

我有一个大的 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 输入到程序中并获得一个文件作为回报(即使它是一个文本文件)还是内联输入它会更简单?

【问题讨论】:

    标签: javascript python excel duplicates geojson


    【解决方案1】:

    我选择使用 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" 
    

    【讨论】:

      猜你喜欢
      • 2018-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-11
      • 1970-01-01
      • 1970-01-01
      • 2016-09-06
      相关资源
      最近更新 更多