
    gG                        d dl Z d dlZd dlZd dlZd dlmZmZmZmZ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 d d	lmZmZmZmZmZ d d
l m!Z!m"Z" d dl#m$Z$ d dl%m&Z&m'Z' d dl(m)Z)  G d de*      Z+ G d de      Z,y)    N)	addressofbyrefc_bufferc_char_pc_doublec_intc_void_p	string_at)Path)Driver)GDALException)rasterBandList)GDALRasterBase)GDAL_RESAMPLE_ALGORITHMSVSI_DELETE_BUFFER_ON_READVSI_FILESYSTEM_PREFIXVSI_MEM_FILESYSTEM_BASE_PATHVSI_TAKE_BUFFER_OWNERSHIP)SpatialReferenceSRSException)
json_regex)force_bytes	force_str)cached_propertyc                        e Zd ZddddZ fdZed        Zej                  d        Zed        Zej                  d	        Z xZ	S )
TransformPoint)r      )      )      )originscaleskewc                     |j                   | j                  |   d      }|j                   | j                  |   d      }t        |   ||g       || _        || _        y )Nr   r    )geotransformindicessuper__init___raster_prop)selfr   propxy	__class__s        }/var/www/python.vincentserveurtest.ovh/public_html/venv/lib/python3.12/site-packages/django/contrib/gis/gdal/raster/source.pyr+   zTransformPoint.__init__*   s^    T 21 56T 21 56!Q 
    c                     | d   S Nr    r.   s    r3   r0   zTransformPoint.x1       Awr4   c                     | j                   j                  }||| j                  | j                     d   <   || j                   _        y r6   r,   r(   r)   r-   r.   valuegtfs      r3   r0   zTransformPoint.x5   9    ll''+0DLL$Q'($'!r4   c                     | d   S Nr    r7   r8   s    r3   r1   zTransformPoint.y;   r9   r4   c                     | j                   j                  }||| j                  | j                     d   <   || j                   _        y rA   r;   r<   s      r3   r1   zTransformPoint.y?   r?   r4   )
__name__
__module____qualname__r)   r+   propertyr0   setterr1   __classcell__r2   s   @r3   r   r   #   sp    G   XX( (
   XX( (r4   r   c                       e Zd ZdZej
                  ZddZ fdZd Z	d Z
d Zed        Zed        Zed	        Zed
        Zed        Zed        Zed        Zej*                  d        Zed        Zej*                  d        Zed        Zej*                  d        Zed        Zed        Zed        Zed        Zed        ZddZddZ	 ddZed        Z  xZ!S ) 
GDALRasterz0
    Wrap a raster GDAL Data Source object.
    c                 
   |rdnd| _         t        j                          t        |t              r*t        j                  |      rt        j                  |      }t        |t        t        f      r}t	        |      }|j                  t              s-t        j                  j                  |      st        d|z        	 t!        j"                  t%        |      | j                         | _        y t        |t*              rd| _         t-        j.                  |      }t1        |      | _        t        j                  j5                  t6        t	        t9        j:                                     }t!        j<                  t%        |      t?        | j2                        |t@               	 t!        j"                  t%        |      | j                         | _        y t        |tD              rd| _         t        |jG                  dd            }|jH                  dk7  r(d|vr$t        d	j)                  |jH                              d
|vsd|vrt        d      d|vrt        d      g }|jG                  di       jK                         D ]D  \  }}	dj)                  ||	      }
|jM                  |
jO                         jQ                                F |jM                  d        tS        tU        |      z  | }t!        jV                  |j&                  t%        |jG                  dd            |d
   |d   |jG                  dtU        |jG                  dg                   |jG                  dd      t?        |            | _        tY        |jG                  dg             D ]  \  }}| jZ                  |   }d|v r@|d   |_.        |j\                  *d|vsd|v sd|v r|j_                  |j\                  fd       |j_                  |jG                  d      |jG                  d      |jG                  d      |jG                  d              |jG                  d      | _0        d|v r&|d   \  | jb                  _2        | jb                  _3        d|v r&|d   \  | jh                  _2        | jh                  _3        d |v r'|d    \  | jj                  _2        | jj                  _3        y y t        |tl              r|| _        y t        d!j)                  to        |                  # t        $ r }t        dj)                  ||            d }~ww xY w# t        $ r* t!        jB                  t%        |             t        d      w xY w)"Nr    r   z(Unable to read raster source input "%s".z+Could not open the datasource at "{}" ({}).z1Failed creating VSI raster from the input buffer.driverMEMnamez5Specify name for creation of raster with driver "{}".widthheightz;Specify width and height attributes for JSON or dict input.sridz$Specify srid for JSON or dict input.papsz_optionsz{}={} nr_of_bandsbandsdatatype   nodata_valuedatasizeshape)r    r    )rZ   r\   offset)rZ   r[   r\   r]   r$   r%   r&   z%Invalid data source input type: "{}".)8_writer   ensure_registered
