Is there a way to select a layer?

1190
14
03-20-2012 06:29 AM
AnthonyTimpson2
Occasional Contributor
Hello,

So my code looks for a layer name called flow then proceeds to attempt to zoom to that layer.

The problem i have right now is i cant figure out how to select the layer which i know is being correctly identified in my If statements so that I can zoom to it.

 
for lyr in layerlist:
# if the long layer name does not equal the short layer name
                                              
 if lyr.longName != lyr.name:
     #Find Layer with name == "flow"

     if lyr.name.lower() == "flow":

  #Pan to Extent of Flow Layer
                 df.extent = lyr.getExtent()
                 print extent
                 print lyr.name.lower()
Tags (2)
0 Kudos
14 Replies
MathewCoyle
Frequent Contributor
This should be all you need unless you have some other things going on not in the code you posted.

lyr = arcpy.mapping.ListLayers(mxd, "Flow", df)[0]
df.extent = lyr.getExtent()
arcpy.RefreshActiveView()


Edit: You are just missing the arcpy.RefreshActiveView() I am thinking.
0 Kudos
AnthonyTimpson2
Occasional Contributor
This should be all you need unless you have some other things going on not in the code you posted.

lyr = arcpy.mapping.ListLayers(mxd, "Flow", df)[0]
df.extent = lyr.getExtent()
arcpy.RefreshActiveView()


Edit: You are just missing the arcpy.RefreshActiveView() I am thinking.


Thank you!,

I think, I've tried that and it seems to always pan to an extent which doesnt cover the entire feature.

Ill give it another go with your code.

EDIT: Nope. still not zooming to the extents of my flow features in each layer

Full CODE


#Import the arcpy package
import arcpy                                                        

#Assign the Western States Mapping Document to variable mxd 
mxd = arcpy.mapping.MapDocument("CURRENT")

#Create a data driven pages object using "mxd" and assign it to variable "ddp"
#ddp = mxd.dataDrivenPages                                           

#Create a list of all text elements in the map layout
textlist = arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT")

#Cycle through the text elements    
for txt in textlist:   

    #When you find one whose text says "MAP SUBTITLE"...                                               
    if txt.text == r"MAP SUBTITLE":     
                  
        #....Assign it to variable "subtitle"          
        subtitle = txt                                              

#Create a list of the layers in the map document
layerlist = arcpy.mapping.ListLayers(mxd)                         
df = arcpy.mapping.ListDataFrames(mxd)[0]

#Cycle through each layer
for lyr in layerlist:
# if the long layer name does not equal the short layer name                                          
 if lyr.longName != lyr.name:
  #Find Layer with name == "flow"
  if lyr.name.lower() == "flow":
  #Pan to Extent of Flow Layer
   lyr = arcpy.mapping.ListLayers(mxd, "flow",df)[0]
   df.extent = lyr.getExtent()
   df.scale = df.scale * 1.15
   arcpy.RefreshActiveView()
 #If the layer is NOT visible...
  if lyr.visible == False:                                       

 #...Turn the layer on
   lyr.visible = True   
   list2 = arcpy.mapping.ListLayers(lyr)

 #Change the Subtitle text element to match the layer's name
   subtitle.text = lyr.name                                   

 #Create a string showing the path of the pdf that will be written out
   pdfpath = "C://Users//user//Desktop//PDF_TEMP//" + lyr.name + r" mapbook.pdf"
 
 #Print that we are exporting a mapbook
   print "Exporting " + pdfpath + "....."                     

 #Export the mapbook to pdf
   arcpy.mapping.ExportToPDF(mxd, pdfpath)             

 #Turn the layer off again
   lyr.visible = False                                         

#Delete variables to clean up memory usage
del mxd                                                       

print "Done!"

0 Kudos
MathewCoyle
Frequent Contributor
Are you having a problem with the PDF not exporting at the correct extent, or in your mxd the layer not going to the correct extent?

These flow layers are in a group layer, that's why I assume you are testing for the long name? You can restrict the layer list by just doing something like this.

lyrlist = arcpy.mapping.ListLayers(mxd, "*flow*")
0 Kudos
AnthonyTimpson2
Occasional Contributor
Are you having a problem with the PDF not exporting at the correct extent, or in your mxd the layer not going to the correct extent?

These flow layers are in a group layer, that's why I assume you are testing for the long name? You can restrict the layer list by just doing something like this.

lyrlist = arcpy.mapping.ListLayers(mxd, "*flow*")


The PDF export is at the wrong extent.

Yes, There are multiple group layers, each with a flow layer inside which i want to use for the extents of each group layer PDF, each flow layer is different.

I'll try hitting at it with the above definition.
0 Kudos
AnthonyTimpson2
Occasional Contributor
Ok still nothing, It makes no adjustments to the dataframe now and just exports all of them in the same extents.

anyone have any ideas what it going wrong



#Import the arcpy package
import arcpy                                                        

#Assign the Western States Mapping Document to variable mxd 
mxd = arcpy.mapping.MapDocument("CURRENT")

