Select to view content in your preferred language

Expand extent for purposes of clipping

6348
14
Jump to solution
12-09-2015 11:27 AM
RebeccaStrauch__GISP
MVP Emeritus

I'm wanting to clip a hydro layer (with a reselect) based on the extent of my study area extent....but expanded a bit (% to be determined). I have code that works, but it's a bit clunky. I grab the extent, create a poly, buffer, grab-new-extent, create the poly and then do my clip/query.  It's the getting the extent->poly->buffer->extent->poly that seems a bit overkill.  I'm wondering if I am missing an obvious arcpy option for expanding the extent for the clip.  Trying to keep it to arcpy and not get deep into the geometry of this one...but all suggestions will be considered.

Thanks.

This works:

import arcpy
from arcpy import env
import os
arcpy.env.overwriteOutput = "True"

inStudy =  r'C:\Prep.gdb\study_boundary'
srDesc = arcpy.Describe(inStudy).spatialReference
extent = arcpy.Describe(inStudy).extent
arcpy.env.outputCoordinateSystem = srDesc.PCSCode

# ---> Ugly way to do this, but it works and fairly fast.  Ok for now.
# get extent, a buff distance (1% of X), and create a temp polygon
extBuffDist = ((int(abs(extent.lowerLeft.X - extent.lowerRight.X))) * .01)

# Array to hold points for the bounding box for initial extent
origExtentPts = arcpy.Array()
origExtentPts.add(extent.lowerLeft)
origExtentPts.add(extent.lowerRight)
origExtentPts.add(extent.upperRight)
origExtentPts.add(extent.upperLeft)
origExtentPts.add(extent.lowerLeft)  # ensures polygon closes
# Create and buffer the polygon object
polygonTmp1 = arcpy.Polygon(origExtentPts)
arcpy.Buffer_analysis(polygonTmp1, "polyBuff", extBuffDist, "OUTSIDE_ONLY")
origExtentPts.removeAll()

#get extent of buffered poly
extent = arcpy.Describe("polyBuff").extent
# Array to hold points for the buffer bounding box
newExtentPts = arcpy.Array()
newExtentPts.add(extent.lowerLeft)
newExtentPts.add(extent.lowerRight)
newExtentPts.add(extent.upperRight)
newExtentPts.add(extent.upperLeft)
newExtentPts.add(extent.lowerLeft) # ensures polygon closes
polygonTmp2 = arcpy.Polygon(newExtentPts)

# save to disk
#extentPoly = "extentPoly" 
arcpy.CopyFeatures_management(polygonTmp2, "extentPoly")
del polygonTmp1, polygonTmp2

hydroNHDSource = r"\\myServer\NHDH_AK.gdb\Hydrography\NHDFlowline"
hydroNHDQuery = "(GNIS_Name >= '1' AND ( FType = 566 OR FType = 460 OR FType = 558))"

arcpy.Clip_analysis(hydroNHDSource, "extentPoly", "hydroExtent")   
arcpy.MakeFeatureLayer_management("hydroExtent", "HydroReselect", hydroNHDQuery)
arcpy.Buffer_analysis("HydroReselect", "hydroBuff", 500, "FULL", "ROUND", "ALL" )
hydroBuffStudy = arcpy.Clip_analysis("hydroBuff", inStudy)
0 Kudos
14 Replies
JoshuaBixby
MVP Esteemed Contributor

I am not sure if the OP stated whether this is for ArcGIS for Desktop or ArcGIS Pro.  Since extended iterable unpacking (PEP 3132) wasn't introduced until Python 3.0, neither lines 3 nor 4 will work with ArcGIS for Desktop since it is stuck with Python 2.x.

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

Joshua, it is initially in a toolbox (so could be for either), but will be part of a python addin which aren't support by Pro.  I'm trying to keep it simple, and in arcpy as much possible.  Improved speed of numpy isn't necessary in this case, since this is a pretty minor part....the clipping is the main part.

0 Kudos
DanPatterson_Retired
MVP Emeritus

ooh yea.... you would have to expand the *zzmm term out in full... such a nice enhancement when you only need the first few and can't remember the rest...but it does save all that XMin...Xmax explicit calling stuff though.  This would be a an area that would be nice to see arcpy go...reflecting more generic objects than proprietary

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Extended iterable unpacking is a fantastic enhancement, I really wish it could have made its way backwards to 2.7.  Of course, Esri could help the situation by moving ArcGIS for Desktop to Python 3.x, but I am becoming suspicious that will never happen.

I am sure I have lamented about this before, but I think Esri really missed an opportunity with ArcGIS Pro to take ArcPy to the next level.  In some sense, that was done out of necessity with the ArcPy Mapping module (arcpy.mapping => arcpy.mp)  because the interface is so different between Desktop and Pro, but the changes to all of the other modules and base package were minimal.  As has been brought up in GeoNet before, it would be great to see the ArcPy Geometry classes expanded and made much more Pythonic, wouldn't have the change to ArcGIS Pro been a great break in the product line to introduce such new functionality?  Oh well, c'est la vie in Esri-land.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Not sure what version of ArcGIS you are using, but starting at ArcGIS 10.3 the "Extent object now supports JSON and polygon properties."

>>> ext = arcpy.Describe(inStudy).extent
>>> new_ext_poly = ext.polygon.buffer(int((ext.XMax - ext.XMin)*0.01)).extent.polygon
>>>

If you don't mind just a little rounding of the extent corners, you could drop .extent.polygon from the end of line 2.