我不确定,可能答案已经在这里了,但是由于我的" noob-status"我很难理解所有问题是否真的与我的问题有关并且相信我,我从昨天早上起就试图寻找解决方案,明天我必须完成,所以我走了:
我需要用Python27编写一个脚本,它自动将带有坐标的给定CSV文件转换为可用于谷歌地球的KML文件。
这是我到目前为止所得到的:
# imports the module urllib2 to open URLs
import urllib2
# ask for URL and load it
# Bsp.: http://koenigstuhl.geog.uni-heidelberg.de/~bhoefle/geoscripting/GPS_track_HD_Bodensee.csv
URL = raw_input("Enter the URL to your CSV-file: ")
response = urllib2.urlopen(URL)
html = response.read()
#print html
Zielverzeichnis = raw_input("Enter the path where you would like to store the file:")
Dateiname = "/" + raw_input("Name your file:") + ".kml"
#print Dateiname
#print Zielverzeichnis
#print Zielverzeichnis + Dateiname
Tabelle = Zielverzeichnis + Dateiname
fileobj = file(Tabelle, "w")
with open(Tabelle, "w") as writefile:
writefile.write(html)
#import csv
#data = csv.reader(open(Tabelle), delimiter = ',')
f = open(Tabelle, 'w')
f.write("<?xml version='1.0' encoding='UTF-8'?>\n")
f.write("<kml xmlns='http://www.opengis.net/kml/2.2'>\n")
f.write("<Placemark>\n")
f.write("<name>" + Dateiname +"</name>\n")
f.write("<MultiGeometry>\n")
f.write("<LineString>\n")
f.write("<coordinates>\n")
html2 = html.split()
Koordinaten = html2[3:1003]
#Koordinaten.insert([n*30], ]])
f.writelines(Koordinaten)
f.close()
#with open(Tabelle) as rfile:
# lines = rfile.readlines()[1:1001]
# print lines
# linevalues = lines.split(",")
# firstval = float(linevalues[0])
# secondval = float(linevalues[1])
# print firstval, secondval
最后的东西只是如何解决它的想法。如果您执行脚本,您将看到第1000个坐标被粘贴到文件中,但是如何将lat / long / elevation的三个值分开并将它们按照google earth(long / lat / elevation)的必要顺序排列?我很乐意很快收到你的来信! 感谢
编辑: 现在它看起来像那样:
<?xml version='1.0' encoding='UTF-8'?>
<kml xmlns='http://www.opengis.net/kml/2.2'>
<Placemark>
<name>/Testfile.xml</name>
<MultiGeometry>
<LineString>
<coordinates>
49.40336870,8.681468410,117.049.40212450,8.680264000,118.049.40083450,8.678729290,112.049.39974100,8.676724960,115.049.39909890,8.675406580,113.049.39823150,8.673896850,111.049.39741020,8.672800160,111.049.39693920,8.671590970,111.049.39594540,8.669930680,110.049.39212020,8.665451860,111.049.38969310,8.662548160,110.049.38685910,8.658890010,109.049.38423320,8.655726650,110.049.37958570,8.650085210,110.049.37725820,8.647567880,108.049.37616580,8.646088460,110.049.37584310,8.646435030,109.049.37551880,8.646217030,110.049.37448140,8.645122860,107.0
&#13;
我需要它看起来像那样:
<?xml version='1.0' encoding='UTF-8'?>
<kml xmlns='http://www.opengis.net/kml/2.2'>
<Placemark>
<name>/Testfile.xml</name>
<MultiGeometry>
<LineString>
<coordinates>
49.40336870,8.681468410,117.0
49.40212450,8.680264000,118.0
49.40083450,8.678729290,112.0
49.39974100,8.676724960,115.0
49.39909890,8.675406580,113.0
49.39823150,8.673896850,111.0
49.39741020,8.672800160,111.0
49.39693920,8.671590970,111.0
49.39594540,8.669930680,110.0
49.39212020,8.665451860,111.0
49.38969310,8.662548160,110.0
49.38685910,8.658890010,109.0
49.38423320,8.655726650,110.0
49.37958570,8.650085210,110.0
49.37725820,8.647567880,108.0
49.37616580,8.646088460,110.0
49.37584310,8.646435030,109.0
49.37551880,8.646217030,110.0
49.37448140,8.645122860,107.0
&#13;
有一点不同:值49.xy(纬度)和8.xy(经度)也应该改变。
答案 0 :(得分:0)
我建议只打开一次文件并编写所有内容,而不是多次打开和关闭。
问题是这一行:
f.writelines(Koordinaten)
而不是这个,请使用:
f.write('\n'.join(Koordinaten))