2013-05-12 29 views
2

我在一個文件夾中有一系列kmz文件(1000+),這些文件是由要素類和相應圖像(所有圖像位於單獨的文件夾中)的每個多邊形生成的。這些kmz是從arcGIS的shapefile文件的屬性表中自動生成的。在每個KMZ文件我有一個鏈接到對應於該功能作爲這樣一個形象:Python:在kmz中嵌入圖像

<tr> 
<td>Preview</td>  
<td>G:\Temp\Figures\Ovr0.png</td>  
</tr> 

目前每個圖像只是一個表格文本引用在目錄/溫度/圖圖像。什麼樣的ID是所有這些文本轉化爲鏈接的東西沿着

<img src="file:///G:/Temp/Figures/Ovr0.png" width = 750 height=500/> 

由於大體積文件,這將是理想的,如果這可以蟒蛇內完成的線,simplekml?另一方面 - 在某個階段,我想分享一些kmz文件,因此我想知道是否最好的解決方案是將每個kmz和圖像對細分爲各自的目錄並以某種方式重新壓縮kmz文件?

回答

3

我已經設法通過迭代每個kmz和圖像並使用zipfile模塊來讀取內容來解決我的問題,重寫doc.kml並將文件重新劃分爲kmz。目前這些圖像放在kmz後面的< body中,但是更復雜的參數可以用re來書寫。

如果有一個更有效的方法,請讓我知道...

def edit_kmz(kmz,output,image): 

     ##Read the doc.kml file in the kmz and rewrite the doc.kml file 

     zf = zipfile.ZipFile(kmz) 
     temp = r'tempfolder\doc.kml' 
     for line in zf.read("doc.kml").split("\n"): 
      with open(temp,'a') as wf: #Create the doc.kml 
       if "</body>" in line: 
        wf.write("</body>\n<img src='files/Ovr0.png' width = 750 height=500</img>\n") 
       else: 
        wf.write('%s\n'%(line)) 
     zf.close() 

     ##Rezip the file 

     zf = zipfile.ZipFile(output,'a') 
     zf.write(image,arcname='files/Ovr0.png') ##Relative Path to the Image 
     zf.write(temp,arcname='doc.kml') ##Add revised doc.kml file 
     zf.close()