#Create a data driven pages object using "mxd" and assign it to variable "ddp"
#ddp = mxd.dataDrivenPages                                           

#Create a list of all text elements in the map layout
textlist = arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT")

#Cycle through the text elements    
for txt in textlist:   

    #When you find one whose text says "MAP SUBTITLE"...                                               
    if txt.text == r"MAP SUBTITLE":     
                  
        #....Assign it to variable "subtitle"          
        subtitle = txt                                              

#Create a list of the layers in the map document
layerlist = arcpy.mapping.ListLayers(mxd)                         
df = arcpy.mapping.ListDataFrames(mxd)[0]

#Cycle through each layer
for lyr in layerlist:
# if the long layer name does not equal the short layer name                                          
 if lyr.longName != lyr.name:
  #Find Layer with name == "flow"
  if lyr.name.lower() == "flow":
   lyrslist2 = arcpy.mapping.ListLayers(mxd, "*flow*")
  #Pan to Extent of Flow Layer
   
   for lyr in lyrslist2:
    
    df.extent = lyrs.getExtent()
    #df.scale = df.scale * 1.15
    arcpy.RefreshActiveView()
 #If the layer is NOT visible...
  if lyr.visible == False:                                       

 #...Turn the layer on
   lyr.visible = True   
   list2 = arcpy.mapping.ListLayers(lyr)

 #Change the Subtitle text element to match the layer's name
   subtitle.text = lyr.name                                   

 #Create a string showing the path of the pdf that will be written out
   pdfpath = "C://Users//atimpson//Desktop//DDP_TEMP//" + lyr.name + r" mapbook.pdf"
 
 #Print that we are exporting a mapbook
   print "Exporting " + pdfpath + "....."                     

 #Export the mapbook to pdf
   arcpy.mapping.ExportToPDF(mxd, pdfpath)             

 #Turn the layer off again
   lyr.visible = False                                         

#Delete variables to clean up memory usage
del mxd                                                       

print "Done!"

 

0 Kudos
MathewCoyle
Frequent Contributor
In the last code you posted you have a typo I believe.

df.extent = lyrs.getExtent()

Should be
df.extent = lyr.getExtent()


Also, you don't need to make two layer lists. Replace your main one with the second one.
0 Kudos
AnthonyTimpson2
Occasional Contributor
Fixed the typo and commented out the other layerlist

Still doesnt change the view.

😕

now i get this error

Runtime error <type 'exceptions.ValueError'>: Unknown value for Extent argument
0 Kudos
MathewCoyle
Frequent Contributor
You're script has some indentation/tabify issues as well, at least when I copied it over it did. I cleaned it up a bit and worked fine for me. Also switched your layer name check to this, Wasn't working how it was set up before since the PDFs would be exported with the same name every time.

if "flow" in lyr.name.lower():


#Import the arcpy package
import arcpy

#Assign the Western States Mapping Document to variable mxd
mxd = arcpy.mapping.MapDocument("CURRENT")

#Create a data driven pages object using "mxd" and assign it to variable "ddp"
#ddp = mxd.dataDrivenPages

#Create a list of all text elements in the map layout
textlist = arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT")

#Cycle through the text elements
for txt in textlist:

    #When you find one whose text says "MAP SUBTITLE"...
    if txt.text == r"MAP SUBTITLE":

        #....Assign it to variable "subtitle"
        subtitle = txt

#Create a list of the layers in the map document
layerlist = arcpy.mapping.ListLayers(mxd, "*flow*")
df = arcpy.mapping.ListDataFrames(mxd)[0]

#Cycle through each layer
for lyr in layerlist:
# if the long layer name does not equal the short layer name
    if lyr.longName != lyr.name:
        #Find Layer with name == "flow"
        if "flow" in lyr.name.lower():

            #Pan to Extent of Flow Layer

            df.extent = lyr.getExtent()
            #df.scale = df.scale * 1.15
            arcpy.RefreshActiveView()
        #If the layer is NOT visible...
        if lyr.visible == False:

            #...Turn the layer on
            lyr.visible = True

        #Change the Subtitle text element to match the layer's name
        subtitle.text = lyr.name

        #Create a string showing the path of the pdf that will be written out
        pdfpath = "C:/test/pdf/" + lyr.name + r" mapbook.pdf"

        #Print that we are exporting a mapbook
        print "Exporting " + pdfpath + "....."

        #Export the mapbook to pdf
        arcpy.mapping.ExportToPDF(mxd, pdfpath)

        #Turn the layer off again
        lyr.visible = False

#Delete variables to clean up memory usage
del mxd

print "Done!"
0 Kudos
AnthonyTimpson2
Occasional Contributor
Well i still have the problem of it going to extents which are completely different than the extents I get when I do a manual Zoom to Layer

I dont know if i was explaining the intent very well.

I have group layers, Those are the relevant layers.

In each group layer is a layer called flow which I want to ONLY use as a zoom guide

once zoomed to the flow layer it should execute the export for the group layer.
0 Kudos