ó
ų¢TQc           @   s9  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
 d  d l m Z m Z d  d l m Z i  a d   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 e f d     YZ d e f d     YZ d S(   i’’’’(   t   Field(   t   SQLEvaluator(   t   ugettext_lazy(   t   forms(   t   GeometryProxy(   t   Geometryt   GeometryException(   t   sixc         C   sČ   y | j  j   } Wn t k
 r' d SX| j t k rG i  t | j <n  |  t | j k r¹ | j j | j  j d |   } | j	 \ } } | j
 | j  } | | | f t | j |  <n  t | j |  S(   sß   
    Returns the units, unit name, and spheroid WKT associated with the
    given SRID from the `spatial_ref_sys` (or equivalent) spatial database
    table for the given database connection.  These results are cached.
    t   sridN(   NNN(   t   opst   spatial_ref_syst   NotImplementedErrort   Nonet   aliast   _srid_cachet   objectst   usingt   gett   unitst   get_spheroidt   wkt(   R   t
   connectiont   SpatialRefSyst   srR   t
   units_namet   spheroid(    (    s<   ../Django//lib/python/django/contrib/gis/db/models/fields.pyt   get_srid_info   s    !t   GeometryFieldc           B   sČ   e  Z d  Z d Z d Z e d   Z d d e d e	 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	 d  Z d   Z d   Z d   Z RS(   sF   The base GIS field -- maps to the OpenGIS Specification Geometry type.t   GEOMETRYs   Decimal Degreet   degreeię  i   c         K   sr   | |  _  | |  _ | |  _ | | d <| |  _ | j d d
  |  _ | j d d  |  _ t t |   j	 |   d	 S(   s  
        The initialization function for geometry fields.  Takes the following
        as keyword arguments:

        srid:
         The spatial reference system identifier, an OGC standard.
         Defaults to 4326 (WGS84).

        spatial_index:
         Indicates whether to create a spatial index.  Defaults to True.
         Set this instead of 'db_index' for geographic fields since index
         creation is different for geometry columns.

        dim:
         The number of dimensions for this geometry.  Defaults to 2.

        extent:
         Customize the extent, in a 4-tuple of WGS 84 coordinates, for the
         geometry field entry in the `USER_SDO_GEOM_METADATA` table.  Defaults
         to (-180.0, -90.0, 180.0, 90.0).

        tolerance:
         Define the tolerance, in meters, to use for the geometry field
         entry in the `USER_SDO_GEOM_METADATA` table.  Defaults to 0.05.
        t   verbose_namet   extentg     fĄg     VĄg     f@g     V@t	   toleranceg©?N(   g     fĄg     VĄg     f@g     V@(
   t   spatial_indexR   t   dimt	   geographyt   popt   _extentt
   _tolerancet   superR   t   __init__(   t   selfR   R   R!   R"   R#   t   kwargs(    (    s<   ../Django//lib/python/django/contrib/gis/db/models/fields.pyR(   5   s    			
	c         C   s(   t  |  j |  \ |  _ |  _ |  _ d  S(   N(   R   R   t   _unitst   _units_namet	   _spheroid(   R)   R   (    (    s<   ../Django//lib/python/django/contrib/gis/db/models/fields.pyt   _get_srid_infok   s    c         C   s&   t  |  d  s |  j |  n  |  j S(   NR-   (   t   hasattrR.   R-   (   R)   R   (    (    s<   ../Django//lib/python/django/contrib/gis/db/models/fields.pyR   o   s    c         C   s&   t  |  d  s |  j |  n  |  j S(   NR+   (   R/   R.   R+   (   R)   R   (    (    s<   ../Django//lib/python/django/contrib/gis/db/models/fields.pyR   t   s    c         C   s&   t  |  d  s |  j |  n  |  j S(   NR,   (   R/   R.   R,   (   R)   R   (    (    s<   ../Django//lib/python/django/contrib/gis/db/models/fields.pyR   y   s    c         C   s   |  j  |  |  j k S(   s   
        Returns true if this field's SRID corresponds with a coordinate
        system that uses non-projected units (e.g., latitude/longitude).
        (   R   t   geodetic_units(   R)   R   (    (    s<   ../Django//lib/python/django/contrib/gis/db/models/fields.pyt   geodetic   s    c         C   s   | j  j |  | |  S(   sĆ   
        Returns a distance number in units of the field.  For example, if
        `D(km=1)` was passed in and the units of the field were in meters,
        then 1000 would be returned.
        (   R	   t   get_distance(   R)   t   valuet   lookup_typeR   (    (    s<   ../Django//lib/python/django/contrib/gis/db/models/fields.pyR2      s    c         C   s  t  | t  r | St  | t t f  r; | d } t } n | } t } t  | t  rY ns t  | t t j	 f  s t
 | d  r³ y t |  } WqĢ t k
 rÆ t d   qĢ Xn t d t |  j   |  j |  | _ | r| g } | j | d  t |  S| Sd S(   sF  
        Spatial lookup values are either a parameter that is (or may be
        converted to) a geometry, or a sequence of lookup values that
        begins with a geometry.  This routine will setup the geometry
        value properly, and preserve any other lookup parameters before
        returning to the caller.
        i    t   __geo_interface__s,   Could not create geometry from lookup value.s?   Cannot use object with type %s for a geometry lookup parameter.i   N(   t
   isinstanceR   t   tuplet   listt   Truet   FalseR   t   bytesR   t   string_typesR/   R   t
   ValueErrort   typet   __name__t   get_sridR   t   extend(   R)   R3   t   geomt	   seq_valuet
   lookup_val(    (    s<   ../Django//lib/python/django/contrib/gis/db/models/fields.pyt   get_prep_value   s*    
	'	
c         C   sN   | j  } | d k s? |  j  d k s? | d k rF |  j  d k rF |  j  S| Sd S(   sÜ   
        Returns the default SRID for the given geometry, taking into account
        the SRID set for the field.  For example, if the input geometry
        has no SRID, then that of the field will be returned.
        i’’’’N(   R   R   (   R)   RB   t   gsrid(    (    s<   ../Django//lib/python/django/contrib/gis/db/models/fields.pyR@   µ   s    	6c         C   s9   t  t |   j | |  t | |  j t t |    d  S(   N(   R'   R   t   contribute_to_classt   setattrt   attnameR   R   (   R)   t   clst   name(    (    s<   ../Django//lib/python/django/contrib/gis/db/models/fields.pyRG   Ā   s    c         C   s   | j  j |   S(   N(   R	   t   geo_db_type(   R)   R   (    (    s<   ../Django//lib/python/django/contrib/gis/db/models/fields.pyt   db_typeČ   s    c         K   sQ   i t  j d 6|  j d 6|  j d 6|  j d 6} | j |  t t |   j |   S(   Nt
   form_classt   nullt	   geom_typeR   (   R   R   RO   RP   R   t   updateR'   t	   formfield(   R)   R*   t   defaults(    (    s<   ../Django//lib/python/django/contrib/gis/db/models/fields.pyRR   Ė   s    

c         C   sł   | | j  j k rŁ | d k r" g  St | t t f  rØ | j  j | d  g } | | j  j k r | |  j | d | |  7} qÕ | | j  j k r qÕ | | d 7} n- t | t	  rĄ g  } n | j  j |  g } | St
 d | |  j j f   d S(   sC  
        Prepare for the database lookup, and return any spatial parameters
        necessary for the query.  This includes wrapping any geometry
        parameters with a backend-specific adapter and formatting any distance
        parameters into the correct units for the coordinate system of the
        field.
        t   isnulli    i   s(   %s is not a valid spatial lookup for %s.N(   R	   t	   gis_termsR6   R7   R8   t   Adaptert   distance_functionsR2   t   truncate_paramsR   R=   t	   __class__R?   (   R)   R4   R3   R   t   preparedt   params(    (    s<   ../Django//lib/python/django/contrib/gis/db/models/fields.pyt   get_db_prep_lookupŌ   s      	c         C   s'   | d k r t  |  S|  j |  Sd  S(   NRT   (   t   boolRE   (   R)   R4   R3   (    (    s<   ../Django//lib/python/django/contrib/gis/db/models/fields.pyt   get_prep_lookupų   s    
c         C   s-   | d k r d S| j j |  j |   Sd S(   s.   Prepares the value for saving in the database.N(   R   R	   RV   RE   (   R)   R3   R   (    (    s<   ../Django//lib/python/django/contrib/gis/db/models/fields.pyt   get_db_prep_savež   s    c         C   s   | j  j |  |  S(   s^   
        Returns the placeholder for the geometry column for the
        given value.
        (   R	   t   get_geom_placeholder(   R)   R3   R   (    (    s<   ../Django//lib/python/django/contrib/gis/db/models/fields.pyt   get_placeholder  s    (   s   Decimal DegreeR   N(   R?   t
   __module__t   __doc__RP   R0   t   _t   descriptionR   R9   R:   R(   R.   R   R   R   R1   R2   RE   R@   RG   RM   RR   R\   R^   R_   Ra   (    (    (    s<   ../Django//lib/python/django/contrib/gis/db/models/fields.pyR   *   s*   5							'					$		t
   PointFieldc           B   s   e  Z d  Z e d  Z RS(   t   POINTt   Point(   R?   Rb   RP   Rd   Re   (    (    (    s<   ../Django//lib/python/django/contrib/gis/db/models/fields.pyRf     s   t   LineStringFieldc           B   s   e  Z d  Z e d  Z RS(   t
   LINESTRINGs   Line string(   R?   Rb   RP   Rd   Re   (    (    (    s<   ../Django//lib/python/django/contrib/gis/db/models/fields.pyRi     s   t   PolygonFieldc           B   s   e  Z d  Z e d  Z RS(   t   POLYGONt   Polygon(   R?   Rb   RP   Rd   Re   (    (    (    s<   ../Django//lib/python/django/contrib/gis/db/models/fields.pyRk     s   t   MultiPointFieldc           B   s   e  Z d  Z e d  Z RS(   t
   MULTIPOINTs   Multi-point(   R?   Rb   RP   Rd   Re   (    (    (    s<   ../Django//lib/python/django/contrib/gis/db/models/fields.pyRn     s   t   MultiLineStringFieldc           B   s   e  Z d  Z e d  Z RS(   t   MULTILINESTRINGs   Multi-line string(   R?   Rb   RP   Rd   Re   (    (    (    s<   ../Django//lib/python/django/contrib/gis/db/models/fields.pyRp     s   t   MultiPolygonFieldc           B   s   e  Z d  Z e d  Z RS(   t   MULTIPOLYGONs   Multi polygon(   R?   Rb   RP   Rd   Re   (    (    (    s<   ../Django//lib/python/django/contrib/gis/db/models/fields.pyRr   !  s   t   GeometryCollectionFieldc           B   s   e  Z d  Z e d  Z RS(   t   GEOMETRYCOLLECTIONs   Geometry collection(   R?   Rb   RP   Rd   Re   (    (    (    s<   ../Django//lib/python/django/contrib/gis/db/models/fields.pyRt   %  s   N(   t   django.db.models.fieldsR    t    django.db.models.sql.expressionsR   t   django.utils.translationR   Rd   t   django.contrib.gisR   t"   django.contrib.gis.db.models.proxyR   t#   django.contrib.gis.geometry.backendR   R   t   django.utilsR   R   R   R   Rf   Ri   Rk   Rn   Rp   Rr   Rt   (    (    (    s<   ../Django//lib/python/django/contrib/gis/db/models/fields.pyt   <module>   s    	ć