Select to view content in your preferred language

Zoom to Layer Extent

3304
7
02-07-2017 08:05 PM
Leith_JohnHAWKINS
Deactivated User

Hi I am working on a process of producing some maps for a process we have to do at work

I am just struggling to get the zoom to layer to work.  The rest of the script works fine.#

#buffer set up
Holdings = 'N:\GIS\Projects\AA_Leith_Hawkins_TestBed\Search_Cursor\Search_Cursor.gdb\Data\Holdings'
distances = [1000, 4000]
unit = "Meters"
#Make a feature layer
arcpy.MakeFeatureLayer_management(Holdings,'Holdings_Layer')

#searchcursor for evey row in dataset
with arcpy.da.SearchCursor(Holdings, ['Holding_Reference_Number'])as Holdings_Ref_cursor:
    for row in Holdings_Ref_cursor:
        print row[0]
        query = "Holding_Reference_Number = " + str(row[0])
        print query
        File_output = file_workspace+ '\\' 'Buffer_'+str(row[0])
        #print File_output
        #PNG file outp put location


        #Select Feature using the reference number from holdings layer
        arcpy.SelectLayerByAttribute_management('Holdings_Layer', 'NEW_SELECTION',"Holding_Reference_Number = " + str(row[0]))

        # Export holding to geodatabase
        Holding_Boundary = file_workspace+ '\\' 'Holding_'+str(row[0])
        arcpy.management.CopyFeatures('Holdings_Layer', Holding_Boundary)

        #Mutliple ring Buffer using Selected Features
        arcpy.MultipleRingBuffer_analysis('Holdings_Layer', File_output, distances, unit, "", "ALL")
        arcpy.MakeFeatureLayer_management(File_output, 'Buffer_Layer')
       #arcpy.Buffer_analysis("Holdings_Layer", ofc, var_Buffer, "FULL", "ROUND", "ALL", "")
        print 'Buffer complete'

        #Intersect Features
        Intersect_out_feaatures = file_workspace+ '\\' 'Intersect_'+str(row[0])
        arcpy.Intersect_analysis([Holdings,File_output],Intersect_out_feaatures,"","", "INPUT")
        print "intersect Complete"

        #print Png_output


        #add Layers to the Map
        mxd = arcpy.mapping.MapDocument('N:\\GIS\Projects\\AA_Leith_Hawkins_TestBed\\Search_Cursor\\Search_Cursor_mxd.mxd')
        df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
        addLayer = arcpy.mapping.Layer(File_output)
        addLayer2 = arcpy.mapping.Layer(Holding_Boundary)
        addLayer3 = arcpy.mapping.Layer(Intersect_out_feaatures)
        arcpy.mapping.AddLayer(df, addLayer, "TOP")
        print 'Buffer to map'
        arcpy.mapping.AddLayer(df, addLayer2, "TOP")
        print 'Holding to Map'
        arcpy.mapping.AddLayer(df, addLayer3, "TOP")
        print 'Intersect out Features to Map'

        #zoom to layer
        Selection = 'distance = 4000'
        print Selection

        mxd = arcpy.mapping.MapDocument('N:\\GIS\Projects\\AA_Leith_Hawkins_TestBed\\Search_Cursor\\Search_Cursor_mxd.mxd')
        df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
        lyr = arcpy.mapping.ListLayers(mxd,addLayer, df)[0]
        extent = lyr.getExtent()
        #arcpy.SelectLayerByAttribute_management('Buffer_Layer', 'NEW_SELECTION', Selection)
        df.extent = extent

        #Export Map to PNG File
        Png_output = "N:\\GIS\Projects\\AA_Leith_Hawkins_TestBed\\" + str(row[0]) + '.png'
        arcpy.mapping.ExportToPNG(mxd,Png_output)
        print 'Map Created'
        del mxd

when ever I run it I am getting told my

Traceback (most recent call last):
  File "N:\GIS\Projects\AA_Leith_Hawkins_TestBed\Search_Cursor\Script\SearchCUrsor.py", line 66, in <module>
    lyr = arcpy.mapping.ListLayers(mxd,addLayer, df)[0]
IndexError: list index out of range

its most liekly a very simple error but I cant seem to get my head around the syntax to make it work

Any suggestions

Tags (1)
0 Kudos
7 Replies
AdrianWelsh
MVP Honored Contributor

Could it be an issue with your mxd path?

It looks like you have a single \ between GIS and Projects.

So I'm guessing your script isn't finding your mxd, maybe.

0 Kudos
Leith_JohnHAWKINS
Deactivated User

 hmmm sadly no.  Ill keep digging

But thanks for the catch of the single \ 

Regards

Leith

0 Kudos
AdrianWelsh
MVP Honored Contributor

I'm wondering if your mxd path is being seen at all. Since the error says that the layer list is out of range makes me think it isn't seeing a layer list and therefore not seeing an mxd (like would it be better to use double quotes and forward slashes to help with this:

"N:/GIS/Projects/AA_Leith_Hawkins_TestBed/Search_Cursor/Search_Cursor_mxd.mxd"

)

Leith_JohnHAWKINS
Deactivated User

I wonder if its more to do with the addlayer variable that its trying to refernce in the list ?

Hmm i think it will have to be tommorrows job   as I have a meeting now !!

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

I would look at the sample in the help

ListLayers—Help | ArcGIS for Desktop 

I think you will at least need to change that line to

lyr = arcpy.mapping.ListLayers(mxd,addLayer, df)[0].name

and then print it to see if it is ass expected.  But I would leave the "addLayer" as a wildcard and test by printing out all the layers, then add the variable back in if needed. 

btw - haven't looked at entire script, but that is where I would start if I was debugging. fwiw.

Leith_JohnHAWKINS
Deactivated User

Thanks illl have a play and see what happens.  I appreciate the input

0 Kudos
Leith_JohnHAWKINS
Deactivated User

So I got it to work

lyr = arcpy.mapping.ListLayers(mxd, '', df)[2]print lyr.name extent = lyr.getExtent()print extent df.extent = extent
0 Kudos