0

I have a geodatabase feature class of a polygon with multiple rows, where each row represents an individual polygon. I need to loop through each one of these polygons (rows) and export them to a KML file. What would be the best approach to deal with this using arcpy?

  • 2
    Welcome to GIS StackExchange! As a new user please take the tour to learn about our focused Q&A format. Please note that questions regarding code expect you to include a code snippet or what you've already tried, otherwise it may be closed as off-topic. – smiller Oct 22 '18 at 14:55
  • The general tools you can use for this are: arcpy.da.SearchCursor, arcpy.MakeFeatureLayer_management and arcpy.LayerToKML_conversion. – smiller Oct 22 '18 at 14:59
1

Not tested, but general process:

import arcpy
# Determine range of FIDs (Shapefile) or ObjectIDs (GDB feature class)
polyg = "PATH_TO_FEATURECLASSS"
polygIDS = [x[0] for x in arcpy.da.SearchCursor(polyg, ["OBJECTID"])]
# Alternative method that also works:
# with arcpy.da.SearchCursor(polyg, ["OBJECTID"]) as cursor:
#    polgyIDS = [row[0] for row in cursor]
# Loop through rows, creating layer each time
for i in polygIDs:
    whereclause = """{} = {}""".format(arcpy.AddFieldDelimiters(polyg, "OBJECTID"), i)
    arcpy.MakeFeatureLayer_management(polyg,"polyg_lyr{}".format(i), whereclause)
    # Setup Output name / destination
    outKML = "PATHTOKML{}".format(i) #unique name by ObjectID
    # Output KML / KMZ file 
    arcpy.LayerToKML_conversion("polyg_lyr{}".format(i), outKML)
    # Delete layer to free memory
    arcpy.Delete_management("polyg_lyr{}".format(i))

References: Layer to KML

| improve this answer | |
  • polygIDS = [x[0] for x in arcpy.da.SearchCursor(polyg, ["OBJECTID"]] = invalid syntax. You can keep the disclaimer in your post "not tested", but whatever if you want to role out my edit that did work. – KHibma Oct 22 '18 at 16:04
  • Hmm, I attempted to edit but it seems to have fallen back. Two possible valid syntaxes, both with list comprehension. I'll list both for clarity: polygIDS = [x[0] for x in arcpy.da.SearchCursor(polyg, ["OBJECTID"])] (Added an ending ")"). or with arcpy.da.SearchCursor(polyg, ["OBJECTID"]) as cursor: polygIDS = [row[0] for row in cursor]. – smiller Oct 22 '18 at 16:16
  • what exactly would go for datasource and field? here: whereclause = """{} = {}""".format(arcpy.AddFieldDelimiters(datasource, field), i) would that be polyg and field OBJECTID? – athila Oct 22 '18 at 20:40
  • Yes, but double-check that the fieldname is OBJECTID in your source (for any of the rows that reference that field). – smiller Oct 22 '18 at 20:51

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