Quantcast
Channel: Forums - Python
Viewing all articles
Browse latest Browse all 2485

raster calculation in python

$
0
0
Hi ArcGIS Python experts,

I am working on automating my workflow for a project. For now I am testing my script for only five rasters, then scale-up my analysis if the script will work okay. Once the code is already running, I will incorporate it in another code that I am setting up. I want to get the percentage of each cell using raster calculator. I got the initial code from another thread.

The equation that I am trying to execute is (raster1) / (a specified value) * 100 # computing for percentage

The workflow looks like:
RE_suitable_dek001 / 11.2 * 100
RE_suitable_dek002 / 14.9 * 100
RE_suitable_dek003 / 9.4 * 100
RE_suitable_dek004 / 10.4 * 100
RE_suitable_dek005 / 12.4 * 100

Some details:
Software: ArcGIS 10.2
OS: Windows 7 Prof 64 bit
Python console: PythonWin

Code:

import arcpy, os, sys
from arcpy import env
from arcpy.sa import *
arcpy.env.overwriteOutput = True
arcpy.CheckOutExtension("Spatial")
arcpy.env.workspace = r'E:\Test_awd\awd_pct'

# Raster workspace
raster_ws = r'E:\Test_awd\awd_pct'

# Getting the list of raster dataset
arcpy.env.workspace = raster_ws
rasters = [os.path.join(raster_ws, r) for r in arcpy.ListRasters()]

#Output workspace
raster_ow = r'E:\Test_awd\awd_pct'

# Get dictionary (raster1:value, ..., raster5:value)
rastVal = {"RE_suitable_dek001":11.2,"RE_suitable_dek002":14.9, "RE_suitable_dek003":9.4, "RE_suitable_dek004":10.4, "RE_suitable_dek005":12.4}

# Raster map algebra
for rast in rastVal: # Iterate to all raster in the directory
  outRaster = os.path.join(raster_ow, os.path.basename(rast)) # output path and filename of my raster
  if rast in rastVal: # iterate my raster if in the dictionary
      outRaster = rasters / rastVal[rast] * 100 # the equation that I want to execute...(raster1/11.2)*100
      outRaster.save(outRaster + "pct") # output raster with pct (percentage) suffix
  else:
      print "ERROR: No matching entry in raster value look up dictionary!"


This is the error that I am getting:
Traceback (most recent call last):
File "C:\Python27\ArcGIS10.2\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 326, in RunScript
exec codeObject in __main__.__dict__
File "E:\Python_scripts\ArcGIS\AWD_pct_v2.py", line 25, in <module>
outRaster = rasters / rastVal[rast] * 100 # the equation that I want to execute...(raster1/11.2)*100
TypeError: unsupported operand type(s) for /: 'list' and 'float'

Thanks,
-Leo

Viewing all articles
Browse latest Browse all 2485

Trending Articles