[GIS] Using Arcpy, how can I determine min and max elevation from a raster DEM layer within the current extent

arcpydemelevation

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?

Best Answer

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)