osm文件的读取、转换及处理

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) 

  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值