ó
®â0_c           @   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 d e f d „  ƒ  YZ d S(   s·  
 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
iÿÿÿÿ(   t   byref(   t   GDALBase(   t   Driver(   t   GDALExceptiont   OGRIndexError(   t   Layer(   t   ds(   t   six(   t   force_bytest
   force_text(   t   ranget
   DataSourcec           B   sk   e  Z d  Z e j Z e e d d „ Z d „  Z d „  Z	 d „  Z
 d „  Z e d „  ƒ Z e d „  ƒ Z RS(	   s    Wraps an OGR Data Source object.s   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 | ƒ ‚ qÞ XnC t | |  j ƒ rÈ t | t j ƒ rÈ | } n t d t | ƒ ƒ ‚ | rÿ | |  _ t | ƒ |  _ n t d | ƒ ‚ d  S(   Ni   i    s%   Could not open the datasource at "%s"s"   Invalid data source input type: %ss   Invalid data source file "%s"(   t   _writet   encodingR   t   ensure_registeredt
   isinstanceR   t   string_typest   ptr_typet   capit   open_dsR   R    R   t   typet   ptrt   driver(   t   selft   ds_inputt	   ds_drivert   writeR   R   (    (    sD   /tmp/pip-unpacked-wheel-BAJOf3/django/contrib/gis/gdal/datasource.pyt   __init__8   s$    		
($		c         c   s'   x  t  |  j ƒ D] } |  | Vq Wd S(   s6   Allows for iteration over the layers in a data source.N(   R
   t   layer_count(   R   t   i(    (    sD   /tmp/pip-unpacked-wheel-BAJOf3/django/contrib/gis/gdal/datasource.pyt   __iter__Y   s    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(   s@   Allows use of the index [] operator to get a layer at the index.s"   invalid OGR Layer name given: "%s"i    s   index out of ranges   Invalid index type: %s(   R   R   R   R   t   get_layer_by_nameR   R   R   t   intR   t	   get_layert   _ptrt	   TypeErrorR   R   (   R   t   indext   layer(    (    sD   /tmp/pip-unpacked-wheel-BAJOf3/django/contrib/gis/gdal/datasource.pyt   __getitem__^   s    c         C   s   |  j  S(   s4   Returns the number of layers within the data source.(   R   (   R   (    (    sD   /tmp/pip-unpacked-wheel-BAJOf3/django/contrib/gis/gdal/datasource.pyt   __len__l   s    c         C   s   d |  j  t |  j ƒ f S(   s3   Returns OGR GetName and Driver for the Data Source.s   %s (%s)(   t   namet   strR   (   R   (    (    sD   /tmp/pip-unpacked-wheel-BAJOf3/django/contrib/gis/gdal/datasource.pyt   __str__p   s    c         C   s   t  j |  j ƒ S(   s0   Returns the number of layers in the data source.(   R   t   get_layer_countR"   (   R   (    (    sD   /tmp/pip-unpacked-wheel-BAJOf3/django/contrib/gis/gdal/datasource.pyR   t   s    c         C   s(   t  j |  j ƒ } t | |  j d t ƒS(   s$   Returns the name of the data source.t   strings_only(   R   t   get_ds_nameR"   R	   R   t   True(   R   R(   (    (    sD   /tmp/pip-unpacked-wheel-BAJOf3/django/contrib/gis/gdal/datasource.pyR(   y   s    (   t   __name__t
   __module__t   __doc__R   t
   destroy_dst
   destructort   FalseR   R   R&   R'   R*   t   propertyR   R(   (    (    (    sD   /tmp/pip-unpacked-wheel-BAJOf3/django/contrib/gis/gdal/datasource.pyR   4   s   	!				N(   R1   t   ctypesR    t   django.contrib.gis.gdal.baseR   t   django.contrib.gis.gdal.driverR   t   django.contrib.gis.gdal.errorR   R   t   django.contrib.gis.gdal.layerR   t"   django.contrib.gis.gdal.prototypesR   R   t   django.utilsR   t   django.utils.encodingR   R	   t   django.utils.six.movesR
   R   (    (    (    sD   /tmp/pip-unpacked-wheel-BAJOf3/django/contrib/gis/gdal/datasource.pyt   <module>#   s   