
 Xt6                 @   s  d  d l  m Z d  d l Z d  d l m Z d  d l m Z d  d l m Z m	 Z	 d  d l
 m Z m Z m Z d  d l m Z i  Z Gd d	   d	 e  Z Gd
 d   d e  Z Gd d   d e  Z e e d <Gd d   d e  Z e e d <Gd d   d e  Z e e d <Gd d   d e  Z e e d <Gd d   d e  Z e e d <Gd d   d e  Z e e d <Gd d   d e  Z e e d  <Gd! d"   d" e  Z e e d# <Gd$ d%   d% e  Z e e d& <Gd' d(   d( e  Z e e d) <Gd* d+   d+ e  Z e e d, <Gd- d.   d. e  Z e e d/ <Gd0 d1   d1 e  Z e e d2 <Gd3 d4   d4 e  Z  e  e d5 <Gd6 d7   d7 e  Z! e! e d8 <Gd9 d:   d: e  Z" e" e d; <Gd< d=   d= e  Z# e# e d> <Gd? d@   d@ e  Z$ e$ e dA <GdB dC   dC e  Z% e% e dD <GdE dF   dF e  Z& e& e dG <GdH dI   dI e  Z' e' e dJ <GdK dL   dL e  Z( e( e dM <GdN dO   dO e  Z) e) e dP <GdQ dR   dR e  Z* e* e dS <GdT dU   dU e  Z+ e+ e dV <GdW dX   dX e  Z, e, e dY <GdZ d[   d[ e  Z- Gd\ d]   d] e-  Z. e. e d^ <Gd_ d`   d` e-  Z/ e/ e da <Gdb dc   dc e-  Z0 e0 e dd <Gde df   df e-  Z1 e1 e dg <Gdh di   di e-  Z2 e2 e dj <d S)k    )unicode_literalsN)FieldDoesNotExist)
LOOKUP_SEP)Col
Expression)BuiltinLookupLookup	Transform)sixc               @   s   e  Z d  Z d d   Z d S)RasterBandTransformc             C   s   | j  |  j  S)N)compilelhs)selfcompiler
connection r   R/home/ubuntu/projects/ifolica/build/django/django/contrib/gis/db/models/lookups.pyas_sql   s    zRasterBandTransform.as_sqlN)__name__
__module____qualname__r   r   r   r   r   r      s   r   c                   s   e  Z d  Z d Z d Z d Z d Z d Z   f d d   Z e	 d d    Z
 d d d  Z d	 d
   Z   f d d   Z d d   Z d d   Z   S)	GISLookupNFc                s&   t  t |   j | |   i  |  _ d  S)N)superr   __init__template_params)r   argskwargs)	__class__r   r   r      s    zGISLookup.__init__c             C   s   d d l  m } | j t  } | j   | j   } yJ | j |  } x4 t |  r} | j j	 j
 } | j | j    } qJ WWn t t f k
 r d SYn Xt | |  r | Sd Sd S)a  
        Utility for checking the given lookup with the given model options.
        The lookup is a string either specifying the geographic field, e.g.
        'point, 'the_geom', or a related lookup on a geographic field like
        'address__point'.

        If a BaseSpatialField exists according to the given lookup on the model
        options, it will be returned. Otherwise return None.
        r   )BaseSpatialFieldFN)Z#django.contrib.gis.db.models.fieldsr   splitr   reversepop	get_fieldlenZremote_fieldmodelZ_metar   AttributeError
isinstance)clsoptslookupr   Z
field_listZfld_namegeo_fldr   r   r   _check_geo_field   s    
	zGISLookup._check_geo_fieldc             C   s   | r& d |  _  |  j j d |  _ d St |  j t  rN |  j j d |  _ n	 d |  _ |  j d |  _  t |  j  d k r |  j d |  _ n$ |  j d f |  j d d  |  _ d S)z
        Extract the lhs band index from the band transform class and the rhs
        band index from the input tuple.
           Nr      )band_rhsr   Z
