-
Notifications
You must be signed in to change notification settings - Fork 1
/
svf.py
181 lines (162 loc) · 7.07 KB
/
svf.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
"""
Relief Visualization Toolbox – Visualization Functions
RVT Sky-view factor esri raster function
rvt_py, rvt.vis.sky_view_factor
Credits:
Žiga Kokalj (ziga.kokalj@zrc-sazu.si)
Krištof Oštir (kristof.ostir@fgg.uni-lj.si)
Klemen Zakšek
Peter Pehani
Klemen Čotar
Maja Somrak
Žiga Maroh
Copyright:
2010-2020 Research Centre of the Slovenian Academy of Sciences and Arts
2016-2020 University of Ljubljana, Faculty of Civil and Geodetic Engineering
"""
import numpy as np
import rvt.vis
import rvt.blend_func
class RVTSvf:
def __init__(self):
self.name = "RVT Sky-view factor"
self.description = "Calculates Sky-view factor."
# default values
self.nr_directions = 16.
self.max_rad = 10.
self.noise = "0-don't remove"
self.padding = int(self.max_rad)
# 8bit (bytscale) parameters
self.calc_8_bit = True
self.mode_bytscl = "value"
self.min_bytscl = 0.6375
self.max_bytscl = 1
def getParameterInfo(self):
return [
{
'name': 'raster',
'dataType': 'raster',
'value': None,
'required': True,
'displayName': "Input Raster",
'description': "Input raster for which to create the sky-view factor map."
},
{
'name': 'calc_8_bit',
'dataType': 'boolean',
'value': self.calc_8_bit,
'required': False,
'displayName': "Calculate 8-bit",
'description': "If True it returns 8-bit raster (0-255)."
},
{
'name': 'nr_directions',
'dataType': 'numeric',
'value': self.nr_directions,
'required': False,
'displayName': "Number of directions",
'description': "Number of directions."
},
{
'name': 'max_rad',
'dataType': 'numeric',
'value': self.max_rad,
'required': False,
'displayName': "Max radius",
'description': "Maximal search radius in pixels."
},
{
'name': 'noise_remove',
'dataType': 'string',
'value': self.noise,
'required': False,
'displayName': "Noise removal",
'domain': ("0-don't remove", "1-low", "2-med", "3-high"),
'description': ("The level of noise remove (0-don't remove, 1-low, 2-med, 3-high).")
}
]
def getConfiguration(self, **scalars):
self.prepare(nr_directions=scalars.get('nr_directions'), max_rad=scalars.get("max_rad"),
noise=scalars.get("noise_remove"), calc_8_bit=scalars.get("calc_8_bit"))
return {
'compositeRasters': False,
'inheritProperties': 2 | 4,
'invalidateProperties': 2 | 4 | 8,
'inputMask': False,
'resampling': False,
'padding': self.padding,
'resamplingType': 1
}
def updateRasterInfo(self, **kwargs):
kwargs['output_info']['bandCount'] = 1
r = kwargs['raster_info']
kwargs['output_info']['noData'] = np.nan
if not self.calc_8_bit:
kwargs['output_info']['pixelType'] = 'f4'
else:
kwargs['output_info']['pixelType'] = 'u1'
kwargs['output_info']['histogram'] = ()
kwargs['output_info']['statistics'] = ()
return kwargs
def updatePixels(self, tlc, shape, props, **pixelBlocks):
dem = np.array(pixelBlocks['raster_pixels'], dtype='f4', copy=False)[0] # Input pixel array.
dem = change_0_pad_to_edge_pad(dem, self.padding)
pixel_size = props['cellSize']
if (pixel_size[0] <= 0) | (pixel_size[1] <= 0):
raise Exception("Input raster cell size is invalid.")
no_data = props["noData"]
if no_data is not None:
no_data = props["noData"][0]
dict_svf = rvt.vis.sky_view_factor(dem=dem, resolution=pixel_size[0], compute_svf=True, compute_asvf=False,
compute_opns=False, svf_n_dir=self.nr_directions, svf_r_max=self.max_rad,
svf_noise=self.noise, no_data=no_data)
svf = dict_svf["svf"][self.padding:-self.padding, self.padding:-self.padding] # remove padding
if self.calc_8_bit:
svf = rvt.blend_func.normalize_image(visualization="sky-view factor", image=svf,
min_norm=self.min_bytscl, max_norm=self.max_bytscl,
normalization=self.mode_bytscl)
svf = rvt.vis.byte_scale(data=svf, no_data=no_data)
pixelBlocks['output_pixels'] = svf.astype(props['pixelType'], copy=False)
return pixelBlocks
def updateKeyMetadata(self, names, bandIndex, **keyMetadata):
if bandIndex == -1:
name = "SVF_R{}_D{}".format(self.max_rad, self.nr_directions)
if self.calc_8_bit:
keyMetadata['datatype'] = 'Processed'
name += "_8bit"
else:
keyMetadata['datatype'] = 'Generic'
keyMetadata['productname'] = 'RVT {}'.format(name)
return keyMetadata
def prepare(self, nr_directions=16, max_rad=10, noise="0", calc_8_bit=False):
self.nr_directions = int(nr_directions)
self.max_rad = int(max_rad)
self.noise = int(noise[0])
self.padding = int(max_rad)
self.calc_8_bit = calc_8_bit
def change_0_pad_to_edge_pad(dem, pad_width):
if np.any(dem[0:pad_width, :]) or np.any(dem[-pad_width:, :]) or\
np.any(dem[:, 0:pad_width]) or np.any(dem[:, -pad_width:]):
return dem
dem = dem[pad_width:-pad_width, pad_width:-pad_width] # remove esri 0 padding
dem = np.pad(array=dem, pad_width=pad_width, mode="edge") # add new edge padding
return dem
def change_0_pad_to_edge_pad(dem, pad_width):
dem_out = dem.copy()
if not np.any(dem[:pad_width, :]): # if all top padding zeros
dem_out = dem_out[pad_width:, :] # remove esri 0 padding top
# pad top
dem_out = np.pad(array=dem_out, pad_width=((pad_width,0), (0,0)), mode="edge")
if not np.any(dem[-pad_width:, :]): # if all bottom padding zeros
dem_out = dem_out[:-pad_width, :] # remove esri 0 padding bottom
# pad bottom
dem_out = np.pad(array=dem_out, pad_width=((0, pad_width), (0, 0)), mode="edge")
if not np.any(dem[:, :pad_width]): # if all left padding zeros
dem_out = dem_out[:, pad_width:] # remove esri 0 padding left
# pad left
dem_out = np.pad(array=dem_out, pad_width=((0, 0), (pad_width, 0)), mode="edge")
if not np.any(dem[:, -pad_width:]): # if all right padding zeros
dem_out = dem_out[:, :-pad_width] # remove esri 0 padding right
# pad right
dem_out = np.pad(array=dem_out, pad_width=((0, 0), (0, pad_width)), mode="edge")
return dem_out