1

I 'm new in Python script and i want to export shapefiles in the folder as the kml file with define layer files. for single shapefile it work perfectly refer below script. But I want to export 4 different shapefiles in the folder as kml with define symbology. Can any one help me?

import arcpy

from arcpy import env

#set the current workspace
env.workspace = r"C:\Work\20141118_Addresses\MGA"

#Set layer to apply symbology to
InputLayer1 = r"C:\Work\20141118_Addresses\MGA\Address_project.shp"

#Set layer that output symbology will be based on
symbologyLayer1=r"C:\Work\20141118_Addresses\Layer\Address.lyr"

#Set Output Kmz files at

OutWorkspace1 = r"C:\Work\20141118_Addresses\Output\Address.kmz"

#Apply the symbology from the symbology layer to the Address layer
arcpy.MakeFeatureLayer_management(InputLayer1,"Address_layer")
arcpy.ApplySymbologyFromLayer_management("Address_layer",symbologyLayer1)
arcpy.LayerToKML_conversion("Address_layer",OutWorkspace1,"12000","false","DEFAULT","1024","96","CLAMPED_TO_GROUND")
0

1 Answer 1

2

A list and a for loop will prove quite helpful. In the code below, InLayer1 through InLayer4 are the paths to the shapefiles. I then make a list out of these variables. The proceeding for loop iterates through each input layer and applies the same code to each. Note that this code applies the same symbology to each input layer. If each layer has its own corresponding symbology, I would suggest using a dictionary to relate each input shapefile to its corresponding layer file.

You also need to perform a bit of fanciness with the name so that the resulting KMZs are named differently. In this code, the name of the shapefile will be the name of the KMZ.

Code:

import arcpy
import os

from arcpy import env

#set the current workspace
env.workspace = r"C:\Work\20141118_Addresses\MGA"

#Set layer to apply symbology to
InputLayer1 = r"C:\Work\20141118_Addresses\MGA\Address_project.shp"
InputLayer2 = r"path"
InputLayer3 = r"path"
InputLayer4 = r"path"

#Create list of input shapefiles
InputList = [InputLayer1, InputLayer2, InputLayer3, InputLayer4]

#Set layer that output symbology will be based on
symbologyLayer1=r"C:\Work\20141118_Addresses\Layer\Address.lyr"

for Input in InputList:
    OutWorkspace1 = os.path.join (r"C:\Work\20141118_Addresses\Output",
                                  os.path.basename (Input)[:-4] + ".kmz")
    arcpy.MakeFeatureLayer_management(Input,"Address_layer")
    arcpy.ApplySymbologyFromLayer_management("Address_layer",symbologyLayer1)
    arcpy.LayerToKML_conversion("Address_layer",OutWorkspace1,"12000","false","DEFAULT","1024","96","CLAMPED_TO_GROUND")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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