band_indexband_lhsr&   r   rhsr#   )r   only_lhsr   r   r   process_band_indicesF   s    		zGISLookup.process_band_indicesc             C   sd   t  | t t f  rE | j j | d  g t |  d d   } n | j j |  g } d | f S)Nr   r,   z%s)r&   tuplelistopsAdapter)r   valuer   paramsr   r   r   get_db_prep_lookup]   s    0zGISLookup.get_db_prep_lookupc                s  t  |  j d  r+ t t |   j | |  S|  j } t |  j t  r |  j j } t  | d  sp t d   n  | j	 |  j _	 n t |  j t
  r t d   n t |  j t t f  rL|  j d } t |  j  d k r |  j d k st |  j  d k r|  j d k r|  j   qqt |  j  d k rqt d	 |  j   qqn% t |  j t  rq|  j d
 d  n  t t |   j | |  \ } } | j j |  j j | |  } | | f S)NZ_as_sqlsridz(No geographic field found in expression.z5Complex expressions not supported for spatial fields.r   r-   relate   zTuple too long for lookup %s.r1   T)hasattrr0   r   r   process_rhsr&   r   output_field
ValueErrorr:   r   r4   r3   r#   lookup_namer2   r   r   r5   get_geom_placeholder)r   r   r   Zgeomr*   r0   
rhs_params)r   r   r   r>   f   s,    	%$!zGISLookup.process_rhsc             C   s   | j  j |  j S)N)r5   gis_operatorsrA   )r   r   r0   r   r   r   
get_rhs_op   s    zGISLookup.get_rhs_opc       	      C   s   |  j  | |  \ } } |  j | |  \ } } | j |  i | d 6| d 6d d 6} | j |  j  |  j | |  } | j | |  | |  S)Nr   r0   z%sr7   )process_lhsr>   extendupdater   rE   r   )	r   r   r   Zlhs_sqlZ
sql_paramsZrhs_sqlrC   r   Zrhs_opr   r   r   r      s    zGISLookup.as_sql)r   r   r   sql_templateZtransform_funcdistancer.   r/   r   classmethodr+   r2   r9   r>   rE   r   r   r   )r   r   r      s   (	r   c               @   s   e  Z d  Z d Z d Z d S)OverlapsLeftLookupzy
    The overlaps_left operator returns true if A's bounding box overlaps or is to the
    left of B's bounding box.
    overlaps_leftN)r   r   r   __doc__rA   r   r   r   r   rL      s   rL   rM   c               @   s   e  Z d  Z d Z d Z d S)OverlapsRightLookupz}
    The 'overlaps_right' operator returns true if A's bounding box overlaps or is to the
    right of B's bounding box.
    overlaps_rightN)r   r   r   rN   rA   r   r   r   r   rO      s   rO   rP   c               @   s   e  Z d  Z d Z d Z d S)OverlapsBelowLookupzs
    The 'overlaps_below' operator returns true if A's bounding box overlaps or is below
    B's bounding box.
    overlaps_belowN)r   r   r   rN   rA   r   r   r   r   rQ      s   rQ   rR   c               @   s   e  Z d  Z d Z d Z d S)OverlapsAboveLookupzs
    The 'overlaps_above' operator returns true if A's bounding box overlaps or is above
    B's bounding box.
    overlaps_aboveN)r   r   r   rN   rA   r   r   r   r   rS      s   rS   rT   c               @   s   e  Z d  Z d Z d Z d S)
