a `e3@sddlZddlmZddlmZddlmZddlmZddlZddl Z ddl Z ddl Z ddl m Z ddlmZmZddlmZdd lmZdd lmZdd lmZdd lmZd dZddZdddZd ddZddZd!ddZd"ddZ ifddZ!dS)#N)log)system)Cropper)get_max_memory)edt)Affinerowcol)mask)io)generate_orthophoto_tiles)convert_to_cogeo)gdalcCs0|jr dnd|j|jdvrdndddd|jdS) NNOYES)LZWDEFLATE21IF_SAFER)TILEDCOMPRESS PREDICTORBIGTIFF BLOCKXSIZE BLOCKYSIZEZ NUM_THREADS)orthophoto_no_tiledorthophoto_compressionmax_concurrency)argsr /code/opendm/orthophoto.pyget_orthophoto_varss r"cCs,tdd|i}tdjfi|dS)NzBuilding Overviews orthophotozlgdaladdo -r average --config BIGTIFF_OVERVIEW IF_SAFER --config COMPRESS_OVERVIEW JPEG {orthophoto} 2 4 8 16)rODM_INFOrrunformat)orthophoto_filekwargsr r r!build_overviewss   r)c Cs"|dur tj|\}}|d}d}t|}|jdkrg}td|jdD]}||| qLt t |tdt |d}zV| tj} | tj} | tj} | dus| dus| durtdd| | | f}Wnd}Yn0d}d} |durd|} td ||| |tfdS) Nz.pngzCannot find bandsz-b %s -b %s -b %s -a_nodata 0-b 1 -b 2 -b 3 -a_nodata 0z -outsize %s 0zCgdal_translate -of png "%s" "%s" %s %s --config GDAL_CACHEMAX %s%% )ospathsplitextr Open RasterCountrangeappend GetRasterBandGetColorInterpretationdictziplenget GCI_RedBand GCI_GreenBand GCI_BlueBand Exceptionrr%r) r' output_fileoutsizebaseext bandparamgtifbandsidxredgreenblueZosparamr r r! generate_png(s4       rJcCsX|dur tj|\}}|d}d}t|}|jdkrz'compute_mask_raster..)nodatar+TZ black_borderparallelr,unsafeoutcastingz5%s does not have an alpha band, cannot blend cutline!wrr)r.r/existsr ODM_WARNINGr$rasterioopenfionar9r shapernpmultiplyprofile colorinterpwrite) input_rasterZ vector_mask output_rasterblend_distanceonly_max_coords_featurerastsrcZ burn_featuresZmax_coords_countZmax_coords_featurer_shapes out_imageZ out_transform alpha_banddist_tdstr r r!compute_mask_rasterksD    *rc Cs$tj|std|dStd|t|d}|}|dkr|j ddkr|d}t |ddd}|||k|<d |||k<t j |||d d ntd |tj|d fddi|j "}|j|_||Wdn1s0Y|WdS1s0YdS)Nz(Cannot feather raster, %s does not existzComputing feather raster: %sr[rr+rcTrdr,rfrgz6%s does not have an alpha band, cannot feather raster!rjrr)r.r/rkrrlr$rmrnreadrprrqrrrsrtru)rvrwrxrzr}r~rrr r r!feather_rasters$  (rc. Csg}d}d}|D]J\}}t|s2td|qt|sLtd|q|||fqt|dkrvtddSt|dd:}|j} |j d} |j } |j dd} |j } Wdn1s0Yt dt|d d |D}g}g}|D]H\}}|j\}}}}|||g|||g|j dd krtd qt|t|t|t|f\}}}}t d ||||ft||}|t| d| d 9}tt||| d}tt||| d}|||f\}}t d||ft d||||f|| d<|| d<|| d<|dddk| d<|dd| d<|dd| d<|dd| d<|dd| d <|d!d"| d#<| tj|d$fi| }| |_ |D]\}} || \}}}}| j}!| j| j}"}#|j}$|$|"|#f}%t j!|%| d%}&|D]\}}t"t#t$|j%||t&|d&t$|j%||t&|d&}'t j!|%| d%}(|j'|(|'d'd(d)}(t (|&d*dk|(d*dk})t j)|&|(|)d+t *|&d*|!krqq|D]\}}t"t#t$|j%||t&|d&t$|j%||t&|d&}'t j!|%| d%}(|j'|(|'d'd(d)}(|(d*dk}*t+d| D]H}+|(d*d,|(|+d|(d*d,|&|+},t j)|&|+|,d-|*d.q d,|&d*|*<t *|&d*|!krqq|D]\}}-t"t#t$|-j%||t&|d&t$|-j%||t&|d&}'t j!|%| d%}(|-j'|(|'d'd(d)}(t+d| D]P}+|(d*d,|(|+d|(d*d,|&|+},t j)|&|+|,d-|(d*dkd.qq|j,|&| d/qWdn1sv0Y|S)0zx Based on https://github.com/mapbox/rio-merge-rgba/ Merge orthophotos around cutlines using a blend buffer. Nz4%s does not exist. Will skip from merged orthophoto.rz%No input orthophotos, skipping merge.countr,z$%s valid orthophoto rasters to mergecSs$g|]\}}t|t|fqSr )rmrn)r^ocr r r!r`razmerge..z&Inputs must be at least 2-band rasterszOutput bounds: %r %r %r %rzOutput width: %d, height: %dzAdjusted bounds: %r %r %r %r transformheightwidthrrtiledrr blockxsizer blockysizerrcompressrr predictorrrZbigtiffrj)dtype)op precisionTF)rhwindow boundlessmaskedrc)wherego@rf)rir)r)-r file_existsrrlr4r9rmrnresdtypesrsmetartr$boundsextend ValueErrorminmaxr translationscaleintmathceilr:update block_windows window_boundsrrrrqzerostupler8rrroundr logical_andcopyto count_nonzeror3ru).Zinput_ortho_and_ortho_cutsZoutput_orthophotoorthophoto_varsinputsrrrrfirstrrrsZ num_bandsrtsourcesxsysr{_leftbottomrighttopZdst_wZdst_sZdst_eZdst_nZoutput_transformZ output_widthZ output_heightZdstrastrFZ dst_window blocksizeZdst_rowsZdst_colsZ dst_count dst_shapeZdstarrZ src_windowtempZ write_regionrbZblendedcutr r r!merges      $  $          ,     ,&2r)NN)NN)rZF)rZ)"r.opendmrropendm.cropperropendm.concurrencyrrnumpyrqrmrorrasterio.transformrrZ rasterio.maskr r Zopendm.tiles.tilerr Z opendm.cogeor osgeor r"r)rJrKrYrrrr r r r!s.            "  /