U
    gW                     @   sh   d dl mZ d dl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mZ G dd deZd	S )
    )GDALBase)GDALException)Field)OGRGeometryOGRGeomType)dsgeom)force_bytes	force_strc                   @   s   e Zd ZdZej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 Zedd Zedd Zedd Zedd Zdd Zdd ZdS )Featureza
    This class that wraps an OGR Feature, needs to be instantiated
    from a Layer object.
    c                 C   s   |st d|| _|| _dS )zI
        Initialize Feature from a pointer and its Layer object.
        z1Cannot create OGR Feature, invalid pointer given.N)r   ptr_layer)selfZfeatZlayer r   C/tmp/pip-unpacked-wheel-ye1bytdm/django/contrib/gis/gdal/feature.py__init__   s    zFeature.__init__c                 C   sL   t |tr| |}n,d|  kr,| jk r6n n|}ntd| t| |S )a.  
        Get the Field object at the specified index, which may be either
        an integer or the Field's string label.  Note that the Field object
        is not the field's _value_ -- use the `get` method instead to
        retrieve the value (e.g. an integer) instead of a Field instance.
        r   z9Index out of range when accessing field in a feature: %s.)
isinstancestrindex
num_fields
IndexErrorr   )r   r   ir   r   r   __getitem__   s    
zFeature.__getitem__c                 C   s   | j S )z+Return the count of fields in this feature.)r   r   r   r   r   __len__,   s    zFeature.__len__c                 C   s   d| j | jf S )zThe string name of the feature.zFeature FID %d in Layer<%s>)fid
layer_namer   r   r   r   __str__0   s    zFeature.__str__c                 C   s   t t| j|jS )z'Do equivalence testing on the features.)boolcapiZfeature_equalr   Z_ptr)r   otherr   r   r   __eq__4   s    zFeature.__eq__c                 C   s
   | j jjS )N)r   Z_dsencodingr   r   r   r   r"   9   s    zFeature.encodingc                 C   s   t | jS )zReturn the feature identifier.)r   Zget_fidr   r   r   r   r   r   =   s    zFeature.fidc                 C   s   t | jj}t|| jddS )z-Return the name of the layer for the feature.TZstrings_only)r   Zget_feat_namer   _ldefnr
   r"   )r   namer   r   r   r   B   s    zFeature.layer_namec                 C   s   t | jS )z+Return the number of fields in the Feature.)r   Zget_feat_field_countr   r   r   r   r   r   H   s    zFeature.num_fieldsc                    s    fddt  jD S )z'Return a list of fields in the Feature.c              	      s.   g | ]&}t tt jj| jd dqS )Tr#   )r
   r   Zget_field_nameZget_field_defnr   r$   r"   ).0r   r   r   r   
<listcomp>P   s   z"Feature.fields.<locals>.<listcomp>)ranger   r   r   r   r   fieldsM   s    
zFeature.fieldsc                 C   s   t | j}tt|S )z)Return the OGR Geometry for this Feature.)r   Zget_feat_geom_refr   r   geom_apiZ
clone_geom)r   Zgeom_ptrr   r   r   r   X   s    zFeature.geomc                 C   s   t t| jjS )z.Return the OGR Geometry Type for this Feature.)r   r   Zget_fd_geom_typer   r$   r   r   r   r   	geom_type_   s    zFeature.geom_typec                 C   s   t |d|}| | jS )z
        Return the value of the field, instead of an instance of the Field
        object.  May take a string of the field name or a Field object as
        parameters.
        r%   )getattrvalue)r   field
field_namer   r   r   gete   s    zFeature.getc                 C   s*   t | jt|}|dk r&td| |S )z)Return the index of the given field name.r   z!Invalid OFT field name given: %s.)r   Zget_field_indexr   r	   r   )r   r/   r   r   r   r   r   n   s    zFeature.indexN)__name__
__module____qualname____doc__r   Zdestroy_featureZ
destructorr   r   r   r   r!   propertyr"   r   r   r   r)   r   r+   r0   r   r   r   r   r   r      s.   	







	r   N)Zdjango.contrib.gis.gdal.baser   Zdjango.contrib.gis.gdal.errorr   Zdjango.contrib.gis.gdal.fieldr   Z"django.contrib.gis.gdal.geometriesr   r   Z"django.contrib.gis.gdal.prototypesr   r   r   r*   Zdjango.utils.encodingr	   r
   r   r   r   r   r   <module>   s   