applying label expressions, a clunky work around

2736
8
10-11-2011 07:25 AM
FrankVignati
Occasional Contributor II
is there a better way to apply a label exprssion to a layer in arcmap besides the workaround posted below? there is a layer in arcmap that needs to be symbolized and labeled in the same manner as a layer file and doing an updateLayer TRUE won't do it, so the updateLayer is used with FALSE
then the source and name of the layer gets reset
the layer file was created from a template of the edit feature class in the MXD so that all fields and spatial attributes match

#### Description: Changes the symbology and labels fot the footprint layer

## Import standard modules
import sys, os

## Set the necessary product code
import arcinfo

## Import arcpy module and environment module
import arcpy
import arcpy.mapping
from arcpy import env

## Get the selection layer
SetLayer = arcpy.GetParameterAsText(0)


## Sets the MXD file
IMXD = arcpy.mapping.MapDocument("CURRENT")

## Sets the Dataframe
DF = arcpy.mapping.ListDataFrames(IMXD, "Layers")[0]

## Set a Layer
ILayer = arcpy.mapping.ListLayers(IMXD, SetLayer, DF)[0]


SymbolLayer = arcpy.mapping.Layer(r"C:\WorkSpace\Bldg_Footprints\HollowFootprints.lyr")

KeepName = ILayer.name
KeepDataset = ILayer.datasetName
KeepSource = ILayer.workspacePath

arcpy.mapping.UpdateLayer(DF, ILayer, SymbolLayer, False)

ILayer = arcpy.mapping.ListLayers(IMXD, "Footprints_Layer", DF)[0]

ILayer.replaceDataSource(KeepSource, "FILEGDB_WORKSPACE", KeepDataset)
ILayer.name = KeepName


arcpy.RefreshTOC()
arcpy.RefreshActiveView()
del IMXD, ILayer
Tags (2)
0 Kudos
8 Replies
JeffBarrette
Esri Regular Contributor
When the {symbology_only} property on UpdateLayer is set to False, it pulls across ALL layer properties, not just symbology.

Have you tried working with the LabelClass object.  It allows you to set the expression directly.

http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/LabelClass/00s30000002t000000/

Jeff
0 Kudos
FrankVignati
Occasional Contributor II
thank you, the LabelClass object does allow me to get close to setting the label for a layer as opposed to the work around
however there is still a problem

the layer when originaly added to the MXD is labeling on a single attribute by default and when the following script is run the label expression is not valid since the label expression is not set to advaced, is there a way to set the expression to advanced through arcpy?

this is the script that attempts to set the expression:

## Import standard modules
import sys, os
## Set the necessary product code
import arcinfo
## Import arcpy module and environment module
import arcpy
import arcpy.mapping
from arcpy import env

## Overwrite pre-existing files
arcpy.env.overwriteOutput = True
## Sets the MXD file
IMXD = arcpy.mapping.MapDocument("CURRENT")
## Sets the Dataframe
DF = arcpy.mapping.ListDataFrames(IMXD, "Layers")[0]
## Set a Layer
ILayer = arcpy.mapping.ListLayers(IMXD, "Footprints_0592", DF)[0]

NewX = "Function FindLabel ( [BLDG_CODE] , [ALTKEY]  )" + "\n" + "If  [BLDG_CODE] <> \"Base Residential Area (BAS)\" Then" + "\n" + "  FindLabel = [BLDG_CODE]" + "\n" + "Else" + "\n" + "  FindLabel =  [ALTKEY] &  vbCrLf & [BLDG_CODE]" + "\n" + "End If" + "\n" + "End Function"

for Label in ILayer.labelClasses:
  Label.expression = NewX


the NewX variable comes out like and if the expression is saved it stays this way:
Function FindLabel ( [BLDG_CODE] , [ALTKEY]  )
If  [BLDG_CODE] <> "Base Residential Area (BAS)" Then
  FindLabel = [BLDG_CODE]
Else
  FindLabel =  [ALTKEY] &  vbCrLf & [BLDG_CODE]
End If
End Function

but in the properties for the layer the expression is still set to simple so it will not work
0 Kudos
JeffBarrette
Esri Regular Contributor
No there is not.  This is a property that would need to be authored in the map document or a layer file ahead of time.

If that is not possible, we'd like to learn more about your scenario for possible consideration to future releases.

Jeff
0 Kudos
FrankVignati
Occasional Contributor II
No there is not.  This is a property that would need to be authored in the map document or a layer file ahead of time.

If that is not possible, we'd like to learn more about your scenario for possible consideration to future releases.

Jeff


what is happening is a python script runs creating feature classes from dxf files and adds them to mxds as layers to be edited and the layers are symbolized and labeled in a manner to make it easier for the editors to save time the script does it all and the editor need only open the mxd with the data already to go

besides for purposes like above, it would be helpful for automating output if the "Advanced" on the label could be set through arcpy for folks adding layers and making maps
0 Kudos
JohnHayes2
New Contributor III
I am running into this exact problem.  I've made a post on Esri's Idea page requesting this functionality as well as exposing all of the settings in the label manager through ArcPy. 

http://ideas.arcgis.com/ideaView?id=08730000000bsHbAAI
0 Kudos
JeffBarrette
Esri Regular Contributor
Thanks for posting this to the ideas page.  We'll take this into consideration for our next release.  Note it is too late for the 10.1 timeframe.

Jeff
0 Kudos
ChrisFox3
Occasional Contributor III
Hi Frank,

Is the schema of the output shapefiles similar each time? If so it seems like you could author the layer files for the shapefiles once and in the script update the datasource of the layer through code and add it to the map document.
0 Kudos
deleted-user-MS0lE1F_0-sy
Occasional Contributor
Has anyone come up with a solution to this?  I just completed a 10 page script and this function is the only thing I need to complete my tool.
0 Kudos