osm文件的读取、转换及处理
本代码是对openstreetmap 中武汉市地图的osm文件进行处理,将node进行提取并转换为json文件,然后读取经纬度,寻找重复的node。
import os
import json
from lxml import etree
import xmltodict
os.chdir('your filepath')
osmfile = 'wuhan.osm'
def iter_element(file_parsed, file_length, file_write):
current_line = 0
try:
for event, element in file_parsed:
current_line += 1
print (current_line/float(file_length))
elem_data = etree.tostring(element)
elem_dict = xmltodict.parse(elem_data, attr_prefix="", cdata_key="")
if (element.tag == "node"):
elem_jsonStr = json.dumps(elem_dict["node"])
file_write.write(elem_jsonStr + "\n")
# 每次读取之后进行一次清空
element.clear()
while element.getprevious() is not None:
del element.getparent()[0]
except:
pass
file_length = -1
for file_length, line in enumerate(open(osmfile, 'rU',encoding = 'utf-8')):
pass
file_length += 1
print( "length of the file:\t" + str(file_length))
file_node = open(osmfile+"_node.json","w+")
file_parsed = etree.iterparse(osmfile, tag=["node"])
iter_element(file_parsed, file_length, file_node)
file_node.close()
osm_json_file = 'D:\\360Downloads\\wuhan.osm_node.json'
list1 = []
with open(osm_json_file,encoding = 'utf-8') as f:
for data in f:
tulist = []
data= json.loads(data)
lat = float(data['lat'])
tulist.append(lat)
lon = float(data['lon'])
tulist.append(lon)
list1.append(tuple(tulist))
source = list1
from collections import defaultdict
from collections import Counter
Counter(list1)
i = 0
for k,v in Counter(list1).items():
if v == 2:
i += 1
print(k,v)
print(i)