如何解决合并两个geojson状态邮政编码文件?
我正在一个需要使用US States Zip Code Data的项目中工作。我想合并两个geojson文件,同时保留这些文件中的数据。 geojson-merge https://github.com/mapbox/geojson-merge
可以做到这一点,但是我希望有一个基于python的解决方案。
每个状态都有一个单独的* .json文件。例如:
-
mt_montana_zip_codes_geo.min.json
-
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)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。