Zoom to layer

2744
4
09-18-2014 12:21 AM
Yaron_YosefCohen
Occasional Contributor II

I have a problem with the getExtent function on  arcpy

I try to focus on  layers that start with letters "ra" and then sets the scale of 50000. i try to use asterisk (*) but the code does not work

import arcpy, os, sys

from arcpy import env

 

 

env.workspace = r"C:\Project"

for mxdname in arcpy.ListFiles("*.mxd"):

    print mxdname

    mxd = arcpy.mapping.MapDocument(r"C:\Project\\" + mxdname)

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

    lyr = arcpy.mapping.ListLayers(mxd, "*ra", df)[0]

    ext = lyr.getExtent()

    df.extent = ext

    print 'getExtent'

    df.scale = 50000

    print 'scale'

    mxd.save()

del mxd

 

Any help will be appreciated.

Tags (2)
0 Kudos
4 Replies
DanPatterson_Retired
MVP Emeritus

starts with would be "ra*"  you have ends with "*ra"  see the listlayers menu example

0 Kudos
Yaron_YosefCohen
Occasional Contributor II

it don't work.

import arcpy, os, sys

from arcpy import env

env.workspace = r"C:\Project"

for mxdname in arcpy.ListFiles("*.mxd"):

    print mxdname

    mxd = arcpy.mapping.MapDocument(r"C:\Project\\" + mxdname)

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

    lyr = arcpy.mapping.ListLayers(mxd, "r*", df)[0]

    ext = lyr.getExtent()

    df.extent = ext

    print 'getExtent'

    mxd.save()

del mxd

0 Kudos
DanPatterson_Retired
MVP Emeritus

Then the question is...did you get an error messages? What do you want listlayers within the for loop? it will return a list object and you can't get the extent of a list only individual layers.

0 Kudos
Yaron_YosefCohen
Occasional Contributor II

i want arcpy zoom to 2 lyr then get scale of 50k

0 Kudos