
    !\i3                        S r SSKJr  SSKJrJr  SSKrSSKrSSKrSSK	r	SSK
r
SSKrSSKrSSKrSSKJr  SSKJr  SSKJrJr  SSKJr  SS	KJr  SS
KJr  SSKJr  \R:                  " \5      rSSSSSS\R@                  SSSSSS4S jr!g)zRaster stacking tool.    )Iterable)	ExitStackcontextmanagerN)disjoint_bounds)
Resampling)RasterioError
StackError)DatasetWriter)windows)Affine)	subdivideF@   c                    [        U S   [        [        R                  45      (       a  [        R
                  nO[        S 5       nUnSn[        5        nU" U S   5       nUR                  nUR                  nUR                  nUR                  S   nUnUR                  S   nUc  U  Vs/ s H  nSPM     nn UR                  S5      nSSS5        / n/ nSn[        [!        X5      5       GH	  u  nu  nnU" U5       n U R"                  n!Uc  UU R$                  -  nO)[        U[&        5      (       a  US-  nOU[)        U5      -  nU
(       a  [+        WU R                  S S9nU!R,                  (       d  [/        S5      eU!R0                  S:  a  [/        S5      eU!R2                  S:  a  [/        S	5      eU R4                  u  n"n#n$n%SSS5        UR7                  W"W$/5        UR7                  W#W%/5        GM     U(       a  Uu  n&n'n(n)O/[+        U5      [+        U5      [9        U5      [9        U5      4u  n&n'n(n)U(       d  WnO<[        U[:        R<                  5      (       a  X"4nO[)        U5      S:X  a
  US   US   4nU(       a  [>        R@                  " U&US   -  5      US   -  n&[>        RB                  " U(US   -  5      US   -  n([>        R@                  " U'US   -  5      US   -  n'[>        RB                  " U)US   -  5      US   -  n)['        [E        U(U&-
  US   -  5      5      n*['        [E        U)U'-
  US   -  5      5      n+[F        RH                  " U&U)5      [F        RJ                  " US   US   * 5      -  n,Ub  Un[L        RO                  S
U5        Ub  Un[L        RO                  SU5        Sn-WGb  [P        RR                  " W[P        RT                  5      (       a@  [P        RV                  " U5      n.U.R*                  Us=:*  =(       a    U.R8                  :*  Os  n-O[X        RZ                  " U5      (       ap  [P        R\                  " U5      n.U.R*                  Us=:*  =(       a    U.R8                  :*  Os  n-[P        R^                  " U5      n/U-[P        R`                  " U/U5      -  n-OSn-U-(       d  [b        Rd                  " SU SU S35        WnO[L        RO                  S5        Sn[f        Rh                  " SSU*U+5      n0Ub  [        U[j        5      (       a  UnOgWn1U1Rl                  " S%0 U=(       d    0 D6  U,U1S'   U+U1S'   U*U1S'   UU1S'   WU1S'   Ub  UU1S'   [        R
                  " US40 U1D6nURo                  U5        U	S-  [P        Rp                  " W5      Rr                  U-  -  n2U*U+-  U2:  a  U0/n3O;[>        R@                  " [>        Rt                  " U25      5      n4[w        U0U4U45      n3OU0/n3[L        RO                  SU35        U3 GH  n5[f        R4                  " U5U,5      u  n&n'n(n)[P        Rx                  " UU5Rz                  U5R|                  4WS9n6U-(       a  U6R                  U5        Sn7[        [!        X5      5       GHS  u  n8u  nnU" U5       n [        U&U'U(U)4U R4                  5      (       a&  [L        RO                  SU U&U'U(U)45         SSS5        MX  WU R                  :w  a  [        SU 35      e[f        R                  " U&U'U(U)U R"                  5      RE                  S5      n9Uc  U R                  nO[        U[&        5      (       a  U/n[)        U5      U5Rz                  U5R|                  4n:U R                  U:U9SSUUS9n;SSS5        [        U[&        5      (       a  U6U7SS2SS24   n<U7S-  n7O=[        U[        5      (       a(  U6U7U7[)        U5      -   2SS2SS24   n<U7[)        U5      -  n7[X        R                  " U5      (       a  [P        R                  " W<5      n=OQ[P        RR                  " W<Rp                  [P        RT                  5      (       d  [P        R                  " U<U5      n=OU<U:H  n=W;SS2SU<R                  S   2SU<R                  S    24   n>[P        R                  R                  U>5      n?[P        R                  " U<U>S!S"9  GMV     U(       d  GM  [f        R                  " U&U'U(U)U,5      RE                  S5      n@UR                  U6U@S#9  GM     Uc3  U(       a  [P        R                  R                  W6USS$9n6W6U,4sSSS5        $ W(       a  UR                  SU5        UR                  5          SSS5        gs  snf ! [         a    Sn GNf = f! , (       d  f       GN= f! , (       d  f       GN= f! , (       d  f       GNE= f! , (       d  f       g= f)&am
  Copy valid pixels from input files to an output file.

All files must have the same data type, and
coordinate reference system. Rotated, flipped, or upside-down
rasters cannot be stacked.

Geospatial bounds and resolution of a new output file in the units
of the input file coordinate reference system may be provided and
are otherwise taken from the source datasets.

Parameters
----------
sources : list
    A sequence of dataset objects opened in 'r' mode or Path-like
    objects.
bounds: tuple, optional
    Bounds of the output image (left, bottom, right, top).
    If not set, bounds are determined from bounds of input rasters.
res: tuple, optional
    Output resolution in units of coordinate reference system. If
    not set, a source resolution will be used. If a single value is
    passed, output pixels will be square.
use_highest_res: bool, optional. Default: False.
    If True, the highest resolution of all sources will be used. If
    False, the first source's resolution will be used.
masked: bool, optional. Default: False.
    If True, return a masked array. Note: nodata is always set in
    the case of file output.
nodata: float, optional
    nodata value to use in output file. If not set, uses the nodata
    value in the first input raster.
dtype: numpy.dtype or string
    dtype to use in outputfile. If not set, uses the dtype value in
    the first input raster.
indexes : list of ints or a single int, optional
    bands to read and stack.
output_count: int, optional
    If using callable it may be useful to have additional bands in
    the output in addition to the indexes specified for read.
resampling : Resampling, optional
    Resampling algorithm used when reading input files.
    Default: `Resampling.nearest`.
target_aligned_pixels : bool, optional
    Whether to adjust output image bounds so that pixel coordinates
    are integer multiples of pixel size, matching the ``-tap``
    options of GDAL utilities.  Default: False.
mem_limit : int, optional
    Process stack output in chunks of mem_limit MB in size.
dst_path : str or PathLike, optional
    Path of output dataset.
dst_kwds : dict, optional
    Dictionary of creation options and other parameters that will be
    overlaid on the profile of the output dataset.

Returns
-------
tuple
    Two elements:
        dest: numpy.ndarray
            Contents of all input rasters in single array
        out_transform: affine.Affine()
            Information for mapping pixel coordinates in `dest` to
            another coordinate system

Raises
------
StackError
    When sources cannot be stacked due to incompatibility between
    them or limitations of the tool.
r   c              3       #     U v   g ! f = f7f)N )objs    Q/var/www/html/kml_chatgpt/mouzaenv/lib/python3.13/site-packages/rasterio/stack.pynullcontextstack.<locals>.nullcontextu   s     	s   	 N   c                     [        U [        R                  5      (       a  U $ [        R                  " U S   S-  U S   S-  -   5      $ )Nr      r   )
isinstancenumbersNumbermathsqrt)xs    r   <lambda>stack.<locals>.<lambda>   sC    %a88 '( '>!YYqtqy1Q419'<='>    )keyz3Rotated, non-rectilinear rasters cannot be stacked.zHRasters with negative pixel width ("flipped" rasters) cannot be stacked.zMRasters with negative pixel height ("upside down" rasters) cannot be stacked.zSet dtype: %szSet nodataval: %rFTz)Ignoring nodata value. The nodata value, z8, cannot safely be represented in the chosen data type, zs. Consider overriding it using the --nodata option for better results. Falling back to first source's nodata value.zSet nodataval to 0	transformheightwidthcountdtypenodatawg    .Az	Chunks=%r)r'   z"Skipping source: src=%r, bounds=%rzCRS mismatch with source:    )	out_shapewindow	boundlessmaskedindexes
resamplingr   unsafe)casting)r,   )copyr   )Pr   strosPathLikerasterioopenr   r   profilecrsres
nodatavalsdtypescolormap
ValueError	enumeratezipr#   r&   intlenminis_rectilinearr	   aeboundsextendmaxr   r   r   floorceilroundr   translationscaleloggerdebugnp
issubdtypeintegeriinfocmathisfinitefinfomin_scalar_typecan_castwarningswarnr   Windowr
   updateenter_contextr'   itemsizer   r   zerosr$   r%   fillr   r   from_boundsr/   readr   isnaniscloseshapemagetmaskcopytowritemasked_equalwrite_colormapclose)AsourcesrH   r;   r(   r'   r/   output_countr0   target_aligned_pixels	mem_limituse_highest_resr.   dst_pathdst_kwdsdataset_openerr   dst
exit_stackfirstfirst_profile	first_crsbest_resfirst_nodataval	nodatavaldtsfirst_colormapxsysidatasetsrc_indexessrcsrc_transformleftbottomrighttopdst_wdst_sdst_edst_noutput_widthoutput_heightoutput_transforminrangeinfo	nodata_dtdout_windowout_profile
max_pixelschunksnchunkdestdst_idxidx
src_window
temp_shapetemp_srcregionregion_masktemp	temp_mask
dst_windowsA                                                                    r   stackr      s	   n '!*sBKK011! 
	 
	 %
C	
GAJ'5!MMM		IyyH#..q1O'IaB)01A41&!&!2 (" )23w3H)I%A%(C #& CII-LS11 A%L C$44L"" > H %33$M  !??Q&$b  !??Q&$g  ,/::(feSE )H IItUm$IIvsm$M *JR )/&E5%),R#b'3r7CG)K&E5% CW^^,,*CX]q63q6"C JJus1v~.Q7EIIec!fn-A6EJJus1v~.Q7EIIec!fn-A6E 5%%-3q6!9:;E55=CF":;<!--eU;fllFSVG?
 
 BLL"-ILL,i8 }}R,,xx|((i;;488;>>),,88B<D"hh)??txx?G " 2 29 =I%Ir(BBG"G?	{ K002t 4CC ,	LL-.I nnQ<G(M22+""6hn"6+;K((5H%'3G$'3G$')G$%,2K)mmHcA[A((-"U*bhhrl.C.Cl.RSJm+j8%JJtyy45";15!]F[&)E)0?O)P&E5%88\5<<ERPD		)$G/8W9N/O++g{#G,&ueU'CSZZPP@"E5%8
 ! -, !CGG++.H	,RSS!(!4!4ueUCMM"eAh  #*&)kk#K55'2m"%k"2ELL%++!NJ"xx",)"&# +#-  (  H/ -@ k3//!'1a-0FqLGX66!'Gc+6F,F"F1"LMFs;//G;;y))"$((6"2Kv||RZZ@@"$**VY"?K"(I"5K  #4V\\!_#46GQ6G GHEEMM$/			$g 0Pr s$005%0@%(  		$z	2I L ;uu))$	)F))M 
P ""1n5IIKU 
 2  &!%& (', )(\ -,K 
s   g% Af/,f8f/;f4g% C	g	Sg% <g	
g%&B/g	Eg%0A*g%$)g%f/f,(f/+f,,f//
f>	9g%
gg%
g"g%%
g3)"__doc__collections.abcr   
contextlibr   r   loggingr5   r   rV   r[   r   numpyrR   r7   rasterio.coordsr   rasterio.enumsr   rasterio.errorsr   r	   rasterio.ior
   r   rasterio.transformr   rasterio.windowsr   	getLogger__name__rP   nearestr   r   r!   r   <module>r      s     $ 0  	       + % 5 %  % &			8	$
 
!!Pr!   