In ArcGIS Pro, I am running the AddJoin tool. I have run this command from ModelBuilder and from the Data Management Geoprocessing tool. Obviously, after running the tool, it functions as it should by joining one featureclass to another. After running from Geoprocessing, I saved as a Python script. When I create a new script in Pro, then reference the file I just created, it does not create the join. It runs and completes successfully but no effect?
arcpy.management.AddJoin("INFEATURE1", "JOINFIELD1", "INFEATURE2", "JOINFIELD2", "KEEP_ALL")
Is there some backend event like a refresh or response that occurs from a Geoprocessing tool that would not necessarily be reflected in the Python script?
This join is temporary.
To make a permanent join, either use the Join Fieldtool or use the joined layer as input to one of the following tools:Copy Features,Copy Rows,Feature Class To Feature Class, orTable To Table.When saving the results to a new feature class or table, theQualified Field Namesenvironment can be used to control whether the joined output field names will be qualified with the name of the table the field came from.
The Join is intended to be temporary so I believe the AddJoin command is correct. The issue is it does not create the join. The attribute table does not contain the joined fields and the Data Tab-->Joins dropdown is empty i.e. there are no Joins to remove that would normally be listed after AddJoin. Running the command from Python script does nothing. This is the same exact command and syntax that the Geoprocessing tool uses. The only thing I can come up with is must I explicitly define some Derived Output.
The joins are temporary, so you need to specify a new output featureclass/table to persist the join.
The link and notes I posted was for ArcGIS Pro 2.7.
An alternative is to use numpy structured arrays and arcpy's TableToNumPyArray to get a tabular data out of Pro. To bring an array back in you use arcpy's ExtendTable ... That option makes permanent joins and the whole process doesn't even require a toolset or specifying parameters.
For my workflows, it must be a temporary join with the intent to join, calculate a value, then remove the join. I did find a post from 2019 with the same issue. arcpy.AddJoin Not Working In ArcGIS Pro Script - GeoNet, The Esri Community
I now know the AddJoin command creates the join as expected when run from the Python window. One thing I did notice after running from the Python window was the History results lists the input layer as an Output Layer or View compared to the script results which does not (it reports No Parameters)
That makes the case for using numpy more solid.
Alternately, to use existing tools, do the join but send the output to in_memory, do the calculation, then it gets deleted automatically from memory when you are done
If I understand correctly, when you perform the join using the geoprocessing tool or model builder, you get the expected behaviour. However, running the join from a Python script you saved from the geoprocessing tool results, your dataset does not get joined?
Where are you running your script file from, and where are you expecting to see the results? For example, are you running the Python script file by double-clicking it in Windows, and then looking for the result in ArcGIS Pro?
Correct. The script does not create the Join, but ModelBuilder and the out of the box geoprocessing tool do.
The script is run within Pro.
#Reference map document from within ArcGIS Pro
aprx = arcpy.mp.ArcGISProject('CURRENT')
map = aprx.activeMap
# Process: Add Join (Data Management)
path = "\\\\localpath\Test.gdb\\"
feature1 = "SWNODE"
feature2 = "JUNK"
infeature1 = hostedpath
infeature2 = path + feature2
arcpy.AddJoin_management(feature1, "INPUTID", "feature2", "INPUTID", "KEEP_ALL")
You say you run the script in ArcGIS Pro. Do you have it setup as a Toolbox script tool. Do you copy-paste it into the Python window? Or do you launch it from the Python window (if so, what command do you use to do that).
Yes it is a Toolbox script tool (not a Python Toolbox). I just Run directly from the Toolbox. However, if I copy/paste the entire script into the Python window it creates the join?
Note in my script example above I did remove the quotes from feature2. I inadvertently had that when I copied the code into this post.