

_VB                 @   s  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 d  d l m Z d  d l m Z d  d	 l m Z d  d
 l m Z i  a d d   Z Gd d   d e  Z Gd d   d e  Z Gd d   d e e  Z x! e j   D] Z e j e  q
WGd 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$ Gd d    d  e  Z% Gd! d"   d" e e  Z& Gd# d$   d$ e  Z' d% S)&    )forms)gis_lookups)SpatialProxy)HAS_GDAL)GeometryGeometryException)ImproperlyConfigured)
Expression)Field)six)ugettext_lazyc             C   s   y | j  j   } Wn t k
 r+ d SYn X| j t k rK i  t | j <n  |  t | j k r | j j | j  j d |   } | j \ } } | j	 | j
  } | | | f t | j |  <n  t | j |  S)z
    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.
    Nsrid)NNN)opsZspatial_ref_sysNotImplementedErroralias_srid_cacheZobjectsZusinggetunitsZget_spheroidZwkt)r   
connectionZSpatialRefSyssrr   
units_namespheroid r   E/tmp/pip-build-ghmbqnp_/Django/django/contrib/gis/db/models/fields.pyget_srid_info   s    	!r   c               @   s   e  Z d  Z d d   Z d S)GeoSelectFormatMixinc             C   sm   | j  } | j j d  } | r: d | j j | f } n d } | j j r_ | j j | } n  | | | f S)a.  
        Returns the selection format string, depending on the requirements
        of the spatial backend.  For example, Oracle and MySQL require custom
        selection formats in order to retrieve geometries in OGC WKT. For all
        other fields a simple '%s' format string is returned.
        Ztransformed_sridz%s(%%s, %s)z%s)r   queryZget_contextr   Z	transformselect)selfcompilerZsqlparamsr   r   Zsel_fmtr   r   r   select_format0   s    	z"GeoSelectFormatMixin.select_formatN)__name__
__module____qualname__r!   r   r   r   r   r   /   s   r   c                   s   e  Z d  Z d Z e d  Z d Z d d d   f d d	  Z   f d
 d   Z d d   Z	 d d   Z
 d d   Z d d   Z d d   Z d d   Z d d   Z   S)BaseSpatialFieldz
    The Base GIS Field.

    It's used as a base class for GeometryField and RasterField. Defines
    properties that are common to all GIS fields such as the characteristics
    of the spatial reference system of the field.
    zThe base GIS field.decimal degreedegreeNi  Tc                s6   | |  _  | |  _ | | d <t t |   j |   d S)a  
        The initialization function for base spatial 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.
        verbose_nameN)spatial_indexr   superr%   __init__)r   r(   r   r)   kwargs)	__class__r   r   r+   R   s    		
zBaseSpatialField.__init__c                s]   t  t |   j   \ } } } } |  j | d <|  j d k	 rM |  j | d <n  | | | | f S)Nr   Tr)   )r*   r%   deconstructr   r)   )r   namepathargsr,   )r-   r   r   r.   n   s
    !zBaseSpatialField.deconstructc             C   s   | j  j |   S)N)r   Zgeo_db_type)r   r   r   r   r   db_typew   s    zBaseSpatialField.db_typec             C   s(   t  |  j |  \ |  _ |  _ |  _ d  S)N)r   r   _units_units_name	_spheroid)r   r   r   r   r   _get_srid_info|   s    zBaseSpatialField._get_srid_infoc             C   s&   t  |  d  s |  j |  n  |  j S)Nr5   )hasattrr6   r5   )r   r   r   r   r   r      s    zBaseSpatialField.spheroidc             C   s&   t  |  d  s |  j |  n  |  j S)Nr3   )r7   r6   r3   )r   r   r   r   r   r      s    zBaseSpatialField.unitsc             C   s&   t  |  d  s |  j |  n  |  j S)Nr4   )r7   r6   r4   )r   r   r   r   r   r      s    zBaseSpatialField.units_namec             C   s5   |  j  |  } | r( | j   |  j k S|  j d k S)z
        Returns true if this field's SRID corresponds with a coordinate
        system that uses non-projected units (e.g., latitude/longitude).
        i  )r   lowergeodetic_unitsr   )r   r   r   r   r   r   geodetic   s    zBaseSpatialField.geodeticc             C   s   | j  j |  | |  S)z]
        Returns the placeholder for the spatial column for the
        given value.
        )r   Zget_geom_placeholder)r   valuer   r   r   r   r   get_placeholder   s    z BaseSpatialField.get_placeholder)r&   r'   )r"   r#   r$   __doc___descriptionr9   r+   r.   r2   r6   r   r   r   r:   r<   r   r   )r-   r   r%   F   s   	
