

_V                 @   s   d  d l  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 d  d l m Z d	 d
 l m Z m Z m Z Gd d   d e  Z Gd d   d e  Z d S)    )byrefc_int)GDALBase)GDALException)raster)numpy)six)
force_text)range   )GDAL_INTEGER_TYPESGDAL_PIXEL_TYPESGDAL_TO_CTYPESc               @   s   e  Z d  Z d Z d d   Z e d d    Z e 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 d d d  Z d S)GDALBandzR
    Wraps a GDAL raster band, needs to be obtained from a GDALRaster object.
    c             C   s%   | |  _  t j | j |  |  _ d  S)N)sourcecapiZget_ds_raster_band_ptr)selfr   index r   E/tmp/pip-build-ghmbqnp_/Django/django/contrib/gis/gdal/raster/band.py__init__   s    	zGDALBand.__init__c             C   s   t  t j |  j   S)z=
        Returns 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 |  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 |  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)zB
        Returns the total number of pixels in this band.
        )r   r   )r   r   r   r   pixel_count+   s    zGDALBand.pixel_countc             C   s   t  j |  j t t     S)z@
        Returns the minimum pixel value for this band.
        )r   Zget_band_minimumr   r   r   )r   r   r   r   min2   s    zGDALBand.minc             C   s   t  j |  j t t     S)z@
        Returns the maximum pixel value for this band.
        )r   Zget_band_maximumr   r   r   )r   r   r   r   max9   s    zGDALBand.maxc             C   sR   t    } t j |  j |  } | s- d } n! |  j   t k rN t |  } n  | S)zR
        Returns 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_existsvaluer   r   r   nodata_value@   s    		zGDALBand.nodata_valuec             C   sH   t  | t t f  s$ t d   n  t j |  j |  |  j j   d S)z6
        Sets the nodata value for this band.
        zNodata value must be numeric.N)	
isinstancer   float
ValueErrorr   Zset_band_nodata_valuer   r   _flush)r   r    r   r   r   r!   O   s    Fc             C   s)   t  j |  j  } | r% t | } n  | S)z@
        Returns the GDAL Pixel Datatype for this band.
        )r   Zget_band_datatyper   r   )r   	as_stringdtyper   r   r   r   Y   s    zGDALBand.datatypeNc             C   s  | s d	 } n  | s: |  j  | d |  j | d f } n  t d d   | D  rb t d   n  | d |  j  k s | d |  j k r t d   n  t |  j   | d | d } | d k r d } |   } nT d } t | t t j	 f  st
 rt | t
 j  r| j |  } n | |   } t j |  j | | d | d | d | d t |  | d | d |  j   d d  | d k r| rt	 |  St
 rt
 j | d t
 j |  j |  St |  Sn |  j j   d S)
af  
        Reads 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   c             s   s   |  ] } | d  k Vq d S)r   Nr   ).0xr   r   r   	<genexpr>q   s    z GDALBand.data.<locals>.<genexpr>zOffset too big for this raster.zSize is larger than raster.Nr'   )r   r   )r   r   anyr$   r   r   r"   bytesr   
memoryviewr   Zndarrayfrom_buffer_copyr   Zband_ior   r   Z
frombufferr'   Zreshapelistr   r%   )r   dataoffsetsizeZas_memoryviewZctypes_arrayZaccess_flagZ
data_arrayr   r   r   r0   b   s6    		%& 0
zGDALBand.data)__name__
__module____qualname____doc__r   propertyr   r   r   r   r   r   r!   setterr   r0   r   r   r   r   r      s   
	r   c               @   s@   e  Z d  Z d d   Z d d   Z d d   Z d d   Z d	 S)
BandListc             C   s   | |  _  t j |   d  S)N)r   r/   r   )r   r   r   r   r   r      s    	zBandList.__init__c             c   s9   x2 t  d t |   d  D] } t |  j |  Vq Wd  S)Nr   )r
   lenr   r   )r   idxr   r   r   __iter__   s     zBandList.__iter__c             C   s   t  j |  j j  S)N)r   Zget_ds_raster_countr   r   )r   r   r   r   __len__   s    zBandList.__len__c             C   sA   y t  |  j | d  SWn" t k
 r< t d |   Yn Xd  S)Nr   zUnable to get band index %d)r   r   r   )r   r   r   r   r   __getitem__   s    zBandList.__getitem__N)r3   r4   r5   r   r<   r=   r>   r   r   r   r   r9      s   r9   N)ctypesr   r   Zdjango.contrib.gis.gdal.baser   Zdjango.contrib.gis.gdal.errorr   Z"django.contrib.gis.gdal.prototypesr   r   Zdjango.contrib.gis.shortcutsr   Zdjango.utilsr   Zdjango.utils.encodingr	   Zdjango.utils.six.movesr
   constr   r   r   r   r/   r9   r   r   r   r   <module>   s   