Toggle labels in a map document

I had a request from the Asset Management team to display all the asset management data I had for a particular area, grouped by service category and with labels displaying their unique IDs. My challenge was to turn on the labels for all the layers using a specific expression. However, the ID fields were not named the same in all the layers (again, the joys of working with data received from outside).


#
# @date 17/09/2014
# @author Cindy Williams
#
# Set the label field and switch labels on for
# all layers in a mxd.
#
# For use in the Python window in ArcMap.
#
import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
for lyr in arcpy.mapping.ListLayers(mxd):
if lyr.isFeatureLayer:
f = arcpy.ListFields(lyr, "*GIS*")
if f:
lyr.labelClasses[0].expression = "[" + f[0].name + "]"
lyr.showLabels = True
else:
print lyr.name
lyr.visible = True
arcpy.RefreshActiveView()

On line 19, I set the label expression for the default label class using the correct field name. The square brackets are what ArcGIS is expecting to delimit field names in the label expression dialog. Line 20 turns the labels on, while line 23 turns the layer on.

Convert multiple spreadsheets to geodatabase tables

For years I used the old ArcGIS way of connecting ArcGIS to Excel – saving the tables as dbf files. That was terrible. In 10.2 ESRI added a new conversion toolset for moving back and forth between Excel and ArcGIS.

It is a set of Python scripts using xlrd/xlwt. For a while, I was reading in individual sheets into memory and doing things with them using openpyxl. While that workflow is still valid, most of the time I actually want a gdb table copy of my spreadsheet before I start working with it.

I received a folder containing multiple subfolders with months/years in the folder names. Each subfolder contained multiple spreadsheets with different names. The spreadsheets followed two different field naming conventions.

Since I was not going to be manually processing these dozens of files, I wrote this script to help me.


#
# @author Cindy Williams
# @date 14/11/2014
#
# Takes a folder containing deeds data in spreadsheet
# format and converts it into a GIS table for further processing.
# It must adhere to a specific format.
#
# For use as a script tool in an ArcGIS Toolbox.
#
import arcpy
import os
import sys
# Input variables
fld = arcpy.GetParameterAsText(0)
gdb = arcpy.GetParameterAsText(1)
sheet_name = "Sheet"
for root, dirs, filenames in os.walk(fld):
for f in filenames:
try:
arcpy.AddMessage("Processing " + f)
xls = os.path.join(root, f)
name = os.path.splitext(f)[0]
for i in name:
if i in ["-", " ", "."]:
name = name.replace(i, "_")
tbl = os.path.join(tbl, name)
arcpy.conversion.ExcelToTable(xls, tbl, sheet_name)
arcpy.AddMessage("Processed " + name)
except:
e = sys.exc_info()[0]
arcpy.AddMessage("Error caught: ")
arcpy.AddMessage(e)
arcpy.AddMessage("Processing complete.")

The script is fairly simple. It loops over all the files in the main folder, as well as recursively into the subfolders. Any characters ArcGIS does not like are replaced with underscores, and each sheet is converted to a geodatabase table.

ArcMap Woes Part 1: Random fail in ArcMap works in ArcCatalog

Today I tried to run a script in ArcMap. Nothing fancy, just exporting data driven pages as jpegs in a for loop (basically taken straight from the help docs 2 years ago, and used in endless variations since):


import arcpy
import os
mxd = arcpy.mapping.MapDocument(r"C:\Some\Arb\Folder\Test.mxd")
fld = r"C:\Some\Arb\Folder\jpegs"
ddp = mxd.dataDrivenPages
for i in range(1, ddp.pageCount + 1):
ddp.currentPageId = i
name = os.path.join(fld, ddp.pageRow.getValue(ddp.pageNameField) + ".jpg")
arcpy.mapping.ExportToJPEG(mxd, name)
print("Exported " + name)

Of course, ArcMap crashes. No, not crashes, because then it would pop up the ESRI “Send Error Report” dialog box. It simply terminates. I run the same script in ArcCatalog and it works perfectly. Note that I have run scripts like this many times before in ArcMap without any issues. It’s almost as if ArcMap gets tired of certain things, and refuses to cooperate. I’m pretty sure if I try this script in ArcMap again in a few days time it will be fine.