Select to view content in your preferred language

Creating KML files and store them in the related folders

02-10-2022 11:50 PM
New Contributor III

Hi everyone,

I have a properties feature class and want to use this to create a kml file for each property and store it in the related region folder. I created a python script, but it put all the kml files in one folder and the rest are empty. 

Could you please help me fix my script?




import arcpy
import os
arcpy.env.overwriteOutput = True

# settings
inputFC = r"\\D\Asset.sde\DBO.asset"
output_location = r"\\D\Country\KML"

print "Starting"

# create list of unique properties codes and regions 
lst_property = []
lst_regions= []
cursor = arcpy.SearchCursor(inputFC)
for row in cursor:
del cursor
del row
lst_property = list(set(lst_property))
lst_regions = list(set(lst_regions))

for regions in lst_regions:
    field1 = arcpy.AddFieldDelimiters(inputFC, field_name[2])
    where ="\"{0}\" = '{1}'".format(field1, regions)
    if not os.path.exists(folder):

#loop through properties
for Properties in lst_property:
    field = arcpy.AddFieldDelimiters(inputFC, field_name[0])
    where ="\"{0}\" = '{1}'".format(field, Properties)
    arcpy.MakeFeatureLayer_management(inputFC, "lyr", where)
    kmz_file = os.path.join(folder,"{0}.kmz".format(Properties))
    arcpy.LayerToKML_conversion("lyr", kmz_file)



0 Kudos
1 Reply
MVP Esteemed Contributor

it is using your last folder I presume. how are you supposed to know which folder to put the files in in order to change the "folder" name?  You will have to maintain an index number referencing the folder or more simply, combine the folder creation with the kml export if they are synced 

... sort of retired...
0 Kudos