Hello Community-
First time poster, long time lurker. Hoping I can find some model builder or python help with an automation task.
I have about 300 lines I need to convert to KML based on attribute 'TrailName'.
Conceptually I understand what I need to do but am stuck executing with Model Builder.
I was thinking I could use the iterate row selection in model builder to select by 'TrailName' attribute and convert layer to kml. This process would need to be looped for every 'TrailName'.
Does anyone have experience or understanding on how to execute this?
Thanks in advance.
Zac
Solved! Go to Solution.
Below some untested code that could work.
Explanation of the code:
def main():
import arcpy
import os
arcpy.env.overwriteOutput = True
# settings: edit these
fc_in = r'path to your input featureclass'
fld_trail = 'TrailName'
out_folder = r'path to your output folder for KML files'
# create list of unique trail names
lst_trails = list(set([r[0] for r in arcpy.da.SearchCursor(fc_in, (fld_trail))]))
# loop through trails
for trail_name in lst_trails:
fld = arcpy.AddFieldDelimiters(fc_in, fld_trail)
where ="{0} = '{1}'".format(fld, trail_name)
arcpy.MakeFeatureLayer_management(fc_in, "lyr", where)
kmz_file = os.path.join(out_folder, "{0}.kmz".format(trail_name))
arcpy.LayerToKML_conversion("lyr", kmz_file)
if __name__ == '__main__':
main()
Is the Trailname unique or can there be multiple features with the same train name? If values repeat the Iterate row would potentially overwrite kml files. In case of scripting this, you could generate a unique list of values from the field Trailname, and for each value set a definition query while creating a featurelayer and export that to KML using the trailname as output kml file name.
Yeah, TrailName is a unique values representing a single line feature.
Below some untested code that could work.
Explanation of the code:
def main():
import arcpy
import os
arcpy.env.overwriteOutput = True
# settings: edit these
fc_in = r'path to your input featureclass'
fld_trail = 'TrailName'
out_folder = r'path to your output folder for KML files'
# create list of unique trail names
lst_trails = list(set([r[0] for r in arcpy.da.SearchCursor(fc_in, (fld_trail))]))
# loop through trails
for trail_name in lst_trails:
fld = arcpy.AddFieldDelimiters(fc_in, fld_trail)
where ="{0} = '{1}'".format(fld, trail_name)
arcpy.MakeFeatureLayer_management(fc_in, "lyr", where)
kmz_file = os.path.join(out_folder, "{0}.kmz".format(trail_name))
arcpy.LayerToKML_conversion("lyr", kmz_file)
if __name__ == '__main__':
main()
This is fantastic - thank you. I will test and reply with questions.
Xander-
This script worked perfectly - now I need to study and understand what it did exactly - thanks again.
Zac
Glad it worked! I don't mind to add some additional explanation to help you understand.
Can you mark the post and the correct answer with the "Mark Correct" button next to it?
Would love an explanation!
Done! If you have any questions, just let me know.