2013-03-12 101 views
3

我试图使用simplekml将一堆带地理标记的照片放入KML文件(实际上是KMZ文件),以便在Google地球中进行查看。我已经获得了要显示的位置,但是当我尝试将图像放入“描述”中时,因此当我单击图像出现的位置时,它不起作用。只有一张空白图片。我试图通过使用显示here的addfile()命令来完成。我的代码如下所示:在simplekml中显示本地图像

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存档,但它们没有出现在位置的气泡中。我认为这可能是因为我在Windows 7上这样做,并且斜杠是倒退的,但我尝试手动将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>&lt;img src="files/DSC00001.JPG" alt="picture" width="400" height="300" align="left" /&gt;</description> 
    <Point id="geom_0"><coordinates>18.9431816667,9.44355222222,0.0</coordinates> 
    </Point></Document></kml> 

(我已经删除了所有,但分之一) 非常感谢,亚历克斯

+0

我也试过选项1(HTTP: //support.google.com/earth/bin/answer.py?hl=zh_CN&answer=1061393),未修复该问题。 – 2013-03-13 23:35:00

回答

2

可能是因为在没有结束的标标签你写的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>&lt;img src="files/DSC00001.JPG" alt="picture" width="400" height="300" align="left" /&gt;</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> 
+1

太好了,非常感谢。我在原始文件中确实有一个我只是在修剪它时忘了复制它,但您的第二个建议确实奏效。我将它添加到我的simplekml脚本中:point.style.balloonstyle.text =“<![CDATA [

​​
]]>有趣的是:即使图像没有这样命名,文件扩展名也必须是小写 - .JPG将不起作用,.jpg会。 – 2013-03-14 15:27:49

+0

现在我想起来了,也许原始脚本的问题在于所有.JPG而不是.jpg的图像。我可以去测试它,但这意味着要取消一些变化,所以我不会去做。 – 2013-03-14 18:25:31