Select to view content in your preferred language

Labeling works in 10, but not 10.1

2521
9
01-23-2013 08:33 AM
by Anonymous User
Not applicable
Original User: SStrand

I have a python script which at one point adds a new set of features, symbolizes, and labels them. In 10 everything works correctly, but when I run the script in 10.1, the labels are lost even though the symbology still applies correctly.

Here is the section of the code:

#select wells by buffer and create selection layer
arcpy.SelectLayerByLocation_management(welltypes, "HAVE_THEIR_CENTER_IN", Bufferresult)
arcpy.AddMessage("Selected Wells within Buffer Area")
arcpy.CopyFeatures_management(welltypes, wellresult)
layer2 = arcpy.mapping.Layer(wellresult)
arcpy.mapping.AddLayer(df, layer2)
updateLayer2 = arcpy.mapping.ListLayers(mxd, wellresult, df)[0]
arcpy.ApplySymbologyFromLayer_management(updateLayer2, wellstyle)
if layer2.supports("LABELCLASSES"):
    for lblclass in layer2.labelClasses:
        lblclass.showClassLabels = True
        lblclass.expression = '"%s" & [WELLNM] & "%s"' % ("<CLR red='36' green='93' blue='206'>", "</CLR>")
layer2.showLabels = True
arcpy.RefreshActiveView()
arcpy.RefreshTOC()
arcpy.AddMessage("Added Selected Wells to Map")


I've been researching but cannot find anything that would help sort this out. Has anyone else had a similar issue?
0 Kudos
9 Replies
by Anonymous User
Not applicable
Original User: mzcoyle

Try switching the syntax to python. This is the default now.

lblclass.expression = '"%s" + [WELLNM] + "%s"' % ("<CLR red='36' green='93' blue='206'>", "</CLR>")
0 Kudos
by Anonymous User
Not applicable
Original User: SStrand

Try switching the syntax to python. This is the default now.


Thanks for the input mzcoyle, but this didn't fix the problem. No labels are showing.
0 Kudos
MathewCoyle
Honored Contributor
Thanks for the input mzcoyle, but this didn't fix the problem. No labels are showing.


Are you on SP1? This method works fine for me.

Edit: Oh your issue may be you are referencing the layer file not the layer object in the mxd. I believe the variable you want to reference is updateLayer2 not layer2.
0 Kudos
by Anonymous User
Not applicable
Original User: SStrand

Are you on SP1? This method works fine for me.

Edit: Oh your issue may be you are referencing the layer file not the layer object in the mxd. I believe the variable you want to reference is updateLayer2 not layer2.


Yeah, I am on SP1. I tried switching in updateLayer2 and it gives me an error for the layer not being defined. This happened if I replaced all label related items with updatelayer2, or if I just replaced the final "layer2.showLabels = True" with updateLayer2.
0 Kudos
MathewCoyle
Honored Contributor
There is probably something else in your code you haven't posted that is the problem.

What exactly is the issue you were getting when it ran with no errors? Is it just you have to turn the labels on manually or something else?
0 Kudos
by Anonymous User
Not applicable
Original User: SStrand

There is probably something else in your code you haven't posted that is the problem.

What exactly is the issue you were getting when it ran with no errors? Is it just you have to turn the labels on manually or something else?


Well the labels are supposed to turn on automatically as part of the larger code which takes a basic map template, geocodes an input address, buffers it, brings in wells within the buffer, labels the wells, and then exports the PDF of the map. There is more to the code and what happens but completely unrelated to anything arcpy.mapping related.

I know the code isn't perfect, as it has some spots in it where things repeat themselves and could be cleaned up, but as far as I can tell, there is nothing going on which would result with the labels working in 10 but not 10.1.

Here is the start to the point of labeling:

# Import the arcpy and os modules.
import arcpy
import os
from arcpy import env
from subprocess import Popen, PIPE

#declare variables and set current environments
env.workspace = "I:\InfoRequest\InfoRequest.gdb"
welltypes = arcpy.GetParameterAsText(0)
inAddress = arcpy.GetParameterAsText(1)
inCity = arcpy.GetParameterAsText(2)
inState = arcpy.GetParameterAsText(3)
BufferDist = arcpy.GetParameterAsText(4)
InfoRequestNum = arcpy.GetParameterAsText(5)
ClientNam = arcpy.GetParameterAsText(6)
Purpose = arcpy.GetParameterAsText(7)
SiteNam = arcpy.GetParameterAsText(8)
includedwells = arcpy.GetParameterAsText(9)
addresstable = "AddressTable"
addresslocator = "OCAddresses_CreateAddressLoc"
geocoderesult = "AddressLocation"
TableWells = "selectedWells"
wellcasingids = "wellcasingids"
wellstyle = arcpy.mapping.Layer(r"K:\\SDE_System\\layers\\AGS10_Layers\\Wells -- All with Detailed WRMS Attributes.lyr")
Bufferresult = ("BufferRequest" + "_" + InfoRequestNum)
wellresult = ("SelectedWells" + "_" + InfoRequestNum)
export_table = ("I:\InfoRequest\InfoRequestTables\InfoRequest_" + InfoRequestNum + "_SelectedRecords.txt")
PDFPathName = ("I:\InfoRequest\InfoRequestPDF\InfoRequest_" + InfoRequestNum + ".pdf")

