Raster to Numpy

1065
1
09-26-2013 03:41 AM
GeorgeNewbury
Occasional Contributor
The raster to numpy tripped me up for a bit until I realized that there while you may specify the lower left for the raster to numpy array function, the actual positions within the array will be from the upper left of that selection box. Hope this helps someone else out there.

import numpy
inputDem = r"inputFileHere"
# Convert Raster to Array, Default using LowerLeft as Origin
rasArray = arcpy.RasterToNumPyArray(inputDem)
maxVal = rasArray.max() #Max Elevation
minVal = rasArray.min() #Min Elevation

maxValpos = numpy.unravel_index(rasArray.argmax(),rasArray.shape) #Max Elevation Position in Array
minValpos = numpy.unravel_index(rasArray.argmin(),rasArray.shape) #Min Elevatino Position in Array

desc = arcpy.Describe(inputDem)

# What an initual guess would be for the actual max position
#  typically think first value '[0]' is associated with 'X' and second value '[1]' is associated with 'Y'
utmX = desc.extent.lowerLeft.X + maxValpos[0]
utmY = desc.extent.lowerLeft.Y + maxValpos[1]


# But the actual result that is correct would be:
utmX = desc.extent.upperLeft.X + maxValpos[1]
utmY = desc.extent.upperLeft.Y - maxValpos[0]

# So while you may specify the lower left corner, that is for defining the origin for your table, but the actual
# table dimension references are from the upper left. With the coordinates within the table being (Y,X) i.e. (row,column)
Tags (2)
0 Kudos
1 Reply
ThomasCummins-Russell
New Contributor
Thanks! This has helped me solve a problem I had converting back from numpy array to raster.
0 Kudos