
 Xj                 @   sz  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 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" Gd d   d e#  Z$ Gd d   d e$  Z% Gd d   d e$  Z& Gd d   d e$  Z' Gd d   d e$  Z( Gd d   d e)  Z* d S)a  
 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/
    N)DecimalInvalidOperation)GeometryField)CoordTransform
DataSourceGDALExceptionOGRGeometryOGRGeomTypeSpatialReference)OFTDateOFTDateTime
OFTIntegerOFTInteger64OFTReal	OFTStringOFTTime)FieldDoesNotExistObjectDoesNotExist)connectionsmodelsroutertransaction)six)
force_textc               @   s   e  Z d  Z d S)LayerMapErrorN)__name__
__module____qualname__ r   r   S/home/ubuntu/projects/ifolica/build/django/django/contrib/gis/utils/layermapping.pyr      s   r   c               @   s   e  Z d  Z d S)InvalidStringN)r   r   r   r   r   r   r   r        s   r    c               @   s   e  Z d  Z d S)InvalidDecimalN)r   r   r   r   r   r   r   r!   $   s   r!   c               @   s   e  Z d  Z d S)InvalidIntegerN)r   r   r   r   r   r   r   r"   (   s   r"   c               @   s   e  Z d  Z d S)MissingForeignKeyN)r   r   r   r   r   r   r   r#   ,   s   r#   c            	   @   s0  e  Z d  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 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 d d d d d  Z  d d   Z! d d   Z" d d   Z# d d   Z$ d d   Z% d d    Z& d! d"   Z' d# d$   Z( d% d&   Z) d' d(   Z* d) d*   Z+ d+ d,   Z, d- d- d- d- d- e- j. d- d. d/  Z/ d S)0LayerMappingz1A class that maps OGR Layers to GeoDjango Models.Z
MultiPoint   ZMultiLineString   ZMultiPolygon   ZMultiPoint25DZPoint25DZMultiLineString25DZLineString25DZMultiPolygon25DZ
Polygon25Dr   Nzutf-8commit_on_successTc             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 d } | 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)	a  
        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.
        encodingNFr   )lookupZ
autocommitr(   z!Unrecognized transaction mode: %s)!
isinstancer   string_typesr   Zdslayerr   Zdb_for_writeusingr   opsspatial_backendmappingmodelcheck_layerfeaturesZsupports_transformgeometry_field	geo_field	check_srs
source_srscoord_transform	transformcodecsr*   r)   check_uniqueuniquetransaction_modetransaction_decoratorr   Zatomicr   )selfr2   datar1   r-   r8   r)   r>   r:   r=   r.   r*   r   r   r   __init__Q   s@    	$		
	
			zLayerMapping.__init__c             C   sI   | rA t  | t t f  r% t |   St  | t  r8 | St  n d Sd S)z$This checks the `fid_range` keyword.N)r+   tuplelistslice	TypeError)r@   	fid_ranger   r   r   check_fid_range   s    
	zLayerMapping.check_fid_rangec                s  d |  _  i  |  _ |  j j   |  j j }   f d d   } x|  j j   D]\ } } y |  j j j |  } Wn" t	 k
 r t
 d |   Yn 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 |   Yn X|  j j }	 |	 j j | j  pV|  j |	 |  st
 d	 | | d k rtd
 n d |	 f   n  | |  _  | |  _ | }
 n,t | t j  rUt | t  rF| j j } xk | j   D]] \ } } | |  } y | j j |  Wqt	 k
 r8t
 d | | j j f   YqXqW| }
 qt d   ny | j |  j k rzt
 d |   n  | |  } | | } t | |  j | j  st
 d | | j | f   n  | }
 |
 |  j | <qL Wd S)z
        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.
        Fc                s<   y   j  |   } Wn" t k
 r7 t d |    Yn X| S)Nz4Given mapping OGR field "%s" not found in OGR Layer.)index
ValueErrorr   )Zogr_map_fldidx)
ogr_fieldsr   r   check_ogr_fld   s
    z/LayerMapping.check_layer.<locals>.check_ogr_fldz3Given mapping field "%s" not in given Model fields.zDLayerMapping does not support more than one GeometryField per model.r'   Z25Dz'Invalid mapping for GeometryField "%s".zDInvalid mapping geometry; model has %s%s, layer geometry type is %s.z(dim=3) z/ForeignKey mapping field "%s" not in %s fields.z.ForeignKey mapping must be of dictionary type.z0Django field type "%s" has no OGR mapping (yet).z:OGR field "%s" (of type %s) cannot be mapped to Django %s.N)
