10.1 arcpy.CopyFeatures_management fails when using joined inputs

3672
6
10-11-2012 12:35 PM
MarkWilloughby
New Contributor III
Scenario:

-fresh File Geodatabase created using ArcCatalog 10.1;
-one feature class in FGDB;
-one table in the same FGDB;
-make a feature layer from feature class;
-make a table view from table;
-add join using feature layer and tableview as inputs, KEEP_COMMON;
-execute copyfeatures to write new featureclass into the same FGDB and preserve joined data in output;

This works (sometimes) in modelbuilder but doesnt work in a standalone python script. It doesn't seem to matter if join columns are indexed or not.

Is this a known defect and if so, is it going to be fixed soon as I have dozens of scripts that use this workflow that work just fine in earlier releases?
0 Kudos
6 Replies
NobbirAhmed
Esri Regular Contributor
I cannot reproduce it in 10.1. I can test if you can share your data (or part of data).

Here is my script:

import arcpy
import os

wks = os.getcwd()
arcpy.env.workspace = os.path.join(wks, "joins.gdb")

in_fc = "states_few"
in_id = "geocompID"
join_table = "table1"
join_id = "ID"
join_type = "KEEP_COMMON"

try:

    # note the output layer name is states_layer
    # use this output layer in subsequent tools
    arcpy.MakeFeatureLayer_management(in_fc, "states_layer")

    arcpy.AddJoin_management("states_layer", in_id, join_table, join_id, join_type)

    arcpy.CopyFeatures_management("states_layer", "states_joined")

    print arcpy.GetMessages()

except:
    print arcpy.GetMessages(2)
0 Kudos
DanPatterson_Retired
MVP Emeritus
Try to add the intermediate step of exporting the joined file to a new one and see if it fails as well
0 Kudos
KimOllivier
Occasional Contributor III
Fails for me every time in a script, but not interactively. A complete pain.

Even if I run it successfully interactively, copy-as-python-snippet and repeat in a script it fails to populate the joined fields.
There is no error message, just blank (not null) fields.

Running 10.0 SP5 but a test on 10.1 SP1 had the same result.

My (terrible) workaround is to create a dictionary of the joined feature content, add the fields and then run an updatecursor with the dictionary.

Alternatively I just open the results the next month and run it again while I surf the net watching the election.....
0 Kudos
JonathanCusick
New Contributor II
I feel like I'm missing something obvious, but I'm having trouble with the CopyFeatures_management command as well.

My script creates a new file gdb into which I would then like to copy feature classes. The script works fine when I define the name of the new gdb ahead of time within the code, but I'd rather have the name generated using GetParametersAsText to make the script more interactive. I'm very new to python so I don't know if I'm I just formatting my CopyFeatures command wrong?

As of now I get TypeError: unsupported operand type (s) for +: 'Result' and 'str'
0 Kudos
JonathanCusick
New Contributor II
Turns out it was a simple fix. I added:

output_str = str (output)

after making my new gdb to convert it to a string. This (I think?) allows it to be read by other commands down the line.
0 Kudos
NobbirAhmed
Esri Regular Contributor
The '+' operand works when both are string (or of same type). Your output is an object of type 'Result'. When you converted it to a str your code worked 🙂

A better practice is to call getOutput to get the whole path of the geodatabase:

output.getOutput(0)
0 Kudos