I'm not sure, probably the answer is somewhere here allready, but due to my "noob-status" it is very difficult for me to understand if the allready asked questions really have something to do with my problem and believe me, I've tried to find a solution since yesterday morning and I have to be done tomorrow, so here i go:
我不确定,可能答案已经在这里了,但由于我的“noob-status”,我很难理解所有问题是否真的与我的问题有关并且相信我,我'我从昨天早上起就试图找到一个解决方案,我必须明天完成,所以我去了:
I need to write a Script with Python27 which automatically converts a given CSV-file with Coordinates into a KML-file which can be used for google earth.
我需要用Python27编写一个脚本,它自动将带有坐标的给定CSV文件转换为可用于谷歌地球的KML文件。
This is what I got so far:
这是我到目前为止所得到的:
# 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
The Stuff at the end are/were just ideas of how to solve it. If you execute the script you will see that the fist 1000 coordinates are pasted into the file, but how do i seperate the three Values for lat/long/elevation and put them in the order neccessary for google earth (long/lat/elevation)? I would be very glad to hear from you soon! Thanks
最后的东西只是如何解决它的想法。如果您执行脚本,您将看到第1000个坐标被粘贴到文件中,但是我如何分隔lat / long / elevation的三个值并将它们按照google earth(long / lat / elevation)的必要顺序排列?我很乐意很快收到你的来信!谢谢
EDIT: Right now it looks like that:
编辑:现在它看起来像这样:
<?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
And i need it to look like that:
我需要它看起来像这样:
<?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
With one difference: the values 49.xy (latitude) and 8.xy (longitude) should be changed as well.
有一点不同:值49.xy(纬度)和8.xy(经度)也应该改变。
1 个解决方案
#1
0
I would recommend opening the file only once and writing everything, rather than opening and closing multiple times.
我建议只打开文件一次并写入所有内容,而不是多次打开和关闭。
The problem is this line: f.writelines(Koordinaten)
问题是这一行:f.writelines(Koordinaten)
Instead of this, use:
而不是这个,使用:
f.write('\n'.join(Koordinaten))
#1
0
I would recommend opening the file only once and writing everything, rather than opening and closing multiple times.
我建议只打开文件一次并写入所有内容,而不是多次打开和关闭。
The problem is this line: f.writelines(Koordinaten)
问题是这一行:f.writelines(Koordinaten)
Instead of this, use:
而不是这个,使用:
f.write('\n'.join(Koordinaten))