
    gy"                         d dl mZmZ d dlmZ d dlmZmZ 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 d dlmZmZ  G d de      Zy)    )byrefc_double)GDALBase)EnvelopeOGREnvelope)GDALExceptionSRSException)Feature)OGRFieldTypes)OGRGeometry)OGRGeomType)ds)geom)srs)SpatialReference)force_bytes	force_strc                      e Zd ZdZd Zd Zd Zd Zd Zd Z	e
d        Ze
d	        Ze
dd
       Ze
d        Ze
d        Ze
d        Ze
d        Ze
d        Ze
d        Ze
d        Zd Zd Z e
ee      Zd ZddZd Zy)Layerza
    A class that wraps an OGR Layer, needs to be instantiated from a DataSource
    object.
    c                     |st        d      || _        || _        t        j                  | j
                        | _        | j                  d      | _        y)a7  
        Initialize on an OGR C pointer to the Layer and the `DataSource` object
        that owns this layer.  The `DataSource` object is required so that a
        reference to it is kept with this Layer.  This prevents garbage
        collection of the `DataSource` while this Layer is still active.
        z*Cannot create Layer, invalid pointer givens
   RandomReadN)	r   ptr_dscapiget_layer_defn_ptr_ldefntest_capability_random_read)self	layer_ptrr   s      u/var/www/python.vincentserveurtest.ovh/public_html/venv/lib/python3.12/site-packages/django/contrib/gis/gdal/layer.py__init__zLayer.__init__   sJ      LMM))$))4 00?    c                 6   t        |t              r!|dk  rt        d      | j                  |      S t        |t              rI|j                  | j                        \  }}}t        |||      D cg c]  }| j                  |       c}S t        d      c c}w )z'Get the Feature at the specified index.r   z/Negative indices are not allowed on OGR Layers.z>Integers and slices may only be used when indexing OGR Layers.)	
isinstanceint
IndexError_make_featuresliceindicesnum_featrange	TypeError)r   indexstartstopstridefids         r!   __getitem__zLayer.__getitem__*   s    eS! qy !RSS%%e,,u%"'--">E47<UD&7QR7QD&&s+7QRRP  Ss   0Bc              #      K   t        j                  | j                         t        | j                        D ]-  }t        t        j                  | j                        |        / yw)z'Iterate over each Feature in the Layer.N)r   reset_readingr   r,   r+   r
   get_next_featurer   is     r!   __iter__zLayer.__iter__<   sI      	499%t}}%A$//		:DAA &s   A&A(c                     | j                   S )z%The length is the number of features.)r+   r   s    r!   __len__zLayer.__len__C   s    }}r#   c                     | j                   S )zThe string name of the layer.)namer;   s    r!   __str__zLayer.__str__G   s    yyr#   c                     | j                   r+	 t        t        j                  | j                  |      |       S | D ]  }|j                  |k(  s|c S  t        d|z        # t
        $ r Y w xY w)a.  
        Helper routine for __getitem__ that constructs a Feature from the given
        Feature ID.  If the OGR Layer does not support random-access reading,
        then each feature of the layer will be incremented through until the
        a Feature is found matching the given feature ID.
        zInvalid feature id: %s.)r   r
   r   get_featurer   r   r2   r'   )r   feat_idfeats      r!   r(   zLayer._make_featureK   st     t//'BDII 88w&K  2W<== ! s   )A   	A,+A,c                     t               }t        j                  | j                  t	        |      d       t        |      S )z.Return the extent (an Envelope) of this layer.   )r   r   
get_extentr   r   r   )r   envs     r!   extentzLayer.extentb   s-     m%*a0}r#   c                     t        j                  | j                        }t        || j                  j
                  d      S )z1Return the name of this layer in the Data Source.Tstrings_only)r   get_fd_namer   r   r   encoding)r   r>   s     r!   r>   z
Layer.namei   s1     ,txx00tDDr#   c                 B    t        j                  | j                  |      S )z+Return the number of features in the Layer.)r   get_feature_countr   )r   forces     r!   r+   zLayer.num_feato   s     %%dhh66r#   c                 @    t        j                  | j                        S )z)Return the number of fields in the Layer.)r   get_field_countr   r;   s    r!   
num_fieldszLayer.num_fieldst   s     ##DKK00r#   c                 R    t        t        j                  | j                              S )z4Return the geometry type (OGRGeomType) of the Layer.)r   r   get_fd_geom_typer   r;   s    r!   	geom_typezLayer.geom_typey   s     400=>>r#   c                     	 t        j                  | j                        }t        t	        j
                  |            S # t        $ r Y yw xY w)z0Return the Spatial Reference used in this Layer.N)r   get_layer_srsr   r   srs_api	clone_srsr	   )r   r   s     r!   r   z	Layer.srs~   sC    	$$TXX.C#G$5$5c$:;; 		s   <? 	A