LeftLookupzo
    The 'left' operator returns true if A's bounding box is strictly to the left
    of B's bounding box.
    leftN)r   r   r   rN   rA   r   r   r   r   rU      s   rU   rV   c               @   s   e  Z d  Z d Z d Z d S)RightLookupzq
    The 'right' operator returns true if A's bounding box is strictly to the right
    of B's bounding box.
    rightN)r   r   r   rN   rA   r   r   r   r   rW      s   rW   rX   c               @   s   e  Z d  Z d Z d Z d S)StrictlyBelowLookupzp
    The 'strictly_below' operator returns true if A's bounding box is strictly below B's
    bounding box.
    strictly_belowN)r   r   r   rN   rA   r   r   r   r   rY      s   rY   rZ   c               @   s   e  Z d  Z d Z d Z d S)StrictlyAboveLookupzp
    The 'strictly_above' operator returns true if A's bounding box is strictly above B's
    bounding box.
    strictly_aboveN)r   r   r   rN   rA   r   r   r   r   r[      s   r[   r\   c               @   s   e  Z d  Z d Z d Z d S)SameAsLookupz
    The "~=" operator is the "same as" operator. It tests actual geometric
    equality of two features. So if A and B are the same feature,
    vertex-by-vertex, the operator returns true.
    same_asN)r   r   r   rN   rA   r   r   r   r   r]      s   r]   r^   c               @   s   e  Z d  Z d Z d S)ExactLookupexactN)r   r   r   rA   r   r   r   r   r_      s   r_   r`   c               @   s   e  Z d  Z d Z d Z d S)BBContainsLookupzq
    The 'bbcontains' operator returns true if A's bounding box completely contains
    by B's bounding box.
    
bbcontainsN)r   r   r   rN   rA   r   r   r   r   ra      s   ra   rb   c               @   s   e  Z d  Z d Z d Z d S)BBOverlapsLookupz_
    The 'bboverlaps' operator returns true if A's bounding box overlaps B's bounding box.
    
bboverlapsN)r   r   r   rN   rA   r   r   r   r   rc      s   rc   rd   c               @   s   e  Z d  Z d Z d Z d S)ContainedLookupzt
    The 'contained' operator returns true if A's bounding box is completely contained
    by B's bounding box.
    	containedN)r   r   r   rN   rA   r   r   r   r   re     s   re   rf   c               @   s   e  Z d  Z d Z d S)ContainsLookupcontainsN)r   r   r   rA   r   r   r   r   rg     s   rg   rh   c               @   s   e  Z d  Z d Z d S)ContainsProperlyLookupcontains_properlyN)r   r   r   rA   r   r   r   r   ri     s   ri   rj   c               @   s   e  Z d  Z d Z d S)CoveredByLookup	coveredbyN)r   r   r   rA   r   r   r   r   rk     s   rk   rl   c               @   s   e  Z d  Z d Z d S)CoversLookupcoversN)r   r   r   rA   r   r   r   r   rm     s   rm   rn   c               @   s   e  Z d  Z d Z d S)CrossesLookupcrossesN)r   r   r   rA   r   r   r   r   ro   $  s   ro   rp   c               @   s   e  Z d  Z d Z d S)DisjointLookupdisjointN)r   r   r   rA   r   r   r   r   rq   )  s   rq   rr   c               @   s   e  Z d  Z d Z d S)EqualsLookupequalsN)r   r   r   rA   r   r   r   r   rs   .  s   rs   rt   c               @   s   e  Z d  Z d Z d S)IntersectsLookup
intersectsN)r   r   r   rA   r   r   r   r   ru   3  s   ru   rv   c               @   s"   e  Z d  Z d Z d d   Z d S)IsValidLookupisvalidc             C   s   |  j  j j d k r$ t d   n  | j j |  j } |  j | |  \ } } d i | j d 6| d 6} |  j	 s d | } n  | | f S)NZRASTERz8The isvalid lookup is only available on geometry fields.z%(func)s(%(lhs)s)funcr   zNOT )
r   fieldZ	geom_typer@   r5   rD   rA   rF   ry   r0   )r   r   r   Zgis_opsqlr8   r   r   r   r   ;  s    	zIsValidLookup.as_sqlN)r   r   r   rA   r   r   r   r   r   rw   8  s   rw   rx   c               @   s   e  Z d  Z d Z d S)OverlapsLookupoverlapsN)r   r   r   rA   r   r   r   r   r|   G  s   r|   r}   c                   s=   e  Z d  Z d Z d Z e j d  Z   f d d   Z   S)RelateLookupr;   z%(func)s(%(lhs)s, %(rhs)s, %%s)z^[012TF\*]{9}$c                s   t  |  d k r! t d   n  | j j |  j } t | d  rW | j | d  nC | d } t | t j	  s |  j
 j |  r t d |   n  t t |   j | |  S)Nr-   z!relate must be passed a two-tuplecheck_relate_argumentr,   z)Invalid intersection matrix pattern "%s".)r#   r@   r5   rD   rA   r=   r   r&   r
   string_typespattern_regexmatchr   r~   r9   )r   r7   r   Z
backend_oppattern)r   r   r   r9   Q  s    
&zRelateLookup.get_db_prep_lookup)	r   r   r   rA   rI   rer   r   r9   r   r   )r   r   r~   L  s   r~   r;   c               @   s   e  Z d  Z d Z d S)TouchesLookuptouchesN)r   r   r   rA   r   r   r   r   r   `  s   r   r   c               @   s   e  Z d  Z d Z d S)WithinLookupwithinN)r   r   r   rA   r   r   r   r   r   e  s   r   r   c               @   s(   e  Z d  Z d Z d Z d d   Z d S)DistanceLookupBaseTz+%(func)s(%(lhs)s, %(rhs)s) %(op)s %(value)sc             C   s  t  |  j t t f  s? d t |  j  k o9 d k n rU t d |  j   n8 t |  j  d k r |  j d d k r t d   n  t |  j  d k r |  j d d k r |  j   n  | j j	 |  j d  g } |  j d } t
 | d	  r?| j | j  } | j |  \ } } | |  j d
 <| j |  n? | | j j |  j j | f |  j d d   |  j d d 7} | j j |  j j | d |  } | | f S)Nr-      z22, 3, or 4-element tuple required for '%s' lookup.r<   ZspheroidzGFor 4-element tuples the last argument must be the 'speroid' directive.r   r,   resolve_expressionr7   Zhandle_spheroidF)r&   r0   r3   r4   r#   r@   rA   r2   r5   r6   r=   r   queryr   r   rG   Zget_distancer   r?   rB   )r   r   r   r8   Z
