
 XF                 @   s  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 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 x! e j   D] Z e j  e  qWGd d   d e 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!  Z( Gd" d#   d# e e  Z) Gd$ d%   d% e  Z* d& S)'    )forms)RasterBandTransformgis_lookups)SpatialProxy)HAS_GDAL)GDALException)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   Q/home/ubuntu/projects/ifolica/build/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_format3   s    	z"GeoSelectFormatMixin.select_formatN)__name__
__module____qualname__r#   r   r   r   r   r   2   s   r   c                   s   e  Z d  Z d Z e d  Z 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 d d   Z d d   Z d d    Z   f 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.F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-   V   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   r0   r   s
    !zBaseSpatialField.deconstructc             C   s   | j  j |   S)N)r   Zgeo_db_type)r    r   r   r   r   db_type{   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)Nr7   )hasattrr8   r7   )r    r   r   r   r   r      s    zBaseSpatialField.spheroidc             C   s&   t  |  d  s |  j |  n  |  j S)Nr5   )r9   r8   r5   )r    r   r   r   r   r      s    zBaseSpatialField.unitsc             C   s&   t  |  d  s |  j |  n  |  j S)Nr6   )r9   r8   r6   )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_placeholderc             C   sN   | j  } | d k s? |  j  d k s? | d k rF |  j  d k rF |  j  S| Sd S)a  
        Return the default SRID for the given geometry or raster, taking into
        account the SRID set for the field. For example, if the input geometry
        or raster doesn't have an SRID, then the SRID of the field will be
        returned.
        N   r@   r@   )r   )r    objr   r   r   r   get_srid   s    	6zBaseSpatialField.get_sridc             C   s'   | s
 d S| j  j |  j |   Sd S)z?
        Prepare the value for saving in the database.
        N)r   Adapterget_prep_value)r    r=   r   r   r   r   get_db_prep_save   s    z!BaseSpatialField.get_db_prep_savec             C   s   d d l  m } t | |  r# | S| rO y | |  SWq t k
 rK Yq XnE t | t  r y | |  SWq t k
 r t d |   Yq Xn  d S)zY
        Return a GDALRaster if conversion is successful, otherwise return None.
        r   )
GDALRasterz6Couldn't create spatial object from lookup value '%s'.N)django.contrib.gis.gdalrF   
isinstancer   dict
ValueError)r    r=   is_candidaterF   r   r   r   get_raster_prep_value   s    z&BaseSpatialField.get_raster_prep_valuec                su  t  t |   j |  } t | t t f  r1 | St | t t f  rY | d } d } n | } d } t | t  rw n t | t	 t
 j f  p t | d  } d } t r |  j | |  } n  | r | } n] | ry t |  } Wq+t t f k
 rt d |   Yq+Xn t d t |  j   |  j |  | _ | rm| g } | j | d d   t |  S| Sd S)	aA  
        Spatial lookup values are either a parameter that is (or may be
        converted to) a geometry or raster, or a sequence of lookup values
        that begins with a geometry or raster. This routine sets up the
        geometry or raster value properly and preserves any other lookup
        parameters.
        r   TFZ__geo_interface__z6Couldn't create spatial object from lookup value '%s'.z>Cannot use object with type %s for a spatial lookup parameter.r?   N)r,   r'   rD   rH   r   booltuplelistr   bytesr   string_typesr9   r   rL   r	   r   rJ   typer$   rB   r   extend)r    r=   rA   Z	seq_valuerK   ZrasterZ
lookup_val)r/   r   r   rD      s8    
	'		
