# Using Arcpy, how can I determine min and max elevation from a raster DEM layer within my current extent?

Using Arcpy, how can I determine min and max elevation from a raster DEM layer within my current extent? I'm currently using Data Driven Pages to centre the layout on each feature extent.

I need to determine this elevation difference value in order to get Python to automatically activate the appropriate contour interval feature class (25m, 100m, etc)

The current logic I'm thinking of is to select all cells within current extent, (what is the most convenient way, or arcpy methods() to do this?) place their values in a list, then:

``````minElev = 99999
maxElev = -99999

for i in myList:
if i < minElev:
minElev = i

for i in myList:
if i > maxElev:
maxElev = i
``````

then subtract max and min to get the difference value.

I don't think this is the easiest way, is there someone experienced who knows a faster way?

• At the moment it sounds like the fact that you are using Data Driven Pages is an aside so I think you should edit that out of your question which is really "Determining min and max elevation from raster DEM layer within current extent?"
– PolyGeo
Commented Jul 16, 2014 at 23:33

Use Get Raster Properties, specifically the `MINIMUM` and `MAXIMUM` values.

In theory, this code should make a temporary raster layer using the display extent -- although I have not tested it, and am not sure whether it will work -- and then the raster properties tool will apply to only the raster currently shown in the display.

``````import arcpy

arcpy.env.extent = arcpy.mapping.MapDocument.activeView.Extent

MakeRasterLayer_management("c:/temp/raster.tif", "rlayer", "#", arcpy.env.extent)

# Get the geoprocessing result object
elevMinResult = arcpy.GetRasterProperties_management("rlayer", "MINIMUM")
# Get the elevation minimum value from geoprocessing result object
elevMin = elevMinResult.getOutput(0)
``````

Turn the current data frame extent to a polygon via this

``````    extent = inFeature.extent
extentPoly = drive + "\\NCT_GIS\\Projects\\Temp\\extentPoly.shp"
# Array to hold points
array = arcpy.Array()
# Create the bounding box
# ensure the polygon is closed
# Create the polygon object
polygon = arcpy.Polygon(array)
array.removeAll()
``````

Then read the output statistics

``````import arcpy
import numpy

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
anExtent=df.extent

raster=arcpy.GetParameterAsText(0)
desc=arcpy.Describe(raster)
cSize=desc.meanCellHeight
nRows=int(anExtent.height/cSize)+1
nCols=int(anExtent.width/cSize)+1
myArray = arcpy.RasterToNumPyArray(raster,anExtent.lowerLeft,nCols,nRows,-9999)

``````>>> myList=[3,9,15,1]