Select to view content in your preferred language

Help with Feature Class To Feature Class

1792
8
10-14-2011 01:31 PM
TonyAlmeida
MVP Regular Contributor
I am trying to join a shapefile to a dbf table then create a new shapefile with certain fields. Feature class and table are on SDE.
I am having trouble putting it together. Any help would be great, thanks!

My code

# Join a table to a featureclass and select the desired attributes


# Import system modules
import arcpy
from arcpy import env



# Set environment settings
env.workspace = "C:/TEMP"
env.qualifiedFieldNames = False
   
# Set local variables   
inFeatures = "**\\**.sde\\vector.DBO.**\\vector.DBO.**"
layerName = "TAX_layer"
layerName2 = "TAX_layer2"
joinTable = "**\\**.sde\\Vector.DBO.**"
joinField = "ACCOUNT"
joinField2 = "Acct"
outFeature = "TAX_ADDMIN.shp"
outFeature2 = "TAX_ADDMIN2.shp"
expression = "ACCOUNT", "OwnerName", "Address", "City" , "State", "ZipCode"
outLocation = "C:/temp"
   
# Create a feature layer from the vegtype featureclass
arcpy.MakeFeatureLayer_management (inFeatures,  layerName)
   
# Join the feature layer to a table
arcpy.AddJoin_management(layerName, joinField, joinTable, joinField2)
   
# Copy the layer to a new permanent feature class
arcpy.CopyFeatures_management(layerName, outFeature)

# Create a feature layer from the vegtype featureclass
arcpy.MakeFeatureLayer_management (outFeature,  layerName2)

# Execute FeatureClassToFeatureClass
arcpy.FeatureClassToFeatureClass_conversion(layerName2, outLocation,
                                            outFeature2, expression)



My error.

Traceback (most recent call last):
  File "C:\GIS\Python Scripts\JoinField Example 2_2_1.py", line 41, in <module>
    outFeature2, expression)
  File "C:\Program Files\ArcGIS\Desktop10.0\arcpy\arcpy\conversion.py", line 1132, in FeatureClassToFeatureClass
    raise e
RuntimeError: Object: Error in executing tool
Tags (2)
0 Kudos
8 Replies
DuncanHornby
MVP Notable Contributor
Tony,

The problem is your FeatureClassToFeatureClass tool. In your code you create a string called expression.

First thing to note is that this line is wrong

expression = "ACCOUNT", "OwnerName", "Address", "City" , "State", "ZipCode"

it is creating a list object. I am assuming your intention is to limit the fields to the final dataset to these? This would be the field mapping parameter not the where clause parameter.

expression = "ACCOUNT,Ownername,Address" would create a single string.

Secondly the parameter order of FeatureClassToFeatureClass is incorrect. If you read the help on this tool it is where clause then field mapping. As you want to limit the fields you need to specify the where clause which I assume is nothing so a simple "" should suffice.

So before you do anything fire up help and study the parameter order and their specifications to get the code working.

Duncan
0 Kudos
TonyAlmeida
MVP Regular Contributor
Thanks for the reply,

I tried to follow the example but it's doing something totally different then what i need it to do. Is there an example somewhere i look at that is similar to my issue?

Thanks.
0 Kudos
ChristopherStorer
Emerging Contributor
Encase your code in a try/catch loop to get the actual error out of the geoprocessor; that will give you a much better idea what the script doesn't like.  You're certainly going about it the right way, you just need to read the error messages from arcpy.

    try:
        print("Place code here")
    except arcpy.ExecuteError:
        Errors = "TRUE"
        logger.error("An error occurred during geoprocessing:")
        logger.error(arcpy.GetMessages(2))
        return 2
    except Exception as ErrorDesc:
        Errors = "TRUE"
        logger.error("An unknown error occurred:")
        logger.error(ErrorDesc.message)
        return 2
0 Kudos
TonyAlmeida
MVP Regular Contributor
Thank you storercd for your reply.

I was trying to find an example of how to do field mapping but i couldn't find an example similar to my situation. I am using the arcpy.DeleteField_managment, but it takes for EVER. If anyone can help me out with the Field Mapping i would gratefully appreciate it. Thanks!

# Purpose: Join a table to a featureclass and select the desired attributes


# Import system modules
import arcpy
from arcpy import env

# Set environment settings
env.workspace = "C:/TEMP"
env.qualifiedFieldNames = False
   
# Set local variables   
inFeatures = "Database Connections\\********.sde\\vector.DBO.********\\vector.DBO.********"
layerName = "TAX_layer"
joinTable = "Database Connections\\********.sde\\Vector.DBO.********"
joinField = "ACCOUNT"
joinField2 = "Acct"
outFeature = "TAX_ADDMIN.shp"

              

# Create a feature layer from the vegtype featureclass
arcpy.MakeFeatureLayer_management (inFeatures,  layerName)

# Join the feature layer to a table
arcpy.AddJoin_management(layerName, joinField, joinTable, joinField2)
  
