def __init__(self, points_file, class_file=''): # Load points file if points_file.endswith('.las') or points_file.endswith('.laz'): lfile = LasFile(points_file, mode='r') self.x = np.copy(lfile.X).astype('f8') * lfile.header.scale[0] self.y = np.copy(lfile.Y).astype('f8') * lfile.header.scale[1] self.z = np.copy(lfile.Z).astype('f8') * lfile.header.scale[2] self.i = np.copy(lfile.Intensity).astype('f8') self.r = np.copy(lfile.return_num).astype('f8') self.c = np.copy(lfile.Classification) lfile.close() elif points_file.endswith('.txt'): data = np.loadtxt(points_file, delimiter=',', dtype='f8') self.x = data[:, 0] self.y = data[:, 1] self.z = data[:, 2] self.i = data[:, 3] self.r = data[:, 4] if not class_file: if data.shape[1] > 5: self.c = data[:, 5].astype('uint8') else: self.c = np.zeros(self.x.shape, dtype='uint8') else: self.c = np.loadtxt(class_file, dtype='uint8') else: raise ValueError('Unknown file type extension: ' + points_file) self.filepath = points_file self.filename = os.path.splitext(os.path.basename(points_file))[0] if self.filename.endswith('_PC3'): self.filename = self.filename[:-4]
def save(self, output_file, class_file=''): if output_file.endswith('.txt'): if (not class_file and self.c.any()): np.savetxt( output_file, np.stack([self.x, self.y, self.z, self.i, self.r, self.c], axis=1), fmt='%.2f,%.2f,%.2f,%d,%d,%d') else: np.savetxt(output_file, np.stack([self.x, self.y, self.z, self.i, self.r], axis=1), fmt='%.2f,%.2f,%.2f,%d,%d') if class_file: self.save_classifications_txt(class_file) elif output_file.endswith('.las') or output_file.endswith('.laz'): lfile = LasFile(output_file, mode='w', header=LasHeader(x_scale=0.01, y_scale=0.01, z_scale=0.01)) lfile.X = self.x / 0.01 lfile.Y = self.y / 0.01 lfile.Z = self.z / 0.01 lfile.Intensity = self.i lfile.flag_byte = self.r lfile.Classification = self.c lfile.close() else: raise ValueError('Unknown file type extension: ' + output_file)
print("Usage: voxelizer.py input.las vox_size out_dir") print("Loading file...", end='') source_files = glob.glob(sys.argv[1]) out_dir = sys.argv[3] if not os.path.exists(out_dir): os.makedirs(out_dir) for source in source_files: t = time.time() source_basename = os.path.basename(source) source_fname, source_ext = os.path.splitext(source_basename) if source_ext == ".las" or source_ext == ".laz": inFile = LasFile(source, mode='r') data = np.vstack((inFile.x, inFile.y, inFile.z)).transpose() elif source_ext == ".txt" or source_ext == ".asc" or source_ext == ".xyz" or source_ext == "csv": data = pandas.read_csv(source, delimiter=' ', skipinitialspace=True, usecols=(0, 1, 2)).to_numpy(dtype=float) else: print( "Please give a valid LAS or ASCII point cloud.\n" "Supported extensions: ['.las'., '.laz', '.txt', '.asc', '.xyt', '.csv']" ) sys.exit() print(" [done (%.3f s)].\nVoxelizing..." % (time.time() - t), end='') t = time.time()