0

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?

1
  • 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, 2014 at 23:33

4 Answers 4

2

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)
1

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
    array.add(extent.lowerLeft)
    array.add(extent.lowerRight)
    array.add(extent.upperRight)
    array.add(extent.upperLeft)
    # ensure the polygon is closed
    array.add(extent.lowerLeft)
    # Create the polygon object
    polygon = arcpy.Polygon(array)
    array.removeAll()

Then read the output statistics

0
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)

arcpy.AddMessage(numpy.min(myArray))
-1
>>> myList=[3,9,15,1]
>>> mX=max(myList)
>>> mN=min(myList)
>>> mX-mN
14
3
  • 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, 2014 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, 2014 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, 2014 at 1:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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