# Copy the layer to a new permanent feature class
arcpy.CopyFeatures_management(layerName, outFeature)

arcpy.env.overwriteOutput = True

#arcpy.CopyFeatures_management("TAX_ADDMIN.shp", "C:/temp/TAX_ADDMIN3.shp")
arcpy.DeleteField_management("TAX_ADDMIN3.shp",
                             ["OBJECTID_1", "DXF_LAYER"])
0 Kudos
DuncanHornby
MVP Notable Contributor
Tony,

A very simple way to work out how to set up your field map is:


  1. Start Arcmap and load your table and layer

  2. In Arcmap join your table to your layer

  3. Start a new blank model

  4. Drop a FeatureClassToFeatureClass tool on your model

  5. Open this tool and complete it setting all the options (this includes your field mapping)

  6. Export the model to a Python script from the Model > Export menu option

  7. Open that script and discover the syntax of the tool which is using your data!


Duncan
0 Kudos
TonyAlmeida
MVP Regular Contributor
I know i can do that way, but it doesn't show how to use the field mapping. I am new to python and i am trying to learn. An example of field mappig close to what i am trying to do would great.

Thanks!
0 Kudos
DuncanHornby
MVP Notable Contributor
Tony,

Exporting the model to a script does show the field mapping parameter. But I agree it's not clear how you would construct that string in a programmatic way. Looking at the Help file it talks about a fieldmappings object being a collection of fieldmap objects but nothing seems to show that long string that the tool builds.

Maybe a simpler alternative is to use a Make FeatureLayer tool to turn on/off fields and then the Copy Features tool?

Duncan
0 Kudos
MathewCoyle
Honored Contributor
Here's an example of some verbose field mapping. Probably not the best example, but a place to start.

                field_mappings = arcpy.FieldMappings()

                field_mappings.addTable(block_lyr)

                fldmap_OBJID = arcpy.FieldMap()
                fldmap_BLOCKSTAGE = arcpy.FieldMap()
                fldmap_AREAGIS = arcpy.FieldMap()
                fldmap_OPTYPE = arcpy.FieldMap()
                fldmap_BLOCKTYPE = arcpy.FieldMap()
                fldmap_HARVSEASON = arcpy.FieldMap()
                fldmap_ROADPERCNT = arcpy.FieldMap()

                fldmap_OBJID.addInputField(block_lyr, "OBJECTID")
                fld_OBJID = fldmap_OBJID.outputField
                fld_OBJID.name = "O_OID"
                fldmap_OBJID.outputField = fld_OBJID
                field_mappings.addFieldMap(fldmap_OBJID)

                fldmap_BLOCKSTAGE.addInputField(block_lyr, "BLOCKSTAGE")
                fld_BLOCKSTAGE = fldmap_BLOCKSTAGE.outputField
                fld_BLOCKSTAGE.name = "BLOCKSTAGE"
                fldmap_BLOCKSTAGE.outputField = fld_BLOCKSTAGE
                field_mappings.addFieldMap(fldmap_BLOCKSTAGE)

                fldmap_AREAGIS.addInputField(block_lyr, "AREAGIS")
                fld_AREAGIS = fldmap_AREAGIS.outputField
                fld_AREAGIS.name = "AREAGIS"
                fldmap_AREAGIS.outputField = fld_AREAGIS
                field_mappings.addFieldMap(fldmap_AREAGIS)

                fldmap_OPTYPE.addInputField(block_lyr, "OPERATIONSTYPE")
                fld_OPTYPE = fldmap_OPTYPE.outputField
                fld_OPTYPE.name = "OPTYPE"
                fldmap_OPTYPE.outputField = fld_OPTYPE
                field_mappings.addFieldMap(fldmap_OPTYPE)

                fldmap_BLOCKTYPE.addInputField(block_lyr, "BLOCKTYPE")
                fld_BLOCKTYPE = fldmap_BLOCKTYPE.outputField
                fld_BLOCKTYPE.name = "BLOCKTYPE"
                fldmap_BLOCKTYPE.outputField = fld_BLOCKTYPE
                field_mappings.addFieldMap(fldmap_BLOCKTYPE)

                fldmap_HARVSEASON.addInputField(block_lyr, "HARVESTSEASON")
                fld_HARVSEASON = fldmap_HARVSEASON.outputField
                fld_HARVSEASON.name = "HARVSEASON"
                fldmap_HARVSEASON.outputField = fld_HARVSEASON
                field_mappings.addFieldMap(fldmap_HARVSEASON)

                fldmap_ROADPERCNT.addInputField(block_lyr, "ROADPERCENTAGE")
                fld_ROADPERCNT = fldmap_ROADPERCNT.outputField
                fld_ROADPERCNT.name = "ROADPERCNT"
                fldmap_ROADPERCNT.outputField = fld_ROADPERCNT
                field_mappings.addFieldMap(fldmap_ROADPERCNT)
0 Kudos