o
    þÞh$  ã                   @   sp   d 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 )a�  
DataSource is a wrapper for the OGR Data Source object, which provides
an interface for reading vector geometry data from many different file
formats (including ESRI shapefiles).

When instantiating a DataSource object, use the filename of a
GDAL-supported data source.  For example, a SHP file or a
TIGER/Line file from the government.

The ds_driver keyword is used internally when a ctypes pointer
is passed in directly.

Example:
 ds = DataSource('/home/foo/bar.shp')
 for layer in ds:
     for feature in layer:
         # Getting the geometry for the feature.
         g = feature.geom

         # Getting the 'description' field for the feature.
         desc = feature['description']

         # We can also increment through all of the fields
         #  attached to this feature.
         for field in feature:
             # Get the name of the field (e.g. 'description')
             nm = field.name

             # Get the type (integer) of the field, e.g. 0 => OFTInteger
             t = field.type

             # Returns the value the field; OFTIntegers return ints,
             #  OFTReal returns floats, all else returns string.
             val = field.value
é    )ÚPath)ÚGDALBase)ÚDriver)ÚGDALException)ÚLayer)Úds)Úforce_bytesÚ	force_strc                   @   sP   e Zd ZdZejZddd„Zdd„ Zdd	„ Z	d
d„ Z
edd„ ƒZedd„ ƒZdS )Ú
DataSourcez Wraps an OGR Data Source object.Fúutf-8c                 C   sÈ   |rt jnt j| _|| _t ¡  t|tt	fƒr6zt  
t|ƒ| jt jB d d d ¡}W n$ ty5   td| ƒ‚w t|| jƒrEt|tjƒrE|}ntdt|ƒ ƒ‚|r^|| _t  |¡}t|ƒ| _d S td| ƒ‚)Nz%Could not open the datasource at "%s"z"Invalid data source input type: %szInvalid data source file "%s")ÚcapiÚGDAL_OF_UPDATEÚGDAL_OF_READONLYÚ_writeÚencodingr   Úensure_registeredÚ
isinstanceÚstrr   Úopen_dsr   ÚGDAL_OF_VECTORr   Úptr_typeÚtypeÚptrÚget_dataset_driverÚdriver)ÚselfÚds_inputÚ	ds_driverÚwriter   r   r   © r   úb/var/www/html/premium_crap/venv/lib/python3.10/site-packages/django/contrib/gis/gdal/datasource.pyÚ__init__8   s2   
ûýÿ
zDataSource.__init__c                 C   s–   t |tƒrzt | jt|ƒ¡}W n5 ty   td| ƒ‚w t |tƒr>d|  kr.| j	k r8n nt 
| j|¡}ntd| ƒ‚tdt|ƒ ƒ‚t|| ƒS )z@Allows use of the index [] operator to get a layer at the index.z!Invalid OGR layer name given: %s.r   z=Index out of range when accessing layers in a datasource: %s.zInvalid index type: %s)r   r   r   Úget_layer_by_namer   r   r   Ú
IndexErrorÚintÚlayer_countÚ	get_layerÚ_ptrÚ	TypeErrorr   r   )r   ÚindexÚlayerr   r   r    Ú__getitem__]   s    
ÿ
ÿÿ
zDataSource.__getitem__c                 C   s   | j S )z3Return the number of layers within the data source.)r%   ©r   r   r   r    Ú__len__p   s   zDataSource.__len__c                 C   s   d| j | jf S )z2Return OGR GetName and Driver for the Data Source.z%s (%s))Únamer   r,   r   r   r    Ú__str__t   s   zDataSource.__str__c                 C   s   t  | j¡S )z/Return the number of layers in the data source.)r   Úget_layer_countr'   r,   r   r   r    r%   x   s   zDataSource.layer_countc                 C   s   t  | j¡}t|| jdd�S )z#Return the name of the data source.T)Ústrings_only)r   Úget_ds_namer'   r	   r   )r   r.   r   r   r    r.   }   s   zDataSource.nameN)FFr   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ú
destroy_dsÚ
destructorr!   r+   r-   r/   Úpropertyr%   r.   r   r   r   r    r
   3   s    
%
r
   N)r6   Úpathlibr   Údjango.contrib.gis.gdal.baser   Údjango.contrib.gis.gdal.driverr   Údjango.contrib.gis.gdal.errorr   Údjango.contrib.gis.gdal.layerr   Ú"django.contrib.gis.gdal.prototypesr   r   Údjango.utils.encodingr   r	   r
   r   r   r   r    Ú<module>   s    $