Skip to main content
4 of 5
added 2 characters in body; edited tags; edited title
PolyGeo
  • 65.4k
  • 29
  • 114
  • 345

Updating layer in corresponding data frame in single MXD in ArcPy

I have a shapefile and a layer file. In the shapefile, I am updating the last column using a .dbf file, which has numeric values. So, the last column of the shapefile will be updated five times and I have five data frames in one MXD file. So, whenever the shapefile gets updated, it has to go to a new data frame.

Here is my code:

import arcpy
arcpy.env.workspace = 'C:\\Users\\Jaggu\\Documents\\ArcGIS\\Default.gdb'
mxd = arcpy.mapping.MapDocument(r"D:\Project\CROPWAT\WRR\DistrictScaleNew1\Block_scale\ArcMap\IWR2.mxd")
lyrfile = r'D:\Project\CROPWAT\WRR\DistrictScaleNew1\Block_scale\ArcMap\AP_APWRIMS7.lyr'
lyrfile1 = arcpy.mapping.Layer(lyrfile) #Converting to a layer file in the current env

#update field
updateFld = "IWR"
#update fc key field
IdFld = "dtname"

#join feature class
joinFc = r"D:\Project\CROPWAT\WRR\DistrictScaleNew1\Block_scale\ArcMap\IWR.dbf"
#join value field to be transferred
#joinValFld = dbffields[i]
#join key field
joinIdFld = "district"

#create dictionary
#Key: join field
#Value: field with value to be transferred

for index, item in enumerate(arcpy.mapping.ListDataFrames(mxd)):
    
valueDists = []
for i in range(len(dfs)):
    arcpy.mapping.AddLayer(dfs[i],lyrfile1,"BOTTOM")
    joinValFld = dbffields[i]
    valueDist = {}
    valueDist = dict ([(key, val) for key, val in arcpy.da.SearchCursor(joinFc, [joinIdFld, joinValFld])])
    valueDists.append(valueDist)
currentlayers = []
#update feature class
for i in range(len(valueDists)):
    #currentlayer = arcpy.mapping.ListLayers(dfs[i])[0]
    with arcpy.da.UpdateCursor (shpfile, [updateFld, IdFld]) as cursor:
        valueDist = valueDists[i]
        for update, key in cursor:
            row = (valueDist [key], key)
            print row
            cursor.updateRow (row)
        arcpy.mapping.UpdateLayer(dfs[i], shpfile, currentlayer, True)
    currentlayer = arcpy.mapping.ListLayers(dfs[i])[0]
    currentlayers.append(currentlayer)
mxd.saveACopy('D:\\Project\\CROPWAT\\WRR\\DistrictScaleNew1\\Block_scale\\ArcMap\\AP_APWRIMS1.mxd')

The problem that I am facing is that the code is running without any errors but all five data frames were updated with the last updated shapefile data. Plot is looking like this: enter image description here

But I want five different choropleths. I have already checked the last column of shapefile and it gets updated but it is not showing in ArcMap.

What is wrong?

G.S. J
  • 323
  • 1
  • 6