Select to view content in your preferred language

Labels in .lyr file

3011
13
Jump to solution
02-20-2013 09:34 AM
GlenReid
Deactivated User
I'm converting an older GP task made using MB to Python.

In the original, data was saved to disk (.shp) and rendered with a layer file (.lyr).  Labels were displayed in the output.  I've updated the .lyr file to work with an in_memory layer (longer field names).  Here is the code:

import arcpy  arcpy.env.workspace = r"C:\gis-ags\src\gp"  # temporary date_range = "\"OBDATE\" BETWEEN date '2008-01-01' AND date '2008-01-03'"  # Script arguments #date_range = arcpy.GetParameterAsText(0) if date_range == '#' or not date_range:     date_range = "\"OBDATE\" BETWEEN date '2008-01-01' AND date '2008-01-03'"  # Local variables accum_table = "in_memory\\accumulation_table" stat_table = "in_memory\\stat_table" db_table = r"C:\gis-ags\src\gp\gisselect_cdo.sde\GIS.DAILYSNOW_GP_DATA"  # Table Select arcpy.TableSelect_analysis(db_table, accum_table, date_range)  # Summary Statistics arcpy.Statistics_analysis(accum_table, stat_table, "NAME FIRST;STATE FIRST;SNOWFALL SUM;LATITUDE FIRST;LONGITUDE FIRST", "COBAN")  # Spatial Reference sr = arcpy.SpatialReference() sr.factoryCode = 4269 sr.create()  # Make XY Event Layer lyr = "accum_xy_layer" arcpy.MakeXYEventLayer_management(stat_table, "FIRST_LONGITUDE", "FIRST_LATITUDE", lyr, sr)  # Symbology symbology_layer = "accumulation.lyr" arcpy.ApplySymbologyFromLayer_management(lyr, symbology_layer) 


For some reason, the labels are not getting displayed when I run in the ArcMap Python Window.

Am I missing something?

Thanks,
Glen
Tags (2)
0 Kudos
13 Replies
GlenReid
Deactivated User
Yes, the accumulation.lyr file is in the workspace.  The code:

import arcpy

arcpy.env.workspace = r"C:\gis-ags\src\gp"

# temporary
date_range = "\"OBDATE\" BETWEEN date '2008-01-01' AND date '2008-01-03'"

# Script arguments
#date_range = arcpy.GetParameterAsText(0)
if date_range == '#' or not date_range:
    date_range = "\"OBDATE\" BETWEEN date '2008-01-01' AND date '2008-01-03'"

# Local variables
accum_table = "in_memory\\accumulation_table"
stat_table = "in_memory\\stat_table"
db_table = r"C:\gis-ags\src\gp\gisselect_cdo.sde\GIS.DAILYSNOW_GP_DATA"

# Table Select
arcpy.TableSelect_analysis(db_table, accum_table, date_range)

# Summary Statistics
arcpy.Statistics_analysis(accum_table, stat_table, "NAME FIRST;STATE FIRST;SNOWFALL SUM;LATITUDE FIRST;LONGITUDE FIRST", "COBAN")

# Spatial Reference
sr = arcpy.SpatialReference()
sr.factoryCode = 4269
sr.create()

# Make XY Event Layer
lyr = "accum_xy_layer"
arcpy.MakeXYEventLayer_management(stat_table, "FIRST_LONGITUDE", "FIRST_LATITUDE", lyr, sr)

# Symbology
symbology_layer = "accumulation.lyr"
arcpy.ApplySymbologyFromLayer_management(lyr, symbology_layer)

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

for lyr in arcpy.mapping.ListLayers(mxd, 'accum_xy_layer', df)[0]:
    lyr.showLabels = True

arcpy.RefreshActiveView()



The error:

Runtime error
Traceback (most recent call last):
  File "<string>", line 50, in <module>
  File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\arcobjects\mixins.py", line 361, in __iter__
    self.layers
AttributeError: LayerObject: Get attribute layers does not exist

I'll take a look at the referenced thread.

Thanks,
Glen
0 Kudos
GlenReid
Deactivated User
The line that was throwing the error:

for lyr in arcpy.mapping.ListLayers(mxd, 'accum_xy_layer', df)[0]:


Not sure why this is, I'll look into it.

Using Craig's code from the referenced thread fixed the error and displayed the labels:

import arcpy

arcpy.env.workspace = r"C:\gis-ags\src\gp"

# temporary
date_range = "\"OBDATE\" BETWEEN date '2008-01-01' AND date '2008-01-03'"

# Script arguments
#date_range = arcpy.GetParameterAsText(0)
if date_range == '#' or not date_range:
    date_range = "\"OBDATE\" BETWEEN date '2008-01-01' AND date '2008-01-03'"

# Local variables
accum_table = "in_memory\\accumulation_table"
stat_table = "in_memory\\stat_table"
db_table = r"C:\gis-ags\src\gp\gisselect_cdo.sde\GIS.DAILYSNOW_GP_DATA"

# Table Select
arcpy.TableSelect_analysis(db_table, accum_table, date_range)

# Summary Statistics
arcpy.Statistics_analysis(accum_table, stat_table, "NAME FIRST;STATE FIRST;SNOWFALL SUM;LATITUDE FIRST;LONGITUDE FIRST", "COBAN")

# Spatial Reference
sr = arcpy.SpatialReference()
sr.factoryCode = 4269
sr.create()

# Make XY Event Layer
lyr = "accum_xy_layer"
arcpy.MakeXYEventLayer_management(stat_table, "FIRST_LONGITUDE", "FIRST_LATITUDE", lyr, sr)

# Symbology
symbology_layer = "accumulation.lyr"
arcpy.ApplySymbologyFromLayer_management(lyr, symbology_layer)

mxd = arcpy.mapping.MapDocument("CURRENT")
lyr = arcpy.mapping.ListLayers(mxd, "")[0]

if lyr.supports("LABELCLASSES"):
    for lblclass in lyr.labelClasses:
        lblclass.showClassLabels = True
    lblclass.expression = "[SUM_SNOWFALL]"
    lyr.showLabels = True
    arcpy.RefreshActiveView()



Thanks for all the help!

Glen
0 Kudos
LucasDanzinger
Esri Frequent Contributor
The line that was throwing the error:

for lyr in arcpy.mapping.ListLayers(mxd, 'accum_xy_layer', df)[0]:


Not sure why this is, I'll look into it.


Glen,

I'm guessing that this probably doesn't work because you have two different things going on. When you select an item in the list by using the index [0], you are grabbing that one item out of the list. So what your code is saying is that you want to loop through the list, but also only grab index [0] in the list. Instead, I would do either one of the following:

for lyr in arcpy.mapping.ListLayers(mxd, 'accum_xy_layer', df):


lyr = arcpy.mapping.ListLayers(mxd, 'accum_xy_layer', df)[0]:
GlenReid
Deactivated User
Lucus,

This does work, getting the layer and turning the default labels on:

for lyr in arcpy.mapping.ListLayers(mxd, 'accum_xy_layer', df):
    lyr.showLabels = True

arcpy.RefreshActiveView()


Thanks for the hint.

Glen
0 Kudos