Skip to content

Instantly share code, notes, and snippets.

@knu2xs
Last active October 25, 2020 15:11
Show Gist options
  • Select an option

  • Save knu2xs/d8d0119709fc75cc9a71 to your computer and use it in GitHub Desktop.

Select an option

Save knu2xs/d8d0119709fc75cc9a71 to your computer and use it in GitHub Desktop.
Select and zoom to features in ArcMap
# import modules
import arcpy
def select_zoom(layer, sql):
"""
Select from the current map document, a feature in a layer and zoom to the specified feature.
"""
# select by attributes enforcing new selection method to clear any previous selections
SelectLayerByAttribute_management(in_layer_or_view=layer,
selection_type='NEW_SELECTION',
where_clause=sql)
# identify the current map document
mxd = arcpy.mapping.MapDocument('CURRENT')
# identify the data frame
df = arcpy.mapping.ListDataFrames(mxd)[0]
# use the zoom to selected features method of the data frame to update the extent
df.zoomToSelectedFeatures ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment