AnsweredAssumed Answered

Zoom to Layer Extent

Question asked by Leithhawkins on Feb 7, 2017
Latest reply on Feb 8, 2017 by Leithhawkins

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

Outcomes