
v^O!                 @   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 m Z m Z d  d
 l m Z d  d l m Z m Z Gd 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               @   sc  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 d) d* d+  Z d, d-   Z d. S)/LayerzSA class that wraps an OGR Layer, needs to be instantiated from a DataSource object.c             C   sO   | s t  d   | |  _ | |  _ t j |  j  |  _ |  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capiZget_layer_defn_ptr_ldefntest_capability_random_read)selfZ	layer_ptrr    r   ?/tmp/pip-build-8lau8j11/django/django/contrib/gis/gdal/layer.py__init__   s    		zLayer.__init__c                s   t  | t  r4 | d k  r' t d     j |  St  | t  r | j   j  \ } } }   f d d   t | | |  D St d   d S)z'Get the Feature at the specified index.r   z/Negative indices are not allowed on OGR Layers.c                s   g  |  ] }   j  |   q Sr   )_make_feature).0fid)r   r   r   
<listcomp>3   s   	 z%Layer.__getitem__.<locals>.<listcomp>z>Integers and slices may only be used when indexing OGR Layers.N)	
isinstanceint
IndexErrorr   sliceindicesnum_featrange	TypeError)r   indexstartstopZstrider   )r   r   __getitem__'   s    #zLayer.__getitem__c             c   sH   t  j |  j  x1 t |  j  D]  } t t  j |  j  |   Vq  Wd S)z'Iterate over each Feature in the Layer.N)r   Zreset_readingr   r)   r(   r	   Zget_next_feature)r   ir   r   r   __iter__7   s    zLayer.__iter__c             C   s   |  j  S)z%The length is the number of features.)r(   )r   r   r   r   __len__>   s    zLayer.__len__c             C   s   |  j  S)zThe string name of the layer.)name)r   r   r   r   __str__B   s    zLayer.__str__c             C   sy   |  j  rA y  t t j |  j |  |   SWqe t k
 r= Yqe Xn$ x! |  D] } | j | k rH | SqH Wt d |   d S)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.N)r   r	   r   Zget_featurer   r   r!   r%   )r   Zfeat_idfeatr   r   r   r   F   s    	 zLayer._make_featurec             C   s/   t    } t j |  j t |  d  t |  S)z.Return the extent (an Envelope) of this layer.   )r   r   Z
get_extentr   r   r   )r   envr   r   r   extent]   s    	zLayer.extentc             C   s+   t  j |  j  } t | |  j j d d S)z1Return the name of this layer in the Data Source.strings_onlyT)r   Zget_fd_namer   r   r   encoding)r   r2   r   r   r   r2   d   s    z
Layer.namer5   c             C   s   t  j |  j |  S)z+Return the number of features in the Layer.)r   Zget_feature_countr   )r   forcer   r   r   r(   j   s    zLayer.num_featc             C   s   t  j |  j  S)z)Return the number of fields in the Layer.)r   Zget_field_countr   )r   r   r   r   
num_fieldso   s    zLayer.num_fieldsc             C   s   t  t j |  j   S)z4Return the geometry type (OGRGeomType) of the Layer.)r   r   Zget_fd_geom_typer   )r   r   r   r   	geom_typet   s    zLayer.geom_typec             C   sF   y) t  j |  j  } t t j |   SWn t k
 rA d SYn Xd S)z0Return the Spatial Reference used in this Layer.N)r   Zget_layer_srsr   r   srs_apiZ	clone_srsr   )r   r   r   r   r   r   y   s
    z	Layer.srsc                s      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                sC   g  |  ]9 } t  t j t j   j |     j j d  d  q S)r8   T)r   r   Zget_field_nameget_field_defnr   r   r9   )r    r/   )r   r   r   r"      s   	z Layer.fields.<locals>.<listcomp>)r)   r;   )r   r   )r   r   fields   s    zLayer.fieldsc                s      f d d   t    j  D S)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                s2   g  |  ]( } t  t j t j   j |    q Sr   )r
   r   Zget_field_typer>   r   )r    r/   )r   r   r   r"      s   	z%Layer.field_types.<locals>.<listcomp>)r)   r;   )r   r   )r   r   field_types   s    zLayer.field_typesc                s      f d d   t    j  D S)z;Return a list of the maximum field widths for the features.c                s.   g  |  ]$ } t  j t  j   j |    q Sr   )r   Zget_field_widthr>   r   )r    r/   )r   r   r   r"      s   	z&Layer.field_widths.<locals>.<listcomp>)r)   r;   )r   r   )r   r   field_widths   s    zLayer.field_widthsc                s      f d d   t    j  D S)z-Return the field precisions for the features.c                s.   g  |  ]$ } t  j t  j   j |    q Sr   )r   Zget_field_precisionr>   r   )r    r/   )r   r   r   r"      s   	z*Layer.field_precisions.<locals>.<listcomp>)r)   r;   )r   r   )r   r   field_precisions   s    zLayer.field_precisionsc             C   s@   y# t  t j t j |  j    SWn t k
 r; d  SYn Xd  S)N)r   geom_apiZ
clone_geomr   Z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 | j  n t  | t t f  r t |  d k s[ t d   t	 t
 |  \ } } } } t j |  j | | | |  n. | d  k r t j |  j d   n t d   d  S)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   Zset_spatial_filterr   tuplelistlen
ValueErrormapr   Zset_spatial_filter_rectr*   )r   filterZxminZyminZxmaxZymaxr   r   r   _set_spatial_filter   s    zLayer._set_spatial_filterc                s6     |  j  k 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  |  ] } | j      q Sr   )get)r    r4   )
field_namer   r   r"      s   	 z$Layer.get_fields.<locals>.<listcomp>)r?   r   )r   rN   r   )rN   r   
get_fields   s    zLayer.get_fieldsFc                sB   | r- d d l  m     f d d   |  D Sd d   |  D Sd S)zb
        Return a list containing the OGRGeometry for every Feature in
        the Layer.
        r   )GEOSGeometryc                s"   g  |  ] }   | j  j   q Sr   )r   Zwkb)r    r4   )rP   r   r   r"      s   	 z#Layer.get_geoms.<locals>.<listcomp>c             S   s   g  |  ] } | j   q Sr   )r   )r    r4   r   r   r   r"      s   	 N)Zdjango.contrib.gis.geosrP   )r   Zgeosr   )rP   r   	get_geoms   s    zLayer.get_geomsc             C   s   t  t j |  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   Z
capabilityr   r   r   r      s    zLayer.test_capabilityN)__name__
__module____qualname____doc__r   r.   r0   r1   r3   r   propertyr7   r2   r(   r;   r<   r   r?   r@   rA   rB   rD   rL   Zspatial_filterrO   rQ   r   r   r   r   r   r      s0   	
	r   N) ctypesr   r   Zdjango.contrib.gis.gdal.baser   Z django.contrib.gis.gdal.enveloper   r   Zdjango.contrib.gis.gdal.errorr   r   Zdjango.contrib.gis.gdal.featurer	   Zdjango.contrib.gis.gdal.fieldr
   Z"django.contrib.gis.gdal.geometriesr   Z django.contrib.gis.gdal.geomtyper   Z"django.contrib.gis.gdal.prototypesr   r   r   rC   r   r=   Zdjango.contrib.gis.gdal.srsr   Zdjango.utils.encodingr   r   r   r   r   r   r   <module>   s   