1

I am trying to normalize the raster in arcpy by unchanged location over time. As such, I

  1. read the raster ('inRaster')
  2. read unchanged locations as polygon ('inPoly')
  3. Extract by mask unchanged location
  4. calculate MEAN and SD of unchanged extracted land
  5. normalize original raster by values of unchanged land

Everything works great until step 5, where I got an error:

RuntimeError: ERROR 000732: Input Raster: Dataset 0.152711537046664 does not exist or is not supported

Well, my Input Raster had somehow to changed, as the Dataset 0.152711537046664 represents exactly my mean value... When I let to print all values (mean, SD, inRasterandoutRaster), my input variables seem correct:

 inRaster is C:/Users/ndvi_0419
 Mean is 0.152711537046664
 SD is 2.85148713249969E-02
 outRaster is C:/Users/norm_ndvi_0419

For normalization formula, I am using arcpy.sa algebra, suggested here: Using Loop with Raster Calculator in ArcPy?

normRaster = (Raster(inRaster) - rMean)/rSD

Do you know what could be the issue in my code?


My whole code:

# Import modules
import itertools, arcpy, os

# import envronmental settings
from arcpy import env
from arcpy.sa import *

# Define working environment
inWD = "C:/Users/analyzed"

# Set Snap Raster environment
arcpy.env.snapRaster = os.path.join(inWD, "rectif.gdb/r_20160502")

# Define variables
inRaster = os.path.join(inWD, "VI.gdb", "ndvi_0419")
inPoly = os.path.join(inWD, "normBlackSquare.gdb", "n_0419")

# Check out the ArcGIS Spatial Analyst extension license
arcpy.CheckOutExtension("Spatial")

# Execute ExtractByMask
outExtractByMask = arcpy.sa.ExtractByMask(inRaster, inPoly)

#Get raster properties: Mean and STD
rMean = arcpy.GetRasterProperties_management(outExtractByMask, "MEAN")
rSD = arcpy.GetRasterProperties_management(outExtractByMask, "STD")

# Save the output
outName = os.path.basename(inRaster)
outRaster = os.path.join(inWD, "normVI.gdb", outName)


# Print values to check if inputs are correct
print "inRaster is " + inRaster
print "Mean is " + str(rMean)
print "SD is " + str(rSD)
print "outRaster is " + outRaster


# This formula seems not to work right!!!
normRaster = (Raster(inRaster) - rMean)/rSD
#normRaster.save(outRaster)
# print "Normalized raster saved"


#Return (check-in) the spatial extension
arcpy.CheckInExtension("spatial")
1

Well, it seems that I have solved my issue.

I have realized that the problem is only in final "Normalization part", i.e. in this part of a code:

normRaster = (Raster(inRaster) - rMean)/rSD

I have tested if the code will work well with hard coded values instead of calculated mean and SD:

normRaster = (Raster(inRaster) - 5)/1

and this worked well. So, i have realized, that my problem is in the declaration of the mean and SD values. I needed to:

  • correctly declare the output from arcpy.getRasterProperties_management using file.getOutput(0)

    # Get output of GetRasterProperties
    rMeanResult = arcpy.GetRasterProperties_management(outExtractByMask, "MEAN")
    rMean = rMeanResult.getOutput(0)
    
  • and declare them as float numbers:

    outRaster = (Raster(inRaster) - float(rMean))/float(rSD)
    

And it worked!!


Whole script:

Normalize rasters over time based on pixels values of unchanged area

# ----------------------------
# Import modules
import itertools, arcpy, os

# import envronmental settings
from arcpy import env
from arcpy.sa import *


# Allow file to overwrite
arcpy.env.overwriteOutput = True


# Define working environment
inWD = "C:/Users/Projects/analyzed"

# Define variables
inRaster = os.path.join(inWD, "VI.gdb", "ndvi_0419")
inPoly = os.path.join(inWD, "normBlackSquare.gdb", "n_0419")

# Check out the ArcGIS Spatial Analyst extension license
arcpy.CheckOutExtension("Spatial")

# Execute ExtractByMask
outExtractByMask = arcpy.sa.ExtractByMask(inRaster, inPoly)

#Get raster properties: Mean and SD (STD)
rMeanResult = arcpy.GetRasterProperties_management(outExtractByMask, "MEAN")
rMean = rMeanResult.getOutput(0)

rSDResult = arcpy.GetRasterProperties_management(outExtractByMask, "STD")
rSD = rSDResult.getOutput(0)

# Save the output
outName = os.path.basename(inRaster)
print "out raster basename is " + outName


# Normalize original raster
print "inRaster is " + inRaster
print "Mean is " + str(rMean)
print "SD is " + str(rSD)
outRaster = (Raster(inRaster) - float(rMean))/float(rSD)

# Name and save normalized output raster
outRasterPath = os.path.join(inWD, "normVI.gdb", "n_" + outName)
outRaster.save(outRasterPath)
print "Normalized raster saved"


#Return (check-in) the spatial extension
arcpy.CheckInExtension("spatial")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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