Select to view content in your preferred language

Creating KML files and store them in the related folders

315
1
02-10-2022 11:50 PM
MaryamPanji
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?

Maryam

 

 

import arcpy
import os
arcpy.env.overwriteOutput = True

# settings
inputFC = r"\\D\Asset.sde\DBO.asset"
field_name=["Property_code","Property_Name","Region"]
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:
    lst_property.append(row.getValue(field_name[0]))
    lst_regions.append(row.getValue(field_name[2]))
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)
    folder=os.path.join(output_location,regions)
    if not os.path.exists(folder):
        os.makedirs(folder)
    folder=r"{}\{}".format(output_location,regions)
   


#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
DanPatterson
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