Map Algebra in Python Using Variable

3366
5
Jump to solution
06-11-2013 07:43 AM
RustyRex
Occasional Contributor
I am trying to move from raster calculator to the python window for my spatial analysis and moving from ERDAS Model Maker altogether.  I am already struggling on only my first task.  Here I am just trying to 'scale' my raster from 0-1

import arcpy from arcpy import env from arcpy.sa import * #Get Max and Min gridMaxResult = arcpy.GetRasterProperties_management("RASTER1", "MAXIMUM") gridMinResult = arcpy.GetRasterProperties_management("RASTER1", "MINIMUM") #Assign output gridMax = gridMaxResult.getOutput(0) gridMin = gridMinResult.getOutput(0)  #Rescale or 'normalize' OutRas = (((Raster("RASTER1") - gridMin) * (1 - 0)) / (gridMax - gridMin)) + 0 OutRas.save("NormalizedRas")

I get:
Runtime error <type 'exceptions.RuntimeError'>: ERROR 000732: Input Raster: Dataset 0 does not exist or is not supported

It is reading the variable correctly (gridMin is equal to 0) but it seems like it doesnt know it is a number.  Any advice would help me with this problem and my syntax going forward.

Thanks
0 Kudos
1 Solution

Accepted Solutions
curtvprice
MVP Esteemed Contributor
What would the syntax of this code look like if i were to make it a python script to import as a script tool with input and output parameters?


The normal way to provide information to an arcpy script tool is to use GetParameterAsText(), setting up the input parameter as a Raster Layer, and the output as a Raster Dataset. Raster() is used to convert the input string to a raster data arcpy map algebra can work with.

import arcpy from arcpy.sa import * inRaster = arcpy.GetParameterAsText(0) # input raster layer name or raster dataset path outRaster = arcpy.GetParameterAsText(1) # output: raster dataset  ras = Raster(inRaster) range = ras.maximum - ras.minimum mappedras = (ras - ras.minimum) / range mappedras.save(outRaster)


Beyond that, you're kind of out of the scope of this thread.

View solution in original post

0 Kudos
5 Replies
Luke_Pinner
MVP Regular Contributor
It is reading the variable correctly (gridMin is equal to 0) but it seems like it doesnt know it is a number
That's because it isn't a number.

The getOutput method returns a unicode string, see the help for the result object.

Cast your gridMax/Min variables to float before using them and the map algebra expression will work.

>>> type(gridMax)
<type 'unicode'>
>>> gridMax = float(gridMaxResult.getOutput(0))
>>> gridMin = float(gridMinResult.getOutput(0))
0 Kudos
curtvprice
MVP Esteemed Contributor
I am just trying to 'scale' my raster from 0-1


Three thoughts:

1) The Raster object has the properties you want without having to use that tool:

from arcpy.sa import *
rasPath = "RASTER1"
ras = Raster(rasPath)
print ras.minimum
print ras.maximum


2) I think your mapping expression needs some tweaking.
range = ras.maximum - ras.minimum
mappedras = (ras - ras.minimum) / range
mappedras.save("RASTER1_MAP") # save as permanent raster


3) The Slice tool can be very handy for this task depending on whether you need the resolution (it outputs integers so there may be some accuracy issues with this approach).

mappedras = Slice(ras, 1001, "EQUAL_INTERVAL", 0) * .001
0 Kudos
RustyRex
Occasional Contributor
Perfect, guys.  Thanks also for the other tips.  I kept my expression more complex than it needed to be so I could keep everything straight and in case I wanted to scale within a range not quite as simple as the 0-1 I have now.  But for the code I will def. construct it the way you laid it out.
0 Kudos
RustyRex
Occasional Contributor
Thanks for the help.  What would the syntax of this code look like if i were to make it a python script to import as a script tool with input and output parameters?

Thanks.
0 Kudos
curtvprice
MVP Esteemed Contributor
What would the syntax of this code look like if i were to make it a python script to import as a script tool with input and output parameters?


The normal way to provide information to an arcpy script tool is to use GetParameterAsText(), setting up the input parameter as a Raster Layer, and the output as a Raster Dataset. Raster() is used to convert the input string to a raster data arcpy map algebra can work with.

import arcpy from arcpy.sa import * inRaster = arcpy.GetParameterAsText(0) # input raster layer name or raster dataset path outRaster = arcpy.GetParameterAsText(1) # output: raster dataset  ras = Raster(inRaster) range = ras.maximum - ras.minimum mappedras = (ras - ras.minimum) / range mappedras.save(outRaster)


Beyond that, you're kind of out of the scope of this thread.
0 Kudos