[GIS] TypeError: unsupported operand type(s) for -: ‘str’ and ‘int’

arcpypython-2.7raster-calculatorspatial-analyst

I have 732 temperature rasters (366 min and 366 max temp) that I want to manipulate depending on if the max temperature was greater than a specified and constant crop category temperature.

import os
import arcpy
import sys

if arcpy.CheckExtension("Spatial") == "Available":
    arcpy.AddMessage("Checking out Spatial Analyst")
    arcpy.CheckOutExtension("Spatial")
else:
    arcpy.AddError("Unable to get spatial analyst extension")
    sys.exit(0)
'''
HU formula if Max Temp > Max Crop Temp HU formula if Max Temp < Max Crop Temp
1   85  (85-(MaxTemp - 85) + MinTemp)/2) - 45   ((MaxTemp + MinTemp)/2) - 45
2   75  (75-(MaxTemp - 75) + MinTemp)/2) - 40   ((MaxTemp + MinTemp)/2) - 40
3   75  (75-(MaxTemp - 75) + MinTemp)/2) - 45   ((MaxTemp + MinTemp)/2) - 45
4   80  (80-(MaxTemp - 80) + MinTemp)/2) - 50   ((MaxTemp + MinTemp)/2) - 50
5   95  (95-(MaxTemp - 95) + MinTemp)/2) - 50   ((MaxTemp + MinTemp)/2) - 50
6   90  (90-(MaxTemp - 90) + MinTemp)/2) - 50   ((MaxTemp + MinTemp)/2) - 50
7   90  (90-(MaxTemp - 90) + MinTemp)/2) - 60   ((MaxTemp + MinTemp)/2) - 60
8   80  (80-(MaxTemp - 80) + MinTemp)/2) - 65   ((MaxTemp + MinTemp)/2) - 65
9   95  (95-(MaxTemp - 95) + MinTemp)/2) - 65   ((MaxTemp + MinTemp)/2) - 65
'''
DIR = os.path.dirname(__file__)
xlsFilePath = os.path.join(DIR, '..', 'resources', 'xls')
INPUT_NORMAL_HEATUNIT_RAST_MAX_FOLDER = os.path.join(DIR, '..', 'resources',      'input', 'max')
INPUT_NORMAL_HEATUNIT_RAST_MIN_FOLDER = os.path.join(DIR, '..', 'resources', 'input', 'min')
OUTPUT = os.path.join(DIR, '..', 'resources', 'output')

for day in range(0, 367):
    maxNormName = 'Nor_Max_{0}.tif'.format(str(day).zfill(3))
    minNormName = 'Nor_Min_{0}.tif'.format(str(day).zfill(3))
    maxNormFile = os.path.join(INPUT_NORMAL_HEATUNIT_RAST_MAX_FOLDER, maxNormName)
     minNormFile = os.path.join(INPUT_NORMAL_HEATUNIT_RAST_MIN_FOLDER, minNormName)
    categories = [(maxNormFile, minNormFile, 85, 45, str(1)), (maxNormFile, minNormFile, 75, 40, str(2)),
              (maxNormFile, minNormFile, 75, 45, str(3)), (maxNormFile, minNormFile, 80, 50, str(4)),
              (maxNormFile, minNormFile, 95, 50, str(5)), (maxNormFile, minNormFile, 90, 50, str(6)),
              (maxNormFile, minNormFile, 90, 60, str(7)),
              (maxNormFile, minNormFile, 80, 65, str(8)),
              (maxNormFile, minNormFile, 95, 65, str(9))]

for category in categories:
    print "Con(\"%{0}%\" > {2}, ((({2}-(\"%{0}%\"-{2}))+\"%{1}%\")/2)-{3}, ((\"%{0}%\" + \"%{1}%\")/2)-{3})".format(
        *category)

    output = 'Nor_HU{0}_{1}.tif'.format(category[4], str(day).zfill(3))
    outputLocation = os.path.join(OUTPUT, output)

    arcpy.gp.RasterCalculator_sa(
        "Con(\"%{0}%\" > {2}, ((({2}-(\"%{0}%\"-{2}))+\"%{1}%\")/2)-{3}, ((\"%{0}%\" + \"%{1}%\")/2)-{3})".format(
            *category),
        outputLocation)

When I run this, I get a type error pointing to the last line where I am using a con statement in raster calculator. My console output is:

Con("%C:\Users\uma.bhandaram\Desktop\Peak\peakseason\normalCalc..\resources\input\max\Nor_Max_000.tif%" > 85, (((85-("%C:\Users\uma.bhandaram\Desktop\Peak\peakseason\normalCalc..\resources\input\max\Nor_Max_000.tif%"-85))+"%C:\Users\uma.bhandaram\Desktop\Peak\peakseason\normalCalc..\resources\input\min\Nor_Min_000.tif%")/2)-45, (("%C:\Users\uma.bhandaram\Desktop\Peak\peakseason\normalCalc..\resources\input\max\Nor_Max_000.tif%" + "%C:\Users\uma.bhandaram\Desktop\Peak\peakseason\normalCalc..\resources\input\min\Nor_Min_000.tif%")/2)-45)

This looks right to me but clearly it's not. Where am I going wrong?

Best Answer

Without knowing the type error, i would suggest the following:

  • Catch the exception and post more infos.
  • Copy the data to c:\temp as this will shorten the paths and will make errors more visible, as it prevents [...] in the path
  • Try to set the values to a floating point number, by adding a .0 so that 85 (integer or string (?)) becomes 85.0 (float)