-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlaspy_funs.py
193 lines (152 loc) · 7.34 KB
/
laspy_funs.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
# -*- coding: utf-8 -*-
"""
/***************************************************************************
LiDARForestryHeight
A QGIS plugin. LiDAR Forestry Height
generates a DEM with the forest height,
calculated from a classified LiDAR point
cloud using LasPy Library
Generated by Plugin Builder: http://g-sherman.github.io/Qgis-Plugin-Builder/
-------------------
begin : 2018-09-24
copyright : (C) 2019 by PANOimagen S.L.
email : info@panoimagen.com
git sha : $Format:%H$
***************************************************************************/
/***************************************************************************
* *
* 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 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""
import numpy as np
import os
from .lfh_errors import LasPyNotFoundError
try:
import laspy
except ModuleNotFoundError:
raise LasPyNotFoundError
from laspy.file import File
from laspy.header import Header
from . import files_paths_funs as dir_fns
class LiDAR(object):
def __init__(self, in_las_path, out_path, partials_create):
""" Init variables
"""
self.in_las_path = in_las_path
self.out_path = out_path
self.partials_create = partials_create
_, self.filename = os.path.split(in_las_path)
self.dirs = dir_fns.DirAndPaths(self.filename, out_path)
self.read_las_file()
self.get_all_points()
self.get_scaled_points()
self.get_file_extent()
self.get_terrain_points_array()
self.get_first_returns_array()
self.in_file.close()
self.lidar_results = [self.terrain_arrays_list,
self.surfaces_arrays_list,
self.las_file_extent]
# TODO: Si .laz descomprimir
def read_las_file(self):
""" Read the input LiDAR file in las format. Not laz format
"""
try:
self.in_file = File(self.in_las_path, mode='r')
except OSError:
raise OSError(u"LiDAR Forestry Height can't open the file.\n" +
u"Please try again or with other LiDAR file")
self.scale = self.in_file.header.scale
self.offset = self.in_file.header.offset
def get_all_points(self):
""" Get points for file (points information and coordinates)
"""
self.points_array = self.in_file.get_points()
self.points_number = len(self.in_file)
def get_file_extent(self):
""" Get extent of the lidar file
"""
self.las_file_extent = [(max(self.x_dimension), max(self.y_dimension)),
(max(self.x_dimension), min(self.y_dimension)),
(min(self.x_dimension), max(self.y_dimension)),
(min(self.x_dimension), min(self.y_dimension))]
def get_scaled_points(self):
""" Get the coordinates scalated
"""
x = self.in_file.X
y = self.in_file.Y
z = self.in_file.Z
self.x_dimension = x * self.scale[0] + self.offset[0]
self.y_dimension = y * self.scale[1] + self.offset[1]
self.z_dimension = z * self.scale[-1] + self.offset[-1]
def get_points_by_class(self, classif=2):
""" Get points array with the given classification id (ASPRS classes)
"""
class_points_bool = self.in_file.Classification == classif
return self.points_array[class_points_bool], class_points_bool
def get_terrain_points_array(self):
""" Creates arrays for a given class (default=2) with the coordinates
of the points classificated by that class flag
"""
self.class_flag = 2
class_2_points, class_2_bool = self.get_points_by_class(
self.class_flag)
size = class_2_points.shape[0]
x_array = self.x_dimension[class_2_bool].reshape(size, 1)
y_array = self.y_dimension[class_2_bool].reshape(size, 1)
z_array = self.z_dimension[class_2_bool]
xy_array = np.concatenate((x_array, y_array), axis=1)
self.terrain_arrays_list = [xy_array, z_array]
if self.partials_create:
full_path = self.dirs.out_paths['las_surfaces']
self.dirs.create_dir(self.dirs.out_dirs['las'])
self.write_las_file()
def get_first_returns_array(self):
# Guardo el archivo para poder leerlo
if self.partials_create:
full_path = self.dirs.out_paths['las_surfaces']
self.dirs.create_dir(self.dirs.out_dirs['las'])
else:
full_path = self.dirs.temp_full_paths['las_surfaces']
self.dirs.create_dir(self.dirs.temp_dirs['temp_dir'])
out_file = File(full_path, mode='w', header=self.in_file.header)
out_file.points = self.in_file.points[
self.in_file.return_num == 1]
out_file.close()
#leo el archivo
in_file = File(full_path, mode='r')
scale = in_file.header.scale
offset = in_file.header.offset
x = in_file.X
y = in_file.Y
z = in_file.Z
x_dimension = x * scale[0] + offset[0]
y_dimension = y * scale[1] + offset[1]
z_dimension = z * scale[-1] + offset[-1]
size = x_dimension.shape[0]
x_array = x_dimension.reshape(size, 1)
y_array = y_dimension.reshape(size, 1)
z_array = z_dimension
# Cerrar archivo para poder eliminarlo
in_file.close()
if not self.partials_create:
self.dirs.remove_temp_file(full_path)
self.dirs.remove_temp_dir(self.dirs.temp_dirs['temp_dir'])
xy_array = np.concatenate((x_array, y_array), axis=1)
self.surfaces_arrays_list = [xy_array, z_array]
def write_las_file(self):
""" Create and write a new lidar file with the desirable points
"""
self.dirs.set_output_dir()
full_path = self.dirs.out_paths['las_terrain']
self.dirs.create_dir(self.dirs.out_dirs['las'])
out_file = File(full_path, mode='w',
header=self.in_file.header)
class_2_points, class_2_bool = self.get_points_by_class(
self.class_flag)
out_file.points = self.in_file.points[class_2_bool]
out_file.close()