geom_fieldfieldsr-   Zfield_typesr1   itemsr2   _meta	get_fieldr   r   	__class__r   r+   r   Zdimr	   r   	geom_typename
startswith
make_multi	coord_dimr   Z
ForeignKeydictZremote_fieldrF   FIELD_TYPES
issubclass)r@   Zogr_field_typesrM   
field_nameogr_namemodel_fieldZfld_namerY   ZgtypeZltypeZ
fields_val	rel_modelZrel_name	ogr_fieldrK   r   )rL   r   r3      sb    						'(				
zLayerMapping.check_layerc             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)z?Checks the compatibility of the given spatial reference object.z#No source reference system defined.N)
r+   r
   r0   spatial_ref_syssrsintr   r,   r-   r   )r@   r8   srr   r   r   r7     s    	zLayerMapping.check_srsc             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)zEChecks the `unique` keyword parameter -- may be a sequence or string.zBUnique keyword argument must be set with a tuple, list, or string.N)r+   rD   rC   r1   rJ   r   r,   rF   )r@   r=   attrr   r   r   r<     s    zLayerMapping.check_uniquec             C   s   i  } x |  j  j   D] \ } } |  j | } t | t  r{ y |  j | j |  } Wq t k
 rw t d   Yq XnC t | t	 j
 j  r |  j | | |  } n |  j | | |  } | | | <q W| S)z
        Given an OGR Feature, this will return a dictionary of keyword arguments
        for constructing the mapped model.
        z)Could not retrieve geometry from feature.)r1   rQ   rP   r+   r   verify_geomgeomr   r   r   baseZ	ModelBase	verify_fkverify_ogr_field)r@   featkwargsr]   r^   r_   valr   r   r   feature_kwargs'  s    zLayerMapping.feature_kwargsc                sH   t  |  j t j  r* i   |  j |  j 6S  f d d   |  j D Sd S)z
        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 Sr   r   ).0Zfld)rm   r   r   
<dictcomp>Q  s   	 z.LayerMapping.unique_kwargs.<locals>.<dictcomp>N)r+   r=   r   r,   )r@   rm   r   )rm   r   unique_kwargsH  s    zLayerMapping.unique_kwargsc       
      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	nlt  | t  rt  | t j  ry t t | j   } Wn% t k
 r t d | j   Yn X| j   } | d } | d } | j | j } | d k  rTt	 | d |   }	 n t	 |  | }	 |	 | k rt d | j | j | f   n  | } nn t  | t t f  r t  | t j  r y t | j  } Wq	t k
 rt d | j   Yq	Xn	 | j } | S)	z
        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.
        z@%s model field maximum string length is %s, given %s characters.z$Could not construct decimal from: %sr%   r&   r   NzeA DecimalField with max_digits %d, decimal_places %d must round to an absolute value less than 10^%d.z$Could not construct integer from: %s)r+   r   r   	CharField	TextFieldr)   r   value
max_lengthlenr    rV   r   DecimalFieldr   strDecimalInvalidOperationr!   as_tupleZ
max_digitsZdecimal_placesIntegerFieldrd   rJ   r"   )
r@   ra   r_   rn   dZdtupdigitsZd_idxZmax_precZn_precr   r   r   rk   T  s@    		%!

	'	zLayerMapping.verify_ogr_fieldc             C   s   i  } x@ | j    D]2 \ } } |  j | | | j j |   | | <q Wy  | j j |  j  j |   SWn+ t k
 r t d | j	 | f   Yn Xd S)z
        Given an OGR Feature, the related model and its dictionary mapping,
        this routine will retrieve the related model for the ForeignKey
        mapping.
        z7No ForeignKey %s model found with keyword arguments: %sN)
rQ   rk   rR   rS   objectsr.   getr   r#   r   )r@   rl   r`   Zrel_mappingZ	fk_kwargsr]   r^   r   r   r   rj     s    * zLayerMapping.verify_fkc             C   s   |  j  | j  k r! |  j  | _  n  |  j | j |  re |  j | j j } t |  } | j |  n | } |  j r | j |  j  n  | j S)z
        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).
        )	rY   rX   rU   MULTI_TYPESnumr   addr:   wkt)r@   rh   r_   Z
