2013-03-12 103 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