Ac                     t        | j                        D cg c]U  }t        t        j                  t        j
                  | j                  |            | j                  j                  d      W c}S c c}w )zt
        Return a list of string names corresponding to each of the Fields
        available in this Layer.
        TrJ   )	r,   rS   r   r   get_field_nameget_field_defnr   r   rM   r7   s     r!   fieldszLayer.fields   sj     4??+
 , ##D$7$7Q$GH!!!
 ,
 	
 
s   AA5c           
          t        | j                        D cg c]<  }t        t        j                  t        j
                  | j                  |               > c}S c c}w )z
        Return a list of the types of fields in this Layer.  For example,
        return the list [OFTInteger, OFTReal, OFTString] for an OGR layer that
        has an integer, a floating-point, and string fields.
        )r,   rS   r   r   get_field_typer]   r   r7   s     r!   field_typeszLayer.field_types   sS     4??+
+ $--d.A.A$++q.QRS+
 	
 
s   AAc           	          t        | j                        D cg c]5  }t        j                  t        j                  | j
                  |            7 c}S c c}w )z;Return a list of the maximum field widths for the features.)r,   rS   r   get_field_widthr]   r   r7   s     r!   field_widthszLayer.field_widths   sN    
 4??+
+   !4!4T[[!!DE+
 	
 
   :Ac           	          t        | j                        D cg c]5  }t        j                  t        j                  | j
                  |            7 c}S c c}w )z-Return the field precisions for the features.)r,   rS   r   get_field_precisionr]   r   r7   s     r!   field_precisionszLayer.field_precisions   sN    
 4??+
+ $$T%8%8a%HI+
 	
 
re   c                     	 t        t        j                  t        j                  | j
                                    S # t        $ r Y y w xY w)N)r   geom_api
clone_geomr   get_spatial_filterr   r   r;   s    r!   _get_spatial_filterzLayer._get_spatial_filter   s>    	x2243J3J4883TUVV 		s   := 	A	A	c                    t        |t              r+t        j                  | j                  |j                         y t        |t
        t        f      rRt        |      dk(  st        d      t        t        |      \  }}}}t        j                  | j                  ||||       y |!t        j                  | j                  d        y t        d      )N   z/Spatial filter list/tuple must have 4 elements.zJSpatial filter must be either an OGRGeometry instance, a 4-tuple, or None.)r%   r   r   set_spatial_filterr   tuplelistlen
ValueErrormapr   set_spatial_filter_rectr-   )r   filterxminyminxmaxymaxs         r!   _set_spatial_filterzLayer._set_spatial_filter   s    fk*##DHHfjj9.v;!# !RSS &)6%:"D$d((4tTJ^##DHHd3 r#   c                     || j                   vrt        d|z        | D cg c]  }|j                  |       c}S c c}w )zg
        Return a list containing the given field name for every Feature
        in the Layer.
        zinvalid field name: %s)r^   r   get)r   
field_namerC   s      r!   
get_fieldszLayer.get_fields   sB    
 T[[( 8: EFF156$666s   <c                     |r0ddl m} | D cg c]  } ||j                  j                          c}S | D cg c]  }|j                   c}S c c}w c c}w )zb
        Return a list containing the OGRGeometry for every Feature in
        the Layer.
        r   )GEOSGeometry)django.contrib.gis.geosr   r   wkb)r   geosr   rC   s       r!   	get_geomszLayer.get_geoms   sN    
 <<@ADDL/DAA*./$$DII$// B/s
   #AAc                 f    t        t        j                  | j                  t	        |                  S )ah  
        Return a bool indicating whether the this Layer supports the given
        capability (a string).  Valid capability strings include:
          'RandomRead', 'SequentialWrite', 'RandomWrite', 'FastSpatialFilter',
          'FastFeatureCount', 'FastGetExtent', 'CreateField', 'Transactions',
          'DeleteFeature', and 'FastSetNextByIndex'.
        )boolr   r   r   r   )r   
capabilitys     r!   r   zLayer.test_capability   s%     D((;z3JKLLr#   N)rE   )F)__name__
__module____qualname____doc__r"   r3   r9   r<   r?   r(   propertyrH   r>   r+   rS   rV   r   r^   ra   rd   rh   rm   r|   spatial_filterr   r   r    r#   r!   r   r      s   
@$B>.   E E
 7 7 1 1 ? ?   
 
 	
 	
 
 
 
 
$ 13FGN7
0Mr#   r   N) ctypesr   r   django.contrib.gis.gdal.baser    django.contrib.gis.gdal.enveloper   r   django.contrib.gis.gdal.errorr   r	   django.contrib.gis.gdal.featurer
   django.contrib.gis.gdal.fieldr   "django.contrib.gis.gdal.geometriesr    django.contrib.gis.gdal.geomtyper   "django.contrib.gis.gdal.prototypesr   r   r   rj   r   rY   django.contrib.gis.gdal.srsr   django.utils.encodingr   r   r   r   r#   r!   <module>r      s<    " 1 B E 3 7 : 8 9 ? = 8 8UMH UMr#   