î
ªÍ Xñ!  ã               @   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
 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 d  d l m  Z  m! Z! d  d l" m# Z# Gd d „  d e ƒ Z$ d S)é    )ÚbyrefÚc_double)ÚGDALBase)ÚEnvelopeÚOGREnvelope)ÚGDALExceptionÚOGRIndexErrorÚSRSException)ÚFeature)ÚOGRFieldTypes)ÚOGRGeometry)ÚOGRGeomType)ÚdsÚgeomÚsrs)ÚSpatialReference)Úsix)Úforce_bytesÚ
force_text)Úrangec               @   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   sR   | s t  d ƒ ‚ n  | |  _ | |  _ t j |  j ƒ |  _ |  j d ƒ |  _ d S)a8  
        Initializes 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   úK/home/ubuntu/projects/ifolica/build/django/django/contrib/gis/gdal/layer.pyÚ__init__   s    		zLayer.__init__c                s—   t  | t j ƒ r: | d k  r- t d ƒ ‚ n  ˆ  j | ƒ St  | t ƒ r‡ | j ˆ  j ƒ \ } } } ‡  f d d †  t | | | ƒ Dƒ St	 d ƒ ‚ d S)z(Gets 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>7   s   	 z%Layer.__getitem__.<locals>.<listcomp>z>Integers and slices may only be used when indexing OGR Layers.N)
Ú
isinstancer   Úinteger_typesr   r"   ÚsliceÚindicesÚnum_featr   Ú	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(Iterates over each Feature in the Layer.N)r   Zreset_readingr   r   r*   r
   Zget_next_feature)r   Úir   r   r    Ú__iter__;   s    zLayer.__iter__c             C   s   |  j  S)z%The length is the number of features.)r*   )r   r   r   r    Ú__len__B   s    zLayer.__len__c             C   s   |  j  S)zThe string name of the layer.)Úname)r   r   r   r    Ú__str__F   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"   J   s    	 zLayer._make_featurec             C   s/   t  ƒ  } t j |  j t | ƒ d ƒ t | ƒ S)z/Returns the extent (an Envelope) of this layer.é   )r   r   Z
get_extentr   r   r   )r   Úenvr   r   r    Úextenta   s    	zLayer.extentc             C   s+   t  j |  j ƒ } t | |  j j d d ƒS)z2Returns the name of this layer in the Data Source.Ústrings_onlyT)r   Zget_fd_namer   r   r   Úencoding)r   r3   r   r   r    r3   h   s    z
Layer.namer6   c             C   s   t  j |  j | ƒ S)z,Returns the number of features in the Layer.)r   Zget_feature_countr   )r   Úforcer   r   r    r*   n   s    zLayer.num_featc             C   s   t  j |  j ƒ S)z*Returns the number of fields in the Layer.)r   Zget_field_countr   )r   r   r   r    Ú
num_fieldss   s    zLayer.num_fieldsc             C   s   t  t j |  j ƒ ƒ S)z5Returns the geometry type (OGRGeomType) of the Layer.)r   r   Zget_fd_geom_typer   )r   r   r   r    Ú	geom_typex   s    zLayer.geom_typec             C   sF   y) t  j |  j ƒ } t t j | ƒ ƒ SWn t k
 rA d SYn Xd S)z1Returns the Spatial Reference used in this Layer.N)r   Zget_layer_srsr   r   Úsrs_apiZ	clone_srsr	   )r   r   r   r   r    r   }   s
    z	Layer.srsc                s    ‡  f d d †  t  ˆ  j ƒ Dƒ S)zu
        Returns 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)r9   T)r   r   Zget_field_nameÚget_field_defnr   r   r:   )r#   r0   )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ó
        Returns a list of the types of fields in this Layer.  For example,
        the list [OFTInteger, OFTReal, OFTString] would be returned for
        an OGR layer that had 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#   r0   )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<Returns 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#   r0   )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.Returns the field precisions for the features.c                s.   g  |  ]$ } t  j t  j ˆ  j | ƒ ƒ ‘ q Sr   )r   Zget_field_precisionr?   r   )r#   r0   )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 ƒ ‚ n  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                s9   ˆ  |  j  k r" t d ˆ  ƒ ‚ n  ‡  f d d †  |  Dƒ S)zh
        Returns 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#   r5   )Ú
field_namer   r    r%   Æ   s   	 z$Layer.get_fields.<locals>.<listcomp>)r@   r   )r   rO   r   )rO   r    Ú
get_fields¿   s    zLayer.get_fieldsFc                sB   | r- d d l  m ‰  ‡  f d d †  |  Dƒ Sd d „  |  Dƒ Sd S)zc
        Returns a list containing the OGRGeometry for every Feature in
        the Layer.
        r   )ÚGEOSGeometryc                s"   g  |  ] } ˆ  | j  j ƒ ‘ q Sr   )r   Zwkb)r#   r5   )rQ   r   r    r%   Ï   s   	 z#Layer.get_geoms.<locals>.<listcomp>c             S   s   g  |  ] } | j  ‘ q Sr   )r   )r#   r5   r   r   r    r%   Ñ   s   	 N)Zdjango.contrib.gis.geosrQ   )r   Zgeosr   )rQ   r    Ú	get_geomsÈ   s    zLayer.get_geomsc             C   s   t  t j |  j t | ƒ ƒ ƒ S)ai  
        Returns 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/   r1   r2   r4   r"   Úpropertyr8   r3   r*   r<   r=   r   r@   rA   rB   rC   rE   rM   Zspatial_filterrP   rR   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   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   rD   r   r>   Zdjango.contrib.gis.gdal.srsr   Zdjango.utilsr   Zdjango.utils.encodingr   r   Zdjango.utils.six.movesr   r   r   r   r   r    Ú<module>   s   