Select to view content in your preferred language

Converting an arcobject to a number

788
1
09-20-2011 03:15 PM
MichelleTotman1
Emerging Contributor
I've been stuck on this one for a while. Below is the script I want to be able to run, but the numbers ArcGIS is providing for me are actually imported to Python as arcobjects, so I cannot run the calculation at the end of my script. Can anyone tell me how to convert these arcobjects into numbers so I can use them in calculations? 

import arcpy
# Check out any necessary licenses
arcpy.CheckOutExtension("spatial")
#Establish Environmental Settings
arcpy.env.workspace = "C:/GIS_Rainier/Rainier.gdb"
arcpy.env.overwriteOutput = True
#Establish Input Raster
inRaster = "DEM10"
#Get Raster Properties
STD = arcpy.GetRasterProperties_management(inRaster, "STD")
Mean = arcpy.GetRasterProperties_management(inRaster, "MEAN")
#Check Data Values and Types
print Mean
print type(Mean)
print STD
print type(STD)
#Calculate z-score
outRaster = (inRaster - Mean)/STD

I also created the function as a model in ArcGIS using raster calculator, and it works when run as a model. If I export my model as a python script, I get what is copy and pasted below. This does not run properly.


# Import arcpy module
import arcpy

# Check out any necessary licenses
arcpy.CheckOutExtension("spatial")

# Script arguments
Input_raster = arcpy.GetParameterAsText(0)

rastercalc = arcpy.GetParameterAsText(1)
if rastercalc == '#' or not rastercalc:
    rastercalc = "C:\\GIS_Rainier\\Rainier.gdb\\rastercalc" # provide a default value if unspecified

# Local variables:
Mean = Input_raster
St_Dev = Input_raster

# Process: Get Raster Properties
arcpy.GetRasterProperties_management(Input_raster, "MEAN")

# Process: Get Raster Properties (2)
arcpy.GetRasterProperties_management(Input_raster, "STD")

# Process: Raster Calculator
arcpy.gp.RasterCalculator_sa("(\"%Input raster%\" - %Mean%) / %St Dev rastercalc)


I have a working model to do these calculations, so there is no urgency in getting an answer, I'd just be thrilled to learn the solution to my puzzle.

Thanks,
Michelle
Tags (2)
0 Kudos
1 Reply
Luke_Pinner
MVP Regular Contributor
There's a few issues:

1. You assign the mean & st_dev variables to a raster instead of assigning to the output of the GetRasterProperties tool. 

2. The GetRasterProperties tool returns a "Result" object which has a GetOutput method that you need to use to access the mean/std values.

3. Your calculation has a few errors - no terminating quote, the text doesn't match any of the variables you set previously, and the "rastercalc" raster has no operator linking it to the rest of the expression.

The following code works, but you'll need to check the actual expression as I stuck a "+" operator in front of the  "rastercalc" raster so the expression would actually run, substitute it with whatever operator you require.
#...
# Process: Get Raster Properties
Mean = float(arcpy.GetRasterProperties_management(Input_raster, "MEAN").getOutput(0))

# Process: Get Raster Properties (2)
St_Dev = float(arcpy.GetRasterProperties_management(Input_raster, "STD").getOutput(0))

# Process: Raster Calculator
calc='("%s" - %s) / %s + "%s"'%(Input_raster,Mean,St_Dev,rastercalc)
result=arcpy.gp.RasterCalculator_sa(calc)
print result.GetMessages()
print result.GetOutput(0)
0 Kudos