|
| 1 | +import os |
| 2 | +from pathlib import Path |
| 3 | +import pdal |
| 4 | +import rasterio |
| 5 | +import rasterio.merge |
| 6 | +from tqdm import tqdm |
| 7 | +import pathos.pools as pp |
| 8 | + |
| 9 | + |
| 10 | +class QuickLook: |
| 11 | + |
| 12 | + def __init__(self, out_dir): |
| 13 | + self.out_meta = None |
| 14 | + self.out_dir = out_dir |
| 15 | + |
| 16 | + def get_tile_dems(self, mtype): |
| 17 | + print(f'retreiving individual {mtype} grids...') |
| 18 | + dems = [] |
| 19 | + for dem in list(self.out_dir.glob(f'*_{mtype}.tif')): |
| 20 | + print(dem) |
| 21 | + src = rasterio.open(dem) |
| 22 | + dems.append(src) |
| 23 | + self.out_meta = src.meta.copy() # uses last src made |
| 24 | + return dems |
| 25 | + |
| 26 | + def gen_mosaic(self, mtype): |
| 27 | + |
| 28 | + quick_look_path = self.out_dir / f'QUICK_LOOK_{mtype}.tif' |
| 29 | + |
| 30 | + dems = self.get_tile_dems(mtype) |
| 31 | + if dems: |
| 32 | + print('generating {}...'.format(quick_look_path)) |
| 33 | + mosaic, out_trans = rasterio.merge.merge(dems) |
| 34 | + self.out_meta.update({ |
| 35 | + 'driver': "GTiff", |
| 36 | + 'height': mosaic.shape[1], |
| 37 | + 'width': mosaic.shape[2], |
| 38 | + 'transform': out_trans}) |
| 39 | + |
| 40 | + with rasterio.open(quick_look_path, 'w', **self.out_meta) as dest: |
| 41 | + dest.write(mosaic) |
| 42 | + else: |
| 43 | + print('No DEM tiles were generated.') |
| 44 | + |
| 45 | + def gen_mean_z_surface(self, las_path): |
| 46 | + import pdal |
| 47 | + from pathlib import Path |
| 48 | + |
| 49 | + las_str = str(las_path).replace('\\', '/') |
| 50 | + |
| 51 | + extra_bytes = [ |
| 52 | + 'total_tvu' |
| 53 | + ] |
| 54 | + |
| 55 | + for eb in extra_bytes: |
| 56 | + |
| 57 | + gtiff_path = self.out_dir / las_path.name.replace('.las', f'_{eb}.tif') |
| 58 | + gtiff_path = str(gtiff_path).replace('\\', '/') |
| 59 | + |
| 60 | + |
| 61 | + pdal_json = """{ |
| 62 | + "pipeline":[ |
| 63 | + { |
| 64 | + "type": "readers.las", |
| 65 | + "filename": """ + '"{}"'.format(las_str) + """, |
| 66 | + "extra_dims": """ + '"{}=float"'.format(eb) + """, |
| 67 | + "use_eb_vlr": "true" |
| 68 | + }, |
| 69 | + { |
| 70 | + "type":"filters.range", |
| 71 | + "limits": "Classification[26:26]" |
| 72 | + }, |
| 73 | + { |
| 74 | + "filename": """ + '"{}"'.format(gtiff_path) + """, |
| 75 | + "dimension": """ + '"{}"'.format(eb) + """, |
| 76 | + "gdaldriver": "GTiff", |
| 77 | + "output_type": "mean", |
| 78 | + "resolution": "0.5", |
| 79 | + "type": "writers.gdal" |
| 80 | + } |
| 81 | + ] |
| 82 | + }""" |
| 83 | + |
| 84 | + try: |
| 85 | + pipeline = pdal.Pipeline(pdal_json) |
| 86 | + __ = pipeline.execute() |
| 87 | + arrays = pipeline.arrays |
| 88 | + print(arrays) |
| 89 | + metadata = pipeline.metadata |
| 90 | + except Exception as e: |
| 91 | + print(e) |
| 92 | + |
| 93 | + def gen_mean_z_surface_multiprocess(self, las_paths): |
| 94 | + p = pp.ProcessPool(4) |
| 95 | + num_las_paths = len(list(las_paths)) |
| 96 | + for _ in tqdm(p.imap(self.gen_mean_z_surface, las_paths), |
| 97 | + total=num_las_paths, ascii=True): |
| 98 | + pass |
| 99 | + p.close() |
| 100 | + p.join() |
| 101 | + |
| 102 | + |
| 103 | +def set_env_vars(env_name): |
| 104 | + user_dir = os.path.expanduser('~') |
| 105 | + conda_dir = Path(user_dir).joinpath('AppData', 'Local', |
| 106 | + 'Continuum', 'anaconda3') |
| 107 | + env_dir = conda_dir / 'envs' / env_name |
| 108 | + share_dir = env_dir / 'Library' / 'share' |
| 109 | + script_path = conda_dir / 'Scripts' |
| 110 | + gdal_data_path = share_dir / 'gdal' |
| 111 | + proj_lib_path = share_dir |
| 112 | + |
| 113 | + if script_path.name not in os.environ["PATH"]: |
| 114 | + os.environ["PATH"] += os.pathsep + str(script_path) |
| 115 | + os.environ["GDAL_DATA"] = str(gdal_data_path) |
| 116 | + os.environ["PROJ_LIB"] = str(proj_lib_path) |
| 117 | + |
| 118 | + |
| 119 | +def main(): |
| 120 | + |
| 121 | + set_env_vars('cblue_diag') |
| 122 | + |
| 123 | + las_dir = Path(r'D:\JeromesCreek\tpu_dir') |
| 124 | + las_paths = list(las_dir.glob('*.las')) |
| 125 | + |
| 126 | + out_dir = las_dir / 'DEMs' |
| 127 | + |
| 128 | + ql = QuickLook(out_dir) |
| 129 | + ql.gen_mean_z_surface_multiprocess(las_paths) |
| 130 | + |
| 131 | + ql.gen_mosaic('total_tvu') |
| 132 | + |
| 133 | + |
| 134 | +if __name__ == '__main__': |
| 135 | + main() |
0 commit comments