Zoom To Each Record Search Cursor

1030
7
11-08-2017 04:00 PM
DevinUnderwood2
Occasional Contributor

I know I almost have this right but it selects all records and doesn't zoom to each record - one by one

import arcpy
mxd = arcpy.mapping.MapDocument ('CURRENT')
df = arcpy.mapping.ListDataFrames(mxd,"Layers")[0]

# y = input("Type Layer Nmae :")
fc = r'Orders_High_RD'
lyr = arcpy.mapping.ListLayers(mxd, fc, df)[0]
#df.extent = lyr.getSelectedExtent()
df.scale = 1000 # we set the scale to 1000
#arcpy.RefreshActiveView()


with arcpy.da.SearchCursor(fc, "OBJECTID") as cursor:
    row = cursor.next()
    for row in cursor:
        df.extent = lyr.getSelectedExtent()
        arcpy.SelectLayerByAttribute_management(fc,"NEW_SELECTION")
        #df.zoomToSelectedFeatures()
        arcpy.RefreshTOC()
        arcpy.RefreshActiveView()
0 Kudos
7 Replies
DanPatterson_Retired
MVP Emeritus

you are going to have to 'pause' the display once you have zoomed to a section, otherwise, it is just going to blast through.

import time  # ---- goes in your import section
 
# Wait for 0.5 seconds
time.sleep(0.5)  # ---- goes where you want to pause ----
DevinUnderwood2
Occasional Contributor

This makes sense, where exactly would I put it, as I know I want to pause after each selected record.

What is the difference between using  df.zoomToSelectedFeatures() & df.extent = lyr.getSelectedExtent()

0 Kudos
DanPatterson_Retired
MVP Emeritus

Well I would have to experiment, but I would uncomment the df.zoomtoselected... first and maybe put the sleep just after the zoom... or maybe before the zoom (up to you) experiment.  the import time line has to go at the top of the script.

That is the only way you can 'pause' in python... give it shot

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

The difference is that one is focused on selected features within a data frame while the other is only about the selected features in a layer.  If a data frame has more than 1 layer with selected features, the DataFrame.zoomToSelectedFeatures() will zoom to the union of the extent of all selected features in all layers.

0 Kudos
DevinUnderwood2
Occasional Contributor

great advice, I will work with this, and it seems I would not want the DF.zoomToSelectedFeatures()

0 Kudos
DevinUnderwood2
Occasional Contributor

I changed it up trying to get it to work and used while instead for loop.

I noticed that all the records all selected then all records unselected so the cursor is not working like I want it to. One by one

import arcpy, time
mxd = arcpy.mapping.MapDocument ('CURRENT')
df = arcpy.mapping.ListDataFrames(mxd,"Layers")[0]
field = "OBJECTID"
# y = input("Type Layer Name :")
fc = r'Orders_High_RD'
lyr = arcpy.mapping.ListLayers(mxd, fc, df)[0]
##df.extent = lyr.getSelectedExtent()
df.scale = 1000 # we set the scale to 1000
##arcpy.RefreshActiveView()

cursor = arcpy.SearchCursor(fc)
row = cursor.next()
while row:
    row = cursor.next()
##    print (row.getValue(field))
##    time.sleep(5)
    arcpy.SelectLayerByAttribute_management(fc,"NEW_SELECTION")
    time.sleep(5)
    df.extent = lyr.getSelectedExtent()
    arcpy.RefreshActiveView()

##        df.zoomToSelectedFeatures()
##        arcpy.RefreshTOC()
0 Kudos
DanPatterson_Retired
MVP Emeritus

you really changed it since it hasn't got any criteria to select on.  Did you previous example work with selecting things one at a time even if it didn't stop?  Why not do a query on the OBJECTID value incrementing it one at a time in your selectbyattributes... or something

0 Kudos