python将经纬度转换为kml文件,,读取文件,将其中的经纬度
python将经纬度转换为kml文件,,读取文件,将其中的经纬度
读取文件,将其中的经纬度坐标更换为kml文件
主函数 file2Kml(原文件,目标kml文件,分隔符默认为逗号)
import reimport osimport stringdef blh2num(sstr): dd=int(sstr[:sstr.index(":")]) mm=string.atof(sstr[sstr.index(":")+1:sstr.rindex(":")]) ss_=sstr[sstr.rindex(":")+1:] tt="" rr=re.search("[NEWSnews]",ss_) iend=0 if rr==None: iend=len(ss_) tt="N" else: t=ss_[rr.start()] print(t) tt="Y" if t in "WwEe" else "X" iend=rr.start()-1 ss=string.atof(ss_[:iend]) if mm>60 or ss>60: raise ValueError else: return dd+mm/60+ss/3600,ttdef getCoorOrder(a,b): """ This is used to sort the x,y order in order that the latitude and longtitude can be displayed in a right way """ if "N" not in (a[1],b[1]): exec("{0[1]}=\"{0[0]}\"".format(a)) exec("{0[1]}=\"{0[0]}\"".format(b)) return '<coordinates>'+Y+','+X+'</coordinates>'def file2Kml(src,dest,sep=","): """ The source file is need like name,45:32:32.32N,121:44:43.43E """ rf=open(src,"r") wf=open(dest,"w") assert rf!=None or wf!=None,"Make Sure the file exists" wf.write('<?xml version="1.0" encoding="UTF-8"?><kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom"><Document>') while True: ff=rf.readline() if ff=="": break fn,bb,ll=tuple(ff.split(sep)[:3]) wf.write('<Placemark><name>'+fn+'</name><Point>'+getCoorOrder(blh2num(bb),blh2num(ll))+'</Point></Placemark>') wf.write('</Document></kml>') rf.close() wf.close()
相关内容
- python 获得日期是星期几,python星期几,如下代码:from
- python 获得一个月有多少天,python获得,在python的date
- Python 模板引擎性能对比,python性能对比,对比目标,j
- 用PYTHON写 的linux下more命令脚本,pythonmore,依赖:linux
- python使用PIL给图片加水印,,如下代码使用PIL给图片
- python 判断文件是否是图片以及图片的类型,,在python有
- 递归简单模拟Python的range,递归pythonrange,类似于Python的
- Python通过正则表达式读取tomcat的日志并打印日期,pyt
- python登录豆瓣,发帖,python豆瓣发帖,学习了urllib、ur
- python 压缩文件夹内的所有文件为zip,pythonzip,import zip
评论关闭