我正在尝试使用simplekml将一堆带地理标记的照片放入一个KML文件(实际上是一个KMZ文件)中,以便在Google Earth中查看。我已经得到了要显示的位置,但是当我试图将图像放在“描述”中时,当我单击图像出现的位置时,它不起作用。只有一张空白图像。我正在尝试使用显示为here的addfile()命令来完成此操作。我的代码如下所示:
import os, simplekml
path = r'C:\Users\as\Desktop\testpics'
kml = simplekml.Kml()
for (dirpath, dirnames, filenames) in os.walk(path):
for filename in filenames:
fullpath = os.path.join(dirpath, filename)
try:
Lat, Long, Alt = GetLatLong(fullpath) #Didn't include this function, but it appears to work
except:
Lat, Long, Alt = (None, None, None)
if Lat: #Only adds to kml if it has Lat value.
x, y = (FormatLatLong(Lat), FormatLatLong(Long)) #puts into decimal coords
point = kml.newpoint(name = filename , coords = [(y,x)])
picpath = kml.addfile(fullpath)
point.description = '<img src="' + picpath +'" alt="picture" width="400" height="300" align="left" />'
kml.savekmz("kmltest2.kmz", format = False)如你所见,我已经从上面页面的说明中剪切并粘贴了使用"addfile“的说明。point.description行似乎就是出问题的地方。
这些图片将被添加到kmz存档中,但它们不会显示在位置气泡中。我想这可能是因为我在Windows7上这样做,斜杠是向后的,但我尝试手动将files\image.jpg更改为files/image.jpg,但没有解决这个问题。生成的KMZ doc.kml文件如下所示:
<kml xmlns="http://www.opengis.net/kml/2.2"xmlns:gx="http://www.google.com/kml/ext/2.2">
<Document id="feat_1">
<Placemark id="feat_2">
<name>DSC00001.JPG</name>
<description><img src="files/DSC00001.JPG" alt="picture" width="400" height="300" align="left" /></description>
<Point id="geom_0"><coordinates>18.9431816667,9.44355222222,0.0</coordinates>
</Point></Document></kml>(我删除了所有的分数,只剩下一分)非常感谢,Alex
发布于 2013-03-14 19:08:54
可能是因为你写的kml文件中有未封闭的placemark标签。因此,在关闭点标记之后关闭placemark标记。
<kml xmlns="http://www.opengis.net/kml/2.2"xmlns:gx="http://www.google.com/kml/ext/2.2">
<Document id="feat_1">
<Placemark id="feat_2">
<name>DSC00001.JPG</name>
<description><img src="files/DSC00001.JPG" alt="picture" width="400" height="300" align="left" /></description>
<Point id="geom_0"><coordinates>18.9431816667,9.44355222222,0.0</coordinates>
</Point></Placemark></Document></kml>如果在放置位置标记标签后上面的代码不起作用,请尝试使用气球样式而不是描述标签尝试使用以下代码
<kml xmlns="http://www.opengis.net/kml/2.2"
xmlns:gx="http://www.google.com/kml/ext/2.2"
xmlns:kml="http://www.opengis.net/kml/2.2"
xmlns:atom="http://www.w3.org/2005/Atom"
>
<Document id="feat_1">
<Placemark id="feat_2">
<name>DSC00001.JPG</name>
<Style>
<BalloonStyle>
<text><![CDATA[
<table width=100% cellpadding=0 cellspacing=0>
<tr><td><img width=100% src='files/DSC00001.jpg' /></td></tr></table>]]>
</text>
</BalloonStyle>
</Style>
<Point id="geom_0">
<coordinates>18.9431816667,9.44355222222</coordinates>
</Point>
</Placemark>
</Document>
</kml>https://stackoverflow.com/questions/15373973
复制相似问题