o
    þÞhy"  ã                   @   sÀ   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„ 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                   @   sî   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	e
dd„ ƒZe
dd„ ƒZe
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
dd „ ƒZe
d!d"„ ƒZd#d$„ Zd%d&„ Ze
eeƒZd'd(„ Zd0d*d+„Zd,d-„ Zd.S )1ÚLayerza
    A class that wraps an OGR Layer, needs to be instantiated from a DataSource
    object.
    c                 C   s6   |st dƒ‚|| _|| _t | 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   © r   ú]/var/www/html/premium_crap/venv/lib/python3.10/site-packages/django/contrib/gis/gdal/layer.pyÚ__init__   s   zLayer.__init__c                    sb   t |tƒr|dk rtdƒ‚ˆ  |¡S t |tƒr-| ˆ j¡\}}}‡ fd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                    s   g | ]}ˆ   |¡‘qS r   )Ú_make_feature)Ú.0Úfid©r   r   r    Ú
<listcomp>6   ó    z%Layer.__getitem__.<locals>.<listcomp>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Ústrider   r%   r    Ú__getitem__*   s   


ÿzLayer.__getitem__c                 c   s6   � t  | j¡ t| jƒD ]}tt  | j¡| ƒV  qdS )z'Iterate over each Feature in the Layer.N)r   Úreset_readingr   r.   r-   r	   Úget_next_feature)r   Úir   r   r    Ú__iter__<   s
   €ÿzLayer.__iter__c                 C   ó   | j S )z%The length is the number of features.)r-   r%   r   r   r    Ú__len__C   ó   zLayer.__len__c                 C   r9   )zThe string name of the layer.)Únamer%   r   r   r    Ú__str__G   r;   zLayer.__str__c                 C   sX   | j rztt | j|¡| ƒW S  ty   Y nw | D ]}|j|kr%|  S qtd| ƒ‚)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Úfeatr   r   r    r"   K   s   ÿ
ÿzLayer._make_featurec                 C   s"   t ƒ }t | jt|ƒd¡ t|ƒS )z.Return the extent (an Envelope) of this layer.é   )r   r   Ú
get_extentr   r   r   )r   Úenvr   r   r    Úextentb   s   zLayer.extentc                 C   s   t  | j¡}t|| jjdd�S )z1Return the name of this layer in the Data Source.T©Ústrings_only)r   Úget_fd_namer   r   r   Úencoding)r   r<   r   r   r    r<   i   s   z
Layer.namerA   c                 C   s   t  | j|¡S )z+Return the number of features in the Layer.)r   Úget_feature_countr   )r   Úforcer   r   r    r-   o   s   zLayer.num_featc                 C   s   t  | j¡S )z)Return the number of fields in the Layer.)r   Úget_field_countr   r%   r   r   r    Ú
num_fieldst   s   zLayer.num_fieldsc                 C   s   t t | j¡ƒS )z4Return the geometry type (OGRGeomType) of the Layer.)r   r   Úget_fd_geom_typer   r%   r   r   r    Ú	geom_typey   s   zLayer.geom_typec                 C   s2   zt  | j¡}tt |¡ƒW S  ty   Y dS w )z0Return the Spatial Reference used in this Layer.N)r   Úget_layer_srsr   r   Úsrs_apiÚ	clone_srsr   )r   r   r   r   r    r   ~   s   ÿz	Layer.srsc                    ó   ‡ fdd„t ˆ jƒD ƒS )zt
        Return a list of string names corresponding to each of the Fields
        available in this Layer.
        c              	      s.   g | ]}t t t ˆ j|¡¡ˆ jjd d�‘qS )TrE   )r   r   Úget_field_nameÚget_field_defnr   r   rH   ©r#   r7   r%   r   r    r&   �   s    ûýÿz Layer.fields.<locals>.<listcomp>©r.   rL   r%   r   r%   r    Úfields‡   s   
úzLayer.fieldsc                    rR   )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              	      s$   g | ]}t t t ˆ j|¡¡ ‘qS r   )r
   r   Úget_field_typerT   r   rU   r%   r   r    r&   �   s    ÿÿz%Layer.field_types.<locals>.<listcomp>rV   r%   r   r%   r    Úfield_types–   s   
þzLayer.field_typesc                    rR   )z;Return a list of the maximum field widths for the features.c                    ó    g | ]}t  t  ˆ j|¡¡‘qS r   )r   Úget_field_widthrT   r   rU   r%   r   r    r&   ¥   ó    ÿÿz&Layer.field_widths.<locals>.<listcomp>rV   r%   r   r%   r    Úfield_widths¢   ó   
þzLayer.field_widthsc                    rR   )z-Return the field precisions for the features.c                    rZ   r   )r   Úget_field_precisionrT   r   rU   r%   r   r    r&   ­   r\   z*Layer.field_precisions.<locals>.<listcomp>rV   r%   r   r%   r    Úfield_precisionsª   r^   zLayer.field_precisionsc                 C   s.   zt t t | j¡¡ƒW S  ty   Y d S w )N)r   Úgeom_apiÚ
clone_geomr   Úget_spatial_filterr   r   r%   r   r   r    Ú_get_spatial_filter²   s
   ÿzLayer._get_spatial_filterc                 C   sŒ   t |tƒrt | j|j¡ d S t |ttfƒr5t|ƒdks tdƒ‚t	t
|ƒ\}}}}t | j||||¡ d S |d u rBt | 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_rectr/   )r   ÚfilterÚxminÚyminÚxmaxÚymaxr   r   r    Ú_set_spatial_filter¸   s   
ÿzLayer._set_spatial_filterc                    s(   ˆ | j vrtdˆ  ƒ‚‡ fdd„| D ƒS )zg
        Return a list containing the given field name for every Feature
        in the Layer.
        zinvalid field name: %sc                    s   g | ]}|  ˆ ¡‘qS r   )Úget©r#   r@   ©Ú
field_namer   r    r&   Ô   r'   z$Layer.get_fields.<locals>.<listcomp>)rW   r   )r   rv   r   ru   r    Ú
get_fieldsÍ   s   
zLayer.get_fieldsFc                    s0   |rddl m‰  ‡ fdd„| D ƒS dd„ | D ƒS )zb
        Return a list containing the OGRGeometry for every Feature in
        the Layer.
        r   ©ÚGEOSGeometryc                    s   g | ]}ˆ |j jƒ‘qS r   )r   Úwkbrt   rx   r   r    r&   Þ   s    z#Layer.get_geoms.<locals>.<listcomp>c                 S   s   g | ]}|j ‘qS r   r   rt   r   r   r    r&   à   s    )Údjango.contrib.gis.geosry   )r   Úgeosr   rx   r    Ú	get_geomsÖ   s   zLayer.get_geomsc                 C   s   t t | 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   Ú
capabilityr   r   r    r   â   s   zLayer.test_capabilityN)rA   )F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r!   r4   r8   r:   r=   r"   ÚpropertyrD   r<   r-   rL   rN   r   rW   rY   r]   r`   rd   rr   Úspatial_filterrw   r}   r   r   r   r   r    r      sD    










	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   ra   r   rP   Údjango.contrib.gis.gdal.srsr   Údjango.utils.encodingr   r   r   r   r   r   r    Ú<module>   s    