
    !\i[                     t   S r SSKrSSKJr  SSKrSSKrSSKrSSKJr  SSK	r	SSK
rSSKJrJr  SSKJrJr   " S S5      rS	 rS
 rS!S jrS rS r\S 5       r\S 5       rS rS r\S 5       r S"S jrS rS#S jrS r S$S jr!S%S jr"S#S jr#S#S jr$S r%\	RL                  " SSS9 " S S5      5       r'S  r(g)&as  Window utilities and related functions.

A window is an instance of Window

    Window(column_offset, row_offset, width, height)

or a 2D N-D array indexer in the form of a tuple.

    ((row_start, row_stop), (col_start, col_stop))

The latter can be evaluated within the context of a given height and
width and a boolean flag specifying whether the evaluation is boundless
or not. If boundless=True, negative index values do not mean index from
the end of the array dimension as they do in the boundless=False case.

The newer float precision read-write window capabilities of Rasterio
require instances of Window to be used.
    N)Iterable)Affine)WindowErrorRasterioDeprecationWarning)rowcolguard_transformc                   .    \ rS rSrSrSS jrS rS rSrg)	WindowMethodsMixin"   zMixin providing methods for window-related calculations.
These methods are wrappers for the functionality in
`rasterio.windows` module.

A subclass with this mixin MUST provide the following
properties: `transform`, `height` and `width`.

Nc           	      ~    Ub  [         R                  " S[        5        [        UUUU[	        U R
                  5      S9$ )a	  Get the window corresponding to the bounding coordinates.

The resulting window is not cropped to the row and column
limits of the dataset.

Parameters
----------
left: float
    Left (west) bounding coordinate
bottom: float
    Bottom (south) bounding coordinate
right: float
    Right (east) bounding coordinate
top: float
    Top (north) bounding coordinate
precision: int, optional
    This parameter is unused, deprecated in rasterio 1.3.0, and
    will be removed in version 2.0.0.

Returns
-------
window: Window

