
    `jN1                         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
Jr  \R                  " \5      rS r  SS jrSS jrS	 rS
 r      SS jrSS jrSS jrg)zImplementations of various common operations.

Including `show()` for displaying an array or with matplotlib.
Most can handle a numpy array or `rasterio.Band()`.
Primarily supports `$ rio insp`.
    N)OrderedDict)DatasetReader)guard_transformc                  `     SSK Jn   U $ ! [        [        4 a    SnUS-  n[        U5      ef = f)zKimport matplotlib.pyplot
raise import error if matplotlib is not installed
r   NzCould not import matplotlib
z*matplotlib required for plotting functions)matplotlib.pyplotpyplotImportErrorRuntimeError)pltmsgs     P/var/www/html/Sattelite-Image/venv/lib/python3.13/site-packages/rasterio/plot.pyget_pltr      s>    '
& -;;#s   
 #-c
                    [        5       n[        U [        5      (       aU  U S   R                  U S   5      n[	        UR
                  5      S:  a  [        U5      nU(       a  [        U S   5      U
S'   GO^[        U [        5      (       a  U(       a  [        U 5      U
S'   U R                  S::  a  U R                  SSS9nGO [        [        U R                  U R                  5      5      n[        R                  R                   nUR"                  UR$                  UR&                  4 Vs/ s H  nX   PM	     nnU R                  USS9n[        U5      nO}U R*                  S:  a  [,        R.                  R1                  U 5      n U R*                  S:  a   U=(       d    S	n[        XS
S
2S
S
24   5      nOU nU(       a  U(       a  [        X5      U
S'   U	(       a  U(       a  [3        X5      nOUR*                  S:X  a  [5        U5      nOfUR*                  S:  aV  [7        U5      R9                  [,        R:                  5      n[=        U5       H  u  nn[5        U5      UU'   M     [        U5      nSnU(       d  SnUR?                  5       nU(       a  SU
;  a  U
RA                  SS5      U
S'   U
RA                  SS5      U
S'   U
RA                  SS5      U
S'   URB                  " U4SS0U
D6nUc
  [E        SSS9nU(       a  URF                  " U40 UD6  OURH                  " U40 U
D6  U(       a  URK                  USS9  U(       a  URM                  5         U$ s  snf ! [(         a    U=(       d    SnU R                  USS9n GN9f = f)a  Display a raster or raster band using matplotlib.

Parameters
----------
source : array or dataset object opened in 'r' mode or Band or tuple(dataset, bidx)
    If Band or tuple (dataset, bidx), display the selected band.
    If raster dataset display the rgb image
    as defined in the colorinterp metadata, or default to first band.
with_bounds : bool (opt)
    Whether to change the image extent to the spatial bounds of the image,
    rather than pixel coordinates. Only works when source is
    (raster dataset, bidx) or raster dataset.
contour : bool (opt)
    Whether to plot the raster data as contours
contour_label_kws : dictionary (opt)
    Keyword arguments for labeling the contours,
    empty dictionary for no labels.
indexes: list or tupel, optional, defines the color composite of bands.
ax : matplotlib.axes.Axes, optional
    Axes to plot on, otherwise uses current axes.
title : str, optional
    Title for the figure.
transform : Affine, optional
    Defines the affine transform if source is an array
percent_range: tuple, optional
    percent_range[0], the minimum value (cumulative percentage) of the histogram for histogram streching,
    percent_range[1], the maximum value (cumulative percentage) of the histogram for histogram streching
    default percent_range is set to (2, 98).
adjust : bool
    If the plotted data is an RGB image, adjust the values of
    each band so that they fall between 0 and 1 before plotting. If
    True, values will be adjusted by the min / max of each band. If
    False, no adjustment will be applied.
**kwargs : key, value pairings optional
    These will be passed to the :func:`matplotlib.pyplot.imshow` or
    :func:`matplotlib.pyplot.contour` contour method depending on contour argument.

Returns
-------
ax : matplotlib.axes.Axes
    Axes with plot.

r         extent   Tmasked)r   r   r   )r   r   r   NFcmapcolorsred
linewidthsg      ?alphag?originupper   )fontsizeinlinebold
fontweight)'r   
isinstancetuplereadlenshapereshape_as_imageplotting_extentr   countr   zipcolorinterpindexesrasterioenumsColorInterpr   greenblueKeyErrorndimnpmasqueezecontrast_strechadjust_bandreshape_as_rasterastypefloat64	enumerategcagetcontourdictclabelimshow	set_titleshow)sourcewith_boundsr@   contour_label_kwsr-   axtitle	transformpercent_rangeadjustkwargsr   arrsource_colorinterpr,   cirgb_indexesiibandrE   Cs                        r   rE   rE   "   s   Z )C&%  QinnVAY'syy>Q"3'C.vay9F8	FM	*	*.v6F8<<1++a+-C8 &1V5G5G1X%Y"&nn88
  +0A0A;CSCSTT '*T   kk+dk;
 #3'C ;;!UU]]6*F;;!*G"61a-#89C C.s>F8!#5Cxx1}!#&Q',33BJJ? )#HB)$/CG !/&s+DWWY%zz(E:F8%zz,<| **Wc2wJJs575f5$ $a =IIa-,-
		#  
Uv.
IQ  8!.Ykk'$k78s%   A(M *M8M M %NNc                 Z   [        U S5      (       aX  U R                  R                  U R                  R                  U R                  R                  U R                  R
                  4nU$ U(       d  [        S5      e[        U5      nU R                  SS u  p4US-  u  pVXU4-  u  pxXWX4nU$ )a!  Returns an extent in the format needed
 for :func:`matplotlib.pyplot.imshow` (left, right, bottom, top)
 instead of rasterio's bounds (left, bottom, right, top)

Parameters
----------
source : numpy.ndarray or dataset object opened in 'r' mode
    If array, data in the order rows, columns and optionally bands. If array
    is band order (bands in the first dimension), use arr[0]
transform: Affine, required if source is array
    Defines the affine transform if source is an array

Returns
-------
tuple of float
    left, right, bottom, top
boundsz+transform is required if source is an arrayr   r   )r   r   )	hasattrrW   leftrightbottomtop
ValueErrorr   r'   )	rF   rK   r   rowscolsrY   r\   rZ   r[   s	            r   r)   r)      s    $ vx  --$$fmm&9&9--&&(9(9; M 9; 	; $I.	\\!A&
&	!4L0v+M    c                 J    [         R                  R                  U / SQ5      nU$ )a7  Returns the source array reshaped into the order
expected by image processing and visualization software
(matplotlib, scikit-image, etc)
by swapping the axes order from (bands, rows, columns)
to (rows, columns, bands)

Parameters
----------
arr : array-like of shape (bands, rows, columns)
    image to reshape
)r   r   r   )r5   r6   	transposerO   ims     r   r(   r(      s     
i	(BIr`   c                 8    [         R                  " U / SQ5      nU$ )zReturns the array in a raster order
by swapping the axes order from (rows, columns, bands)
to (bands, rows, columns)

Parameters
----------
arr : array-like in the image form of (rows, columns, bands)
    image to reshape
)r   r   r   )r5   rb   rc   s     r   r:   r:      s     
c9	%BIr`   c                    [        5       n[        U [        5      (       a  U R                  US9n	O>[        U [        [
        R                  45      (       a  U S   R                  U S   US9n	OU n	Uc,  [        R                  " U	5      [        R                  " U	5      4n[        U	R                  5      S:X  a3  [        R                  " U	R                  5       S5      R                  n	S/n
O-U	R                  U	R                  S   S5      R                  n	/ SQn
U	R                  S   [        U
5      :  au  U	R                  S   [        U
5      -
  nU
R!                  [        R"                  R%                  UR'                  S	5      " [        R(                  " SSU5      5      5      5        OU
SU	R                  S    n
U(       a  UnOp[        U [        [
        R                  45      (       a  [+        U S   5      /nO;[,        R.                  " [        U	5      5       Vs/ s H  n[+        US-   5      PM     nnU(       a  S
nOSnUR1                  5       nUR3                  5       nUR4                  " U	4XXS.UD6  UR7                  SS9  UR9                  USS9  UR;                  S5        UR=                  S5        UR?                  S5        U(       a  URA                  5         ggs  snf )a  Easily display a histogram with matplotlib.

Parameters
----------
source : array or dataset object opened in 'r' mode or Band or tuple(dataset, bidx)
    Input data to display.
    The first three arrays in multi-dimensional
    arrays are plotted as red, green, and blue.
bins : int, optional
    Compute histogram across N bins.
masked : bool, optional
    When working with a `rasterio.Band()` object, specifies if the data
    should be masked on read.
title : str, optional
    Title for the figure.
ax : matplotlib.axes.Axes, optional
    The raster will be added to this axes if passed.
label : str, optional
    String, or list of strings. If passed, matplotlib will use this label list.
    Otherwise, a default label list will be automatically created
range : list, optional
    List of `[min, max]` values. If passed, matplotlib will use this range.
    Otherwise, a default range will be automatically created
**kwargs : optional keyword arguments
    These will be passed to the :meth:`matplotlib.axes.Axes.hist` method.
r   r   r   Nr   gold)r   r1   r2   violetrg   saddlebrownAccentFT)binscolorlabelrangezupper right)locr    r!   DN	Frequency)!r   r#   r   r%   r$   r.   Bandr5   nanminnanmaxr&   r'   expand_dimsflattenTreshapeextendndarraytolistget_cmaplinspacestrbuiltinsro   r>   
get_figurehistlegendrD   grid
set_xlabel
set_ylabelrE   )rF   rl   r   rJ   rI   rn   ro   rN   r   rO   r   nlabelsirE   figs                   r   	show_histr      s<   H )C&-((kkk(	FUHMM2	3	3QinnVAYvn6} 		#		#.
399~nnS[[]A.00kk#))A,+--J
 yy}s6{"IIbMCK'bjj''X(>r{{1aQR?S(TUV2' 
 fuhmm455&)n%F*2..S*BC*BQc!a%j*BFC	WWY
--/CGGCNdNvNII-I LL6L*GGDMMM$MM+
 # Ds   K c                     [         R                  " [         R                  " U 5      US   US   45      nXS   -
  US   US   -
  -  n [         R                  " U SS5      n U $ )a  
Histogram streching for better image visualization.

Parameters
----------
arr : array-like in the image form of (rows, columns, bands)
    image to reshape
percent_range: tuple, optional
    percent_range[0], the minimum values (cumulative percentage) of the histogram for histogram streching,
    percent_range[1], the maximum value (cumulative percentage) of the histogram for histogram streching
    default percent_range is set to (2, 98).
r   r   )r5   nanpercentilearrayclip)rO   rL   arr_hists      r   r8   r8   S  sc     a0@-PQBR/STH!x{8A;6
7C
''#q!
CJr`   c                     [         R                  " [         R                  " U 5      5      n[         R                  " [         R                  " U 5      5      nX-
  X2-
  -  $ )a5  Adjust a band to be between 0 and 1.
Parameters
----------
band : array, shape (height, width)
    A band of a raster object.
kind : str
    An unused option. For now, there is only one option ('linear').

Returns
-------
band_normed : array, shape (height, width)
    An adjusted version of the input band.

)r5   r<   rt   ru   )rT   kindiminimaxs       r   r9   r9   f  s?     ::biio&D::biio&DKDK((r`   )	TFNNNNNNT)N)
   T	HistogramNNN))g       @g     X@)__doc__r   collectionsr   loggingnumpyr5   r.   rasterio.ior   rasterio.transformr   	getLogger__name__loggerr   rE   r)   r(   r:   r   r8   r9    r`   r   <module>r      s|     #    % .			8	$
 SWIMK\D"" 



^B&)r`   