Reclassify Rasters... part II ... the "Special Analyst"

787
0
02-22-2018 06:07 PM
Labels (1)
DanPatterson_Retired
MVP Emeritus
1 0 787

The original Reclassify Rasters Simply... 

Interesting if you have a function that would be a pain to implement in the spatial analyst since it would take a number of steps... ufunc ... Example follows.  Multiple output formats possible

 

ufunc .... python user functions with numpy... np.frompyfunc

a = np.arange(1, 101).reshape(10, 10)  # ---- make a raster

a
array([[  1,   2,   3,   4,   5,   6,   7,   8,   9,  10],
       [ 11,  12,  13,  14,  15,  16,  17,  18,  19,  20],
       [ 21,  22,  23,  24,  25,  26,  27,  28,  29,  30],
       [ 31,  32,  33,  34,  35,  36,  37,  38,  39,  40],
       [ 41,  42,  43,  44,  45,  46,  47,  48,  49,  50],
       [ 51,  52,  53,  54,  55,  56,  57,  58,  59,  60],
       [ 61,  62,  63,  64,  65,  66,  67,  68,  69,  70],
       [ 71,  72,  73,  74,  75,  76,  77,  78,  79,  80],
       [ 81,  82,  83,  84,  85,  86,  87,  88,  89,  90],
       [ 91,  92,  93,  94,  95,  96,  97,  98,  99, 100]])

# ---- a weird request to reclass the data, expressed as a func ----

def reclass_2(a):
    '''reclassification using ufunc user functions'''
    if a > 80 and a != 91:             # ---- the first reclass option
        a = 8
    elif a > 60 and a <= 80:           # ---- another simple ont
        a = 7
    elif (a <= 60) and (a % 2 == 0):   # ---- now this one is a kicker
        a = 2
    else:                              # ---- I could continue, but I won't
        a = 0
    return a


# ---- Now... make the ufunc using numpy's frompyfunc
# frompyfunc(name of the def, number of input parameters in, results out)

r = np.frompyfunc(reclass_2, 1, 1)     # ---- function made

r                                      # ---- not much info
<ufunc '? (vectorized)'>

# ---- Make it do some work by passing the array

val =r(a)                              # ---- the array, 1 input, 1 output

array([[0, 2, 0, 2, 0, 2, 0, 2, 0, 2],
       [0, 2, 0, 2, 0, 2, 0, 2, 0, 2],
       [0, 2, 0, 2, 0, 2, 0, 2, 0, 2],
       [0, 2, 0, 2, 0, 2, 0, 2, 0, 2],
       [0, 2, 0, 2, 0, 2, 0, 2, 0, 2],
       [0, 2, 0, 2, 0, 2, 0, 2, 0, 2],
       [7, 7, 7, 7, 7, 7, 7, 7, 7, 7],
       [7, 7, 7, 7, 7, 7, 7, 7, 7, 7],
       [8, 8, 8, 8, 8, 8, 8, 8, 8, 8],
       [0, 8, 8, 8, 8, 8, 8, 8, 8, 8]], dtype=object)

# ---- return as integer
val.astype(np.int32)
Out[8]: 
array([[0, 2, 0, 2, 0, 2, 0, 2, 0, 2],
       [0, 2, 0, 2, 0, 2, 0, 2, 0, 2],
       [0, 2, 0, 2, 0, 2, 0, 2, 0, 2],
       [0, 2, 0, 2, 0, 2, 0, 2, 0, 2],
       [0, 2, 0, 2, 0, 2, 0, 2, 0, 2],
       [0, 2, 0, 2, 0, 2, 0, 2, 0, 2],
       [7, 7, 7, 7, 7, 7, 7, 7, 7, 7],
       [7, 7, 7, 7, 7, 7, 7, 7, 7, 7],
       [8, 8, 8, 8, 8, 8, 8, 8, 8, 8],
       [0, 8, 8, 8, 8, 8, 8, 8, 8, 8]])

# ---- how about float?
val.astype(np.float32)

array([[ 0.,  2.,  0.,  2.,  0.,  2.,  0.,  2.,  0.,  2.],
       [ 0.,  2.,  0.,  2.,  0.,  2.,  0.,  2.,  0.,  2.],
       [ 0.,  2.,  0.,  2.,  0.,  2.,  0.,  2.,  0.,  2.],
       [ 0.,  2.,  0.,  2.,  0.,  2.,  0.,  2.,  0.,  2.],
       [ 0.,  2.,  0.,  2.,  0.,  2.,  0.,  2.,  0.,  2.],
       [ 0.,  2.,  0.,  2.,  0.,  2.,  0.,  2.,  0.,  2.],
       [ 7.,  7.,  7.,  7.,  7.,  7.,  7.,  7.,  7.,  7.],
       [ 7.,  7.,  7.,  7.,  7.,  7.,  7.,  7.,  7.,  7.],
       [ 8.,  8.,  8.,  8.,  8.,  8.,  8.,  8.,  8.,  8.],
       [ 0.,  8.,  8.,  8.,  8.,  8.,  8.,  8.,  8.,  8.]], dtype=float32)

# ---- be the first on your block to have a string raster

val.astype(np.str)
Out[10]: 
array([['0', '2', '0', '2', '0', '2', '0', '2', '0', '2'],
       ['0', '2', '0', '2', '0', '2', '0', '2', '0', '2'],
       ['0', '2', '0', '2', '0', '2', '0', '2', '0', '2'],
       ['0', '2', '0', '2', '0', '2', '0', '2', '0', '2'],
       ['0', '2', '0', '2', '0', '2', '0', '2', '0', '2'],
       ['0', '2', '0', '2', '0', '2', '0', '2', '0', '2'],
       ['7', '7', '7', '7', '7', '7', '7', '7', '7', '7'],
       ['7', '7', '7', '7', '7', '7', '7', '7', '7', '7'],
       ['8', '8', '8', '8', '8', '8', '8', '8', '8', '8'],
       ['0', '8', '8', '8', '8', '8', '8', '8', '8', '8']],
      dtype='<U1')

‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

If you have any interesting examples that you try, pass them on

About the Author
Retired Geomatics Instructor at Carleton University. I am a forum MVP and Moderator. Current interests focus on python-based integration in GIS. See... Py... blog, my GeoNet blog...
Labels