Model Builder geoprocessing example - Extent Of Selected Features

File uploaded by curtvprice Champion on Nov 19, 2016Last modified by curtvprice Champion on Nov 19, 2016
Version 4Show Document
  • View in full screen mode

The following Python function returns the extent of selected features as a string.


There may be other ways to do this, but I think this a nice example of using the arcpy geometry object!


import arcpy

def extent_of_selected(lyr):
    """Return the extent of all selected features in a feature layer (as text)"""
    x0, y0, x1, y1 = 1e99, 1e99, -1e99, -1e99
    ext = None
    with arcpy.da.SearchCursor(lyr, "SHAPE@") as rows:
        for row in rows:
            ext = row[0].extent
            x0 = min(x0, ext.XMin)
            y0 = min(y0, ext.YMin)
            x1 = max(x1, ext.XMax)
            y1 = max(y1, ext.YMax)
    if ext:
        ext = "{} {} {} {}".format(x0, y0, x1, y1)
        ext = arcpy.Describe(lyr).Extent
        ext = "{} {} {} {}".format(ext.XMin, ext.YMin, ext.XMax, ext.YMax)
    return ext

Python example

arcpy.SelectLayerByAttribures("wells", "", "DEPTH > 100")
arcpy.env.extent = extent_of_selected("wells")
arcpy.SelectLayerByAttribures("wells", "CLEAR_SELECTION")
# the following will copy all features in the extent of depth > 100 wells
arcpy.CopyFeatures_management("wells", "wells1")

Model Builder example

The attached toolbox demonstrates how this function can be used in Model Builder. The model tool is called Extent Of Selected and takes a feature layer as input.  (Model Builder is the main reason I set up the Python function to return a text representation of extent instead of an extent object -- the text rep is easily represented as a model element which can then be connected to a tool as in the example below.)


extent of selected tool model builder example