【问题标题】:Python: embed images in kmzPython:在 kmz 中嵌入图像
【发布时间】:2013-05-12 03:53:55
【问题描述】:

我在一个文件夹中有一系列 kmz 文件(1000+),这些文件是我为要素类的每个多边形和相应的图像(所有图像都在一个单独的文件夹中)生成的。这些 kmz 是从我的来自 arcGIS 的 shapefile 的属性表中自动生成的。在每个 kmz 文件中,我都有一个指向与该功能相对应的图像的链接:

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

目前,每个图像都只是引用目录 /Temp/Figures 中图像的表格文本。 id 喜欢的是将所有这些文本转换为类似于

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

鉴于文件量很大,如果可以在 python 中完成,那将是理想的,simplekml?另一方面,在某个阶段,我想分享其中一些 kmz 文件,因此我想知道最好的解决方案是将每个 kmz 和图像对细分到各自的目录中,然后以某种方式重新压缩 kmz 文件?

【问题讨论】:

    标签: python google-earth arcgis kmz


    【解决方案1】:

    我已经通过迭代每个 kmz 和图像并使用 zipfile 模块读取内容、重写 doc.kml 并将文件重新压缩到 kmz 来解决我的问题。目前,图像被放置在 kmz 中的 之后,但我认为可以使用 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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-04
      • 2014-10-03
      相关资源
      最近更新 更多