isinstancestrr   matchjsonloadsr   
startswithr   ospathexistsr   capiopen_dsr   _ptrformatbytessys	getsizeofr   	_ds_inputjoinr   uuiduuid4create_vsi_file_from_mem_bufferr   r   unlink_vsi_filedictgetrO   itemsappendupperencoder   len	create_ds	enumeraterV   rY   rZ   srsr$   r0   r1   r%   r&   r	   type)r.   ds_inputwriteerrr[   vsi_pathrM   rS   keyvaloptioni
band_inputbands                 r3   r+   zGDALRaster.__init__M   s    aa  " h$)9)9()Czz(+H hd,8}H&&'<=bggnnG $>I  LLX)>L	
 %(DK==*D &h/DNww||$@#djjlBSTH00H%dnn%)	Y LLX)>L	
 $'DK HLL59:F {{e#h(>#KRR  h&((*B#Q 
 X%#$JKK M$LL"=CCES S1$$V\\^%:%:%<= F   & &M(::]KM HLL45!"]CWb0I,JKZ+m$DI "+8<<+D!E:zz!}!Z/(2>(BD% ((4j0!Z/"j0		(9(9';6	J		#/#/$..1%>>(3	   "F*  ||F+DH 8#/7/A,t{{}("-5g->*

djjl!+3F+;(		TYY[ "(+ DI7>>tH~N a ! #AHHSVW 2 ! Y$$[%:;#$WXXYs$   >.T !.T9 	T6T11T693U,c                     | j                   r(t        j                  t        | j                               t
        |           y N)is_vsi_basedri   ru   r   rO   r*   __del__)r.   r2   s    r3   r   zGDALRaster.__del__   s-      TYY!78r4   c                     | j                   S r   )rO   r8   s    r3   __str__zGDALRaster.__str__   s    yyr4   c                 D    dt        t        | j                              z  S )zJ
        Short-hand representation because WKB may be very large.
        z<Raster object at %s>)hexr   rk   r8   s    r3   __repr__zGDALRaster.__repr__   s     'Ytyy-A)BBBr4   c                 p    | j                   st        d      t        j                  | j                         y)a  
        Flush all data from memory into the source file if it exists.
        The data that needs flushing are geotransforms, coordinate systems,
        nodata_values and pixel values. This function will be called
        automatically wherever it is needed.
        z9Raster needs to be opened in write mode to change values.N)r^   r   ri   flush_dsrk   r8   s    r3   _flushzGDALRaster._flush   s-     {{K  	dii r4   c                    | j                   r| j                  j                  t              sy t	               }t        j                  t        | j                        t        |      t              }t        ||j                        S r   )r   rO   re   r   r   ri   get_mem_buffer_from_vsi_filer   r   r   r
   r=   )r.   
