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

97
3
08-28-2020 08:44 AM
New Contributor II

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

Reply
0 Kudos
3 Replies
MVP Esteemed Contributor

How about you use the syntax highlighter so we can read your script easier?

can't wait to retire....
Reply
0 Kudos
New Contributor II
 


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






‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
Reply
0 Kudos
MVP Regular Contributor

I'm not completely sure of your data structure, but guessing from the code:

Get a list of all your fields in USCounty500k (referencing the datasource not the layer - you could also get the datasource from the layer object if that's an issue) example courtesy of esri help:

fields = arcpy.ListFields("c:/data/municipal.gdb/hospitals")


 indent your current code from line 19:

for field in fields:

   your remaining code

   sym.renderer.classificationField = field.name

outpath_folder = r'C:/Users/ggrodzinsky/Documents/ArcGIS/Projects/MyProject8'

p.saveACopy(os.path.join(outpath_folder, field.name))

Reply
0 Kudos