
    Dvgy"                         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dS )    )byrefc_double)GDALBase)EnvelopeOGREnvelope)GDALExceptionSRSException)Feature)OGRFieldTypes)OGRGeometry)OGRGeomType)dsgeom)srs)SpatialReference)force_bytes	force_strc                   L   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dS )Layerza
    A class that wraps an OGR Layer, needs to be instantiated from a DataSource
    object.
    c                     |st          d          || _        || _        t          j        | j                  | _        |                     d          | _        dS )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      Z/var/www/pixelcanvas.ch/venv/lib/python3.11/site-packages/django/contrib/gis/gdal/layer.py__init__zLayer.__init__   sZ      	N LMMM)$)44 00??    c                 @    t          |t                    r*|dk     rt          d                               |          S t          |t                    r;|                     j                  \  }}} fdt          |||          D             S t          d          )z'Get the Feature at the specified index.r   z/Negative indices are not allowed on OGR Layers.c                 :    g | ]}                     |          S  )_make_feature).0fidr    s     r"   
<listcomp>z%Layer.__getitem__.<locals>.<listcomp>6   s'    RRRD&&s++RRRr$   z>Integers and slices may only be used when indexing OGR Layers.)	
isinstanceint
IndexErrorr(   sliceindicesnum_featrange	TypeError)r    indexstartstopstrides   `    r"   __getitem__zLayer.__getitem__*   s    eS!! 	 qyy !RSSS%%e,,,u%% 	"'--">">E4RRRRuUD&7Q7QRRRRP  r$   c              #      K   t          j        | j                   t          | j                  D ]+}t          t          j        | j                  |           V  ,dS )z'Iterate over each Feature in the Layer.N)r   reset_readingr   r2   r1   r
   get_next_feature)r    is     r"   __iter__zLayer.__iter__<   sh       	49%%%t}%% 	B 	BA$/	::DAAAAAA	B 	Br$   c                     | j         S )z%The length is the number of features.)r1   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
    yr$   c                     | j         r9	 t          t          j        | j        |          |           S # t
          $ r Y nw xY w| D ]}|j        |k    r|c S t          d|z            )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   r*   r.   )r    feat_idfeats      r"   r(   zLayer._make_featureK   s      	 t/'BBDIII    
     8w&&KKK ' 2W<===s   '1 
>>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   s5     mm%**a000}}r$   c                 l    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    rB   s     r"   rB   z
Layer.namei   s0     ,,tx0tDDDDr$   rI   c                 6    t          j        | j        |          S )z+Return the number of features in the Layer.)r   get_feature_countr   )r    forces     r"   r1   zLayer.num_feato   s     %dh666r$   c                 4    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     #DK000r$   c                 N    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==>>>r$   c                     	 t          j        | j                  }t          t	          j        |                    S # t          $ r Y dS 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~   sT    	$TX..C#G$5c$:$:;;; 	 	 	44	s   9< 
A
	A
c                 D      fdt           j                  D             S )zt
        Return a list of string names corresponding to each of the Fields
        available in this Layer.
        c           
          g | ]H}t          t          j        t          j        j        |                    j        j        d           IS )TrN   )r   r   get_field_nameget_field_defnr   r   rQ   r)   r<   r    s     r"   r+   z Layer.fields.<locals>.<listcomp>   sb     
 
 
  #D$7Q$G$GHH!!  
 
 
r$   r2   rW   r?   s   `r"   fieldszLayer.fields   s:    
 
 
 
 4?++
 
 
 	
r$   c                 D      fdt           j                  D             S )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.
        c           	      ~    g | ]9}t           t          j        t          j        j        |                             :S r'   )r   r   get_field_typerb   r   rc   s     r"   r+   z%Layer.field_types.<locals>.<listcomp>   sH     
 
 
 $-d.A$+q.Q.QRRS
 
 