zBaseSpatialField.get_prep_value)r(   r)   )r$   r%   r&   __doc___descriptionZempty_strings_allowedr;   r-   r0   r4   r8   r   r   r   r<   r>   rB   rE   rL   rD   r   r   )r/   r   r'   I   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 d d   Z   f 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)r[   r\   rY   rZ   )dim	geographypopZ_extentZ
_tolerancer,   rW   r-   )r    r*   r]   r^   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)NrX   r]   Fr^   )r,   rW   r0   r]   r^   )r    r1   r2   r3   r.   )r/   r   r   r0   -  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   r`   7  s    zGeometryField.get_distancec             C   sY   | rU t  | t  s$ t |  } n  | j } | rU |  j d k rU |  j | _ qU n  | S)Nr?   r@   )rH   r   r   )r    r=   
expressionr   contextr   r   r   r   from_db_value?  s    	zGeometryField.from_db_valuec                s<   t  t |   j | | |  t | |  j t t |    d  S)N)r,   rW   contribute_to_classsetattrattnamer   r   )r    clsr1   r.   )r/   r   r   re   I  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   rX   widgetZsupports_3dF)ri   rj   r   updater]   getattrrk   r   ZTextarear,   rW   	formfield)r    r.   defaults)r/   r   r   rn   O  s    
zGeometryField.formfieldc             C   sm   t  | t t f  rT | j j | d  g } | |  j | d d  | |  7} n | j j |  g } | S)a  
        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.

        Only used by the deprecated GeoQuerySet and to be
        RemovedInDjango20Warning.
        r   r?   N)rH   rN   rO   r   rC   r`   )r    ra   r=   r   r"   r   r   r   _get_db_prep_lookupZ  s
    &z!GeometryField._get_db_prep_lookup)r$   r%   r&   rT   rU   rV   r   rW   ri   rj   r-   r0   r`   rd   re   rn   rp   r   r   )r/   r   rW     s   	

rW   c               @   s+   e  Z d  Z d Z e j Z e d  Z d S)
PointFieldZPOINTZPointN)	r$   r%   r&   rj   r   rq   ri   rU   rV   r   r   r   r   rq   q  s   	rq   c               @   s+   e  Z d  Z d Z e j Z e d  Z d S)LineStringFieldZ
LINESTRINGzLine stringN)	r$   r%   r&   rj   r   rr   ri   rU   rV   r   r   r   r   rr   w  s   	rr   c               @   s+   e  Z d  Z d Z e j Z e d  Z d S)PolygonFieldZPOLYGONZPolygonN)	r$   r%   r&   rj   r   rs   ri   rU   rV   r   r   r   r   rs   }  s   	rs   c               @   s+   e  Z d  Z d Z e j Z e d  Z d S)MultiPointFieldZ
MULTIPOINTzMulti-pointN)	r$   r%   r&   rj   r   rt   ri   rU   rV   r   r   r   r   rt     s   	rt   c               @   s+   e  Z d  Z d Z e j Z e d  Z d S)MultiLineStringFieldZMULTILINESTRINGzMulti-line stringN)	r$   r%   r&   rj   r   ru   ri   rU   rV   r   r   r   r   ru     s   	ru   c               @   s+   e  Z d  Z d Z e j Z e d  Z d S)MultiPolygonFieldZMULTIPOLYGONzMulti polygonN)	r$   r%   r&   rj   r   rv   ri   rU   rV   r   r   r   r   rv     s   	rv   c               @   s+   e  Z d  Z d Z e j Z e d  Z d S)GeometryCollectionFieldZGEOMETRYCOLLECTIONzGeometry collectionN)	r$   r%   r&   rj   r   rw   ri   rU   rV   r   r   r   r   rw     s   	rw   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)Nrx   r   )r    r   r   r   get_internal_type  s    zExtentField.get_internal_typeN)r$   r%   r&   rT   rU   rV   ry   r   r   r   r   rx     s   rx   c                   s   e  Z d  Z d Z e d  Z 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   f d d   Z   S)RasterFieldzJ
    Raster field for GeoDjango -- evaluates into GDALRaster objects.
    zRaster FieldZRASTERFc                s2   t  s t d   n  t t |   j | |   d  S)NzRasterField requires GDAL.)r   r
   r,   rz   r-   )r    r3   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)r|   r,   rz   r4   )r    r   )r/   r   r   r4     s    zRasterField.db_typec             C   s   | j  j |  S)N)r   Zparse_raster)r    r=   rb   r   rc   r   r   r   rd     s    zRasterField.from_db_valuec                sD   |  j  |  | s( | j j |  } n  t t |   j | | |  S)N)r|   r   Zdeconstruct_rasterr,   rz   get_db_prep_value)r    r=   r   Zprepared)r/   r   r   r}     s    zRasterField.get_db_prep_valuec                sL   t  t |   j | | |  d d l m } t | |  j t | |    d  S)Nr   )rF   )r,   rz   re   rG   rF   rf   rg   r   )r    rh   r1   r.   rF   )r/   r   r   re     s    zRasterField.contribute_to_classc                sU   y* t  |  } t d t f i | d 6 SWn t k
 r> Yn Xt t |   j |  S)NZSpecificRasterBandTransform
band_index)intrR   r   rJ   r,   rz   get_transform)r    r1   r~   )r/   r   r   r     s    zRasterField.get_transform)r$   r%   r&   rT   rU   rV   rj   r^   r-   r|   r4   rd   r}   re   r   r   r   )r/   r   rz     s   
rz   N)+Zdjango.contrib.gisr   Z$django.contrib.gis.db.models.lookupsr   r   Z"django.contrib.gis.db.models.proxyr   rG   r   Zdjango.contrib.gis.gdal.errorr   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   rU   r   r   objectr   r'   valuesklassZregister_lookuprW   rq   rr   rs   rt   ru   rv   rw   rx   rz   r   r   r   r   <module>   s4   l	