zLThe precision parameter is unused, deprecated, and will be removed in 2.0.0.)	transform)warningswarnr   from_boundsr   r   )selfleftbottomrighttop	precisions         S/var/www/html/kml_chatgpt/mouzaenv/lib/python3.13/site-packages/rasterio/windows.pywindowWindowMethodsMixin.window,   sC    2  MM^*
 %dnn5
 	
    c                 B    [        U R                  5      n[        X5      $ )zGet the affine transform for a dataset window.

Parameters
----------
window: rasterio.windows.Window
    Dataset window

Returns
-------
transform: Affine
    The affine transform matrix for the given window

)r   r   )r   r   
gtransforms      r   window_transform#WindowMethodsMixin.window_transformS   s     %T^^4
,,r   c                 B    [        U R                  5      n[        X5      $ )zGet the bounds of a window

Parameters
----------
window: rasterio.windows.Window
    Dataset window

Returns
-------
bounds : tuple
    x_min, y_min, x_max, y_max for the given window

)r   r   bounds)r   r   r   s      r   window_bounds WindowMethodsMixin.window_boundsd   s     $DNN3	f((r    N)	__name__
__module____qualname____firstlineno____doc__r   r   r!   __static_attributes__r#   r   r   r
   r
   "   s    %
N-")r   r
   c                 F   ^  [         R                  " T 5      U 4S j5       nU$ )zlDecorator to allow function to take either ``*args`` or
a single iterable which gets expanded to ``*args``.
c                  l   > [        U 5      S:X  a   [        U S   [        5      (       a  T" U S   6 $ T" U 6 $ )N   r   )len
isinstancer   )argskwargsfunctions     r   wrapperiter_args.<locals>.wrapperz   s7    t9>ja(;;T!W%%T?"r   )	functoolswraps)r2   r3   s   ` r   	iter_argsr7   v   s'     __X# #
 Nr   c                 P    [        U [        5      (       a  U R                  5       $ U $ )z!Normalize Windows to range tuples)r/   Windowtoranges)r   s    r   r:   r:      s!    &&!!  r   c                    SU R                   s=:  a  S::  d  O  [        S5      eUb8  [        R                  " U5      (       a  [        R                  " U 5      ) nOX:g  nO[        R                  R                  U 5      (       a!  [        R                  R                  U 5      ) nOVU R                   S:X  a  SU R                  4S4nO"SU R                  S   4SU R                  S   44n[        R                  " U6 $ U R                   S:X  a  [        R                  " USS9n/ nUR                  5        HY  nUR                  (       a4  UR                  UR                  5       UR                  5       S-   45        MH  UR                  S5        M[     UR                   S:X  a  UR                  S5        [        R                  " U6 $ )	a}  Window covering the input array's valid data pixels.

Parameters
----------
arr: numpy ndarray, <= 3 dimensions
nodata: number
    If None, will either return a full window if arr is not a masked
    array, or will use the mask to determine non-nodata pixels.
    If provided, it must be a number within the valid range of the
    dtype of the input array.

Returns
-------
Window
r      z;get_data_window input array must have 1, 2, or 3 dimensionsr-   r   r   )axis)ndimr   npisnanma	is_maskedgetmasksizeshaper9   from_slicesanynonzeroappendminmax)arrnodataarr_maskfull_windowvnzs         r   get_data_windowrU      sb     sxx!IK 	K 88F~H}H			EEMM#&&88q=sxx=&1Ksyy}-399R=/ABK!!;//
xx1}66(+ 	A 77HHbffh1-.HHV	 ! }}	q!!r   c                 ~   [        U R                  UR                  5      n[        U R                  UR                  5      n[        U R                  U R                  -   UR                  UR                  -   5      U-
  n[        U R                  U R
                  -   UR                  UR
                  -   5      U-
  nX#XE4$ )z Compute the union of two windows)rM   col_offrow_offrN   widthheightw1w2rW   rX   rY   rZ   s         r   _compute_unionr^          "**bjj)G"**bjj)G

288#RZZ%89GCEBII%rzz"))';<wFFU**r   c                 (    [        X5      n[        U6 $ r$   )r^   r9   r\   r]   coeffss      r   _unionrc      s    B#F6?r   c                  8    [         R                  " [        U 5      $ )z
Union windows and return the outermost extent they cover.

Parameters
----------
windows: sequence
    One or more Windows.

Returns
-------
Window
)r5   reducerc   windowss    r   unionrh      s     FG,,r   c                  8    [         R                  " [        U 5      $ )zInnermost extent of window intersections.

Will raise WindowError if windows do not intersect.

Parameters
----------
windows: sequence
    One or more Windows.

Returns
-------
Window
)r5   re   _intersectionrf   s    r   intersectionrk      s     M733r   c                 ~   [        U R                  UR                  5      n[        U R                  UR                  5      n[        U R                  U R                  -   UR                  UR                  -   5      U-
  n[        U R                  U R
                  -   UR                  UR
                  -   5      U-
  nX#XE4$ )-Compute intersection of window 1 and window 2)rN   rW   rX   rM   rY   rZ   r[   s         r   _compute_intersectionrn      r_   r   c                 n    [        X5      nUS   S:  a  US   S:  a  [        U6 $ [        SU  SU 35      e)rm      r   r<   zIntersection is empty  )rn   r9   r   ra   s      r   rj   rj      sD    "2*Fay1}Qv22$at<==r   c                  6     [        U 6   g! [         a     gf = f)zTest if all given windows intersect.

Parameters
----------
windows: sequence
    One or more Windows.

Returns
-------
bool
    True if all windows intersect.
TF)rk   r   rf   s    r   	intersectrs     s$    g s    
c           	         Uc  Uc  Ub  [         R                  " S[        5        [        U[        5      (       d  [        S5      eX -
  UR                  -  S:  a  [        S5      eX-
  UR                  -  S:  a  [        S5      e[        UXX /X3X/[        S9u  p[        U5      [        U5      p[        U	5      [        U	5      p[        UU
[        X-
  S5      [        X-
  S5      S9$ )ax  Get the window corresponding to the bounding coordinates.

Parameters
----------
left: float, required
    Left (west) bounding coordinates
bottom: float, required
    Bottom (south) bounding coordinates
right: float, required
    Right (east) bounding coordinates
top: float, required
    Top (north) bounding coordinates
transform: Affine, required
    Affine transform matrix.
precision, height, width: int, optional
    These parameters are unused, deprecated in rasterio 1.3.0, and
    will be removed in version 2.0.0.

Returns
-------
Window
    A new Window.

Raises
------
WindowError
    If a window can't be calculated.

zaThe height, width, and precision parameters are unused, deprecated, and will be removed in 2.0.0.z6A transform object is required to calculate the windowr   z%Bounds and transform are inconsistent)op        rW   rX   rY   rZ   )r   r   r   r/   r   r   aer   floatrM   rN   r9   )r   r   r   r   r   rZ   rY   r   rowscols	row_startrow_stop	col_startcol_stops                 r   r   r     s    @ U.)2Go&	

 i((RSS	#a'ABB	#a'ABB	e"	6"	JD d)SYxd)SYx(&,8'-	 r   c                     [        U SSS9n XR                  =(       d    SU R                  =(       d    S4-  u  p#[        R                  " X!R
                  -
  X1R                  -
  5      U-  $ )zConstruct an affine transform matrix relative to a window.

Parameters
----------
window: Window
    The input window.
transform: Affine
    an affine transform matrix.

Returns
-------
Affine
    The affine transform matrix for the given window

r   rZ   rY   rv   )evaluaterW   rX   r   translationcf)r   r   xys       r   r   r   W  s_      fQa0F-#v~~/DEEDA	KK[[*,56 6r   c                     [        XUS9n U R                  nX@R                  -   nU R                  nX`R                  -   nXU4-  u  pXU4-  u  pXX4$ )zGet the spatial bounds of a window.

Parameters
----------
window: Window
    The input window.
transform: Affine
    an affine transform matrix.

Returns
-------
left, bottom, right, top: float
    A tuple of spatial coordinate bounding values.
r   )r   rX   rZ   rW   rY   )r   r   rZ   rY   row_minrow_maxcol_mincol_maxr   r   r   r   s               r   r    r    m  sc     f59FnnG%GnnG$G11LDw//JE##r   c                 l   [        XUS9n [        [        U R                  S5      U5      n[        [        U R                  S5      U5      n[        S[        U R                  U R
                  -   U5      5      n[        S[        U R                  U R                  -   U5      5      n[        XCXd-
  XS-
  5      $ )zCrops a window to given height and width.

Parameters
----------
window : Window.
    The input window.
height, width : int
    The number of rows and cols in the cropped window.

Returns
-------
Window
    A new Window object.
r   r   )r   rM   rN   rX   rW   rZ   rY   r9   )r   rZ   rY   r}   r   r~   r   s          r   cropr     s     f59FC*F3IC*E2I1c&..6==8&ABH1c&..6<<7?@H)(<&( (r   c                 b    [        U [        5      (       a  U $ U u  pE[        R                  XEUX#S9$ )a  Evaluates a window tuple that may contain relative index values.

The height and width of the array the window targets is the context
for evaluation.

Parameters
----------
window: Window or tuple of (rows, cols).
    The input window.
height, width: int
    The number of rows or columns in the array that the window
    targets.

Returns
-------
Window
    A new Window object with absolute index values.
)r{   r|   rZ   rY   	boundless)r/   r9   rI   )r   rZ   rY   r   r{   r|   s         r   r   r     s>    & &&!!
!!tv(- " D 	Dr   c                 J    [        XU5      nUR                  UR                  4$ )ak  The shape of a window.

height and width arguments are optional if there are no negative
values in the window.

Parameters
----------
window: Window
    The input window.
height, width : int, optional
    The number of rows or columns in the array that the window
    targets.

Returns
-------
num_rows, num_cols
    The number of rows and columns of the window.
)r   rZ   rY   )r   rZ   rY   	evaluateds       r   rH   rH     s%    & /IY__,,r   c                 6    [        XUS9n U R                  5       $ )a  Construct a pair of slice objects for ndarray indexing

Starting indexes are rounded down, Stopping indexes are rounded up.

Parameters
----------
window: Window
    The input window.

Returns
-------
row_slice, col_slice: slice
    A pair of slices in row, column order

r   )r   toslices)r   rZ   rY   s      r   window_indexr     s      f59F??r   c                    [        [        U5      5      S:w  a  [        S5      e[        XUS9n US   S   nUS   S   nU R	                  5       u  u  pgu  p[        Xd-  5      U-  n
[        Xt-  5      U-  Xt-  S:w  a  UOS-   n[        X-  5      U-  n[        X-  5      U-  X-  S:w  a  UOS-   n[        XX-
  X-
  5      $ )a  Round window to include full expanse of intersecting tiles.

Parameters
----------
window: Window
    The input window.

block_shapes : tuple of block shapes
    The input raster's block shape. All bands must have the same
    block/stripe structure

Returns
-------
Window
r-   z3All bands must have the same block/stripe structurer   r   )r.   setr   r   r:   intr9   )r   block_shapesrZ   rY   height_shapewidth_shaper}   r~   r   r   r   r   r   r   s                 r   round_window_to_full_blocksr     s      3|"AC 	C f59F?1%Lq/!$K39??3D0Y0I)+,|;G(*+l:!0A51>G )*+k9G()*[8 .!3<G 'G$5w7HIIr   c                 6    U(       a  US:  a  [        S5      eg g )Nr   z.Number of columns or rows must be non-negative)
ValueError)instance	attributevalues      r   validate_length_valuer     s    IJJ ur   T)slotsfrozenc                       \ rS rSrSr\R                  " 5       r\R                  " 5       r\R                  " \	S9r
\R                  " \	S9rS rS rS rS rS r\SS	 j5       rS
 rS rS rSS jrS rS rSrg)r9   i  a  Windows are rectangular subsets of rasters.

This class abstracts the 2-tuples mentioned in the module docstring
and adds methods and new constructors.

Attributes
----------
col_off, row_off: float
    The offset for the window.
width, height: float
    Lengths of the window.

Notes
-----
Previously the lengths were called 'num_cols' and 'num_rows' but
this is a bit confusing in the new float precision world and the
attributes have been changed. The originals are deprecated.
)	validatorc                      SR                  U S9$ )z/Return a nicely formatted representation stringz`Window(col_off={self.col_off}, row_off={self.row_off}, width={self.width}, height={self.height})r   )formatr   s    r   __repr__Window.__repr__,  s    89? :@ :	r   c                 ^    U R                   U R                  U R                  U R                  4$ )zxA flattened form of the window.

Returns
-------
col_off, row_off, width, height: float
    Window offsets and lengths.
rw   r   s    r   flattenWindow.flatten3  s#     dllDJJDDr   c                     [         R                  " U R                  U R                  U R                  U R
                  S9$ )z?A mapping of attribute names and values.

Returns
-------
dict
rw   )collectionsOrderedDictrW   rX   rY   rZ   r   s    r   todictWindow.todict=  s1     &&LL$,,djj;;  	 r   c                     U R                   U R                   U R                  -   4U R                  U R                  U R                  -   44$ )z(Makes an equivalent pair of range tuples)rX   rZ   rW   rY   r   s    r   r:   Window.torangesH  s@     \\4<<$++56\\4<<$**457 	7r   c           	         U R                  5       u  u  pu  p4US:  a  SnUS:  a  SnUS:  a  SnUS:  a  Sn[        [        [        R                  " U5      5      [        [        R
                  " U5      5      5      [        [        [        R                  " U5      5      [        [        R
                  " U5      5      5      4$ )zSlice objects for use as an ndarray indexer.

Returns
-------
row_slice, col_slice: slice
    A pair of slices in row, column order

r   )r:   slicer   mathfloorceil)r   r0r1c0c1s        r   r   Window.toslicesN  s     "]]_(26B6B6B6B #djjn%s499R='9:#djjn%s499R='9:
 	
r   c                    [        U[        [        45      (       a#  [        U5      S:w  a  [	        S5      e[        U6 nO [        U[
        5      (       d  [	        S5      e[        U[        [        45      (       a#  [        U5      S:w  a  [	        S5      e[        U6 nO [        U[
        5      (       d  [	        S5      eUR                  c  US:  a  [	        S5      eUR                  c  US:  a  [	        S5      eUR                  c  S	OUR                  nUR                  c  UOUR                  nUR                  c  S	OUR                  nUR                  c  UOUR                  n	U(       db  US:  d  US:  a%  US:  a  [	        S
5      eUS:  a  Xc-  nUS:  a  Xs-  nUS:  d  U	S:  a%  US:  a  [	        S5      eUS:  a  X-  nU	S:  a  X-  n	[        X-
  S	5      n
[        Xv-
  S	5      nU " XU
US9$ )a5  Construct a Window from row and column slices or tuples / lists of
start and stop indexes. Converts the rows and cols to offsets, height,
and width.

In general, indexes are defined relative to the upper left corner of
the dataset: rows=(0, 10), cols=(0, 4) defines a window that is 4
columns wide and 10 rows high starting from the upper left.

Start indexes may be `None` and will default to 0.
Stop indexes may be `None` and will default to width or height, which
must be provided in this case.

Negative start indexes are evaluated relative to the lower right of the
dataset: rows=(-2, None), cols=(-2, None) defines a window that is 2
rows high and 2 columns wide starting from the bottom right.

Parameters
----------
rows, cols: slice, tuple, or list
    Slices or 2 element tuples/lists containing start, stop indexes.
height, width: float
    A shape to resolve relative values against. Only used when a start
    or stop index is negative or a stop index is None.
boundless: bool, optional
    Whether the inputs are bounded (default) or not.

Returns
-------
Window
rp   z%rows must have a start and stop indexz$rows must be a slice, tuple, or listz%cols must have a start and stop indexz$cols must be a slice, tuple, or listr   z,height is required if row stop index is Nonez+width is required if col stop index is Nonerv   z2height is required when providing negative indexesz1width is required when providing negative indexesrw   )	r/   tuplelistr.   r   r   stopstartrN   )clsr{   r|   rZ   rY   r   rX   r~   rW   r   num_colsnum_rowss               r   rI   Window.from_slicesg  s   D dUDM**4yA~!"IJJ$<DD%((DEEdUDM**4yA~!"IJJ$<DD%((DEE 99!LMM99KLL +#!YY.6DII+# II-5499!x!|A:% '9 : : Q;%Ga<&H!x!|19% '9 : : Q;$Ga<%Hx)3/x)3/78"$ 	$r   c                     [         R                  " U R                  S-   5      n[         R                  " U R                  S-   5      n[	        U R
                  U R                  X#5      $ )zReturn a copy with width and height rounded.

Lengths are rounded to the nearest whole number. The offsets are
not changed.

Parameters
----------
kwds : dict
    Collects keyword arguments that are no longer used.

Returns
-------
Window

g      ?)r   r   rY   rZ   r9   rW   rX   )r   kwdsrY   rZ   s       r   round_lengthsWindow.round_lengths  sI      

4::+,DKK#-.dllDLL%@@r   c                 \    [         R                  " S[        5        U R                  " S0 UD6$ )Nz@round_shape is deprecated and will be removed in Rasterio 2.0.0.r#   )r   r   r   r   )r   r   s     r   round_shapeWindow.round_shape  s*    N&	
 !!)D))r   c                     [         R                  " U R                  S-   5      n[         R                  " U R                  S-   5      n[	        X2U R
                  U R                  5      $ )zReturn a copy with column and row offsets rounded.

Offsets are rounded to the preceding whole number. The lengths
are not changed.

Parameters
----------
kwds : dict
    Collects keyword arguments that are no longer used.

Returns
-------
Window

g?)r   r   rX   rW   r9   rY   rZ   )r   r   rX   rW   s       r   round_offsetsWindow.round_offsets  sI      **T\\C/0**T\\C/0g

DKK@@r   Nc           
          [        [        U R                  US9[        U R                  US9[        U R                  US9[        U R
                  US95      $ )zRound a window's offsets and lengths

Rounding to a very small fraction of a pixel can help treat
floating point issues arising from computation of windows.
)ndigits)r9   roundrW   rX   rY   rZ   )r   r   s     r   r   Window.round  sI     $,,0$,,0$**g.$++w/	
 	
r   c                     [        XU5      $ )z)Return a copy cropped to height and width)r   )r   rZ   rY   s      r   r   Window.crop   s    D%((r   c                     [        X/5      $ )zReturn the intersection of this window and another

Parameters
----------

other: Window
    Another window

Returns
-------
Window
)rk   )r   others     r   rk   Window.intersection  s     TM**r   r#   )r?   r?   Fr$   )r%   r&   r'   r(   r)   attribrW   rX   r   rY   rZ   r   r   r   r:   r   classmethodrI   r   r   r   r   r   rk   r*   r#   r   r   r9   r9     s    $ ggiGggiGGG34EWW45FE	 7
2 Z$ Z$xA(*A(
)+r   r9   c                 h   / nU R                   U R                  -   nU R                  U R                  -   nU R                   nU R                  nXd:  a`  Xa-   U:  a  XF-
  nOUnXu:  a5  Xr-   U:  a  XW-
  n	OUn	UR	                  [        XvX5      5        Xr-  nXu:  a  M5  Xa-  nU R                  nXd:  a  M`  U$ )a  Divide a window into smaller windows.

Windows have no overlap and will be at most the desired
height and width. Smaller windows will be generated where
the height and width do not evenly divide the window dimensions.

Parameters
----------
window : Window
    Source window to subdivide.
height : int
    Subwindow height.
width : int
    Subwindow width.

Returns
-------
list of Windows
)rX   rZ   rW   rY   rL   r9   )
r   rZ   rY   
subwindowsirowicolrX   rW   _height_widths
             r   	subdivider     s    ( J>>FMM)D>>FLL(DnnGnnG
.d"nGGn%fWvGHG n 	..! ." r   r$   )NNNNr=   )F)r?   r?   ))r)   r   collections.abcr   r5   r   r   affiner   r   numpyrB   rasterio.errorsr   r   rasterio.transformr   r   r
   r7   r:   rU   r^   rc   rh   rk   rn   rj   rs   r   r   r    r   r   rH   r   r   r   sr9   r   r#   r   r   <module>r      s   &  $       C 6Q) Q)h
3"l+
 - -  4 4"+>  * RV;|6,$6(4D6-.(#JLK
 d4 }+ }+ !}+@,r   