r%   c                   s   e  Z d  Z d Z e d  Z e j Z d Z	 d d d   f d d  Z
   f d	 d
   Z d d   Z   f d d   Z d d   Z d d   Z   f d d   Z   f d d   Z d d d  Z   f d d   Z d d   Z   S)GeometryFieldzU
    The base Geometry field -- maps to the OpenGIS Specification Geometry type.
    zKThe base Geometry field -- maps to the OpenGIS Specification Geometry type.ZGEOMETRYN   Fc                s\   | |  _  | |  _ | j d d
  |  _ | j d d  |  _ t t |   j d | |  d S)a  
        The initialization function for geometry fields. In addition to the
        parameters from BaseSpatialField, it takes the following as keyword
        arguments:

        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.
        Zextent     f@     V@Z	toleranceg?r(   N     f     V)rD   rE   rB   rC   )dim	geographypopZ_extentZ
_tolerancer*   r@   r+   )r   r(   rF   rG   r,   )r-   r   r   r+      s
    		zGeometryField.__init__c                so   t  t |   j   \ } } } } |  j d k r@ |  j | d <n  |  j d k	 r_ |  j | d <n  | | | | f S)NrA   rF   FrG   )r*   r@   r.   rF   rG   )r   r/   r0   r1   r,   )r-   r   r   r.      s    !zGeometryField.deconstructc             C   s   | j  j |  | |  S)z
        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   get_distance)r   r;   lookup_typer   r   r   r   rI      s    zGeometryField.get_distancec                s/  t  t |   j |  } t | t  r+ | St | t t f  rS | d } d } n | } d } t | t  rq nt t | t t	 j
 f  s t | d  r y t |  } Wq t k
 r t d   Yq Xn t d t |  j   |  j |  | _ | r'| g } | j | d d   t |  S| Sd S)	aF  
        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.
        r   TFZ__geo_interface__z,Could not create geometry from lookup value.z?Cannot use object with type %s for a geometry lookup parameter.   N)r*   r@   get_prep_value
isinstancer	   tuplelistr   bytesr   string_typesr7   r   
ValueErrortyper"   get_sridr   extend)r   r;   geomZ	seq_valueZ
lookup_val)r-   r   r   rL      s,    
	'	
zGeometryField.get_prep_valuec             C   s)   | r% t  | t  r% t |  } n  | S)N)rM   r   )r   r;   
expressionr   contextr   r   r   from_db_value  s    zGeometryField.from_db_valuec             C   sN   | j  } | d k s? |  j  d k s? | d k rF |  j  d k rF |  j  S| Sd S)z
        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.
        NrK   rZ   rZ   )r   )r   rV   Zgsridr   r   r   rT     s    	6zGeometryField.get_sridc                s<   t  t |   j | | |  t | |  j t t |    d  S)N)r*   r@   contribute_to_classsetattrattnamer   r   )r   clsr/   r,   )r-   r   r   r[     s    z!GeometryField.contribute_to_classc                s   i |  j  d 6|  j d 6|  j d 6} | j |  |  j d k rv d | k rv t | d j d d  rv t j | d <n  t	 t
 |   j |   S)N
form_class	geom_typer   rA   widgetZsupports_3dF)r_   r`   r   updaterF   getattrra   r   ZTextarear*   r@   	formfield)r   r,   defaults)r-   r   r   rd     s    
zGeometryField.formfieldc             C   s   | d k r g  S| |  j  k r t | t t f  r | j j | d  g } |  j  | j r | |  j | d d  | |  7} q | | j j k r q | | d d  7} n- t | t	  r g  } n | j j |  g } | St
 d | |  j j f   d S)aC  
        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.
        Zisnullr   rK   Nz(%s is not a valid spatial lookup for %s.)Zclass_lookupsrM   rN   rO   r   AdapterZdistancerI   Ztruncate_paramsr	   rR   r-   r"   )r   rJ   r;   r   preparedr    r   r   r   get_db_prep_lookup&  s     	&	z GeometryField.get_db_prep_lookupc                s2   | d k r |  j  |  St t |   j | |  S)Ncontains)rL   r*   r@   get_prep_lookup)r   rJ   r;   )r-   r   r   rj   I  s    zGeometryField.get_prep_lookupc             C   s'   | s
 d S| j  j |  j |   Sd S)z.Prepares the value for saving in the database.N)r   rf   rL   )r   r;   r   r   r   r   get_db_prep_saveP  s    zGeometryField.get_db_prep_save)r"   r#   r$   r=   r>   r?   r   r@   r_   r`   r+   r.   rI   rL   rY   rT   r[   rd   rh   rj   rk   r   r   )r-   r   r@      s   	
