î
ªÍ X  ã               @   s¼   d  Z  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 d d l m Z d d l m Z d d	 l m Z m Z d d
 l m Z Gd 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
é    )Úbyref)ÚGDALBase)ÚDriver)ÚGDALExceptionÚOGRIndexError)ÚLayer)Úds)Úsix)Úforce_bytesÚ
force_text)Úrangec               @   s‹   e  Z d  Z d Z d d d 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 d S)Ú
DataSourcez Wraps an OGR Data Source object.Fzutf-8c             C   s  | r d |  _  n	 d |  _  | |  _ t j ƒ  t | t j ƒ rœ t j ƒ  } y( t j	 t
 | ƒ |  j  t | ƒ ƒ } Wqß t k
 r˜ t d | ƒ ‚ Yqß XnC t | |  j ƒ rÉ t | t j ƒ rÉ | } n t d t | ƒ ƒ ‚ | r | |  _ t | ƒ |  _ n t d | ƒ ‚ d  S)Né   r   z%Could not open the datasource at "%s"z"Invalid data source input type: %szInvalid data source file "%s")Ú_writeÚencodingr   Zensure_registeredÚ
isinstancer	   Ústring_typesZptr_typeÚcapiZopen_dsr
   r   r   ÚtypeÚptrÚdriver)ÚselfZds_inputZ	ds_driverÚwriter   r   © r   úP/home/ubuntu/projects/ifolica/build/django/django/contrib/gis/gdal/datasource.pyÚ__init__7   s$    		
($		zDataSource.__init__c             C   s3   y t  j |  j ƒ Wn t t f k
 r. Yn Xd S)z#Destroys this DataStructure object.N)r   Z
destroy_dsÚ_ptrÚAttributeErrorÚ	TypeError)r   r   r   r   Ú__del__X   s    zDataSource.__del__c             c   s'   x  t  |  j ƒ D] } |  | Vq Wd S)z6Allows for iteration over the layers in a data source.N)r   Úlayer_count)r   Úir   r   r   Ú__iter___   s    zDataSource.__iter__c             C   s½   t  | t j ƒ rI t j |  j t | ƒ ƒ } | s° t d | ƒ ‚ q° ng t  | t ƒ rš | d k  ss | |  j	 k r‚ t d ƒ ‚ n  t j
 |  j | ƒ } n 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 rangezInvalid index type: %s)r   r	   r   r   Zget_layer_by_namer   r
   r   Úintr    Z	get_layerr   r   r   r   )r   ÚindexÚlr   r   r   Ú__getitem__d   s    zDataSource.__getitem__c             C   s   |  j  S)z4Returns the number of layers within the data source.)r    )r   r   r   r   Ú__len__r   s    zDataSource.__len__c             C   s   d |  j  t |  j ƒ f S)z3Returns OGR GetName and Driver for the Data Source.z%s (%s))ÚnameÚstrr   )r   r   r   r   Ú__str__v   s    zDataSource.__str__c             C   s   t  j |  j ƒ S)z0Returns the number of layers in the data source.)r   Zget_layer_countr   )r   r   r   r   r    z   s    zDataSource.layer_countc             C   s(   t  j |  j ƒ } t | |  j d d ƒS)z$Returns the name of the data source.Zstrings_onlyT)r   Zget_ds_namer   r   r   )r   r(   r   r   r   r(      s    zDataSource.nameN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r"   r&   r'   r*   Úpropertyr    r(   r   r   r   r   r   4   s   !r   N)r.   Úctypesr   Zdjango.contrib.gis.gdal.baser   Zdjango.contrib.gis.gdal.driverr   Zdjango.contrib.gis.gdal.errorr   r   Zdjango.contrib.gis.gdal.layerr   Z"django.contrib.gis.gdal.prototypesr   r   Zdjango.utilsr	   Zdjango.utils.encodingr
   r   Zdjango.utils.six.movesr   r   r   r   r   r   Ú<module>#   s   