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 query = "Holding_Reference_Number = " + str(row) print query File_output = file_workspace+ '\\' 'Buffer_'+str(row) #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)) # Export holding to geodatabase Holding_Boundary = file_workspace+ '\\' 'Holding_'+str(row) 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) 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") 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") lyr = arcpy.mapping.ListLayers(mxd,addLayer, df) 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) + '.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)
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
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:
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 !!
I would look at the sample in the help
I think you will at least need to change that line to
lyr = arcpy.mapping.ListLayers(mxd,addLayer, df).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.