Why do I get two values for longName??

570
3
Jump to solution
07-15-2012 12:41 PM
AnnaBarendt
Emerging Contributor
Hi,
New at this.  Practicing my Python coding.   In the first ListLayer I get the full longName for a layer.  In the second code I don't.  Any ideas why?

Code:
TOClayers = ListLayers(mxd)
    for layer in TOClayers:
        print "Layer LongName is : " + layer.longName
        print "Layer name is : " + layer.name

    print"\nStart the dataframe loop."
    dataframes = ListDataFrames(mxd, '')
    for frame in dataframes:
        layers = ListLayers(mxd, '', frame)
        i = 1
        for layer in layers:
            print '\nData Frame : ' + frame.name +'\tLayer longName ' + str(i) + ': ' + layer.longName
            print 'Data Frame : ' + frame.name +'\tLayer Name ' + str(i) + ': ' + layer.name
            i += 1
   Results:
Layer LongName is : Detail Map
Layer name is : Detail Map
Layer LongName is : Detail Map\Streets
Layer name is : Streets
Layer LongName is : Detail Map\Parcels
Layer name is : Parcels
Layer LongName is : Detail Map\Neighborhoods
Layer name is : Neighborhoods
Layer LongName is : Background
Layer name is : Background
Layer LongName is : Background
Layer name is : Background

Start the dataframe loop.

Data Frame : MainLayer Layer longName 1: Detail Map
Data Frame : MainLayer Layer Name 1: Detail Map

Data Frame : MainLayer Layer longName 2: Streets
Data Frame : MainLayer Layer Name 2: Streets

Data Frame : MainLayer Layer longName 3: Parcels
Data Frame : MainLayer Layer Name 3: Parcels

Data Frame : MainLayer Layer longName 4: Neighborhoods
Data Frame : MainLayer Layer Name 4: Neighborhoods

Data Frame : MainLayer Layer longName 5: Background
Data Frame : MainLayer Layer Name 5: Background

Data Frame : Location_Layer Layer longName 1: Background
Data Frame : Location_Layer Layer Name 1: Background

thx,
Anna
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JeffBarrette
Esri Regular Contributor
I don't see anything wrong with your code.  I ran it on some MXDs and it worked as expected. I did have to modify it because it would not run by itself. 

Layer.longName returns the layer name preceeded by its group layer name (if it exists).

Here is the code I ran:

import arcpy from arcpy.mapping import *  #Brings in entire functionality of the mapping module  mxd = MapDocument(r"C:\Temp\Test.mxd")  TOClayers = ListLayers(mxd) for layer in TOClayers:     print "Layer LongName is : " + layer.longName     print "Layer name is : " + layer.name  print"\nStart the dataframe loop." dataframes = ListDataFrames(mxd) for frame in dataframes:     layers = ListLayers(mxd, '', frame)     for layer in layers:         print '\nData Frame : ' + frame.name +'\tLayer longName ' + ': ' + layer.longName         print 'Data Frame : ' + frame.name +'\tLayer Name ' + ': ' + layer.name


Jeff

View solution in original post

0 Kudos
3 Replies
AnnaBarendt
Emerging Contributor
Sorry, here is the code with the indents (the results are still as they are above):
import arcpy, sys, traceback
from arcpy.mapping import *  #Brings in entire functionality of the mapping module

#Create variables to hold vector files, tables, etc.
datapath = "M:\\Independent_Study\\PP4AG\\Chapter09\\"
mappath = datapath + 'MyData\\Maps\\'  # outpath for pdf documents
mxd = MapDocument(datapath + "MappingModule2Layer.mxd")

#4. Add try: and except: blocks
try:
#5. Add routines

    TOClayers = ListLayers(mxd)
    for layer in TOClayers:
        print "Layer LongName is : " + layer.longName
        print "Layer name is : " + layer.name

    print"\nStart the dataframe loop."
    dataframes = ListDataFrames(mxd, '')
    for frame in dataframes:
        layers = ListLayers(mxd, '', frame)
        i = 1
        for layer in layers:
            print '\nData Frame : ' + frame.name +'\tLayer longName ' + str(i) + ': ' + layer.longName
            print 'Data Frame : ' + frame.name +'\tLayer Name ' + str(i) + ': ' + layer.name
            i += 1
0 Kudos
JeffBarrette
Esri Regular Contributor
I don't see anything wrong with your code.  I ran it on some MXDs and it worked as expected. I did have to modify it because it would not run by itself. 

Layer.longName returns the layer name preceeded by its group layer name (if it exists).

Here is the code I ran:

import arcpy from arcpy.mapping import *  #Brings in entire functionality of the mapping module  mxd = MapDocument(r"C:\Temp\Test.mxd")  TOClayers = ListLayers(mxd) for layer in TOClayers:     print "Layer LongName is : " + layer.longName     print "Layer name is : " + layer.name  print"\nStart the dataframe loop." dataframes = ListDataFrames(mxd) for frame in dataframes:     layers = ListLayers(mxd, '', frame)     for layer in layers:         print '\nData Frame : ' + frame.name +'\tLayer longName ' + ': ' + layer.longName         print 'Data Frame : ' + frame.name +'\tLayer Name ' + ': ' + layer.name


Jeff
0 Kudos
AnnaBarendt
Emerging Contributor
Hi Jeff,

First, thank you for your input. 

I took the code you wrote, modifying it for the path name to the mxd on my computer, and ran the code.  Still came up with two different values for the .longName.  The first .longName outside the dataFrame loop gave the proper results.  The .longName within the dataFrame loop was not correct. 

So, the next thing I will do is go to the college, use one of their computers, and see what happens.  If their computers give the correct response, I probably will strip Python off my comupter and reload it.

I am using PythonWin 2.6.5 and version 10.0 of arcMap.

I'll let you know the results, regardless.

Update:

I am at the college now, and on their machine(s) the code runs fine, getting the same correct results for the .longName object property. 
Thanks so much Jeff, if you hadn't replied, I never would have questioned that the IDLE or Esri programs could be loaded incorrectly on my computer. 
a

Thanks again for looking at this.
a
0 Kudos