0

Edit:

Solved it. When I was creating my new zip/kmz my code was creating various folders within the zip/kmz.

Changed: NewZip.write(addedfile)

to: NewZip.write(addedfile, newfl)

/Edit

I have KMZs that need changes to their points. The changes are simply updating their attributes - changing the values in the tables that appear when a point is clicked.

My method involves extracting the files from the KMZ to a temp location, making a copy of the "doc.kml", digging through the copied "doc.kml" for instances of text that needs to be changed, and then renaming the old KMZ, creating a new KMZ with the same name as the original, and adding the all the files (including my edited "doc.kml") to the new KMZ.

I have automated this process with a pthython script, but when I run it, any KMZs that gets updated have broken point icons. i can't find anything that would cause this - my changes are quite minimal and do not replace and obvious KML. Changes are formatted: "PR_A_1567_20" to "PR_AB_1567_20", so essentially I am adding an extra letter into a few lines of the KML.

Anyone know why this might break icons? Everything else still functions perfectly.

Thanks.

Edit: Simpler question: Why does this KMZ work: https://drive.google.com/file/d/0B9n9YNu8ulsmb1ljQmJMdW5QQXc/edit?usp=sharing

But not this one: https://drive.google.com/file/d/0B9n9YNu8ulsmLXYwNkpIQlY2STg/edit?usp=sharing

/Edit

Code:

import os
import zipfile
import arcpy

folder =r"C:\KMZTest"
table = r"C:\ProjectNumberSwitch_20140729.xlsx\C_Projects_Being_Switched$"
tblcolm = "Old_P6_NBR"
temploc = r"C:\KMZTest\Temp"

oldP6list = []
print "Finding project numbers"
cursor = arcpy.da.SearchCursor(table, tblcolm)
for row in cursor:
    oldP6list.append(row[0])
del row
del cursor

updatedlist = []
for root, dirs, files in os.walk(folder):
    for fil in files:
        if "Projects.kmz" in fil or "Points.kmz" in fil:
            print
            print fil
            print
            flpath = os.path.join (root, fil)
            zipfl = zipfile.ZipFile(flpath)
            foundoldP6 = False
            for content in zipfl.namelist():
                zipfl.extract(content, temploc)
                if content == "doc.kml":
                    doc = os.path.join(temploc, content)
                    os.rename(doc, doc + ".tmp")
                    destination = open(doc, "w")
                    source = open(doc + ".tmp", "r")
                    for line in source:
                        for oldP6 in oldP6list:
                            if oldP6 in line:
                                foundoldP6 = True
                                print "found:", oldP6
                                line = (line.replace(oldP6, oldP6.split("C")[0] + "VC" + oldP6.split("C")[1]))
                        destination.write(line)
                    source.close()
                    destination.close()
            zipfl.close()
            if foundoldP6 == True:
                print "Updating", fil
                updatedlist.append(fil)
                os.rename(flpath, flpath + ".tmp")
                NewZip = zipfile.ZipFile(flpath, "w")
                for newfl in os.listdir(temploc):
                    if not newfl.endswith(".tmp"):
                        addedfile = os.path.join (temploc, newfl)
                        NewZip.write(addedfile)                   
                os.remove(flpath + ".tmp")
            for oldfile in os.listdir(temploc):
                os.remove (os.path.join(temploc, oldfile))
print
print
print "Updated:"
for update in updatedlist:
    print update

print
print "Done"
  • To keep with the Q&A format of GIS SE would you be able to edit the "answer within your question" out into an answer, please? It is perfectly acceptable to do this and even to accept your own answer. – PolyGeo Jul 31 '14 at 10:59
0

Solved it. When I was creating my new zip/kmz my code was creating various folders within the zip/kmz.

Changed: NewZip.write(addedfile)

to: NewZip.write(addedfile, newfl)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.