1

I am performing a simple for-loop in ArcGIS calculator through python. I want to generate transition maps using cover classes from year one (r2005) to year two (r2009). Here is the code:

# Importing libraries

import arcpy, sys, string, os, arcgisscripting, dircache
import numpy as np
from arcpy import env
from arcpy.sa import *

# Creating the geoprocessor object
gp = arcgisscripting.create()

# Setting environment
arcpy.env.compression = "LZW"

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

# Reading data
classes = np.genfromtxt("Globcover_Legend_Neotropics.txt", dtype="int")
r2005 = Raster("2005_Clip.tif")
r2009 = Raster("2009_Clip.tif")

# Creating transition rasters
for i in classes:
    for j in classes:       
        #OPTION1
        #outEqualTo1 = r2005==i
        #outEqualTo2 = r2009==j
        #transition_raster = (outEqualTo1) & (outEqualTo2)
        #OPTION2
        transition_raster = (r2005 == i) & (r2009 == j)
        transition_raster.save(str(i)+"_"+str(j)+".tif")
        print (str(i)+"_"+str(j))

The object "classes" is a numpy array containing the cover classes to compare:

>>> classes
array([ 14,  20,  30,  40,  50,  60,  70, 100, 110, 120, 130, 140, 150,
       160, 170, 180, 190, 200, 210, 220])

When I run the code using any element in the loop, it runs perfectly. For example:

>>> i=j=14
>>> transition_raster = (r2005 == i) & (r2009 == j)
>>> transition_raster.save(str(i)+"_"+str(j)+".tif")

However, when I run the complete for-loop I get this error:

Traceback (most recent call last):
  File "D:\Cover\GlobCover\transitions.py", line 31, in <module>
    transition_raster = (r2005 == i) & (r2009 == j)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\sa\Functions.py", line 3831, in EqualTo
    in_raster_or_constant2)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\sa\Utils.py", line 47, in swapper
    result = wrapper(*args, **kwargs)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\sa\Functions.py", line 3828, in Wrapper
    ["EqualTo", in_raster_or_constant1, in_raster_or_constant2])
TypeError: expected a raster or layer name

I've tried with the two options in the code (see code) and I get the same error. Any idea about it? Thanks in advance and cheers!

Jaime
  • 111
  • 7
  • You could try `(r2005 == int(i)) & (r2009 == int(j))` to make the types match the code you know works, but I don't know if that would help or not. – cxw Aug 25 '16 at 13:51
  • Or try `for i in np.nditer(classes)`, ditto for `j`, per [this](http://docs.scipy.org/doc/numpy/reference/arrays.nditer.html#single-array-iteration) – cxw Aug 25 '16 at 13:55
  • That was all! Thanks! – Jaime Aug 25 '16 at 13:57
  • The first one. Thanks. – Jaime Aug 25 '16 at 20:24

1 Answers1

0

You know that the value 14 works, but you are getting a TypeError with whatever the loop over classes is returning. The type of 14 is int, so try typecasting i and j to int before making the comparison:

for i in classes:
    for j in classes:
        transition_raster = ( (r2005 == int(i)) & (r2009 == int(j)) )
        # added typecasts:              ^^^^ ^              ^^^^ ^
        ...
cxw
  • 16,685
  • 2
  • 45
  • 81