ArcGIS Pro Python AddJoin

4078
15
01-12-2021 02:16 PM
Darrin_Smith
New Contributor II

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?

0 Kudos
15 Replies
DanPatterson
MVP Esteemed Contributor

Add Join (Data Management)—ArcGIS Pro | Documentation

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.

Join Field (Data Management)—ArcGIS Pro | Documentation

is different


... sort of retired...
0 Kudos
Darrin_Smith
New Contributor II

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.

0 Kudos
DanPatterson
MVP Esteemed Contributor

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.

TableToNumPyArray—ArcGIS Pro | Documentation

ExtendTable—ArcGIS Pro | Documentation


... sort of retired...
0 Kudos
Darrin_Smith
New Contributor II

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)

0 Kudos
DanPatterson
MVP Esteemed Contributor

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

Considerations when using the in_memory workspace—ArcGIS Pro | Documentation


... sort of retired...
0 Kudos
DavidBlanchard
Esri Contributor

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?

0 Kudos
Darrin_Smith
New Contributor II

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.

  • The in_layer_or_view is a hosted feature layer.
  • I have tried variables for the path + layer as well as the simple layer name as found in the map.  Both of which demonstrate a successful run (the green checkmark).  Below is the script which uses the simple layername:
  • To confirm it does or does not work, I check two sources.
    • One is to open the in layer attribute table which does not contain the join fields
    • The other is to look at Data Tab-->Joins pulldown menu where "Remove Join" and "Remove All Joins" are grayed out.
  • The only difference I see in the Script and Geoprocessor is the Parameters from History where the Script has no parameters but the Geoprocessor notes an updated layer (see attachment)

import arcpy

#Reference map document from within ArcGIS Pro

aprx = arcpy.mp.ArcGISProject('CURRENT')

map = aprx.activeMap

# Process: Add Join (Data Management)

path = "\\\\localpath\Test.gdb\\"

hostedpath = "https://services7.arcgis.com/zHNS16tz3znqN3gM/arcgis/rest/services/TEST_gdb/FeatureServer/0"

feature1 = "SWNODE"

feature2 = "JUNK"

infeature1 = hostedpath

infeature2 = path + feature2

arcpy.AddJoin_management(feature1, "INPUTID", "feature2", "INPUTID", "KEEP_ALL")

0 Kudos
DavidBlanchard
Esri Contributor

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).

0 Kudos
Darrin_Smith
New Contributor II

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.

0 Kudos