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 Jul 16 '14 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)

Try using this, http://resources.arcgis.com/en/help/main/10.1/index.html#//0017000000m3000000

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)

Then read the output statistics

import arcpy
import numpy

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

myArray = arcpy.RasterToNumPyArray(raster,anExtent.lowerLeft,nCols,nRows,-9999)

>>> myList=[3,9,15,1]
>>> mX=max(myList)
>>> mN=min(myList)
>>> mX-mN
  • Thank you! This is helpful! Now I just need to know how to extract the values for all raster cells within only the current extent and bring them into Python. – CompNat Jul 16 '14 at 23:44
  • The question is "Using Arcpy, how can I determine min and max elevation from a raster DEM layer within my current extent?" but this answer does not use ArcPy. – PolyGeo Jul 17 '14 at 0:18
  • It doesn't matter. I just thought Arcpy had a method to do it simply, but if it doesn't, well, no biggie! – CompNat Jul 18 '14 at 1:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.