a `e' @svddlZddlZddlZddlZddlZddlZddlZddlZddlZddl m Z m Z ddl m Z ddlmZddlmZddlmZddlmZmZmZddlmZdd lmZdd lmZd d lmZd d lmZzdd lmZ Wn2e!yzdd l"mZ Wn Yn0Yn0ddZ#d'ddZ$dZ%ddgdddd dddddf ddZ&d(ddZ'd)d"d#Z(d*d%d&Z)dS)+N)delayedParallel)run) point_cloud)io)system)get_max_memory parallel_mapget_total_memory)datetime)main)log)run_rectification)pdalcCsZt}zt||||||Wntd|Yn0td|t|f|S)Nz!Error creating classified file %sCreated %s in %s)r nowrrun_pdaltranslate_smrfr ODM_WARNINGODM_INFO)lasFilescalarslope thresholdwindowstartr/code/opendm/dem/commands.pyclassify!src Cst}zLtd||||t||d|dd||dtd|t|fWn:ty}z"td|t|fWYd}~n d}~00|S)NzQRectifying {} using with [reclassify threshold: {}, min area: {}, min points: {}]medianZ surroundingr)inputoutputZreclassify_planreclassify_thresholdZ extend_planZextend_grid_distancemin_area min_pointsrz&Error rectifying ground in file %s: %s) r rr rformatr Exceptionrstr)rr%r&r'rerrrrectify,s,r,maxz0.56Tg?iFc Cst} |||dtt||| dur*dn| | dur8dn| |dkrFdnd|d }tjd jfi|d |id d |}tj tj ||}g}t tj tj |d D]>}tj |}t d|}|dur||t|ddqt|dkrtdtdt||jddddtj tj |d}tj tj |d}t|d.}|D]}||ddqdWdn1s0Ytd||ftj tj |d}tj tj |d}tj tj |d}tj tj |d}t|r|nd |||||d!}|rtd"jfi|td#d$d%|d&d$d'd$d(d)d*t|d+d,d-d.d/d0|d1|d2gtd3|||ftd4jfi|ntd5jfi|| rt|||d6t|n t||tj |r| rtj |d7d8}t!||dd9|||||fD]}tj |r*t|q*|D]&}tj |drNt|dqNtd:|t| fdS);z8 Create DEM from multiple radii, and optionally gapfill ,Nrrdtm) r#outdir outputTyperadiuses resolutionZmaxTiles decimationclassificationZtileSizezrenderdem "{input}" --outdir "{outdir}" --output-type {outputType} --radiuses {radiuses} --resolution {resolution} --max-tiles {maxTiles} --decimation {decimation} --classification {classification} --tile-size {tileSize} --force OMP_NUM_THREADSenv_varsz%s.tifz*.tifz^r([\d\.]+)_x\d+_y\d+\.tif)filenameradiusz1No DEM tiles were generated, something went wrongzGenerated %s tilescSs t|dS)Nr=)float)trrrnzcreate_dem..T)keyreversez tiles.vrtztiles_list.txtwr< z(gdalbuildvrt -input_file_list "%s" "%s" z merged.vrtztiles.small.tifztiles.small_filled.tifz tiles.tifZALL_CPUS) max_memorythreadsZ tiles_vrtZ merged_vrtgeotiff geotiff_smallgeotiff_small_filledzgdal_translate -co NUM_THREADS={threads} -co BIGTIFF=IF_SAFER -co COMPRESS=DEFLATE --config GDAL_CACHEMAX {max_memory}% -outsize 10% 0 "{tiles_vrt}" "{geotiff_small}".-cozNUM_THREADS=%srGBIGTIFF=IF_SAFERCOMPRESS=DEFLATEz--configZGDAL_CACHE_MAXrF%z-b1z-ofGTiffrIrJz;gdalbuildvrt -resolution highest -r bilinear "%s" "%s" "%s"zgdal_translate -co NUM_THREADS={threads} -co TILED=YES -co BIGTIFF=IF_SAFER -co COMPRESS=DEFLATE --config GDAL_CACHEMAX {max_memory}% "{merged_vrt}" "{geotiff}"zgdal_translate -co NUM_THREADS={threads} -co TILED=YES -co BIGTIFF=IF_SAFER -co COMPRESS=DEFLATE --config GDAL_CACHEMAX {max_memory}% "{tiles_vrt}" "{geotiff}") num_workersz .euclideand)postfix) overwritezCompleted %s in %s)"r rjoinmapr*rrr(ospathabspathglobbasenamerematchappendr>grouplen ExitExceptionr rsortopenwritergdal_fillnodatamedian_smoothingremovereplaceexistsrrelated_file_pathcompute_euclidean_map)input_point_cloudZdem_type output_typer5Zgapfillr3r6 max_workersZ max_tile_sizer7Zwith_euclidean_mapZapply_smoothingZ max_tilesrkwargs output_file output_pathtilespr<mZtiles_vrt_pathZtiles_file_listfr?Zmerged_vrt_pathZgeotiff_small_pathZgeotiff_small_filled_path geotiff_pathZ emap_pathZ cleanup_filerrr create_dem@s        6            $rwc Cs8tj|std|dSd}t|}|jd}Wdn1sL0Ytj|rh|r"tj|r~t |t d|t durz&t d||dt |dddd dd g Wn6t y}ztd t |WYd}~n d}~00tj|r|Std |n td nt d||SdS)Nz6Cannot compute euclidean map (file does not exist: %s)irz Computing euclidean distance: %szgdal_proximity.pyz-valuesrLz TILED=YESrMrNz%Cannot compute euclidean distance: %sz*Cannot compute euclidean distance file: %sz7Cannot compute euclidean map, gdal_proximity is missingz"Found a euclidean distance map: %s)rWrXrir rrasteriorc nodatavalsisfilergrgdal_proximityr*r))rvrqrTnodatarur+rrrrks4  (     ( rkcCsjt}tj|s td|||||d}tjdjfi|d|idt d|t|f|S)z Apply median smoothing zFile %s does not exist!)r#r$rr=zfastrasterfilter "{input}" --output "{output}" --window-size {window} --radius {radius} --co TILED=YES --co BIGTIFF=IF_SAFER --co COMPRESS=DEFLATE r9r:z&Completed smoothing to create %s in %s) r rrWrXrir)rrr(r r)rvrq window_sizerRr=rrorrrrfs   rf?cCs@t|||g}t|dD]}||dtdq|S)Nrr2r1)r get_spacingranger^mathsqrt) stats_filestepsr6 multiplierZ radius_steps_rrrget_dem_radius_stepssr)rr r!)F)r}rr~)r)*rWsysrxnumpyrtimeshutilrZr\joblibrr opendm.systemropendmrrropendm.concurrencyrr r r Zopendm.vendor.gdal_fillnodatar rer Zground_rectification.rectifyrr.rZosgeo_utils.gdal_proximityr{ModuleNotFoundErrorZosgeo.utils.gdal_proximityrr,errorrwrkrfrrrrrsL             &