Trouble deleting feature layer

2327
13
01-07-2016 12:11 PM
Highlighted
New Contributor III

I am working on a Python script that includes a cursor looping process to create KML files for individual towns.  For each iteration of the loop, the script creates a feature layer, then exports that feature layer to KML.

The problem is, the script tends to stop working for no apparent reason.  After each KML is created from the feature layer, I delete the feature layer so a new one can be created for the next town.  The script tends to stop at this point.  No error messages, and it doesn't stop at the same town every time I try.  Here is the code that manipulates the feature layer:

    try:

        print "Deleting feature layer..."

        arcpy.Delete_management(parcelsProject_Layer)

        print "...deleted it"

    except:

        print "Couldn't delete feature layer!", sys.exc_info()[0]

        raise

    else:

        # Process: Make Feature Layer

        arcpy.MakeFeatureLayer_management(parcelsProject_shp, parcelsProject_Layer, expression, "", "FID FID VISIBLE NONE;TOWN TOWN VISIBLE NONE;COUNTY COUNTY VISIBLE NONE;GEOCODE GEOCODE VISIBLE NONE;STATE_ID STATE_ID VISIBLE NONE;MAP_BK_LOT MAP_BK_LOT VISIBLE NONE;PARENT PARENT VISIBLE NONE;PROP_LOC PROP_LOC VISIBLE NONE;PROPLOCNUM PROPLOCNUM VISIBLE NONE;TYPE TYPE VISIBLE NONE;FMUPDORG FMUPDORG VISIBLE NONE;FMUPDAT FMUPDAT VISIBLE NONE;FMSRCORG FMSRCORG VISIBLE NONE;Shape_STAr Shape_STAr VISIBLE NONE;Shape_STLe Shape_STLe VISIBLE NONE;Shape Shape VISIBLE NONE;Shape_ST_1 Shape_ST_1 HIDDEN NONE;Shape_ST_2 Shape_ST_2 HIDDEN NONE")

        # Process: Layer To KML

        gp.AddMessage("Converting town to KML")

        arcpy.LayerToKML_conversion(parcelsProject_Layer, parcels, "0", "false", parcelsProject_Layer, "1024", "96", "CLAMPED_TO_GROUND")

--Any suggestions?

Reply
0 Kudos
13 Replies
Highlighted
Esri Esteemed Contributor

Hi Bob,

Instead of deleting the FeatureLayer, try overwriting it by adding the following line at the beginning of your script:

arcpy.env.overwriteOutput = 1

env—ArcPy Classes | ArcGIS for Desktop

Highlighted
New Contributor III

Hi Jake, and thanks for the reply.  I added the overwriteOutput line to my script, and I removed the delete featurelayer code.  Also added more Print lines so I can see what's going on.  The script is still quitting randomly.  In some attempts, it stopped while creating a new output directory for a KML, in other cases it quit while creating a feature layer.  But again, it quits with a different record each time.  And I am not seeing any error messages.

Reply
0 Kudos
Highlighted
MVP Honored Contributor

Can you post more of your code (in code tags) for context?

Reply
0 Kudos
Highlighted
New Contributor III

I'm sorry, I don't see how to put it in code tags, but here is the code:

rows = gp.SearchCursor(workspace + "\\parcels_ut_town_list.dbf")

for row in rows:

    town = row.town

    # Create Output Directory

    towndir = town.strip()   

    # Create Output Directory

    gp.AddMessage("Processing" + " " + town)

    print "Processing" + " " + town

    gp.AddMessage("Creating Directory For" + " " + town) 

    print "creating outfolder for "+town  

    outfolder = gp.CreateFolder_management(workspace, towndir)               ##Sometimes quitting here

    expression = "TOWN = " + "'" + town.strip() + "'"

    parcels = outfolder + "\\parcels"+towndir+".kmz"

    # Process: Make Feature Layer

    print "making feature layer"                                                                          ##-And sometimes quitting around the next line-

    arcpy.MakeFeatureLayer_management(parcelsProject_shp, parcelsProject_Layer, expression, "", "FID FID VISIBLE NONE;TOWN TOWN VISIBLE NONE;COUNTY COUNTY VISIBLE NONE;GEOCODE GEOCODE VISIBLE NONE;STATE_ID STATE_ID VISIBLE NONE;MAP_BK_LOT MAP_BK_LOT VISIBLE NONE;PARENT PARENT VISIBLE NONE;PROP_LOC PROP_LOC VISIBLE NONE;PROPLOCNUM PROPLOCNUM VISIBLE NONE;TYPE TYPE VISIBLE NONE;FMUPDORG FMUPDORG VISIBLE NONE;FMUPDAT FMUPDAT VISIBLE NONE;FMSRCORG FMSRCORG VISIBLE NONE;Shape_STAr Shape_STAr VISIBLE NONE;Shape_STLe Shape_STLe VISIBLE NONE;Shape Shape VISIBLE NONE;Shape_ST_1 Shape_ST_1 HIDDEN NONE;Shape_ST_2 Shape_ST_2 HIDDEN NONE")

    # Process: Layer To KML

    gp.AddMessage("Converting town to KML")

    print "Converting town to KML"

    arcpy.LayerToKML_conversion(parcelsProject_Layer, parcels, "0", "false", parcelsProject_Layer, "1024", "96", "CLAMPED_TO_GROUND")

    #Copy Metadata   

    gp.AddMessage("Copying parcels Metadata For" + " " + town)

    print "Copying parcels Metadata For" + " " + town

    shutil.copy(metat, outfolder + "\\" + "parcels.txt")

    gp.AddMessage("Done Processing parcels For" + " " + town)

    print "Done Processing parcels For" + " " + town

     #Create The Zipfile

    #gp.AddMessage(output + "\\Parcels_" + towndir)

    zpfile = zipfile.ZipFile(output + "\\Parcels_" + towndir + ".zip", 'w')

    zpfile.write(outfolder + "\\parcels"+towndir+".kmz", "Parcels_" + towndir + ".kmz", zipfile.ZIP_DEFLATED)

    zpfile.write(outfolder + "\\parcels.txt", "Parcels_" + towndir + ".txt", zipfile.ZIP_DEFLATED)

    zpfile.close()

    #Delete Uncompressed Files

    shutil.rmtree(outfolder)

    gp.AddMessage("All Done Processing" + " " + town)

