使用CSV转换成KML文件

简单的CSV转换为KML文件的Python程序
1、创建KML线文件模板库
LineKml = “”“<?xml version="1.0" encoding="UTF-8"?>



1
1

absolute





”“”
strlist = LineKml.split(“\n”)
2、读取CSV文件,遍历选择目录
for root, dirs, files in os.walk(file_path, topdown=False):
for source_file in files: # 遍历目录文件
if source_file.endswith(‘.csv’): # 遍历csv文件
tmp = os.path.join(root, source_file)
aim_file = tmp[:-3] + ‘kml’
write_data(aim_file, tmp)
aim_num = aim_num + 1
status[‘text’] = status[‘text’] + ‘已转换’ + str(aim_num) + ‘个’ # 修改完更新状态
3、将CSV坐标写入到KML文件中

def write_data(aim_file, file_sourse): # 传入目标文件与待读取csv文件
global Longitude, Latitude, Altitude, StartLine
with open(aim_file, “w”) as file: # 创建目标kml文件
for i in strlist:
file.write(i + “\n”)
if i.endswith(“”):
with open(file_sourse, ‘r’, encoding=‘utf-8’, newline=‘’) as csv_file: # 读取csv文件
data = csv.reader(csv_file)
line_idx = 0
for csv_row in data:
if line_idx > StartLine:
# tmp = csv_row[ord(Longitude)-ord(‘A’)]+‘,’+csv_row[ord(Latitude)-ord(‘A’)]+‘,’+csv_row[ord(Altitude)-ord(‘A’)]#
tmp = csv_row[Longitude-1] + ‘,’ + csv_row[Latitude-1] + ‘,’ + csv_row[Altitude-1]
# print(tmp)
file.write(“\t\t\t” + tmp) # 写入数据
file.write(‘\n’)
line_idx += 1

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

哈嗨哈

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值