gpt4 book ai didi

python - 使用 Python 构建 KML

转载 作者:行者123 更新时间:2023-12-01 05:43:34 24 4
gpt4 key购买 nike

我正在尝试从 csv 构建 KML 文件以在 Google 地球中显示点,但遇到一些小问题...首先,我可以用它来构建文件,但 csv 中有六组坐标,而 kml 只显示两组(实际上一组显示两次)。我显然把循环搞错了,但不知道怎么搞的。其次,生成的地标 ['53.66018'],['-1.94925'] 显示在海洋中的某处,而它应该在曼彻斯特(英国)我将不胜感激任何和所有的帮助。提前致谢!这是Python代码-

import csv
import os
fp = "C:\\Python27\\test.csv"
file = open(fp)
lines =file.readlines()
for line in lines:
line = line.strip()
fields = line.split(';') #semicolon seperated
LCID = fields[0].split() #splitting
CID = fields[1].split()
LAC = fields[2].split()
NET = fields[3].split()
LAT = fields[4].split()
LON = fields[5].split()

print 'LCID' #print splitted
print ("CID: " + str(CID))
# print ("LAC: " + str(LAC))
# print ("NET: " + str(NET))
# print ("LAT: " + str(LAT))
# print ("LON: " + str(LON))

f = open('C:\\Python27\\csv17kml.kml', 'w')
fname = "testing_Actions"
#Writing the kml file.
f.write("<?xml version='1.0' encoding='UTF-8'?>\n")
f.write("<kml xmlns='http://www.opengis.net/kml/2.2'>\n")
f.write("<Folder>\n")
f.write(" <name> Op Example </name>\n")
for row in lines:
f.write(" <Placemark>\n")
f.write(" <ExtendedData>\n")
f.write(" <Data name='CID'>\n")
f.write(" <value>\n")
f.write(" " + str(CID) +"\n")
f.write(" </value>\n")
f.write(" </Data>\n")
f.write(" <Data name='LAC'>\n")
f.write(" <value>\n")
f.write(" " + str(LAC) + "\n")
f.write(" </value>\n")
f.write(" </Data>\n")
f.write(" </ExtendedData>\n")
f.write(" <Point>\n")
f.write(" <coordinates>" + str(LAT) + "," + str(LON) + "</coordinates>\n")
f.write(" </Point>\n")
f.write(" </Placemark>\n")
f.write("</Folder>\n")
f.write("</kml>\n")
print ("File Created. ")
f.close
file.close()

这是 csv 中的数据:

LCID;CID;LAC;NET;LAT;LON

26599;26599;3569;23410;53.66018;-1.94925;
26599;26599;3569;23410;53.66018;-1.94923;
26599;26599;3569;23410;53.66019;-1.94922;
26599;26599;3569;23410;53.66019;-1.94919;
26599;26599;3569;23410;53.66018;-1.94919;
26599;26599;3569;23410;53.66018;-1.94917

这是我添加的页眉和页 footer 分...

def write_header(output_file):
return
"""<?xml version='1.0' encoding='UTF-8'?>\n
<kml xmlns='http://www.opengis.net/kml/2.2'>\n
<Folder>\n
<name> Op Example </name>\n"""

def write_footer(output_file):
return
"""</Folder>\n")
</kml>\n
print ("File Created. ")
f.close
file.close()"""

最佳答案

除了 Blutack 指出的错误之外,您还通读了输入文件,但没有将坐标存储在任何地方。

如果将代码拆分为更多模块化功能,则可以更轻松地推理功能并检查它们是否正常工作。

此外,您手动完成所有工作也太辛苦了。有一个模块用于读取csv files ,手动处理文件的关闭已被上下文管理器淘汰,如果您想要多行字符串,您可以使用三引号。此外,如果您想在模板中填写一些值,您可以使用 string formatting使用命名替换以使其更具可读性。

这就是我想到的:(使用namedtuple来获得每行的更舒适的表示)

from collections import namedtuple
import csv

location_info = namedtuple('location_info', 'LCID,CID,LAC,NET,LAT,LON')
input_filename = "input.csv"

def write_header(output_file):
pass # left as an excercise

def write_footer(output_file):
pass # also left as an exercise

def get_kml(location_info):
return """ <Placemark>
<ExtendedData>
<Data name='CID'>
<value>
{CID}
</value>
</Data>
<Data name='LAC'>
<value>
{LAC}
</value>
</Data>
</ExtendedData>
<Point>
<coordinates>{LON},{LAT}</coordinates>
</Point>
</Placemark>""".format(**location_info._asdict())

with open(input_filename, 'r') as input_file:
csv_reader = csv.reader(input_file, delimiter=';')
print next(csv_reader) # gets rid of the header line
all_locations = (location_info(*line[0:6]) for line in csv_reader) # the slicing is due to the trailing ;

with open(output_filename, 'w') as output_file:
write_header(output_file)
for location in all_locations:
output_file.write(get_kml(location))
write_footer(output_file)

关于python - 使用 Python 构建 KML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16833402/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com