del rows

Reply
0 Kudos
Highlighted
MVP Honored Contributor

Can you provide the line where you specify the workspace?

On a side note, why does your code flipflop between 'arcpy' and 'gp'? Are they both pointing to arcpy? Have you tried this with the superior data access module SearchCursor?

Here's how to post in code tags (it's a bit convoluted):

Posting Code blocks in the new GeoNet

rows = gp.SearchCursor(workspace + "\\parcels_ut_town_list.dbf")

for row in rows:

    town = row.town

    # Create Output Directory

    towndir = town.strip()  

    # Create Output Directory

    gp.AddMessage("Processing" + " " + town)

    print "Processing" + " " + town

    gp.AddMessage("Creating Directory For" + " " + town)

    print "creating outfolder for "+town 

    outfolder = gp.CreateFolder_management(workspace, towndir)               ##Sometimes quitting here

    expression = "TOWN = " + "'" + town.strip() + "'"

    parcels = outfolder + "\\parcels"+towndir+".kmz"

    # Process: Make Feature Layer

    print "making feature layer"                                                                          ##-And sometimes quitting around the next line-

    arcpy.MakeFeatureLayer_management(parcelsProject_shp, parcelsProject_Layer, expression, "", "FID FID VISIBLE NONE;TOWN TOWN VISIBLE NONE;COUNTY COUNTY VISIBLE NONE;GEOCODE GEOCODE VISIBLE NONE;STATE_ID STATE_ID VISIBLE NONE;MAP_BK_LOT MAP_BK_LOT VISIBLE NONE;PARENT PARENT VISIBLE NONE;PROP_LOC PROP_LOC VISIBLE NONE;PROPLOCNUM PROPLOCNUM VISIBLE NONE;TYPE TYPE VISIBLE NONE;FMUPDORG FMUPDORG VISIBLE NONE;FMUPDAT FMUPDAT VISIBLE NONE;FMSRCORG FMSRCORG VISIBLE NONE;Shape_STAr Shape_STAr VISIBLE NONE;Shape_STLe Shape_STLe VISIBLE NONE;Shape Shape VISIBLE NONE;Shape_ST_1 Shape_ST_1 HIDDEN NONE;Shape_ST_2 Shape_ST_2 HIDDEN NONE")

    # Process: Layer To KML

    gp.AddMessage("Converting town to KML")

    print "Converting town to KML"

    arcpy.LayerToKML_conversion(parcelsProject_Layer, parcels, "0", "false", parcelsProject_Layer, "1024", "96", "CLAMPED_TO_GROUND")

    #Copy Metadata  

    gp.AddMessage("Copying parcels Metadata For" + " " + town)

    print "Copying parcels Metadata For" + " " + town

    shutil.copy(metat, outfolder + "\\" + "parcels.txt")

    gp.AddMessage("Done Processing parcels For" + " " + town)

    print "Done Processing parcels For" + " " + town

     #Create The Zipfile

    #gp.AddMessage(output + "\\Parcels_" + towndir)

    zpfile = zipfile.ZipFile(output + "\\Parcels_" + towndir + ".zip", 'w')

    zpfile.write(outfolder + "\\parcels"+towndir+".kmz", "Parcels_" + towndir + ".kmz", zipfile.ZIP_DEFLATED)

    zpfile.write(outfolder + "\\parcels.txt", "Parcels_" + towndir + ".txt", zipfile.ZIP_DEFLATED)

    zpfile.close()

    #Delete Uncompressed Files

    shutil.rmtree(outfolder)

    gp.AddMessage("All Done Processing" + " " + town)

del rows

Highlighted
New Contributor III

The workspace was set in an earlier part of the code, which wasn't pasted here.  This is the line:

workspace = 'C:\workspace\parcelsut'

Will also try the data access module search cursor.

Reply
0 Kudos
Highlighted
MVP Esteemed Contributor

you need this:

workspace = 'C:\workspace\parcelsut'       to be in "raw" format

workspace = r'C:\workspace\parcelsut'     or

workspace = 'C:/workspace/parcelsut'     or

workspace = 'C:\\workspace\\parcelsut'

If that is indeed the issue and not a copy paste error

Highlighted
New Contributor III

I've made modifications as suggested by everyone, but still having the same problem.  In particular, I made the following changes:

Using arcpy.OverwriteOutput = 1 as suggested by Jake

Changed the rows with gp.SearchCursor to the arcpy.da.SearchCursor as suggested by Darren

Changed the workspace variable to raw format: workspace = r'C:\workspace\parcelsut as suggested by Dan

I tested the looping process by commenting out the conversion to KMZ.  The looping/iteration works fine.  So I uncommented the KML conversion again.  Once again, the program will stop either when creating the feature layer, or when converting the feature layer to KMZ.  It stops with a different town each time.

But the problem appears to be in the KMZ conversion.  Is there any way to further diagnose why this is happening?  Is there an alternate approach to converting to KMZ?

Reply
0 Kudos
Highlighted
MVP Honored Contributor

What are the names of the towns that cause it to fail? In particular, are there any special characters, like an apostrophe? Or, is it a different town each time?

Reply
0 Kudos