out_lengthdats      r3   
vsi_bufferzGDALRaster.vsi_buffer   se     $))"6"67S"TW
//		"*%
 j..//r4   c                 \    | j                   xr | j                  j                  t              S r   )rk   rO   re   r   r8   s    r3   r   zGDALRaster.is_vsi_based  s!    yyHTYY112GHHr4   c                 R    t        t        j                  | j                              S )zi
        Return the name of this raster. Corresponds to filename
        for file-based rasters.
        )r   ri   get_ds_descriptionrk   r8   s    r3   rO   zGDALRaster.name  s     00;<<r4   c                 V    t        j                  | j                        }t        |      S )z>
        Return the GDAL Driver used for this raster.
        )ri   get_ds_driverrk   r   )r.   	ds_drivers     r3   rM   zGDALRaster.driver  s#    
 &&tyy1	i  r4   c                 @    t        j                  | j                        S )z+
        Width (X axis) in pixels.
        )ri   get_ds_xsizerk   r8   s    r3   rP   zGDALRaster.width      
   ++r4   c                 @    t        j                  | j                        S )z,
        Height (Y axis) in pixels.
        )ri   get_ds_ysizerk   r8   s    r3   rQ   zGDALRaster.height  r   r4   c                     	 t        j                  | j                        }|syt        |d      S # t        $ r Y yw xY w)zF
        Return the SpatialReference used in this GDALRaster.
        Nwkt)srs_type)ri   get_ds_projection_refrk   r   r   )r.   r   s     r3   r   zGDALRaster.srs&  s@    
	,,TYY7C#C%88 		s   !1 1 	==c                    t        |t              r|}n-t        |t        t        f      rt        |      }nt	        d      t        j                  | j                  |j                  j                                | j                          y)z
        Set the spatial reference used in this GDALRaster. The input can be
        a SpatialReference or any parameter accepted by the SpatialReference
        constructor.
        z/Could not create a SpatialReference from input.N)r`   r   intra   
ValueErrorri   set_ds_projection_refrk   r   r{   r   )r.   r=   r   s      r3   r   zGDALRaster.srs3  s`     e-.CSz*"5)CNOO""499cggnn.>?r4   c                 .    | j                   j                  S )zA
        Shortcut to access the srid of this GDALRaster.
        )r   rR   r8   s    r3   rR   zGDALRaster.sridC  s    
 xx}}r4   c                     || _         y)zE
        Shortcut to set this GDALRaster's srs from an srid.
        N)r   )r.   r=   s     r3   rR   zGDALRaster.sridJ  s    
 r4   c                     t        dz         }t        j                  | j                  t	        |             t        |      S )z
        Return the geotransform of the data source.
        Return the default geotransform if it does not exist or has not been
        set previously. The default is [0.0, 1.0, 0.0, 0.0, 0.0, -1.0].
        rX   )r   ri   get_ds_geotransformrk   r   list)r.   r>   s     r3   r(   zGDALRaster.geotransformQ  s2     !|  E#J7Cyr4   c                     t        |      dk7  st        d |D              st        d      t        dz  | }t	        j
                  | j                  t        |             | j                          y)z)Set the geotransform for the data source.rX   c              3   H   K   | ]  }t        |t        t        f        y wr   )r`   r   float).0r0   s     r3   	<genexpr>z*GDALRaster.geotransform.<locals>.<genexpr>`  s     &SFqz!c5\'BFs    "z.Geotransform must consist of 6 numeric values.N)	r|   allr   r   ri   set_ds_geotransformrk   r   r   )r.   valuess     r3   r(   zGDALRaster.geotransform]  sX     v;!3&SF&S#SMNNQ,(  E&M:r4   c                     t        | d      S )z3
        Coordinates of the raster origin.
        r$   r   r8   s    r3   r$   zGDALRaster.origing  s    
 dH--r4   c                     t        | d      S )z@
        Pixel scale in units of the raster projection.
        r%   r   r8   s    r3   r%   zGDALRaster.scalen  s    
 dG,,r4   c                     t        | d      S )z7
        Skew of pixels (rotation parameters).
        r&   r   r8   s    r3   r&   zGDALRaster.skewu  s    
 dF++r4   c                    | j                   j                  | j                  j                  | j                  z  z   }| j                   j                  | j                  j                  | j
                  z  z   }t        || j                   j                        }t        || j                   j                        }t        || j                   j                        }t        || j                   j                        }||||fS )zJ
        Return the extent as a 4-tuple (xmin, ymin, xmax, ymax).
        )r$   r0   r%   rP   r1   rQ   minmax)r.   xvalyvalxminxmaxyminymaxs          r3   extentzGDALRaster.extent|  s     {{}}tzz||djj88{{}}tzz||dkk994'4'4'4'T4%%r4   c                     t        |       S r   r   r8   s    r3   rV   zGDALRaster.bands  s    ~r4   c                    |j                  d| j                         |j                  d| j                         |j                  d| j                  j                         |j                  d| j
                         |j                  d| j                         |j                  d| j                         |j                  d| j                  j                         d|vr)| j                  d	z   | j                  j                  z   |d<   d
|vr | j                  d   j                         |d
<   | j                  D cg c]  }d|j                  i c}|d<   t        |d      }t        |   }t        j                   | j"                  | j                  j$                  j'                         |j"                  |j                  j$                  j'                         |d|t)               t)               t)               
       |j+                          |S c c}w )a  
        Return a warped GDALRaster with the given input characteristics.

        The input is expected to be a dictionary containing the parameters
        of the target raster. Allowed values are width, height, SRID, origin,
        scale, skew, datatype, driver, and name (filename).

        By default, the warp functions keeps all parameters equal to the values
        of the original source raster. For the name of the target raster, the
        name of the source raster will be used and appended with
        _copy. + source_driver_name.

        In addition, the resampling algorithm can be specified with the "resampling"
        input parameter. The default is NearestNeighbor. For a list of all options
        consult the GDAL_RESAMPLE_ALGORITHMS constant.
        rP   rQ   rR   r$   r%   r&   rM   rO   _copy.rW   r   rY   rV   Tr           )