dist_paramr{   Zexpr_paramsr0   r   r   r   r>   n  s$    ?)) "zDistanceLookupBase.process_rhsN)r   r   r   rJ   rI   r>   r   r   r   r   r   j  s   r   c               @   s   e  Z d  Z d Z d Z d S)DWithinLookupdwithinz%(func)s(%(lhs)s, %(rhs)s, %%s)N)r   r   r   rA   rI   r   r   r   r   r     s   r   r   c               @   s   e  Z d  Z d Z d S)DistanceGTLookupdistance_gtN)r   r   r   rA   r   r   r   r   r     s   r   r   c               @   s   e  Z d  Z d Z d S)DistanceGTELookupdistance_gteN)r   r   r   rA   r   r   r   r   r     s   r   r   c               @   s   e  Z d  Z d Z d S)DistanceLTLookupdistance_ltN)r   r   r   rA   r   r   r   r   r     s   r   r   c               @   s   e  Z d  Z d Z d S)DistanceLTELookupdistance_lteN)r   r   r   rA   r   r   r   r   r     s   r   r   )3
__future__r   r   Zdjango.core.exceptionsr   Zdjango.db.models.constantsr   Zdjango.db.models.expressionsr   r   Zdjango.db.models.lookupsr   r   r	   Zdjango.utilsr
   Zgis_lookupsr   r   rL   rO   rQ   rS   rU   rW   rY   r[   r]   r_   ra   rc   re   rg   ri   rk   rm   ro   rq   rs   ru   rw   r|   r~   r   r   r   r   r   r   r   r   r   r   r   r   <module>   s   

























 



