ó
i4Vdc           @   st  d  Z  d d l Z d d l m Z m Z d d l m Z d d l m	 Z	 m
 Z
 m Z m Z m Z m Z d d l m Z m Z m Z m Z m Z m Z d d l m Z m Z d d l m Z m Z m Z m Z d d	 l m Z d d
 l  m! Z! d e" f d „  ƒ  YZ# d e# f d „  ƒ  YZ$ d e# f d „  ƒ  YZ% d e# f d „  ƒ  YZ& d e# f d „  ƒ  YZ' d e( f d „  ƒ  YZ) d S(   s  
 The LayerMapping class provides a way to map the contents of OGR
 vector files (e.g. SHP files) to Geographic-enabled Django models.

 For more information, please consult the GeoDjango documentation:
   https://docs.djangoproject.com/en/dev/ref/contrib/gis/layermapping/
iÿÿÿÿN(   t   Decimalt   InvalidOperation(   t   GeometryField(   t   CoordTransformt
   DataSourcet   GDALExceptiont   OGRGeometryt   OGRGeomTypet   SpatialReference(   t   OFTDatet   OFTDateTimet
   OFTIntegert   OFTRealt	   OFTStringt   OFTTime(   t   FieldDoesNotExistt   ObjectDoesNotExist(   t   connectionst   modelst   routert   transaction(   t   six(   t
   force_textt   LayerMapErrorc           B   s   e  Z RS(    (   t   __name__t
   __module__(    (    (    ss   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/contrib/gis/utils/layermapping.pyR      s   t   InvalidStringc           B   s   e  Z RS(    (   R   R   (    (    (    ss   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/contrib/gis/utils/layermapping.pyR      s   t   InvalidDecimalc           B   s   e  Z RS(    (   R   R   (    (    (    ss   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/contrib/gis/utils/layermapping.pyR   #   s   t   InvalidIntegerc           B   s   e  Z RS(    (   R   R   (    (    (    ss   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/contrib/gis/utils/layermapping.pyR   '   s   t   MissingForeignKeyc           B   s   e  Z RS(    (   R   R   (    (    (    ss   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/contrib/gis/utils/layermapping.pyR   +   s   t   LayerMappingc           B   sô  e  Z d  Z i e d ƒ d 6e d ƒ d 6e d ƒ d 6e d ƒ e d ƒ j 6e d	 ƒ e d
 ƒ j 6e d ƒ e d ƒ j 6Z i e e j 6e e	 e
 f e j 6e e	 f e j 6e e j 6e e j 6e
 e j 6e e j 6e e	 f e j 6e
 e j 6e
 e j 6e
 e j 6e
 e j 6e e	 e
 f e j 6e e	 e
 f e j 6e e	 e
 f e j 6Z d d d d e d d d „ Z d „  Z  d „  Z! d „  Z" d „  Z# d „  Z$ d „  Z% d „  Z& d „  Z' d „  Z( d „  Z) d „  Z* d „  Z+ e, e, e, e, e, e- j. e, d „ Z/ RS(   s1   A class that maps OGR Layers to GeoDjango Models.t
   MultiPointi   t   MultiLineStringi   t   MultiPolygoni   t   MultiPoint25Dt   Point25Dt   MultiLineString25Dt   LineString25Dt   MultiPolygon25Dt
   Polygon25Di    s   utf-8t   commit_on_successc         C   s§  t  | t j ƒ r* t | d | ƒ|  _ n	 | |  _ |  j | |  _ |
 d k	 rU |
 n t j | ƒ |  _	 t
 |  j	 j |  _ | |  _ | |  _ |  j ƒ  t
 |  j	 j j r» |  j ƒ  |  _ n t } | rë |  j | ƒ |  _ |  j ƒ  |  _ n	 | |  _ | r d d l m } | | ƒ | |  _ n	 d |  _ |	 rN|  j |	 ƒ d } |	 |  _ n	 d |  _ | |  _ | d k rxd |  _ n+ | d k r“t  j! |  _ n t" d | ƒ ‚ d S(   s  
        A LayerMapping object is initialized using the given Model (not an instance),
        a DataSource (or string path to an OGR-supported data file), and a mapping
        dictionary.  See the module level docstring for more details and keyword
        argument usage.
        t   encodingiÿÿÿÿ(   t   lookupt
   autocommitR(   s!   Unrecognized transaction mode: %sN(#   t
   isinstanceR   t   string_typesR   t   dst   layert   NoneR   t   db_for_writet   usingR   t   opst   spatial_backendt   mappingt   modelt   check_layert   featurest   supports_transformt   geometry_fieldt	   geo_fieldt   Falset	   check_srst
   source_srst   coord_transformt	   transformt   codecsR*   R)   t   check_uniquet   uniquet   transaction_modet   transaction_decoratorR   t   atomicR   (   t   selfR6   t   dataR5   R/   R>   R)   RD   R@   RC   R2   R*   (    (    ss   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/contrib/gis/utils/layermapping.pyt   __init__O   s@    	$		
	
			c         C   sI   | rA t  | t t f ƒ r% t | Œ  St  | t ƒ r8 | St ‚ n d Sd S(   s$   This checks the `fid_range` keyword.N(   R,   t   tuplet   listt   slicet	   TypeErrorR0   (   RG   t	   fid_range(    (    ss   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/contrib/gis/utils/layermapping.pyt   check_fid_range—   s    
	c            sÝ  t  |  _ i  |  _ |  j j ‰  |  j j } ‡  f d †  } x|  j j ƒ  D]Œ\ } } y |  j j j	 | ƒ } Wn! t
 k
 r‘ t d | ƒ ‚ n X| j j } t | t ƒ r|  j rÅ t d ƒ ‚ n  | j } y/ | d k rð t | d ƒ } n t | ƒ } Wn! t k
 r t d | ƒ ‚ n X|  j j }	 |	 j j | j ƒ pQ|  j |	 | ƒ s‚t d | | d k rod n d	 |	 f ƒ ‚ n  | |  _ | |  _ | }
 n+t | t j ƒ rOt | t ƒ r@| j j } xj | j ƒ  D]\ \ } } | | ƒ } y | j j	 | ƒ Wq×t
 k
 r2t d
 | | j j f ƒ ‚ q×Xq×W| }
 qÈt d ƒ ‚ ny | j |  j k rtt d | ƒ ‚ n  | | ƒ } | | } t | |  j | j ƒ sÂt d | | j | f ƒ ‚ n  | }
 |
 |  j | <qI Wd S(   sè   
        This checks the Layer metadata, and ensures that it is compatible
        with the mapping information and model.  Unlike previous revisions,
        there is no need to increment through each feature in the Layer.
        c            s;   y ˆ  j  |  ƒ } Wn! t k
 r6 t d |  ƒ ‚ n X| S(   Ns4   Given mapping OGR field "%s" not found in OGR Layer.(   t   indext
   ValueErrorR   (   t   ogr_map_fldt   idx(   t
   ogr_fields(    ss   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/contrib/gis/utils/layermapping.pyt   check_ogr_fldµ   s
    s3   Given mapping field "%s" not in given Model fields.sD   LayerMapping does not support more than one GeometryField per model.i   t   25Ds'   Invalid mapping for GeometryField "%s".sD   Invalid mapping geometry; model has %s%s, layer geometry type is %s.s   (dim=3)t    s/   ForeignKey mapping field "%s" not in %s fields.s.   ForeignKey mapping must be of dictionary type.s0   Django field type "%s" has no OGR mapping (yet).s:   OGR field "%s" (of type %s) cannot be mapped to Django %s.N(   R<   t
   geom_fieldt   fieldsR/   t   field_typesR5   t   itemsR6   t   _metat	   get_fieldR   R   t	   __class__R   R,   R   t   dimR   R   t	   geom_typet   namet
   startswitht
   make_multit	   coord_dimR   t
   ForeignKeyt   dictt   remote_fieldRM   t   FIELD_TYPESt
   issubclass(   RG   t   ogr_field_typesRU   t
   field_namet   ogr_namet   model_fieldt   fld_nameRd   t   gtypet   ltypet
   fields_valt	   rel_modelt   rel_namet	   ogr_fieldRS   (    (   RT   ss   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/contrib/gis/utils/layermapping.pyR7   £   sb    						'(				
c         C   sŒ   t  | t ƒ r | } nW t  | |  j j ƒ  ƒ r< | j } n3 t  | t t j f ƒ rc t | ƒ } n |  j j } | s„ t	 d ƒ ‚ n | Sd S(   s?   Checks the compatibility of the given spatial reference object.s#   No source reference system defined.N(
   R,   R   R4   t   spatial_ref_syst   srst   intR   R-   R/   R   (   RG   R>   t   sr(    (    ss   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/contrib/gis/utils/layermapping.pyR=     s    	c         C   s~   t  | t t f ƒ rA xb | D] } | |  j k r t ‚ q q Wn9 t  | t j ƒ rn | |  j k rz t ‚ qz n t d ƒ ‚ d S(   sE   Checks the `unique` keyword parameter -- may be a sequence or string.sB   Unique keyword argument must be set with a tuple, list, or string.N(   R,   RK   RJ   R5   RQ   R   R-   RM   (   RG   RC   t   attr(    (    ss   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/contrib/gis/utils/layermapping.pyRB     s    c         C   sÏ   i  } xÂ |  j  j ƒ  D]± \ } } |  j | } t | t ƒ rz y |  j | j | ƒ } Wq½ t k
 rv t d ƒ ‚ q½ XnC t | t	 j
 j ƒ r§ |  j | | | ƒ } n |  j | | | ƒ } | | | <q W| S(   s…   
        Given an OGR Feature, this will return a dictionary of keyword arguments
        for constructing the mapped model.
        s)   Could not retrieve geometry from feature.(   R5   R[   RY   R,   R   t   verify_geomt   geomR   R   R   t   baset	   ModelBaset	   verify_fkt   verify_ogr_field(   RG   t   featt   kwargsRk   Rl   Rm   t   val(    (    ss   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/contrib/gis/utils/layermapping.pyt   feature_kwargs%  s    c            sE   t  |  j t j ƒ r* i ˆ  |  j |  j 6S‡  f d †  |  j Dƒ Sd S(   sÈ   
        Given the feature keyword arguments (from `feature_kwargs`) this routine
        will construct and return the uniqueness keyword arguments -- a subset
        of the feature kwargs.
        c            s   i  |  ] } ˆ  | | “ q S(    (    (   t   .0t   fld(   R   (    ss   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/contrib/gis/utils/layermapping.pys
   <dictcomp>O  s   	 N(   R,   RC   R   R-   (   RG   R   (    (   R   ss   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/contrib/gis/utils/layermapping.pyt   unique_kwargsF  s    c   
      C   s  t  | t ƒ r t  | t j t j f ƒ r |  j rK t | j |  j ƒ } n	 | j } | j rt	 | ƒ | j k rt
 d | j | j t	 | ƒ f ƒ ‚ qndt  | t ƒ r”t  | t j ƒ r”y t t | j ƒ ƒ } Wn$ t k
 rý t d | j ƒ ‚ n X| j ƒ  } | d } | d } | j | j } | d k  rMt	 | |  ƒ }	 n t	 | ƒ | }	 |	 | k r‹t d | j | j | f ƒ ‚ n  | } nm t  | t t f ƒ røt  | t j ƒ røy t | j ƒ } Wqt k
 rôt d | j ƒ ‚ qXn	 | j } | S(   sÆ   
        Verifies if the OGR Field contents are acceptable to the Django
        model field.  If they are, the verified value is returned,
        otherwise the proper exception is raised.
        s@   %s model field maximum string length is %s, given %s characters.s$   Could not construct decimal from: %si   i   i    se   A DecimalField with max_digits %d, decimal_places %d must round to an absolute value less than 10^%d.s$   Could not construct integer from: %s(   R,   R   R   t	   CharFieldt	   TextFieldR)   R   t   valuet
   max_lengtht   lenR   Ra   R   t   DecimalFieldR    t   strt   DecimalInvalidOperationR   t   as_tuplet
   max_digitst   decimal_placest   IntegerFieldRw   RQ   R   (
   RG   Rt   Rm   R‚   t   dt   dtupt   digitst   d_idxt   max_prect   n_prec(    (    ss   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/contrib/gis/utils/layermapping.pyR   R  s@    		%!

	'	c         C   sš   i  } x@ | j  ƒ  D]2 \ } } |  j | | | j j | ƒ ƒ | | <q Wy  | j j |  j ƒ j |   SWn* t k
 r• t d | j	 | f ƒ ‚ n Xd S(   s®   
        Given an OGR Feature, the related model and its dictionary mapping,
        this routine will retrieve the related model for the ForeignKey
        mapping.
        s7   No ForeignKey %s model found with keyword arguments: %sN(
   R[   R   R\   R]   t   objectsR2   t   getR   R   R   (   RG   R€   Rr   t   rel_mappingt	   fk_kwargsRk   Rl   (    (    ss   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/contrib/gis/utils/layermapping.pyR~   Œ  s    * c         C   sŽ   |  j  | j  k r! |  j  | _  n  |  j | j | ƒ re |  j | j j } t | ƒ } | j | ƒ n | } |  j r‡ | j |  j ƒ n  | j S(   sß   
        Verifies the geometry -- will construct and return a GeometryCollection
        if necessary (for example if the model field is MultiPolygonField while
        the mapped shapefile only contains Polygons).
        (	   Rd   Rc   R`   t   MULTI_TYPESt   numR   t   addR@   t   wkt(   RG   R{   Rm   t
   multi_typet   g(    (    ss   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/contrib/gis/utils/layermapping.pyRz   ¤  s    	c         C   s”   |  j  j ƒ  } y> | j j |  j ƒ j d |  j j ƒ j } t |  j	 | ƒ SWn@ t
 k
 r } d | } t j t t | ƒ t j ƒ  d ƒ n Xd S(   s-   Returns the coordinate transformation object.t   sridsB   Could not translate between the data source and model geometry: %si   N(   R4   Ru   R™   R2   Rš   R;   R£   Rv   R   R>   t	   ExceptionR   t   reraiseR   t   syst   exc_info(   RG   t   SpatialRefSyst
   target_srst   msgt   new_msg(    (    ss   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/contrib/gis/utils/layermapping.pyR?   À  s    *
c         C   s   |  j  j } | j |  j ƒ S(   sI   Returns the GeometryField instance associated with the geographic column.(   R6   R\   R]   RX   (   RG   t   opts(    (    ss   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/contrib/gis/utils/layermapping.pyR:   Í  s    c         C   s)   | j  |  j k o( | j j d | j k S(   s«   
        Given the OGRGeomType for a geometry and its associated GeometryField,
        determine whether the geometry should be turned into a GeometryCollection.
        s   Multi%s(   Rž   R   R^   R   t   django(   RG   R`   Rm   (    (    ss   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/contrib/gis/utils/layermapping.pyRc   Ô  s    c      
      s£  ˆ j  | ƒ } ˆ  rC ˆ  t k s1 t ˆ  t ƒ r: d ‰ qC ˆ  ‰ n  | d d ‡  ‡ ‡ ‡ ‡ ‡ ‡ f d † }	 ˆ j d	 k	 rŽ ˆ j |	 ƒ }	 n  ˆ j j }
 | r˜t | t ƒ r˜| |
 k  r˜| rÐ t d ƒ ‚ n  d
 \ } } } t	 | |
 | ƒ } t
 | ƒ } xŸ t | ƒ D]‡ \ } } | d | k r8t | d	 ƒ } n t | | ƒ } y" |	 | | | ƒ \ } } | } Wq
ˆ j d d d | f ƒ ‚  q
Xq
Wn |	 ƒ  d	 S(   s³  
        Saves the contents from the OGR DataSource Layer into the database
        according to the mapping dictionary given at initialization.

        Keyword Parameters:
         verbose:
           If set, information will be printed subsequent to each model save
           executed on the database.

         fid_range:
           May be set with a slice or tuple of (begin, end) feature ID's to map
           from the data source.  In other words, this keyword enables the user
           to selectively import a subset range of features in the geographic
           data source.

         step:
           If set with an integer, transactions will occur at every step
           interval. For example, if step=1000, a commit would occur after
           the 1,000th feature, the 2,000th feature etc.

         progress:
           When this keyword is set, status information will be printed giving
           the number of features processed and successfully saved.  By default,
           progress information will pe printed every 1000 features processed,
           however, this default may be overridden by setting this keyword with an
           integer for the desired interval.

         stream:
           Status information will be written to this file handle.  Defaults to
           using `sys.stdout`, but any object with a `write` method is supported.

         silent:
           By default, non-fatal error notifications are printed to stdout, but
           this keyword may be set to disable these notifications.

         strict:
           Execution of the model mapping will cease upon the first error
           encountered.  The default behavior is to attempt to continue.
        iè  i    c            sc  |  r ˆ j  |  } n	 ˆ j  } x7| D]/} | d 7} y ˆ j | ƒ } WnB t k
 r } ˆ rg ‚  q%ˆ s%ˆ j d | j | f ƒ q%n˜Xt } ˆ j rYy– ˆ j | ƒ } ˆ j j	 j
 ˆ j
 ƒ j |   }	 t } t |	 ˆ j ƒ j }
 t | ˆ j ƒ } x | D] } |
 j | ƒ qWt |	 ˆ j |
 j ƒ Wqht k
 rUˆ j |   }	 qhXn ˆ j |   }	 yM |	 j d ˆ j
 ƒ | d 7} ˆ r´ˆ j d | r£d n d |	 f ƒ n  Wnm t k
 r$} ˆ rˆ sûˆ j d | j ƒ ˆ j d | ƒ n  ‚  q%ˆ s%ˆ j d	 | | f ƒ q%n Xˆ  r& | ˆ d
 k r& ˆ j d | | f ƒ q& q& W| | f S(   Ni   s#   Ignoring Feature ID %s because: %s
R2   s   %s: %s
t   Updatedt   SavedsO   Failed to save the feature (id: %s) into the model with the keyword arguments:
s   %s
s"   Failed to save %s:
 %s
Continuing
i    s$   Processed %d features, saved %d ...
(   R/   Rƒ   R   t   writet   fidR<   RC   R†   R6   R™   R2   Rš   t   Truet   getattrRX   t   ogrR   RŸ   t   setattrR    R   t   saveR¤   (   t
   feat_ranget   num_featt	   num_savedt
   layer_iterR€   R   Rª   t	   is_updatet   u_kwargst   mR{   t   newR¢   (   t   progresst   progress_intervalRG   t   silentt   streamt   strictt   verbose(    ss   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/contrib/gis/utils/layermapping.pyt   _save  sX    	
!	!
*sO   The `step` keyword may not be used in conjunction with the `fid_range` keyword.i   s   %s
Failed to save slice: %s
s   =-i   N(   i    i    i    (   RO   R²   R,   Rw   RE   R0   R/   R¸   R   t   rangeR‹   t	   enumerateRL   R°   (   RG   RÄ   RN   t   stepR¿   RÁ   RÂ   RÃ   t   default_rangeRÅ   t   nfeatt   begR¸   R¹   t   indicest   n_it   it   endt
   step_slice(    (   R¿   RÀ   RG   RÁ   RÂ   RÃ   RÄ   ss   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/contrib/gis/utils/layermapping.pyR¶   Ü  s4    *		*H!
N(0   R   R   t   __doc__R   Rž   R   R   R   t	   AutoFieldR   R   R’   t
   FloatFieldR	   t	   DateFieldR
   t   DateTimeFieldt
   EmailFieldR   t	   TimeFieldRŒ   R‡   t	   SlugFieldRˆ   t   URLFieldt   BigIntegerFieldt   SmallIntegerFieldt   PositiveSmallIntegerFieldRh   R0   R²   RI   RO   R7   R=   RB   Rƒ   R†   R   R~   Rz   R?   R:   Rc   R<   R¦   t   stdoutR¶   (    (    (    ss   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/contrib/gis/utils/layermapping.pyR   /   sR   








E		a			!		:						(*   RÑ   R¦   t   decimalR    R   RŽ   t   django.contrib.gis.db.modelsR   t   django.contrib.gis.gdalR   R   R   R   R   R   t   django.contrib.gis.gdal.fieldR	   R
   R   R   R   R   t   django.core.exceptionsR   R   t	   django.dbR   R   R   R   t   django.utilsR   t   django.utils.encodingR   R¤   R   R   R   R   R   t   objectR   (    (    (    ss   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/contrib/gis/utils/layermapping.pyt   <module>   s   .."