3

I am trying to automate a map I create every so often. The map flags counties based on certain commodities, and color codes them based on price changes. I am able to get each layer created, but would like to reorder the commodities so the "red" commodities are on top, orange below that, low risk below that, and then move the State layer to the top.

My code is below - the error result I get is:

Assertion Error: Did not find move layer
arcpy.env.overwriteOutput = True
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]    

arcpy.AddJoin_management("County_Projected_AEAC", "ID", "C:/Users/SS/Desktop/ArcGIS Maps/MTW/Spring_2020.xlsx/Spring_2020_MTW$", "modified_FIPS", "KEEP_ALL")    
arcpy.Select_analysis("County_Projected_AEAC", "C:/Users/SS/Desktop/ArcGIS Maps/MTW/Beef_Cattle_Python.shp", '"Spring_2020_MTW$.Flag_Beef_Cattle" = 1')
arcpy.Select_analysis("County_Projected_AEAC", "C:/Users/SS/Desktop/ArcGIS Maps/MTW/Feed_Cattle_Python.shp", '"Spring_2020_MTW$.Flag_Feed_Cattle" = 1')
arcpy.Select_analysis("County_Projected_AEAC", "C:/Users/SS/Desktop/ArcGIS Maps/MTW/Corn_Python.shp", '"Spring_2020_MTW$.Flag_Corn" = 1')
arcpy.Select_analysis("County_Projected_AEAC", "C:/Users/SS/Desktop/ArcGIS Maps/MTW/Cotton_Python.shp", '"Spring_2020_MTW$.Flag_Cotton" = 1')
arcpy.Select_analysis("County_Projected_AEAC", "C:/Users/SS/Desktop/ArcGIS Maps/MTW/Dairy_Python.shp", '"Spring_2020_MTW$.Flag_Dairy" = 1')
arcpy.Select_analysis("County_Projected_AEAC", "C:/Users/SS/Desktop/ArcGIS Maps/MTW/Hogs_Python.shp", '"Spring_2020_MTW$.Flag_Hogs" = 1')
arcpy.Select_analysis("County_Projected_AEAC", "C:/Users/SS/Desktop/ArcGIS Maps/MTW/Poultry_Python.shp", '"Spring_2020_MTW$.Flag_Poultry" = 1')
arcpy.Select_analysis("County_Projected_AEAC", "C:/Users/SS/Desktop/ArcGIS Maps/MTW/Soybeans_Python.shp", '"Spring_2020_MTW$.Flag_Soybeans" = 1')
arcpy.Select_analysis("County_Projected_AEAC", "C:/Users/SS/Desktop/ArcGIS Maps/MTW/Wheat_Python.shp", '"Spring_2020_MTW$.Flag_Wheat" = 1')

States = arcpy.mapping.ListLayers(mxd)[-1]
County = arcpy.mapping.ListLayers(mxd)[-2]
Beef_Cattle = arcpy.mapping.ListLayers(mxd)[-3]
Feed_Cattle = arcpy.mapping.ListLayers(mxd)[-4]
Corn = arcpy.mapping.ListLayers(mxd)[-5]
Cotton = arcpy.mapping.ListLayers(mxd)[-6]
Dairy = arcpy.mapping.ListLayers(mxd)[-7]
Hogs = arcpy.mapping.ListLayers(mxd)[-8]
Poultry = arcpy.mapping.ListLayers(mxd)[-9]
Soybeans = arcpy.mapping.ListLayers(mxd)[-10]
Wheat = arcpy.mapping.ListLayers(mxd)[-11]

low_risk_commodities = [Poultry]
for x in low_risk_commodities:
    x.visible = False
    arcpy.RefreshTOC()

orange_commodities = [Wheat, Hogs, Dairy, Cotton, Corn, Feed_Cattle, Beef_Cattle]

for x in orange_commodities:
    arcpy.mapping.MoveLayer(df, arcpy.mapping.ListLayers(mxd)[0], x)

The code is correctly turning off the poultry layer but will not move the layers due to the error. I know it is likely in how I'm defining the layers, but I'm not sure how to properly do this.

When I type in Wheat, it gives me a little purplish icon, indicating it is maybe not a layer but is maybe a shapefile?

  • 1
    I think you should reduce your code snippet to getting it working with 3 layers. Also, pleases remove the ellipses at the beginning of many of your lines of code. Potential answerers are far more likely to test code that they can simply copy/paste. – PolyGeo Mar 31 at 23:21
  • 1
    Did you exclude Soybeans intentionally from orange_commodities list? – Kadir Şahbaz Apr 1 at 2:14
  • 1
    How many layers are there in TOC? It would be helpful if you added TOC structure after running Select_analysis lines. – Kadir Şahbaz Apr 1 at 2:28
4

I guess, Wheat layer is the first layer. You get that error, because reference layer (arcpy.mapping.ListLayers(mxd)[0]) and move layer (x) are the same (Wheat) at first running of MoveLayer method.

Change for loop in following way:

for x in orange_commodities:
    ref_layer = arcpy.mapping.ListLayers(mxd)[0]
    if ref_layer != x:
        arcpy.mapping.MoveLayer(df, arcpy.mapping.ListLayers(mxd)[0], x)
| improve this answer | |
  • I'll try shifting the order and let you know. If I still have issues, I'll post the TOC structure after the select analysis lines like you mentioned. – Shawn Schreier Apr 1 at 16:17
  • The TOC Structure is as follows: Wheat_Python Soybeans_Python Poultry_Python Hogs_Python Dairy_Python Cotton_Python Corn_Python Feed_Cattle_Python Beef_Cattle_Python County_Projected_AEAC States_Projected – Shawn Schreier Apr 2 at 19:55

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.