-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathSelectByPixelSize.py
95 lines (83 loc) · 3.66 KB
/
SelectByPixelSize.py
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
import numpy as np
import utils
class SelectByPixelSize():
def __init__(self):
self.name = "Select by Pixel Size"
self.description = "This function returns pixels associated with one of two input rasters based on the request resolution."
self.threshold = 0.0
self.inBands1, self.inBands2, self.outBands = 1, 1, 1
self.trace = utils.Trace()
def getParameterInfo(self):
return [
{
'name': 'r1',
'dataType': 'raster',
'value': None,
'required': True,
'displayName': "Raster 1",
'description': ("The raster that's returned when request cell size is lower than "
"the 'Cell Size Threshold'. A lower cell size value implies finer resolution.")
},
{
'name': 'r2',
'dataType': 'raster',
'value': None,
'required': True,
'displayName': "Raster 2",
'description': ("The raster that's returned when request cell size is higher than or equal to "
"the 'Cell Size Threshold'. A higher cell size value implies coarser resolution.")
},
{
'name': 'threshold',
'dataType': 'numeric',
'value': 0.0,
'required': True,
'displayName': "Cell Size Threshold",
'description': ("The cell size threshold that controls which of the two input "
"rasters contributes pixels to the output.")
},
]
def getConfiguration(self, **scalars):
return {
'inputMask': True,
'resampling': True
}
def updateRasterInfo(self, **kwargs):
self.threshold = float(kwargs.get('threshold', 0.0))
if self.threshold <= 0.0:
self.threshold = np.mean((np.mean(kwargs['r1_info']['cellSize']), np.mean(kwargs['r2_info']['cellSize'])))
self.inBands1 = kwargs['r1_info']['bandCount']
self.inBands2 = kwargs['r2_info']['bandCount']
kwargs['output_info']['bandCount'] = min(self.inBands1, self.inBands2)
kwargs['output_info']['statistics'] = ()
kwargs['output_info']['histogram'] = ()
self.trace.log("Trace|Threshold cell-size|{0}\n".format(self.threshold))
self.trace.log("Trace|output_info|{0}\n".format(kwargs['output_info']))
return kwargs
def selectRasters(self, tlc, shape, props):
cellSize = props['cellSize']
v = 0.5 * (cellSize[0] + cellSize[1])
if v < self.threshold:
return ('r1',)
else: return ('r2',)
def updatePixels(self, tlc, shape, props, **pixelBlocks):
cellSize = props['cellSize']
v = 0.5 * (cellSize[0] + cellSize[1])
self.trace.log("Trace|Request cell-size|{0}\n".format(v))
if v < self.threshold:
sPixels = 'r1_pixels'
sMask = 'r1_mask'
nBands = self.inBands1
else:
sPixels = 'r2_pixels'
sMask = 'r2_mask'
nBands = self.inBands2
if self.outBands == nBands:
p = pixelBlocks[sPixels]
m = pixelBlocks[sMask]
else:
p = pixelBlocks[sPixels][0:self.outBands, :, :]
m = pixelBlocks[sMask][0:self.outBands, :, :]
pixelBlocks['output_pixels'] = p.astype(props['pixelType'])
pixelBlocks['output_mask'] = m.astype('u1')
return pixelBlocks