1

Working on being able to zoom to the extent of features in a search row to produce individual maps for row (like data driven pages but for individual rows on the dataset).

I just can't seem to get it to zoom to extent? I have looked around but can't find what I need in the syntax help guide.

Script below

import arcpy


from arcpy import env
arcpy.env.overwriteOutput = True
#Set File workspace
file_workspace = 'N:\\GIS\\Projects\\AA_Leith_Hawkins_TestBed\\Search_Cursor\\Search_Cursor.gdb'
env.workspace = file_workspace

#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

    #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"

    #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 addLayer
    arcpy.mapping.AddLayer(df, addLayer2, "TOP")
    print addLayer2
    arcpy.mapping.AddLayer(df, addLayer3, "TOP")
    print addLayer3

    #zoom to layer

    print df
    lyr = arcpy.mapping.ListLayers(mxd, '', df)[2].name
    print lyr
    extent = lyr.getExtent()
    print extent
    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

The error message I get is (I have included the prints from the run as well so you can see what they produce to try and make it easier:

108050022
Holding_Reference_Number = 108050022
Buffer complete
intersect Complete
Buffer_108050022
Holding_108050022
Intersect_108050022
<geoprocessing Data Frame object object at 0x1262EC00>
Buffer_108050022
Traceback (most recent call last):
  File "N:\GIS\Projects\AA_Leith_Hawkins_TestBed\Search_Cursor\Script\SearchCUrsor.py", line 65, in <module>
extent = lyr.getExtent()
AttributeError: 'unicode' object has no attribute 'getExtent'
4

Your error indicates that your lyr variable is a unicode object (not a Layer object). This is due to you setting your lyr with .name (your variable is just storing the name, not reference to the actual layer.

lyr = arcpy.mapping.ListLayers(mxd, '', df)[2].name 

Remove the .name here and add it into the print line

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.