reclassify items in list based values in dictionary

3702
2
Jump to solution
06-01-2015 12:33 AM
Leo_KrisPalao
New Contributor II

Dear ArcPy/Python Users,

I am working on four MODIS datasets on the same tile. I reclassified the map into boolean (1 and 0) based on some criteria. My objective is to have a reclassified map which contains some values based on the combination of values for each pixel from my four MODIS dataset. For instance, each pixel of my four rasters would contain one of the possible combinations, i.e., (1,1,0,0) the values are based on the sequence of my rasters -- (pixel value raster 1, pixel value raster 2, ...).

How can I convert this to 1100?

Another example would be:

>>> a = np.array([1,2,3,4])

>>> b = np.array([5,6,7,8])

>>> c = np.array([9,10,11,12])

>>> d = np.array([4,7,8,9])

>>> x

     array([[[ 1,  5,  9,  4],

          [ 2,  6, 10,  7],

          [ 3,  7, 11,  8],

          [ 4,  8, 12,  9]]])

convert the items to 1594, 26107, 37118, 48129

then for instance 1594 will be reclassified to say 2120

The reason is that, I have a dictionary variable (see below) which will reclassify the values for each possible combination. So in the end I will have an array of, i.e., from 1100 to 2120.

The corresponding values are representative on the number of occurrence and the sequence of that event. For instance the 1100 will be reclassified into 2120 -- this means the event of interest occurred twice in week 1 and week 2.

{1000:1100,

  0100:1200,

  0010:0130,

  0001:1400,

  1001:2140,

  1100:2120,

  0110:2230,

  0011:2340,

  1011:3134,

  1101:3124,

  1110:3123,

  0111:3234,

  1111:4111,

  0000:1

255,255,255,255 = 0}

Here is the code that I am trying to complete and run but I am stuck now:

# import arcpy module of arcgis
import arcpy, os, sys
from arcpy import env

# import spatial extension
from arcpy.sa import *
arcpy.env.overwriteOutput = True

# activate spatial extension
arcpy.CheckOutExtension("Spatial")

# environment settings
arcpy.env.extent = "MAXOF"

# workspace
arcpy.env.workspace = r'raster\directory'
rasters = arcpy.ListRasters("*", "tif")

# output workspace
out_ws = 'output/workspace/'

rastersCount = len(rasters)

counter = 0 # starting index

pixReclass = {1000:1100,
              0100:1200,
              0010:0130,
              0001:1400,
              1001:2140,
              1100:2120,
              0110:2230,
              0011:2340,
              1011:3134,
              1101:3124,
              1110:3123,
              0111:3234,
              1111:4111,
              0000:0}
   
# compute dekad rasters
while counter < 4: # I am processing/testing only the first four rasters in my list
  rast = []
  rastMon = []

  for i in range(counter,counter+4):
       #print rasters
       rast.append(rasters)
       print "rasters:{}".format(rast)
       print len(rast)
   
  for r in rast:
       name, ext = r.split(".")
       crit_Out = out_ws + name + "_reclass.tif"
   
       criteria = Raster(r) >= 0001
       criteria.save(crit_Out)
       print criteria
   
       vRast = arcpy.RasterToNumPyArray(crit_Out)
       rastMon.append(vRast)
       print "number of array:{}".format(len(rastMon))
   
  zipped = np.dstack((rastMon[0], rastMon[1], rastMon[2], rastMon[3]))
   
  counter += 4

Hope that I made my query clear.

Thanks for any advice and suggestion.

-Leo

Message was edited by: Leo Kris Palao

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
SepheFox
Frequent Contributor

Hi Leo,  hope I understand your question correctly. How I would approach this would be to use the Times tool (help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//009z0000009p000000.htm). Multiply raster 1 by 1000, raster 2 by 100, raster 3 by 10. Then use Raster Calculator (help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//009z0000009p000000.htm) to add the values of the 4 rasters together. You can then reclass the values of the result using your dictionary.

Note: not sure what's going on with the links today. Usually pasting them results in a clickable link, but that doesn't appear to be happening as usual.

View solution in original post

2 Replies
DarrenWiens2
MVP Honored Contributor

To address your basic problem of concatenating list items, you can use .join():

Strings:

>>> myList = ['1','2','3','4']
>>> ''.join(myList)
'1234'

Integers (uses map(), which first calls str() for each item in myList):

>>> myList = [1,2,3,4]
>>> ''.join(map(str,myList))
'1234'
SepheFox
Frequent Contributor

Hi Leo,  hope I understand your question correctly. How I would approach this would be to use the Times tool (help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//009z0000009p000000.htm). Multiply raster 1 by 1000, raster 2 by 100, raster 3 by 10. Then use Raster Calculator (help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//009z0000009p000000.htm) to add the values of the 4 rasters together. You can then reclass the values of the result using your dictionary.

Note: not sure what's going on with the links today. Usually pasting them results in a clickable link, but that doesn't appear to be happening as usual.