-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhillshader_process.py
245 lines (200 loc) · 10.6 KB
/
hillshader_process.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# -*- coding: utf-8 -*-
"""
/***************************************************************************
Batch_Hillshader
A QGIS plugin to generate a three light
exposure hillshade (shaded relief by
combining three light exposures)
For more information, see the program documentation.
If you uses as input LiDAR data, note that plugin uses LASTools library.
See LASTools License at <https://rapidlasso.com/lastools/>
Plugin also use in LiDAR data mode FUSION LDV.
See FUSION LDV License at <http://forsys.cfr.washington.edu/fusion.html>
-------------------
begin : 2016-07-13
git sha : $Format:%H$
copyright : (C) 2017 by PANOimagen S.L.
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <https://www.gnu.org/licenses/> *
***************************************************************************/
"""
import os
from . import hillshade as hill
from . import bandCalc
from .plugin_utils import raster_funs
from .plugin_utils import files_and_dirs_funs
class LiDAR2DEM(object):
def __init__(self, input_filename, out_path,
partials_create_and_load, size_dem, catalog_params=None):
"""Function to start class variables and launch the process
"""
self.input_file_full_path = input_filename
self.out_path = out_path
self.catalog_params = catalog_params
self.partials_create_and_load = partials_create_and_load
self.size_dem = size_dem
self.init_paths()
self.process()
def init_paths(self):
"""Function to init the output directory and the results full paths.
Also launch the folder creation
"""
self.path, self.file_name = os.path.split(self.input_file_full_path)
self.file_funs = files_and_dirs_funs.DirAndPaths()
self.input_base_name, self.input_ext = \
self.file_funs.init(self.file_name)
self.file_templates = self.file_funs.file_templates(
self.input_base_name)
self.temp_dirs, self.temp_paths = \
self.file_funs.set_temp_dir()
self.out_dirs, self.out_paths = \
self.file_funs.set_output_dir(
self.out_path)
dirs = []
if self.partials_create_and_load:
self.paths = {'las': self.out_paths['las'],
'dem': self.out_paths['dem']}
dirs.append(self.out_dirs['las'])
dirs.append(self.out_dirs['dem'])
else:
self.paths = {'las': self.temp_paths['las'],
'dem': self.temp_paths['dem']}
dirs.append(self.temp_dirs['temp_dir'])
dirs.append(self.temp_dirs['temp_dir'])
if not self.catalog_params is None:
self.paths['catalog'] = self.out_paths['catalog']
self.paths['las_ground'] = self.out_paths['las_ground']
dirs.append(self.out_dirs['catalog'])
dirs.append(self.out_dirs['las_ground'])
for v in dirs:
self.file_funs.create_dir(v)
def process(self):
"""This function runs the LiDAR processing
"""
if self.input_ext.lower() == '.laz':
self.las_full_path = self.paths['las']
lidar_funs.laz2las(self.input_file_full_path, self.las_full_path)
else:
self.las_full_path = self.input_file_full_path
if self.catalog_params is not None:
lidar_funs.filter_las_ground_points(
self.las_full_path, self.paths['las_ground'])
lidar_funs.create_catalog(self.paths['las_ground'],
self.catalog_params,
self.paths['catalog'])
lidar_funs.create_dem(
self.las_full_path, self.paths['dem'], self.size_dem)
class HillshaderDEM(object):
def __init__(self, full_filename, dem_array, no_data_value,
partialsCreateAndLoad, sombrasOutResults, hill_params,
out_path):
"""Function to start class variables and launch the process
"""
self.path, self.file_name = os.path.split(full_filename)
self.file_funs = files_and_dirs_funs.DirAndPaths()
self.out_path = out_path
self.input_base_name, _, = \
self.file_funs.init(self.file_name)
if not os.path.exists(self.out_path):
os.makedirs(self.out_path)
self.dem_full_path = full_filename
self.hill_params = hill_params
self.partials_create_and_load = partialsCreateAndLoad
self.sombras_out = sombrasOutResults
self.init_paths()
self.process(dem_array, no_data_value)
def init_paths(self):
"""Function to init the output directory and the results full paths.
"""
self.file_templates = self.file_funs.file_templates(
self.input_base_name)
self.temp_dirs, self.temp_paths = \
self.file_funs.set_temp_dir()
self.out_dirs, self.out_paths = \
self.file_funs.set_output_dir(self.out_path)
self.paths = {}
self.dirs = {}
self.paths['composed_hillshade'] = self.out_paths['composed_hillshade']
self.dirs['composed_hillshade'] = self.out_dirs['composed_hillshade']
self.file_funs.create_dir(self.dirs['composed_hillshade'])
if self.partials_create_and_load:
self.paths['simple_hillshade'] = self.out_paths['simple_hillshade']
self.dirs['simple_hillshade'] = self.out_dirs['simple_hillshade']
self.file_funs.create_dir(self.dirs['simple_hillshade'])
def process(self, dem_array, no_data_value):
"""This function works with the partial hillshades and generates the
composed hillshade
"""
hillshade_1_array = hill.hillshade(dem_array, no_data_value,
self.hill_params['azimuth1'],
self.hill_params['angle_altitude1'])
hillshade_2_array = hill.hillshade(dem_array, no_data_value,
self.hill_params['azimuth2'],
self.hill_params['angle_altitude2'])
hillshade_3_array = hill.hillshade(dem_array, no_data_value,
self.hill_params['azimuth3'],
self.hill_params['angle_altitude3'])
if self.partials_create_and_load:
eroded_1 = raster_funs.raster_erosion(
hillshade_1_array, dem_array, no_data_value)
hillshade1, hillshade1_filename = self.save_raster(
eroded_1,
self.hill_params['azimuth1'],
self.hill_params['angle_altitude1'])
eroded_2 = raster_funs.raster_erosion(
hillshade_2_array, dem_array, no_data_value)
hillshade2, hillshade2_filename = self.save_raster(
eroded_2,
self.hill_params['azimuth2'],
self.hill_params['angle_altitude2'])
eroded_3 = raster_funs.raster_erosion(
hillshade_3_array, dem_array, no_data_value)
hillshade3, hillshade3_filename = self.save_raster(
eroded_3,
self.hill_params['azimuth3'],
self.hill_params['angle_altitude3'])
self.partial_hills_dic = {hillshade1_filename: hillshade1,
hillshade2_filename: hillshade2,
hillshade3_filename: hillshade3}
for k, v in self.partial_hills_dic.items():
raster_funs.load_raster_layer(v, k)
three_exp_array = bandCalc.merge_arrays(
[hillshade_1_array, hillshade_2_array, hillshade_3_array],
[self.hill_params['transparency1'],
self.hill_params['transparency2'],
self.hill_params['transparency3']],
dem_array, no_data_value)
fn_hillshade_filename = \
self.file_templates['composed_hillshade'].format(
self.input_base_name)
raster_funs.array_2_raster(three_exp_array,
self.dem_full_path,
self.paths['composed_hillshade'])
if self.sombras_out:
raster_funs.load_raster_layer(
self.paths['composed_hillshade'], fn_hillshade_filename)
def save_raster(self, hillshade_array, azimuth, altitude):
"""This function save the arrays of the partial hillshades
"""
hillshade_filename = self.file_templates['simple_hillshade'].format(
self.input_base_name, azimuth, altitude)
hillshade_path = os.path.join(self.dirs['simple_hillshade'],
hillshade_filename)
raster_funs.array_2_raster(hillshade_array,
self.dem_full_path,
hillshade_path)
return hillshade_path, hillshade_filename