AnsweredAssumed Answered

How to make code loop instead of running with specific files one at a time

Question asked by Gil.Grodzinsky_GAEPD on Aug 28, 2020
Latest reply on Aug 28, 2020 by Davepike101@hotmail.com

Hi Everyone: I have this code that works:

 

if not hasattr(sys, 'argv'): #only use if system has issue with relpath command
sys.argv = ['']

import arcpy, os, sys, locale

relpath = os.path.dirname(sys.argv[0])
p = arcpy.mp.ArcGISProject(relpath + r'C:/Users/ggrodzinsky/Documents/ArcGIS/Projects/MyProject8/MyProject8.aprx')
m = p.listMaps()[0]
l = m.listLayers("cb_2018_us_county_500k")[0]
sym = l.symbology
if hasattr(sym, 'renderer'):
if sym.renderer.type == 'SimpleRenderer':
sym.updateRenderer('GraduatedColorsRenderer')

l.symbology = sym

sym.renderer.classificationField = 'Sheet4.AWATER'
sym.renderer.breakCount = 7

breakVal = 1000
cv = 0
lw = 1
for brk in sym.renderer.classBreaks:
brk.upperBound = breakVal
brk.label = "\u2264" + str(locale.format("%d", breakVal, grouping=True))
brk.description = "Description " + str(cv)
brk.symbol.color = {'HSV' : [cv, 100, 100, 100]}
brk.symbol.outlineColor = {'HSV' : [240-cv, 100, 100, 100]}
brk.symbol.size = lw
breakVal +=1000
cv += 40
lw += 0.5
l.symbology = sym
p.saveACopy(relpath + r'C:/Users/ggrodzinsky/Documents/ArcGIS/Projects/MyProject8/SavedOutput2.aprx')

 

However, I want to run it so it creates multiple maps in one efficient, condensed code with a loop instead of a lengthy code where within the code I repeat all the above and just change "AWATER" to the next column in my table and "SavedOutput2.aprx" to "SavedOutput3.aprx" to produced a series of symbolized maps. In short, how can I modify this code so the "sym.renderer.classificationField = 'Sheet4.AWATER' can be modified to allow for a loop where "AWATER" is replaced with a variable that with each loop grabs from the next column in the table. Same goes with the line:  p.saveACopy(relpath + r'C:/Users/ggrodzinsky/Documents/ArcGIS/Projects/MyProject8/SavedOutput2.aprx') where instead of "SavedOutput2.aprx" it is "SavedOutput#.aprx" where # is increased by one with each step in the loop, thus making a new file with each mapped column.

 

Thanks!

 

GG

Outcomes