mxd = arcpy.mapping.MapDocument('CURRENT')
df = arcpy.mapping.ListDataFrames(mxd,"Layers") [0]

#Change from layout view to data view if it's not already in data view
arcpy.mapping.MapDocument("current").activeView = "Layers"

#Workspace clean up - if the featurclasses exist, delete them.
if arcpy.Exists(geocoderesult):
  arcpy.Delete_management(geocoderesult)
if arcpy.Exists(Bufferresult):
  arcpy.Delete_management(Bufferresult)
if arcpy.Exists(TableWells):
  arcpy.Delete_management(TableWells)
if arcpy.Exists(geocoderesult):
  arcpy.Delete_management(geocoderesult)
if arcpy.Exists(Bufferresult):
  arcpy.Delete_management(Bufferresult)
if arcpy.Exists(export_table):
  arcpy.Delete_management(export_table)
if arcpy.Exists(wellresult):
  arcpy.Delete_management(wellresult)
arcpy.AddMessage("Workspace Cleanup Complete")

#calculate table fields for geocoding from user input
cursor = arcpy.UpdateCursor(addresstable)
for row in cursor:
  row.Address = inAddress
  cursor.updateRow(row)
  del cursor

cursor = arcpy.UpdateCursor(addresstable)
for row in cursor:
    row.CITY = inCity
    cursor.updateRow(row)
    del cursor

cursor = arcpy.UpdateCursor(addresstable)
for row in cursor:
    row.STATE = inState
    cursor.updateRow(row)
    del cursor

#geocode address
arcpy.GeocodeAddresses_geocoding(addresstable, addresslocator, "Address Address VISIBLE NONE; CITY CITY VISIBLE NONE; STATE STATE VISIBLE NONE ", geocoderesult)
arcpy.AddMessage("Address Located")

#buffer address
arcpy.Buffer_analysis(geocoderesult, Bufferresult, BufferDist)
arcpy.AddMessage("Buffer Complete")

#Add Address Point to the map
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
layer = arcpy.mapping.Layer(geocoderesult)
arcpy.mapping.AddLayer(df, layer)
updateLayer = arcpy.mapping.ListLayers(mxd, "AddressLocation", df)[0]
sourceLayer = arcpy.mapping.Layer(r"I:\InfoRequest\AddressLocation.lyr")
arcpy.mapping.UpdateLayer(df, updateLayer, sourceLayer, False)
arcpy.RefreshActiveView()
arcpy.RefreshTOC()
arcpy.AddMessage("Added Address Location to Map")

#Add Buffer Polygon to the map
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
layer = arcpy.mapping.Layer(Bufferresult)
arcpy.mapping.AddLayer(df, layer)
updateLayer = arcpy.mapping.ListLayers(mxd, Bufferresult, df)[0]
sourceLayer = arcpy.mapping.Layer(r"I:\InfoRequest\Buffer.lyr")
arcpy.ApplySymbologyFromLayer_management(updateLayer, sourceLayer)
arcpy.RefreshActiveView()
arcpy.RefreshTOC()
arcpy.AddMessage("Added Buffer Polygon to Map")

#select wells by buffer and create selection layer
arcpy.SelectLayerByLocation_management(welltypes, "HAVE_THEIR_CENTER_IN", Bufferresult)
arcpy.AddMessage("Selected Wells within Buffer Area")
arcpy.CopyFeatures_management(welltypes, wellresult)
layer2 = arcpy.mapping.Layer(wellresult)
arcpy.mapping.AddLayer(df, layer2)
updateLayer2 = arcpy.mapping.ListLayers(mxd, wellresult, df)[0]
arcpy.ApplySymbologyFromLayer_management(updateLayer2, wellstyle)
if layer2.supports("LABELCLASSES"):
    for lblclass in updatelayer2.labelClasses:
        lblclass.showClassLabels = True
        lblclass.expression = '"%s" + [WELLNM] + "%s"' % ("<CLR red='36' green='93' blue='206'>", "</CLR>")
layer2.showLabels = True
arcpy.RefreshActiveView()
arcpy.RefreshTOC()
arcpy.AddMessage("Added Selected Wells to Map")
0 Kudos
JimCousins
MVP Alum
Steve,
I was tinkering with your label expression and could not get it to work until I modified it:
"%s %s %s"  % ("<CLR red='36' green='93' blue='206'>", [WELLNM], "</CLR>")
Regards,
Jim
0 Kudos
by Anonymous User
Not applicable
Original User: SStrand

Steve, 
I was tinkering with your label expression and could not get it to work until I modified it: 
"%s %s %s" % ("<CLR red='36' green='93' blue='206'>", [WELLNM], "</CLR>") 
Regards, 
Jim


Hey Jim,

Thanks for trying! Unfortunately this brought an error back as well:

lblclass.expression = "%s %s %s" % ("<CLR red='36' green='93' blue='206'>", [WELLNM], "</CLR>")
NameError: name 'WELLNM' is not defined


So do I need to now define the WELLNM field which is the field being used for the label?
0 Kudos
by Anonymous User
Not applicable
Original User: SStrand

Figured out the issue. It was that the "updateLayer2" needed to be changed to "layer2." Why this worked in 10, but not 10.1 is still a mystery to me.

Thanks to those who provided help!
0 Kudos