setdefaultrP   rQ   r   rR   r$   r%   r&   rM   rO   rV   rW   rY   rK   r   ri   reproject_imagerk   r   r{   r	   r   )r.   r   
resampling	max_errorbndtarget	algorithms          r3   warpzGDALRaster.warp  s   $ 	GTZZ0Hdkk2FDHHMM2Hdkk2GTZZ0FDII.Hdkk&6&67!#yy83dkk6F6FFHVX%#'::a=#9#9#;HZ  LP::V:Cnc.>.>?:V HD1 -Z8	 	IIHHLL!KKJJNN!!#JJJ	
 	3 Ws   8H	c                    |r|}n| j                   j                  dk7  r'| j                  dz   | j                   j                  z   }n?t        j                  j	                  t
        t        t        j                                     }t        t        j                  | j                   j                  t        |      | j                  t               t               t!               t!                     | j"                        S )z"Return a clone of this GDALRaster.rN   r   r   )rM   rO   rf   rg   rq   r   ra   rr   rs   rK   ri   copy_dsrk   r   r   r   r	   r^   )r.   rO   
clone_names      r3   clonezGDALRaster.clone  s    J[[&X-0@0@@J&BC

DUVJLL  J'		


 ++
 	
r4   c           	         t         |   }t        |t              r|}n-t        |t        t        f      rt        |      }nt        d      |j                  | j                  k(  r,|r|| j                  j                  k(  r| j                  |      S t        j                  | j                  | j                  j                  j                         |j                  j                         ||t!                     }t#        |      }|j                  |j$                  |j&                  |j(                  j*                  |j(                  j,                  g|j.                  j*                  |j.                  j,                  g|j0                  j*                  |j0                  j,                  gd}	|r||	d<   |r||	d<   | j3                  |	||      S )zk
        Return a copy of this raster reprojected into the given spatial
        reference system.
        zETransform only accepts SpatialReference, string, and integer objects.)rR   rP   rQ   r$   r%   r&   rM   rO   )r   r   )r   r`   r   r   ra   	TypeErrorrR   rM   rO   r   ri   auto_create_warped_vrtrk   r   r   r{   r	   rK   rP   rQ   r$   r0   r1   r%   r&   r   )
r.   r   rM   rO   r   r   r   
target_srsr   rZ   s
             r3   	transformzGDALRaster.transform  se    -Z8	c+,Jc3Z()#.J 
 ??dii'6T[[EUEU;U::d##,,IIHHLL!NN!!#J
 F# OO\\mm}}8llnnfllnn5[[]]FKKMM2
 #DNDL yy*	yJJr4   c                 ^    t        j                  | j                  d      j                         S )z
        Return information about this raster in a string format equivalent
        to the output of the gdalinfo command line utility.
        N)ri   get_ds_infoptrdecoder8   s    r3   infozGDALRaster.info  s$     $/6688r4   )F)NearestNeighbourr   r   )NNr   r   )"rC   rD   rE   __doc__ri   close_ds
destructorr+   r   r   r   r   rF   r   r   r   rO   rM   rP   rQ   r   rG   rR   r(   r$   r%   r&   r   rV   r   r   r   r   rH   rI   s   @r3   rK   rK   F   s    JGRC! 0 0  I I = = ! ! , , , , 
 
 	ZZ    
[[  	 	   . . - - , , & &  ;z
, UX3Kj 9 9r4   rK   )-rc   rf   rn   rr   ctypesr   r   r   r   r   r   r	   r
   pathlibr   django.contrib.gis.gdal.driverr   django.contrib.gis.gdal.errorr   "django.contrib.gis.gdal.prototypesr   ri   #django.contrib.gis.gdal.raster.bandr   #django.contrib.gis.gdal.raster.baser   $django.contrib.gis.gdal.raster.constr   r   r   r   r   django.contrib.gis.gdal.srsr   r   django.contrib.gis.geometryr   django.utils.encodingr   r   django.utils.functionalr   r   r   rK   r7   r4   r3   <module>r      sh     	 
 	 	 	  1 7 = 8 >  G 2 8 3 (T  (FW9 W9r4   