Tuesday, 6 February 2018

arcpy.AddJoin_management() results not writing to file in ArcMap?


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

Answer



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.




No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...