1

I want to copy specific cell value from one feature class and append to another feature class. I have the following code, however, when I run this, it does not produce the results, nor shows any errors.

import arcpy
import os
import math
from arcpy import *
from arcpy import env

env.workspace = "C:\RG\Kings County"
outworkspace = "C:\RG\Kings County\Test.gdb"

arcpy.CheckOutExtension("Spatial")
arcpy.env.overwriteOutput = 1 # this code overwrites existing output

InFC = "C:\RG\Kings County\LuChange.gdb\NVF_050413"
OutFC = "C:\RG\Kings County\Test.gdb\Summary"

try:
    rows = arcpy.da.SearchCursor(InFC, ("Field_ID"), '"Final" = 1')
    for row in rows:
        CopyFeatures_management(InFC, row[0], OutFC)
    del rows, row

except Exception as e:
    print e
    #print arcpy.GetMessage()
  • 1
    Firstly, there is no need, at least here, to check out the Spatial Analyst extension, as you are not using a function from that extension, unless it appears later in your script. I'm going to try and clean up your code to make it easier to read. – Get Spatial Sep 19 '13 at 15:29
4

Let's take a look at the different parts of the script to see where the problem might be, and how to fix it.

First, assuming that this is the entirety of the script, you can get rid of a couple of the imports, leaving you with:

import arcpy
from arcpy import env

env.workspace = "C:\RG\Kings County"
outworkspace = "C:\RG\Kings County\Test.gdb"

You are not invoking the Spatial Analyst extension, so there is no need to reference it:

arcpy.env.overwriteOutput = 1 # this code overwrites existing output

InFC = "C:\RG\Kings County\LuChange.gdb\NVF_050413"
OutFC = "C:\RG\Kings County\Test.gdb\Summary"

try:

First step is setting the Search Cursor with all of the features where '"Final" = 1', and including the Field_ID attribute. This cursor is assigned to the rows variable. You then start to step through the feature list, each feature in turn being assigned to the row variable.

    rows = arcpy.da.SearchCursor(InFC, ("Field_ID"), '"Final" = 1')
    for row in rows:

Here is where we run into a couple of problems.

  1. What are you trying to accomplish? Here is the help for the Copy Features tool: Copy Features (Data Management). This will copy either a selection of features or an entire feature class, to a new featureclass. In your case, since this is inside your loop, you are copying a single feature at a time. Since you have Overwrite enabled, you are copying each feature to the same output feature class name, overwriting it each time. If this were to work, you would end up with a single feature class, with a single feature that was the last one in order from the source feature class that matched your selection criteria.
  2. The other problem is a syntax error. You are naming 3 parameters for a function that only has 2, plus optional ones at the end. Here, you are listing an input feature class, an output featureclass, and then the row[0] variable, which is problematic on its own. That syntax indicates the first element in a list. The row variable is not a list, so it may be a problem to try and call the first element in a list. The bigger issue though, is that you are naming 3 parameters in the function, so it will be looking at the first two in order as the input and then output.

        CopyFeatures_management(InFC, row[0], OutFC)
    del rows, row
    except Exception as e:
      print e
      #print arcpy.GetMessage()
    

There are also a couple issues with the environment settings, namely, you are not using them at all. You set the environment, but then explicitly named the input and output featureclasses, so they do not need the environments.

Let's try to rebuild this and see if it works. I will assume that you were interested in selecting a subset of your features from the input class. You want to copy these to an output class, but only include the Field_ID attribute. The first part is easy, with the SQL query. Since the features are in a File Geodatabase, the string has to be structured slightly differently as well, which may have been another issue. This is done using the Select (Analysis) function

import arcpy
from arcpy import env

env.workspace = "C:\RG\Kings County\LuChange.gdb"

arcpy.env.overwriteOutput = 1 # this code overwrites existing output

InFC = "NVF_050413"
OutFC = "C:\RG\Kings County\Test.gdb\Summary"

try:
    arcpy.Select_analysis(InFC, OutFC, '"Final" = 1')

except Exception as e:
    print e
    #print arcpy.GetMessage()

If this is not what you are going for, please update your question with more detail. Hope this helps!

  • Thank you so much for your well descriptive solution and code. I appreciate it. It did work except, I need to update [Final] to "Final". – Kuma Sep 20 '13 at 21:05
  • @Kuma - Glad I could help. I fixed the syntax error you noted. If this answer truly helped you, please marked it as accepted. This will allow others searching for a similar problem to see that this question has an answer that works. – Get Spatial Sep 22 '13 at 11:22

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.