Iterating Create KML Script

663
2
11-10-2011 07:56 PM
DaveMarkey
New Contributor III
Hi.

I am attempting to convert a number of layer files to KML/KMZ. The script below is what I have so far. The output component is where the error occurs, something about an invalid file type.

# Import system modules
import arcpy
from arcpy import env

# Obtain a license for the 3D Analyst extension
arcpy.CheckOutExtension("3D")

# Set environment settings
env.workspace = "C:\\data"
Location = "C:\\data\\"


for layer in arcpy.ListFiles("*.lyr"):
    arcpy.LayerToKML_conversion(layer, Location + kmz, "10000", "false", "Default", "1024", "96")
Tags (2)
0 Kudos
2 Replies
EricMcPhee
New Contributor III
Do you have the variable KMZ declared somewhere else in the script? I think what you are trying to do is write out a kmz file for each layer in your directory and give it the Layers name....does that sound right?

Put this into your For loop:

outKMZ = layer[:4] + ".kmz"
arcpy.LayerToKML_conversion(layer, Location + outKMZ, "10000", "NO_COMPOSITE", ' ',"1024", "96")

be aware that you will need to check if the kmz file exists before running the tool a second time....if the kmz file exists the script will fail.
0 Kudos
DaveMarkey
New Contributor III
Hi.

I have finally got the script to work and is shown below. For a while I was getting the lyr extension with the output filename (eg hp.lyr.kmz).

# Import system modules
import arcpy
from arcpy import env

# Obtain a license for the 3D Analyst extension
arcpy.CheckOutExtension("3D")

# Set environment settings
env.workspace = "H:\\DATA\\GENERAL\\Arcpad Mooring Data - Don't Delete\\HORNSBY REGION\\Hawkesbury River\\Mooring Management"

if len(arcpy.ListFiles('*.lyr')) > 0:
    for layer in arcpy.ListFiles("*.kmz"):
        arcpy.Delete_management(layer, "LayerFiles")
    # Strips the '.lyr' part of the name and appends '.kmz'
    for layer in arcpy.ListFiles("*.lyr"):
        outKML = layer[:-4] + ".kmz"
        arcpy.LayerToKML_conversion(layer, outKML, "10000", "false", "Default", "1024", "96")
else:
    print "There are no layer files (*.lyr) in " +env.workspace+ "."
0 Kudos