(#r@   c               @   s+   e  Z d  Z d Z e j Z e d  Z d S)
PointFieldZPOINTZPointN)	r"   r#   r$   r`   r   rl   r_   r>   r?   r   r   r   r   rl   ]  s   	rl   c               @   s+   e  Z d  Z d Z e j Z e d  Z d S)LineStringFieldZ
LINESTRINGzLine stringN)	r"   r#   r$   r`   r   rm   r_   r>   r?   r   r   r   r   rm   c  s   	rm   c               @   s+   e  Z d  Z d Z e j Z e d  Z d S)PolygonFieldZPOLYGONZPolygonN)	r"   r#   r$   r`   r   rn   r_   r>   r?   r   r   r   r   rn   i  s   	rn   c               @   s+   e  Z d  Z d Z e j Z e d  Z d S)MultiPointFieldZ
MULTIPOINTzMulti-pointN)	r"   r#   r$   r`   r   ro   r_   r>   r?   r   r   r   r   ro   o  s   	ro   c               @   s+   e  Z d  Z d Z e j Z e d  Z d S)MultiLineStringFieldZMULTILINESTRINGzMulti-line stringN)	r"   r#   r$   r`   r   rp   r_   r>   r?   r   r   r   r   rp   u  s   	rp   c               @   s+   e  Z d  Z d Z e j Z e d  Z d S)MultiPolygonFieldZMULTIPOLYGONzMulti polygonN)	r"   r#   r$   r`   r   rq   r_   r>   r?   r   r   r   r   rq   {  s   	rq   c               @   s+   e  Z d  Z d Z e j Z e d  Z d S)GeometryCollectionFieldZGEOMETRYCOLLECTIONzGeometry collectionN)	r"   r#   r$   r`   r   rr   r_   r>   r?   r   r   r   r   rr     s   	rr   c               @   s.   e  Z d  Z d Z e d  Z d d   Z d S)ExtentFieldz/Used as a return value from an extent aggregatezExtent Aggregate Fieldc             C   s   d S)Nrs   r   )r   r   r   r   get_internal_type  s    zExtentField.get_internal_typeN)r"   r#   r$   r=   r>   r?   rt   r   r   r   r   rs     s   rs   c                   s   e  Z d  Z d Z e d  Z d Z   f d d   Z d d   Z   f d d	   Z	 d
 d   Z
 d   f d d  Z   f d d   Z   S)RasterFieldzJ
    Raster field for GeoDjango -- evaluates into GDALRaster objects.
    zRaster FieldZRASTERc                s2   t  s t d   n  t t |   j | |   d  S)NzRasterField requires GDAL.)r   r   r*   ru   r+   )r   r1   r,   )r-   r   r   r+     s    zRasterField.__init__c             C   s-   | j  j s | j  j r) t d   n  d  S)Nz3Raster fields require backends with raster support.)featuresZgis_enabledZsupports_rasterr   )r   r   r   r   r   _check_connection  s    zRasterField._check_connectionc                s#   |  j  |  t t |   j |  S)N)rw   r*   ru   r2   )r   r   )r-   r   r   r2     s    zRasterField.db_typec             C   s   | j  j |  S)N)r   Zparse_raster)r   r;   rW   r   rX   r   r   r   rY     s    zRasterField.from_db_valueFc                sD   |  j  |  | s( | j j |  } n  t t |   j | | |  S)N)rw   r   Zdeconstruct_rasterr*   ru   get_db_prep_value)r   r;   r   rg   )r-   r   r   rx     s    zRasterField.get_db_prep_valuec                sL   t  t |   j | | |  d d l m } t | |  j t | |    d  S)Nr   )
GDALRaster)r*   ru   r[   django.contrib.gis.gdalry   r\   r]   r   )r   r^   r/   r,   ry   )r-   r   r   r[     s    zRasterField.contribute_to_class)r"   r#   r$   r=   r>   r?   r`   r+   rw   r2   rY   rx   r[   r   r   )r-   r   ru     s   ru   N)(Zdjango.contrib.gisr   Z$django.contrib.gis.db.models.lookupsr   Z"django.contrib.gis.db.models.proxyr   rz   r   Z#django.contrib.gis.geometry.backendr   r   Zdjango.core.exceptionsr   Zdjango.db.models.expressionsr	   Zdjango.db.models.fieldsr
   Zdjango.utilsr   Zdjango.utils.translationr   r>   r   r   objectr   r%   r@   valuesklassZregister_lookuprl   rm   rn   ro   rp   rq   rr   rs   ru   r   r   r   r   <module>   s2   [	