"Everything is related to everything else, but near things are more related than distant things"
Creating Classified (graduate color) Maps using Python
This script generates graduate color maps with 5 or more classes using geometrical intervals
# Ehsan Momeni (https://ehsanmomeni.wixsite.com/emomeni)
# This script generates graduate color maps with 5 classes using geometrical intervals
# The template, USAStates.lyr and USACounties.lyr are mandatory data layers and they must be in the workspace folder
# Set the workspace
InputPath = r"C:\Users\Desktop\Maps\Data" # Workspace folder
DataLayer = "USACounties.lyr" # The layer needs to be mapped (classified)
UsaStates = "USAStates.lyr" # The borderlines for states
Template = "Template.mxd" # Required template to generate different frames
NumberOfClasses = 5 # Number of classes (if more than 5 you need to edit the template and adjust the covering text box)
JpgResolution = 300 # Resolution of output jpg file
import arcpy
# Setting the environment and inputs
DataLayerPath = InputPath + "\\" + DataLayer
UsaStates = InputPath + "\\" + UsaStates
mxdPath = InputPath + "\\" + Template
arcpy.env.overwriteOutput = True
arcpy.env.workspace = InputPath
InputDataLayer = arcpy.mapping.Layer(DataLayerPath)
UsaStates = arcpy.mapping.Layer(UsaStates)
# Printing available fields to make the map
fields = arcpy.ListFields(DataLayerPath)
i = 0
for field in fields:
print("field {} is {}".format(i, field.name))
i += 1
# Asking for the required fields to generate the map
rawinput = raw_input("\n\nEnter filed number(s). e.g. 15,25,35 ")
RequiredFields = map(int, rawinput.split(","))
# RequiredFields = [15,25,35]
# Main Loop
for RequiredField in RequiredFields:
for counter in range(2): # just a trick to update legend information
if counter == 0:
print("\nField {0} is in process." . format(fields[RequiredField].name))
mxd = arcpy.mapping.MapDocument(mxdPath)
DataFrames = arcpy.mapping.ListDataFrames(mxd)
for DataFrame in DataFrames:
# Adding layers
arcpy.mapping.AddLayer(DataFrame, InputDataLayer, "TOP")
arcpy.mapping.AddLayer(DataFrame, UsaStates, "TOP")
# Classifying
InputDataLayer.symbology.valueField = fields[RequiredField].name
InputDataLayer.symbology.numClasses = NumberOfClasses
# Creating a title for the map
TextElement = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "TitleText")[0]
InputText = " Title for "+fields[RequiredField].name
TextElement.text = raw_input(InputText)
legend = arcpy.mapping.ListLayoutElements(mxd, "LEGEND_ELEMENT", "Legend")[0]
# Saving the map
arcpy.mapping.ExportToJPEG(mxd, TextElement.text+" - Ehsan Momeni" + ".jpg", resolution=JpgResolution)
# arcpy.mapping.ExportToJPEG(mxd, fields[RequiredField].name + ".jpg", resolution=JpgResolution)
print(' A map for {} is generated'. format(fields[RequiredField].name))
del mxd
# mxd.saveACopy("Python_results.mxd")
print ("--------- END -------------")
