2

This is probably just me struggling with some of the ArcGIS concepts, but I can't get the results of an AddJoin_management call to write to the layer where I've made the join.

mxd = arcpy.mapping.MapDocument('C:\\My Documents\\ArcGISmaps\\blank template.mxd')
df = arcpy.mapping.ListDataFrames(mxd)[0]

## Take geography from 'Resource library.mdb'
mapSource = os.path.join('C:\\My Documents\\ArcGISmaps\\Resource library.mdb', 'regions')

#Specify data frame & layer
bLyr = arcpy.mapping.Layer(mapSource)
arcpy.mapping.AddLayer(df, bLyr, 'AUTO_ARRANGE')

imdSource = 'C:\\My Documents\\ArcGISmaps\\IMD_02.dbf'

imdTable = arcpy.mapping.TableView(imdSource)
arcpy.mapping.AddTableView(df, imdTable)

try:
    arcpy.AddJoin_management(bLyr, "Reg_ID", imdTable, "ID", 'KEEP_ALL')
except:
    print arcpy.GetMessages()
mxd.save()

When I open the file in ArcMap 10.4, the tables have been successfully added to the TOC, but there's no evidence of the join being included. If I include getMessages() in the try: block, it suggests the join has been successful.

What do I need to do to write the join onto bLyr?


A combination of SaveToLayerFile_management and reimporting the joined table as a new layer has done the trick so I now have:

arcpy.AddJoin_management(bLyr, "ONSCD", imdTable, "MSOACD", 'KEEP_ALL')
arcpy.SaveToLayerFile_management(bLyr, "C:\\My Documents\\ArcGISmaps\\postJoin.lyr", "ABSOLUTE") 

postJoin = arcpy.mapping.Layer("C:\\My Documents\\ArcGISmaps\\postJoin.lyr")
arcpy.mapping.AddLayer(df, postJoin, "AUTO_ARRANGE")
4
  • 1
    I think it's the sequence on how you get a handle on the layers and tables. You create them from their source path then add them to the mxd. You do not get them from the mxd and create the join. Try creating them, adding them, saving the mxd then getting them from the mxd, build the join then save the mxd.
    – Hornbydd
    Jun 9, 2017 at 10:49
  • I think it is always best to remove any try/except statements from code snippets that you present here.
    – PolyGeo
    Jun 9, 2017 at 10:52
  • Is that the convention round here, to omit any try/except? I'll bear that in mind for future questions!
    – CrowsNose
    Jun 9, 2017 at 11:24
  • It can often mask errors that would otherwise point at the cause of problems.
    – PolyGeo
    Jun 9, 2017 at 12:36

1 Answer 1

2

I think this is explained in the Add Join help where it says:

  • The join lasts only for the duration of the session. To persist the join for use in another session, save the layer to a layer file using the Save Layer To File tool. This only applies to layers; table views cannot be saved in this manner.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.