multi_typegr   r   r   rg     s    	zLayerMapping.verify_geomc             C   s   |  j  j   } y> | j j |  j  j d |  j j  j } t |  j	 |  SWnR t
 k
 r } z2 d | } t j t t |  t j   d  WYd d } ~ Xn Xd S)z-Returns the coordinate transformation object.sridzBCould not translate between the data source and model geometry: %sr&   N)r0   rb   r   r.   r   r6   r   rc   r   r8   	Exceptionr   reraiser   sysexc_info)r@   ZSpatialRefSysZ
target_srsmsgZnew_msgr   r   r   r9     s    *
zLayerMapping.coord_transformc             C   s   |  j  j } | j |  j  S)zIReturns the GeometryField instance associated with the geographic column.)r2   rR   rS   rO   )r@   optsr   r   r   r5     s    zLayerMapping.geometry_fieldc             C   s)   | j  |  j k o( | j j d | j k S)z
        Given the OGRGeomType for a geometry and its associated GeometryField,
        determine whether the geometry should be turned into a GeometryCollection.
        zMulti%s)r   r   rT   r   Zdjango)r@   rU   r_   r   r   r   rX     s    zLayerMapping.make_multiFc                s   j  |  }   rC   d k s1 t   t  r: d  qC    n  | d d         f d 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 r;t
 | d  } n t
 | |  } y" |	 | | |  \ } } | } Wqt k
 r j d	 d
 d | f    YqXqWn |	   d S)a  
        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.
        Ti  r   c          #      s  |  r  j  |  } n	  j  } x\| D]T} | d 7} y  j |  } WnT t k
 r } z4  rj   n#  s  j d | j | f  n  WYd  d  } ~ XnXd }  j rly  j |  }  j j j	  j	  j
 |   }	 d } t |	  j  j }
 t |  j  } x | D] } |
 j |  qWt |	  j |
 j  Wq{t k
 rh j |   }	 Yq{Xn  j |   }	 yM |	 j d  j	  | d 7}  r j d | rd n d |	 f  n  Wn t k
 rI} z_  r s j d	 | j   j d
 |  n    n   s7 j d | | f  n  WYd  d  } ~ Xn X  r& |  d k r&  j d | | f  q& q& W| | f S)Nr%   z#Ignoring Feature ID %s because: %s
FTr.   z%s: %s
ZUpdatedZSavedzOFailed to save the feature (id: %s) into the model with the keyword arguments:
z%s
z"Failed to save %s:
 %s
Continuing
r   z$Processed %d features, saved %d ...
)r-   ro   r   writeZfidr=   rr   r2   r   r.   r   getattrrO   Zogrr   r   setattrr   r   saver   )Z
feat_rangenum_feat	num_savedZ
layer_iterrl   rm   r   Z	is_updateZu_kwargsmrh   newr   )progressprogress_intervalr@   silentstreamstrictverboser   r   _save  sX    	
0	!
*-z LayerMapping.save.<locals>._saveNzOThe `step` keyword may not be used in conjunction with the `fid_range` keyword.r%   z%s
Failed to save slice: %s
z=-   )r   r   r   )rH   r+   rd   r?   r-   r   r   rangerw   	enumeraterE   r   r   )r@   r   rG   stepr   r   r   r   Zdefault_ranger   Znfeatbegr   r   indicesZn_iiendZ
step_slicer   )r   r   r@   r   r   r   r   r   r     s4    *		-H!
zLayerMapping.save)0r   r   r   __doc__r	   r   r   r   r   Z	AutoFieldr   ZBigAutoFieldr   r   r|   Z
FloatFieldr   Z	DateFieldr   ZDateTimeFieldZ
EmailFieldr   Z	TimeFieldrx   rs   Z	SlugFieldrt   ZURLFieldZBigIntegerFieldZSmallIntegerFieldZPositiveSmallIntegerFieldr[   rB   rH   r3   r7   r<   ro   rr   rk   rj   rg   r9   r5   rX   r   stdoutr   r   r   r   r   r$   0   sT   









Ea!:	r$   )+r   r   decimalr   r   rz   Zdjango.contrib.gis.db.modelsr   Zdjango.contrib.gis.gdalr   r   r   r   r	   r
   Zdjango.contrib.gis.gdal.fieldr   r   r   r   r   r   r   Zdjango.core.exceptionsr   r   Z	django.dbr   r   r   r   Zdjango.utilsr   Zdjango.utils.encodingr   r   r   r    r!   r"   r#   objectr$   r   r   r   r   <module>   s   .4"