Tuesday 2 April 2019

arcgis 10.0 - pair value comparison to generate new raster using arcpy/python



I want to write a function which takes in two arguments (from two rasters) and compare those 2 values against a set of hard coded pair values inside function. Then return a value which i want to use to generate a new grid. I am not quite there to complete the function. Any suggestions?


def grid(val1, val2):
table = {'10','1':'75','10,2':'75','10,3':'83','10,4':'87'}
for i, j, k in table.iteritems():
if val1 == i:
if val2 == j:
val = k
return val

t = grid(10,1)


Answer



You should be able to fairly easily grab your raster as a numpy array in ArcGIS (and for that matter put it back).


Then borrow liberally from the answer to this stack overflow question.


In your case you have two grids, each with their own corresponding value. Tuples can act as a dictionary key, so that's what I'd suggest in your case. Then we have a simple bit of code:


#grid_1 and grid_2 are numpy arrays of the input rasters
import numpy as np

table = {(10, 1):75,
(10, 2):75,
(10, 3):83,

(10, 4):87}

#assuming that the input grids are the same dimensions
output_grid = np.ones(grid_1.shape, dtype=np.int) * -9999 #usually a good value for null

for k, v in table.iteritems(): #k will be a tuple
output_grid[[grid_1 == k[0]] and [grid_2 == k[1]]] = v

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...