1

我正在尝试从 csv 构建一个 KML 文件以在 Google Earth 中显示点,但我遇到了一些小问题......首先,我可以让它构建文件,但 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

这是我添加的页眉和页脚部分...

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()"""
4

2 回答 2

2

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

如果您将代码拆分为更模块化的功能,则更容易推理功能并检查它们是否正常工作。

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

所以这就是我想出的:(使用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)
于 2013-05-30T11:22:57.897 回答
0

根据文档:

https://developers.google.com/kml/documentation/kmlreference#point

KML 文件中的坐标应该是 lon、lat 顺序(这似乎有点违反直觉!)。你需要翻转它们。

f.write("           <coordinates>" + str(LON) + "," + str(LAT) + "</coordinates>\n")
于 2013-05-30T11:08:24.593 回答