Labels in .lyr file

2089
13
Jump to solution
02-20-2013 09:34 AM
GlenReid
New Contributor II
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
1 Solution

Accepted Solutions
T__WayneWhitley
Frequent Contributor
I can no longer be certain what line 50 refers to - post your entire code again.  (you should no longer have a line 50)

Is your lyr file where you have set your workspace?

I am posting the full code I think you should have by now:
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"  mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd, "")[0]  lyr = arcpy.mapping.ListLayers(mxd, 'accum_xy_layer', df)[0] arcpy.ApplySymbologyFromLayer_management(lyr, symbology_layer) lyr.showLabels = True  arcpy.RefreshActiveView()



EDIT:  It could be you cannot use ApplySymbologyFromLayer to update labeling - this may be more about what you want, using labelClasses, as posted here by Craig McDade:

http://forums.arcgis.com/threads/78032-Attempting-to-use-showLabels-script-within-a-model...#3


-Wayne

View solution in original post

0 Kudos
13 Replies
by Anonymous User
Not applicable
I think you should just need to add this after you apply the symbology:

lyr.showLabels = True
0 Kudos
GlenReid
New Contributor II
Caleb,

Thanks for the reply.  I had already tried that with this result

Runtime error
Traceback (most recent call last):
  File "<string>", line 47, in <module>
AttributeError: 'str' object has no attribute 'showLabels'

I could try to create a feature class, but thought that shouldn't be necessary.

Thanks,
Glen
0 Kudos
by Anonymous User
Not applicable
Caleb,

Thanks for the reply.  I had already tried that with this result

Runtime error
Traceback (most recent call last):
  File "<string>", line 47, in <module>
AttributeError: 'str' object has no attribute 'showLabels'

I could try to create a feature class, but thought that shouldn't be necessary.

Thanks,
Glen


Ahh, I see the problem here.  You will need to create a layer object before you can change the layer's properties.  Maybe try something like this:

for lyr in arcpy.mapping.ListLayers('accum_xy_layer')[0]:
    lyr.showLabels = True
0 Kudos
T__WayneWhitley
Frequent Contributor
lyr you have set to text, so that's why you get the error.

You used the lyr string to write your event layer - so you need a handle on the event layer object...

Caleb?  Ahhh, I see you posted again - yes, that should do it!  ...he's going to need the code snippet to get at the CURRENT mxd object (and dataframe likely) as well.  After re-reading his post, looks like Glen may want to 'overwrite' his layer file with the label settings from the temp layer in the map.  Better confirm that.


Enjoy,
Wayne
0 Kudos
GlenReid
New Contributor II
Thanks for the replies.

I have added the ListLayers code, so it now looks like:

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 lyr in arcpy.mapping.ListLayers('accum_xy_layer')[0]:
    lyr.showLabels = True



I still get a runtime error.

Runtime error
Traceback (most recent call last):
  File "<string>", line 47, in <module>
  File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\utils.py", line 181, in fn_
    return fn(*args, **kw)
  File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\mapping.py", line 1500, in ListLayers
    result = mixins.MapDocumentMixin(map_document_or_layer).listLayers(wildcard, data_frame)
  File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\arcobjects\mixins.py", line 823, in listLayers
    layers = self.layers
  File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\arcobjects\mixins.py", line 683, in layers
    for frame in reversed(self.dataFrames):
  File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\arcobjects\mixins.py", line 695, in dataFrames
    return map(convertArcObjectToPythonObject, self.pageLayout.dataFrames)
  File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\arcobjects\mixins.py", line 679, in pageLayout
    return convertArcObjectToPythonObject(self._mxd._arc_object.pageLayout)
AttributeError: 'str' object has no attribute '_arc_object'

I thought that since I previously specified labeling and saved to a .lyr file, that this would be taken care of in the ApplySymbologyFromLayer.

Glen
0 Kudos
by Anonymous User
Not applicable
Sorry, I did not look at the full code.  My example was for if you were accessing an mxd.  So are you just wanting to save this "accum_xy_layer" as a layer file?  It would be nice if the apply symbology worked for labels too.  I haven't been able to find anything that shows how to copy the label properties from another layer file.  There is a way to read label classes but I believe that is only for layers inside an mxd.  Hopefully someone else knows how to do this?
0 Kudos
T__WayneWhitley
Frequent Contributor
Yeah, forget please what I said earlier about writing back to lyr file...

Evidently the same problem or 'disconnect' exists, meaning you do not have a handle to the layer...

The layer is in the map, I mean your event layer has been added?

If so try adding this (and I included Caleb's part of it) - I think that will correctly make the association through your currently operating mxd, correct me if I'm wrong please Caleb (I'm not sure):
# 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


You may have to refresh the map yourself...

Sorry, forgot that.

Enjoy,
Wayne
0 Kudos
GlenReid
New Contributor II
The event layer does get added to the mxd, but only because my GP options are set so.

I added the code and get this 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
0 Kudos
T__WayneWhitley
Frequent Contributor
I can no longer be certain what line 50 refers to - post your entire code again.  (you should no longer have a line 50)

Is your lyr file where you have set your workspace?

I am posting the full code I think you should have by now:
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"  mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd, "")[0]  lyr = arcpy.mapping.ListLayers(mxd, 'accum_xy_layer', df)[0] arcpy.ApplySymbologyFromLayer_management(lyr, symbology_layer) lyr.showLabels = True  arcpy.RefreshActiveView()



EDIT:  It could be you cannot use ApplySymbologyFromLayer to update labeling - this may be more about what you want, using labelClasses, as posted here by Craig McDade:

http://forums.arcgis.com/threads/78032-Attempting-to-use-showLabels-script-within-a-model...#3


-Wayne
0 Kudos