r$   rd   r?   s   `r"   field_typeszLayer.field_types   s8    
 
 
 
4?++
 
 
 	
r$   c                 D      fdt           j                  D             S )z;Return a list of the maximum field widths for the features.c                 h    g | ].}t          j        t          j        j        |                    /S r'   )r   get_field_widthrb   r   rc   s     r"   r+   z&Layer.field_widths.<locals>.<listcomp>   sC     
 
 
  !4T[!!D!DEE
 
 
r$   rd   r?   s   `r"   field_widthszLayer.field_widths   8    
 
 
 
4?++
 
 
 	
r$   c                 D      fdt           j                  D             S )z-Return the field precisions for the features.c                 h    g | ].}t          j        t          j        j        |                    /S r'   )r   get_field_precisionrb   r   rc   s     r"   r+   z*Layer.field_precisions.<locals>.<listcomp>   sC     
 
 
 $T%8a%H%HII
 
 
r$   rd   r?   s   `r"   field_precisionszLayer.field_precisions   rn   r$   c                     	 t          t          j        t          j        | j                                      S # t          $ r Y d S w xY w)N)r   geom_api
clone_geomr   get_spatial_filterr   r   r?   s    r"   _get_spatial_filterzLayer._get_spatial_filter   sO    	x243J483T3TUUVVV 	 	 	44	s   7: 
AAc                    t          |t                    r!t          j        | j        |j                   d S t          |t
          t          f          r[t          |          dk    st          d          t          t          |          \  }}}}t          j        | j        ||||           d S |t          j        | j        d            d S 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_rectr3   )r    filterxminyminxmaxymaxs         r"   _set_spatial_filterzLayer._set_spatial_filter   s    fk** 	#DHfj99999.. 	v;;!## !RSSS &)6%:%:"D$d(4tTJJJJJ^#DHd33333  r$   c                 V    | j         vrt          dz            fd| D             S )zg
        Return a list containing the given field name for every Feature
        in the Layer.
        zinvalid field name: %sc                 :    g | ]}|                               S r'   )get)r)   rG   
field_names     r"   r+   z$Layer.get_fields.<locals>.<listcomp>   s%    666$$666r$   )re   r   )r    r   s    `r"   
get_fieldszLayer.get_fields   s@    
 T[(( 8: EFFF66666666r$   Fc                 H    |rddl m fd| D             S d | D             S )zb
        Return a list containing the OGRGeometry for every Feature in
        the Layer.
        r   )GEOSGeometryc                 :    g | ]} |j         j                  S r'   )r   wkb)r)   rG   r   s     r"   r+   z#Layer.get_geoms.<locals>.<listcomp>   s'    AAADLL//AAAr$   c                     g | ]	}|j         
S r'   r   )r)   rG   s     r"   r+   z#Layer.get_geoms.<locals>.<listcomp>   s    ///$DI///r$   )django.contrib.gis.geosr   )r    geosr   s     @r"   	get_geomszLayer.get_geoms   sM    
  	0<<<<<<AAAADAAAA//$////r$   c                 j    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(;z3J3JKKLLLr$   N)rI   )F)__name__
__module____qualname____doc__r#   r8   r=   r@   rC   r(   propertyrL   rB   r1   rW   rZ   r   re   ri   rm   rr   rw   r   spatial_filterr   r   r   r'   r$   r"   r   r      s        
@ @ @  $B B B    > > >.   X E E XE
 7 7 7 X7 1 1 X1 ? ? X?   X 
 
 X
 	
 	
 X	
 
 
 X
 
 
 X
    $ X13FGGN7 7 7
0 
0 
0 
0M M M M 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   rt   r   r]   django.contrib.gis.gdal.srsr   django.utils.encodingr   r   r   r'   r$   r"   <module>r      sO   " " " " " " " " 1 1 1 1 1 1 B B B B B B B B E E E E E E E E 3 3 3 3 3 3 7 7 7 7 7 7 : : : : : : 8 8 8 8 8 8 9 9 9 9 9 9 ? ? ? ? ? ? = = = = = = 8 8 8 8 8 8 8 8 8 8 8 8 8 8UM UM UM UM UMH UM UM UM UM UMr$   