U
    g7                      @   s   d dl mZmZmZmZ d dlmZ d dlmZ	 d dl
mZ d dlmZ d dlmZ ddlmZmZmZmZ G d	d
 d
eZG dd deZdS )    )byrefc_doublec_intc_void_p)GDALException)raster)GDALRasterBase)numpy)	force_str   )GDAL_COLOR_TYPESGDAL_INTEGER_TYPESGDAL_PIXEL_TYPESGDAL_TO_CTYPESc                   @   s   e Zd ZdZdd Zdd Zedd Zedd	 Zed
d Z	edd Z
dZd#ddZedd Zedd Zedd Zedd Zedd Zejdd Zd$ddZd%ddZd&d!d"Zd S )'GDALBandzQ
    Wrap a GDAL raster band, needs to be obtained from a GDALRaster object.
    c                 C   s   || _ t|j|| _d S N)sourcecapiZget_ds_raster_band_ptr)selfr   index r   G/tmp/pip-unpacked-wheel-ye1bytdm/django/contrib/gis/gdal/raster/band.py__init__   s    zGDALBand.__init__c                 C   s   | j   d| _dS )z
        Call the flush method on the Band's parent raster and force a refresh
        of the statistics attribute when requested the next time.
        TN)r   _flush_stats_refreshr   r   r   r   r      s    
zGDALBand._flushc                 C   s   t t| jS )z<
        Return the description string of the band.
        )r
   r   Zget_band_descriptionr   r   r   r   r   description   s    zGDALBand.descriptionc                 C   s   t | jS )z7
        Width (X axis) in pixels of the band.
        )r   Zget_band_xsizer   r   r   r   r   width%   s    zGDALBand.widthc                 C   s   t | jS )z8
        Height (Y axis) in pixels of the band.
        )r   Zget_band_ysizer   r   r   r   r   height,   s    zGDALBand.heightc                 C   s   | j | j S )zA
        Return the total number of pixels in this band.
        )r   r   r   r   r   r   pixel_count3   s    zGDALBand.pixel_countFc                 C   s   t  t  t  t  f\}}}}| jt|t|t|t|t|t t g}|sT| jr\tj}nd}	|dt|	 tj	}z ||  |j
|j
|j
|j
f}
W n tk
r   d}
Y nX d| _|
S )a  
        Compute statistics on the pixel values of this band.

        The return value is a tuple with the following structure:
        (minimum, maximum, mean, standard deviation).

        If approximate=True, the statistics may be computed based on overviews
        or a subset of image tiles.

        If refresh=True, the statistics will be computed from the data directly,
        and the cache will be updated where applicable.

        For empty bands (where all pixel values are nodata), all statistics
        values are returned as None.

        For raster formats using Persistent Auxiliary Metadata (PAM) services,
        the statistics might be cached in an auxiliary file.
        T   )NNNNF)r   r   r   r   r   r   r   Zcompute_band_statisticsinsertZget_band_statisticsvaluer   )r   ZrefreshZapproximateZsminZsmaxZsmeanZsstdZ
stats_argsfuncforceresultr   r   r   
statistics<   s,          

zGDALBand.statisticsc                 C   s   |   d S )z?
        Return the minimum pixel value for this band.
        r   r'   r   r   r   r   minj   s    zGDALBand.minc                 C   s   |   d S )z?
        Return the maximum pixel value for this band.
        r   r(   r   r   r   r   maxq   s    zGDALBand.maxc                 C   s   |   d S )zC
        Return the mean of all pixel values of this band.
        r!   r(   r   r   r   r   meanx   s    zGDALBand.meanc                 C   s   |   d S )zQ
        Return the standard deviation of all pixel values of this band.
           r(   r   r   r   r   std   s    zGDALBand.stdc                 C   s6   t  }t| j|}|sd}n|  tkr2t|}|S )zQ
        Return the nodata value for this band, or None if it isn't set.
        N)r   r   Zget_band_nodata_valuer   datatyper   int)r   Znodata_existsr#   r   r   r   nodata_value   s    zGDALBand.nodata_valuec                 C   sV   |dkr$t jstdt | j n&t|ttfs<tdnt | j| |   dS )z5
        Set the nodata value for this band.
        Nz-GDAL >= 2.1 required to delete nodata values.z%Nodata value must be numeric or None.)	r   Zdelete_band_nodata_value
ValueErrorr   
isinstancer/   floatZset_band_nodata_valuer   )r   r#   r   r   r   r0      s    
c                 C   s   t | j}|rt| }|S )z?
        Return the GDAL Pixel Datatype for this band.
        )r   Zget_band_datatyper   r   )r   	as_stringdtyper   r   r   r.      s    zGDALBand.datatypec                 C   s   t | j}|rt| }|S )z3Return the GDAL color interpretation for this band.)r   Zget_band_color_interpr   r   )r   r4   colorr   r   r   color_interp   s    zGDALBand.color_interpNc           	      C   sl  |pd}|p&| j |d  | j|d  f}|p.|}tdd |D rJtd|d | j ksf|d | jkrntdt|   |d |d   }|dkrd}| }n6d}t|ttfst	rt|t	j
r||}n|| }t| j||d |d |d |d t||d |d |  dd |dkr`|r.t|S t	rVt	j|t	|d	tt|S t|S n|   dS )
ae  
        Read or writes pixel values for this band. Blocks of data can
        be accessed by specifying the width, height and offset of the
        desired block. The same specification can be used to update
        parts of a raster by providing an array of values.

        Allowed input data types are bytes, memoryview, list, tuple, and array.
        )r   r   r   r   c                 s   s   | ]}|d kV  qdS )r   Nr   ).0xr   r   r   	<genexpr>   s     z GDALBand.data.<locals>.<genexpr>zOffset too big for this raster.zSize is larger than raster.N)r5   )r   r   anyr1   r   r.   r2   bytes
memoryviewr	   Zndarrayfrom_buffer_copyr   Zband_ior   r   Z
frombufferr5   Zreshapetuplereversedlistr   )	r   dataoffsetsizeshapeZas_memoryviewZctypes_arrayZaccess_flagZ
data_arrayr   r   r   rB      sJ    	       
 

zGDALBand.data)FF)F)F)NNNNF)__name__
__module____qualname____doc__r   r   propertyr   r   r   r    r   r'   r)   r*   r+   r-   r0   setterr.   r7   rB   r   r   r   r   r      s8   




.






	
r   c                       s4   e Zd Z fddZdd Zdd Zdd Z  ZS )	BandListc                    s   || _ t   d S r   )r   superr   )r   r   	__class__r   r   r      s    zBandList.__init__c                 c   s*   t dt| d D ]}t| j|V  qd S )Nr   )rangelenr   r   )r   idxr   r   r   __iter__   s    zBandList.__iter__c                 C   s   t | jjS r   )r   Zget_ds_raster_countr   r   r   r   r   r   __len__   s    zBandList.__len__c                 C   s8   zt | j|d W S  tk
r2   td| Y nX d S )Nr   zUnable to get band index %d)r   r   r   )r   r   r   r   r   __getitem__   s    zBandList.__getitem__)rF   rG   rH   r   rS   rT   rU   __classcell__r   r   rN   r   rL      s   rL   N)ctypesr   r   r   r   Zdjango.contrib.gis.gdal.errorr   Z"django.contrib.gis.gdal.prototypesr   r   Z#django.contrib.gis.gdal.raster.baser   Zdjango.contrib.gis.shortcutsr	   Zdjango.utils.encodingr
   constr   r   r   r   r   rA   rL   r   r   r   r   <module>   s    _