合并两个 geojson 状态邮政编码文件?

Combine two geojson state zipcode files?

我正在做一个需要用到 US States Zip Code Data 的项目。我想合并两个 geojson 文件,同时保留这些文件中的数据。 geojson-merge https://github.com/mapbox/geojson-merge 这样做,但我希望有一个基于 python 的解决方案。

每个州都有一个单独的 *.json 文件。例如:

  1. mt_montana_zip_codes_geo.min.json

  2. nd_north_dakota_zip_codes_geo.min.json

    import json
    
    nd_boundary_file = r"C:\Data_ZipCodes_States\State-zip-code-GeoJSON-master" \
              r"\nd_north_dakota_zip_codes_geo.min.json"
    
    with open(nd_boundary_file, 'r') as f:
        nd_zipcode_boundary = json.load(f)
    
    mt_boundary_file = r"C:\Data_ZipCodes_States\State-zip-code-GeoJSON-master" \
              r"\mt_montana_zip_codes_geo.min.json"
    
    with open(mt_boundary_file, 'r') as f:
        mt_zipcode_boundary = json.load(f)
    
    
    #This overwrote the mt_zipcode_boundary with the nd_zipcode_boundary into merged
    #merged = {**mt_zipcode_boundary, **nd_zipcode_boundary}
    
    #produced a file with two json objects one 'mt' and the other 'nd'
    data = {'mt': mt_zipcode_boundary, 'nd':nd_zipcode_boundary}
    
    #Also overwrote mt_zipcode_boundary
    mt_zipcode_boundary.update(nd_zipcode_boundary)
    

如何编写代码将这两个 geojson 文件合并为一个文件?

这样的事情怎么样?

import json

fc = {
    'type': 'FeatureCollection',
    'features': []
}

with open("mt_montana_zip_codes_geo.min.json") as json_file:
    obj = json.load(json_file)
    fc['features'].extend(obj['features'])

with open("nd_north_dakota_zip_codes_geo.min.json") as json_file:
    obj = json.load(json_file)
    fc['features'].extend(obj['features'])

with open("merged.json", "w") as outfile:
    json.dump(fc, outfile)