I have a series of kmz files (1000+) within one folder that I have generated of each polygon of a feature class and a corresponding image (all images are in a separate folder). These kmz's are automatically generated from the attribute tables of my shapefiles from arcGIS. Within each kmz file I have a link to an image that corresponds to that feature as such:
<tr>
<td>Preview</td>
<td>G:\Temp\Figures\Ovr0.png</td>
</tr>
At the moment each image is but a tabular text referencing an image in the directory /Temp/Figures. What id like is to convert all those texts into links something along the lines of
<img src="file:///G:/Temp/Figures/Ovr0.png" width = 750 height=500/>
Given the large volume of files it would be ideal if this could be done within python, simplekml? On another note - at some stage I would like to share a few of these kmz files and therefore I was wondering if the best solution was to subdivide each kmz and image pair into their own respective directories and rezip the kmz file somehow?
I have managed to solve my problem by iterating each kmz and image and using the zipfile module to read the contents, rewrite the doc.kml and rezipping the files into a kmz. At the moment the images are placed after the < body >in the kmz but a more complex argument could be written with re I presume.
If there is a more efficient method please let me know...
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()