arcpy.AddJoin Not Working In ArcGIS Pro Script

349
7
10-25-2019 06:53 AM
Highlighted
New Contributor

I am trying to join a layer in an ArcGIS Pro map to a table using a scripting tool. The layer is called 'Heights', and is being joined to a table called 'Names'. The Python code below is working when run from the Python window within ArcGIS Pro. However, when I try to run it as a script from the Catalog Pane then the join does not happen, even though the geoprocessing detail does not throw up any error messages. Am I missing a step or does this just not work in ArcGIS Pro?

import arcpy
#Reference map document from within ArcGIS Pro
aprx = arcpy.mp.ArcGISProject('CURRENT')
map = aprx.activeMap
# Local variables:
Heights = "Heights"
toTable = r"C:\#workspace\Names"
# Process: Add Join
arcpy.AddJoin_management(Heights, 'LINK_ID', toTable, 'AI_LINK_ID', 'KEEP_ALL')‍‍‍‍‍‍‍‍‍‍‍
Reply
0 Kudos
7 Replies
Highlighted
MVP Esteemed Contributor

If you are running as a separate script the concept of "Current" isn't known, it is only valid in Pro's python window.

Have you tried replacing Current with the path to the aprx and setting arcpy's env to it so that layers can be referenced without full paths (eg Heights)

Also   /blogs/dan_patterson/2016/08/14/script-formatting 

would help people reference line numbers, if the above is indeed a script

Reply
0 Kudos
Highlighted
New Contributor

HI Dan

Thanks for the advice about the code rendering. I have now fixed this is the post.

I am bit confused by the 'Current' not being known as I am able to switch the Heights layer off in the map simply by adding the following lines to the script:

lyrs = map.listLayers(World5m_Heights)
lyrs[0].visible = False‍‍‍‍

The full script then becomes:

import arcpy
#Reference map document from within ArcGIS Pro
aprx = arcpy.mp.ArcGISProject('Current')
map = aprx.activeMap
# Local variables:
Heights = "Heights"
toTable = r"C:\#workspace\Names"
# Toggle Heights layer off
lyrs = map.listLayers(Heights)
lyrs[0].visible = False
# Process: Add Join
arcpy.AddJoin_management(Heights, 'LINK_ID', toTable, 'AI_LINK_ID', 'KEEP_ALL')‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

This would indicate that the layer is being referenced and manipulated OK using the script. However, the join does not work. If I change the parameters in the join to...

arcpy.AddJoin_management(lyrs[0], 'LINK_ID', toTable, 'AI_LINK_ID', 'KEEP_ALL')‍‍

...it still doesn't work.

Reply
0 Kudos
Highlighted
MVP Esteemed Contributor

Kenneth, Are you running it in the python window within Pro? or from a separate python IDE?

Add Join—Data Management toolbox | ArcGIS Desktop 

on a lark, pass the full path to both to test whether something is wrong with one of the two tables

If the input is a feature class or dataset path, this tool will automatically create and return a new layer with the result of the tool applied.
Reply
0 Kudos
Highlighted
New Contributor

I'm running the script from a toolbox within the Catalog Pane of an ArcGIS Pro project. This has to run within the project and on layers in the map because the full script will actually loop through all the layers in the map in order to join each onel to the Names table so that the map labels can reference one of the columns in the joined table. This is a script that runs perfectly well in ArcMap using the same method (i.e. a toolbox script in ArcCatalog from within ArcMap) and all I am trying to do is update the script so that it works within ArcGIS Pro.

If I do as you suggest to the actual feature classes rather than the map layers then the join will likely work OK but there will be no way to actually view if it is successful or not because the join will be in a temporary state in order to allow processes like Calculate Fields to run. Instead, I need this to work on the layers within the ArcGIS Pro map. As mentioned it seems that I can manipulate the layers OK (tested using the lyr.visible = False code) but I can't get the join to work.

Since the code works within the Python window in ArcGIS Pro then I'm confident there is nothing wrong with the actual tables.

Reply
0 Kudos
Highlighted
MVP Esteemed Contributor

The last thing I can think of is that if the table is open when the join is being performed, it doesn't show, but needs to be closed and reopened.  I am also a bit confused about the reference to the totable which appears to be on disk and not a reference to a table view.

Maybe someone else can see what I am overlooking

Reply
0 Kudos
Highlighted
New Contributor

Yes, I had thought to reload the table but to no avail I'm afraid. I've also tried to create a view of the Names table and then joining to that. The following code is added with some fieldinfo:

arcpy.MakeTableView_management(toTable, 'Table_View', '', '', fieldinfo)

That then alters the AddJoin command to:

arcpy.AddJoin_management(Heights, 'LINK_ID', 'Table_View', 'AI_LINK_ID', 'KEEP_ALL')‍‍

It sounds like we may not be able to figure this out. If anyone is able to confirm that this indeed does not work in ArcGIS Pro then I will raise it as a bug or future enhancement. Thanks for your help.

Reply
0 Kudos
Highlighted
MVP Esteemed Contributor

Esri Support Search-Results 

nothing obvious... except the warning that the inputs must be layers or table views

Reply
0 Kudos