Feature Class to Feature Class

2090
10
Jump to solution
10-29-2018 03:00 PM
HectorNavarro
New Contributor III

Hello,

I work on a project that is very repetitive and would like to have a python script for it. I am trying to export a feature class from SDE which contains an expression. The geodatabase has been created and I have the following code,

# Import system modules
import arcpy
from arcpy import env

# Set the workspace
arcpy.env.workspace = "Database Connections\ADMIN Edit.sde\GIS.DBO.SewerCollectionNetwork"
ArcSDE = arcpy.env.workspace
env.overwriteOutput = True

# Set local variables
inFeatures = ['sewer']
outputDatabaseLocation = "C:\Users\gis\Desktop\sewer\CCTV.gdb"
outputFeatureClass = "SewerMains"
delimitedField = arcpy.AddFieldDelimiters(arcpy.env.workspace, "OWNER")
expression = delimitedField + " = 'LOCAL'"

# Export feature class
print("Exporting feature class to CCTV geodatabase")
arcpy.FeatureClassToFeatureClass_conversion(inFeatures, outputDatabaseLocation, outputFeatureClass, expression)

I get the following error,

Traceback (most recent call last):
  File "B:\Scripts\Python\ArcSDE Maintenance\CCTV Feature Class.py", line 25, in <module>
    arcpy.FeatureClassToFeatureClass_conversion(inFeatures, outputDatabaseLocation, outputFeatureClass)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.5\ArcPy\arcpy\conversion.py", line 1891, in FeatureClassToFeatureClass
    raise e
RuntimeError: Object: Error in executing tool

I have tried using FeatureClassToGeodatabase and FeatureClassToShapefile with success, but I need to be able to add an expression to only export specific values from a field.

Any help will be appreciated.

0 Kudos
1 Solution

Accepted Solutions
DarrenWiens2
MVP Honored Contributor

Hmmm, a string inside brackets means "list of one string" in Python, but I don't work with SDE.

View solution in original post

10 Replies
AdrianWelsh
MVP Notable Contributor

Hector,

Take a look at this document:

Feature Class to Feature Class—Conversion toolbox | ArcGIS Desktop 

It mentions where and how to add an expression. Here is the example:

# Name: FeatureClassToFeatureClass_Example2.py
# Description: Use FeatureClassToFeatureClass with an expression to create a subset
#  of the original feature class.  
 
# Import system modules
import arcpy
 
# Set environment settings
arcpy.env.workspace = "C:/data/GreenvalleyDB.gdb/Public Buildings"
 
# Set local variables
inFeatures = "buildings_point"
outLocation = "C:/output/output.gdb"
outFeatureClass = "postoffices"
delimitedField = arcpy.AddFieldDelimiters(arcpy.env.workspace, "NAME")
expression = delimitedField + " = 'Post Office'"
 
# Execute FeatureClassToFeatureClass
arcpy.FeatureClassToFeatureClass_conversion(inFeatures, outLocation, 
                                            outFeatureClass, expression)
HectorNavarro
New Contributor III

Thanks for the reply.  This is the documentation I have been following and still getting errors.

0 Kudos
AdrianWelsh
MVP Notable Contributor

Oh, I see that now. Sorry about that. If you were to not include the expression, does the script work ok? I guess I'm trying to figure out where the error is coming in at (maybe it is with the syntax of the expression)?

0 Kudos
HectorNavarro
New Contributor III

I am not sure. I have tried it without the expression and it does the same thing.  I have double checked my variables to make sure everything is correct and still cannot find what I am doing wrong.

0 Kudos
BalajiVeera
Occasional Contributor

Try to change

expression = "OWNER = 'LOCAL'"

since SQL SDE

0 Kudos
HectorNavarro
New Contributor III

Hello Balaji,

I tried your suggestions and got the same error.

0 Kudos
DarrenWiens2
MVP Honored Contributor

inFeatures = ['sewer']

Are you sure this is correct? inFeatures should be a Feature Layer, not a list.

edit: you should also get in the habit of using forward slashes or raw string notation (e.g. r'path') for paths, otherwise you risk introducing unwanted escape characters.

0 Kudos
HectorNavarro
New Contributor III

Hello Darren,

Yes. This is my layer in SDE.

0 Kudos
DarrenWiens2
MVP Honored Contributor

Hmmm, a string inside brackets means "list of one string" in Python, but I don't work with SDE.