1

Using a list of the feature classes matching a wildcard, I loop through 12 feature classes with a dataset. For each feature class, I make second list of all unique values within the REGION field, and then copy the feature class into mutually exclusive subsets (essentially breaking up the feature class using the REGION values). My script runs and creates all of the desired new feature classes, but I get the following error:

line 42, in new_feature_class(), line 28, in new_feature_class arcpy.Select_analysis(in_features, out_feature_class, where_clause), line 90, in Select raise e ExecuteError: Failed to execute. Parameters are not valid. ERROR 000670: output Output Feature Class is same as input Input Features Failed to execute (Select).

My guess was that since the outputs are being saved into the same folder, that they are getting fed back into the for loop list, but an output can't be made because the output feature class that it would make already exists.

I'm wondering if there's a way to eliminate getting this error-- perhaps by stopping the for loop in some way?

I've copied my code below in case it's helpful.

import arcpy
from arcpy.sa import *

arcpy.env.workspace = 'C:/classifications.gdb/cy2016'
arcpy.env.overwriteOutput = True

def new_feature_class():    
#loop through each feature class ending in _SIG
    wild_card = '*SIG'
    featureClassList = arcpy.ListFeatureClasses(wild_card)

    for j in featureClassList:

        # Create list out of all unique values in REGION field.
        def unique_values(table, field):
            with arcpy.da.SearchCursor(table, field) as cursor:
                return sorted({row[0] for row in cursor})

        uniqueRegionList = unique_values(j, 'REGION')

        # 3. For each feature class, create a new feature class subset for every unique region within it
        for i in uniqueRegionList:
            in_features = j
            # Make the output name: region + last 11 characters from feature class 
            out_feature_class = str(i) + in_features[-11:] 
            where_clause = " \"REGION\" = '%s'" % (i)

            arcpy.Select_analysis(in_features, out_feature_class, where_clause)

            # Push new subsetted feature class into next step
            in_table = out_feature_class

            # Select rows where class = 4 and clear values
            field_names = ['CLASS', 'HEIGHT']
            with arcpy.da.UpdateCursor(in_table, field_names) as cursor:
                for row in cursor:
                    if (row[0] == 4):
                        row[1] = 0
                    cursor.updateRow(row)

# Execute function 'new_feature_class'
new_feature_class()
  • 2
    How about outputting to a different directory, it's complaining that you're trying to overwrite the input features. Are your regions numeric, or potentially numeric? It is really bad form to start a feature class with a number (or underscore). It's possible that you're encountering a '' (empty string) so that the input and output have the same name, by exporting to a different folder you're guaranteed to have different paths even if the name is the same - I am assuming you want to keep the empty string features, if not then put in if len(str(i)) > 0: and it should skip over an empty string. – Michael Stimson Mar 14 '18 at 0:00
  • 1
    Dibtou have any null values? – Ben S Nadler Mar 14 '18 at 6:25
  • @MichaelStimson Thank you, I was trying to avoid outputting to a different directory as the file structure format is set up to be exactly this way for other users. The regions aren't numeric. My work around for now will be to break down the workflow into three parts with three different for loops, all with more specific wildcards. – Iracambi Mar 14 '18 at 23:12
  • 1
    Do you have empty strings in the REGION field? A subtle rename of your output like out_feature_class = 'out_' + str(i) + in_features[-11:] will help ensure you're not outputting to the same feature class. – Michael Stimson Mar 15 '18 at 0:09
  • 1
    Hum. May be the unique value function. Try using set() for unique values. Otherwise It has to be how you are generating the output file name then. Add a try-except and print out the offending region name. My money is that is has something to do with the in_features[-11:] parsing of the value – Ben S Nadler Mar 16 '18 at 5:06

Your Answer

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

Browse other questions tagged or ask your own question.