Select to view content in your preferred language

Convert features with the same code to a KML file

2521
17
02-07-2018 01:13 AM
DanutaHodgson
Emerging Contributor

branched from https://community.esri.com/thread/207867-how-to-automate-selection-and-kml-conversion 

Hi,

 I need to do something similar but for areas.

However I have arcmap10.0 and I cannot get this script to work.

Could you please assist me.

I need to select multiple attributes that have the same name in the AA_area field and export them as a Kmz.  I then need it to choose the next area and repeat.

EG. Alexandia is in the attribute table 10 times under AA_area and I would like it to export all 10 polygons into one kmz (this is a shapefile in need to convert from).  I need to do this for all the different areas in the AA_area field.

Thank you for your help.

Tags (3)
0 Kudos
17 Replies
XanderBakker
Esri Esteemed Contributor

Hi Danuta Hodgson ,

What you will need to do is:

  • On line 7 locate the path to your input shapefile like this:
    fc_in = r'C:\Folder\SubFolder\myShapeFileName.shp'

  • On line 8 you will have to specify the field with the names. For each name a KMZ will be created:
    fld_trail = 'AA_area'

On line 9 you will need to specify the output folder location where the KMZ files will be stored:

    out_folder = r'C:\Folder\SubFolder\ResultingKMZfolder'

Since you are using a shapefile your where clause will be different. So change line 17 to this:

        where ="\"{0}\" = '{1}'".format(fld, trail_name)

It is important that the indentation remains the same. Changing it will make the code invalid.

0 Kudos
DanutaHodgson
Emerging Contributor

Thank you for coming back to me.

I tried what you said, but absolutely nothing happened.

Please could you double check

Also should train name not be changed to AA_area?

def main():
... import arcpy
... import os
... arcpy.env.overwriteOutput = True
...
... # settings: edit these
... fc_in = r'C:\Users\Danuta\Desktop\CSS 2016-17\TEST\Agri\Agri_EC_working_data.shp'
... fld_trail = 'AA_area'
... out_folder = r'C:\Users\Danuta\Desktop\CSS 2016-17\TEST'
...
... # 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 AA_area 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)

0 Kudos
XanderBakker
Esri Esteemed Contributor

I'm going to branch your question to a new thread. It would be better if I have access to (a subset) of the data you want to use. Can you attach the shapefile (zipped) to this thread?

0 Kudos
XanderBakker
Esri Esteemed Contributor

I notice that you didn't copy the entire code. The part that starts the code was not included. You should copy and paste the entire code below:

def main():
    import arcpy
    import os
    arcpy.env.overwriteOutput = True

    # settings: edit these
    fc_in = r'C:\Users\Danuta\Desktop\CSS 2016-17\TEST\Agri\Agri_EC_working_data.shp'
    fld_trail = 'AA_area'
    out_folder = r'C:\Users\Danuta\Desktop\CSS 2016-17\TEST'

    # 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()
0 Kudos
DanutaHodgson
Emerging Contributor

Hi,

I have copied the above text into python in arc and I got a runtime error.  

I am using ArcGIS 10.0

>>> def main():
... import arcpy
... import os
... arcpy.env.overwriteOutput = True
...
... # settings: edit these
... fc_in = r'C:\Users\Danuta\Desktop\CSS 2016-17\TEST\Agri\Agri_EC_working_data.shp'
... fld_trail = 'AA_area'
... out_folder = r'C:\Users\Danuta\Desktop\CSS 2016-17\TEST'
...
... # 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()
...
Runtime error <type 'exceptions.AttributeError'>: 'module' object has no attribute 'da'

0 Kudos
XanderBakker
Esri Esteemed Contributor

OK, I see. At 10.1 the arcpy.da cursors were introduced, so the old cursor object should be used. I will post back a revised script.

0 Kudos
XanderBakker
Esri Esteemed Contributor

Try this:

def main():
    import arcpy
    import os
    arcpy.env.overwriteOutput = True

    # settings: edit these
    fc_in = r'C:\Users\Danuta\Desktop\CSS 2016-17\TEST\Agri\Agri_EC_working_data.shp'
    fld_trail = 'AA_area'
    out_folder = r'C:\Users\Danuta\Desktop\CSS 2016-17\TEST'

    # create list of unique trail names
    lst_trails = []
    cursor = arcpy.SearchCursor(fc_in)
    for row in cursor:
        lst_trails.append(row.getValue(fld_trail))
    del cursor
    del row
    lst_trails = list(set(lst_trails))

    # 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()
0 Kudos
DanutaHodgson
Emerging Contributor

Hi, thank you for helping me out.  I really suck at scripts.

I ran the script and and and ArcMap Drawing Error saying one or more layers failed to draw:

lyr: An invalid SQL statement was used.

Runtime error <class 'arcgisscripting.ExecuteError'>: ERROR 000622: Failed to execute (Layer To KML). Parameters are not valid. ERROR 000625: layer_output_scale parameter has no default value. 

Should there be changes in the script. Could you tell me what and I can change them?

Really appreciate the help

0 Kudos
XanderBakker
Esri Esteemed Contributor

Possibly, something related to the data is happening here. To avoid guessing, I really would need to have a look at the data. Can you share it?

0 Kudos