Select to view content in your preferred language

Is there a way to select a layer?

2125
14
03-20-2012 06:29 AM
AnthonyTimpson2
Regular 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
Honored Contributor
There's no problems I can find in the code. The problem may be with your layers or spatial reference.
0 Kudos
AnthonyTimpson2
Regular Contributor
is there no way to simply select that layer in each Group Layer and use the zoomToSelectedFeatures()?
0 Kudos
MathewCoyle
Honored Contributor
You can select all the attributes in a layer using. And if you want to do it a bunch of times use the new selection option.
arcpy.SelectLayerByAttribute_management(layer, "NEW_SELECTION")
0 Kudos
AnthonyTimpson2
Regular Contributor
ok, off to try and finagle that.

Im thinking, like you meantioned, that it has to due with the fact that this data is in a custom projection based on distance from a 0,0 point.. and that's it. I've had trouble with Arcmap handling the custom projection before for certain processes.

I really appreciate your help.
0 Kudos
AnthonyTimpson2
Regular Contributor
OK!!

This  code works pretty darn well.. Only problem Each export is being given the dataframe of the Previous layer.

what happens is this

the first export gets the correct data frame, the second gets the first, the third gets the second, the fourth gets the third etc.

What could be causing that?

import arcpy                                                        
import time

arcpy.env.overwriteOutput = True
mxd = arcpy.mapping.MapDocument("CURRENT")
textlist = arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT")
   
for txt in textlist:                                                
    if txt.text == r"MAP SUBTITLE":              
        subtitle = txt                                              

layerlist = arcpy.mapping.ListLayers(mxd)                         
df = arcpy.mapping.ListDataFrames(mxd)[0]

for lyr in layerlist:
                                          
    if lyr.longName != lyr.name:
 
 if "flow" in lyr.name.lower():
  
  arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION")
  df.zoomToSelectedFeatures()
  arcpy.SelectLayerByAttribute_management(lyr, "CLEAR_SELECTION")
  df.scale = df.scale * 1.15
  arcpy.RefreshActiveView()
   
 if lyr.visible == False:
  lyr.visible = True   
  subtitle.text = lyr.name
  time.sleep(3)
  pdfpath = "C://Users//user//Desktop//DDP_TEMP//" + lyr.name + r" mapbook.pdf"
  print "Exporting " + pdfpath + "....."                     
  arcpy.mapping.ExportToPDF(mxd, pdfpath)             
  lyr.visible = False                                         


del mxd, df                                                       

print "Done!"

 

0 Kudos