1

I am fairly new to python scripting for ArcGIS. I am trying to create maps where each county is selected and then copied into a new layer with only that county's data. Then, I used ApplySymbologyFrom to copy the symbology format from a mock layer. It worked when I ran the code for an individual county, but it doesn't work when I try to use it in a loop statement.

Error details: The code seems to be working up until the loop step. The variables are stored, environment workspace is set, and the current map is being referenced. Cursor variable is stored. When I try to run the code block "for row in cursor" and press enter twice, the program does not have any response besides a new >>> line. There is no error code produced when I run that code block.

Error message from PythonWin run: ExecuteError: Failed to execute. Parameters are not valid. ERROR 000725: Output Feature Class: Dataset T:\HCI\Shapefiles\Inset_Maps\Alameda.shp already exists. Failed to execute (CopyFeatures).

UPDATE: I added the code to delete previous data. Now the new error message is "NameError: name 'row' is not defined"

Here's my code so far:

# Import system modules
import arcpy

# Set workspace
arcpy.env.workspace = r"T:\HCI\Shapefiles\Inset_Maps"
#mxd = arcpy.mapping.MapDocument("CURRENT")
mxd = arcpy.mapping.MapDocument("C:\Users\jchan1\Desktop\Inset_maps_test.mxd")

#background processing was disabled in geoprocessing options in order to access current map
#geoprocessing output overwrite enabled in geoprocessing options
# create local variables
field = "NAME_1"
counties = "Counties_layer"

#remove field delimiters, if needed
arcpy.AddFieldDelimiters(counties,field)

cursor = arcpy.SearchCursor(counties)


for row in cursor:
    whereClause = '"NAME_1" = \'' + row.getValue(field) + "'"
    if arcpy.Exists(row.getValue(field)):
        arcpy.Delete_management(row.getValue(field))
    # Select layer by county name
    arcpy.SelectLayerByAttribute_management(counties,"NEW_SELECTION",whereClause)
    # Write the selected features to a new featureclass
    arcpy.CopyFeatures_management(counties, row.getValue(field))
    # Copy county layer's symbology from sample layer
    arcpy.ApplySymbologyFromLayer_management(row.getValue(field), "dummy")

# export inset image to JPEG
# arcpy.mapping.ExporttoPJEG(mxd,r"file_path_name")

# delete local variables
del row, cursor, mxd

#Refresh screen
arcpy.RefreshTOC()
arcpy.RefreshActiveView()
  • 1
    Would you be able to edit your question to describe precisely what you mean when you say "it doesn't work", please? If there is an error thrown be sure to copy/paste that into your question as text. – PolyGeo Feb 11 '15 at 0:53
  • It had run once before when I was testing it, and it produced layers with the county names but with no data in the layers. I checked the original dataset and confirmed there was a full dataset. Now the code does not produce the layers and returns no output. – Jacqui Chan Feb 11 '15 at 1:02
  • It sounds like you are running this in the Python window of ArcMap. I recommend copy/pasting the code into IDLE and running it from there to see if that will elicit an error message. There you will need to make sure you specify the *.mxd you are using rather than "CURRENT". – PolyGeo Feb 11 '15 at 1:02
  • Error message from PythonWin run: ExecuteError: Failed to execute. Parameters are not valid. ERROR 000725: Output Feature Class: Dataset T:\HCI\Shapefiles\Inset_Maps\Alameda.shp already exists. Failed to execute (CopyFeatures). – Jacqui Chan Feb 11 '15 at 1:48
  • Be sure to keep using the edit button to keep the precise code you are running and any inputs, outputs and errors in sync, as part of your question (which has great formatting options) rather than comments that are temporary. Error 000725 is common and easy to address when approached systematically. – PolyGeo Feb 11 '15 at 2:06
2

If you are running this in the Python window, then you need go to the Geoprocessing menu -> Geoprocessing Options and check the setting for "Overwrite the outputs of geoprocessing operations". If it is not checked than the script is behaving correctly, since your current setting is not supposed to not let tools overwrite outputs. Once that setting is checked, then outputs should be overwritten if you run your tools inside ArcMap and your script should succeed.

However, for a standalone script run outside of ArcMap the geoprocessing option does not apply. You have to add code to check if a feature class already exists and delete it before trying to use Copy_Features to recreate it. That approach always works whether or not you are in ArcMap and even if the Overwrite option is not checked while you are working in ArcMap, so I always use that method. Use code like:

if arcpy.Exists(row.getValue(field)):
    arcpy.Delete_management(row.getValue(field))
# the data is deleted so now you can reuse that name for new data

If this code will be used in a script tool in ArcMap and is for end users who should get dummy warnings if they try to overwrite existing data with geoprocessing tools apart from your tool, you do not want to permanently change the overwrite setting in your script without setting it back to the previous overwrite status when the script succeeds in recreating the data. In that case have the script first read their existing overwrite setting and store it in a variable, then temporarily enable overwrite, and then reset the overwrite to the user's original setting when you are done overwriting data. Something like:

from arcpy import env
origSetting = env.overwriteOutput
env.overwriteOutput = True
# Do some data overwrites
env.overwriteOutput = origSetting
  • I added the code after the cursor was defined and before the cursor loop. But I still get the following error message: ExecuteError: Failed to execute. Parameters are not valid. ERROR 000725: Output Feature Class: Dataset T:\HCI\Shapefiles\Inset_Maps\out_Alameda.shp already exists. Failed to execute (CopyFeatures). – Jacqui Chan Feb 11 '15 at 2:03
  • The code has to be inside the loop. The value of the row is only changing inside the loop. – Richard Fairhurst Feb 11 '15 at 7:34
  • Your whereclause was also constructed incorrectly. You must keep row.getValue(field)) outside of the quotes so that the actual value of that expression is added to the string, not the literal string value "row.getValue(field))". I fixed the code in your post to place the quotes correctly. – Richard Fairhurst Feb 11 '15 at 7:42
  • I am glad to have helped. – Richard Fairhurst Feb 11 '15 at 19:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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