
v^,                 @   sa  d  d l  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 Gd d	   d	 e  Z Gd
 d   d e
  Z e j Gd d   d e   Z e j Gd d   d e   Z e j Gd d   d e   Z e j Gd d   d e   Z e j Gd d   d e   Z e j Gd d   d e   Z e j Gd d   d e   Z e j Gd d   d e   Z e j Gd d   d e   Z e j e d  e j Gd d    d  e   Z e j Gd! d"   d" e   Z e j Gd# d$   d$ e   Z e j Gd% d&   d& e   Z e j Gd' d(   d( e   Z e j Gd) d*   d* e   Z e j Gd+ d,   d, e   Z  e j Gd- d.   d. e   Z! e j Gd/ d0   d0 e   Z" e j Gd1 d2   d2 e   Z# e j Gd3 d4   d4 e   Z$ e j Gd5 d6   d6 e   Z% e j Gd7 d8   d8 e   Z& e j Gd9 d:   d: e   Z' e j Gd; d<   d< e   Z( Gd= d>   d> e  Z) e j Gd? d@   d@ e)   Z* GdA dB   dB e)  Z+ e j GdC dD   dD e+   Z, e j GdE dF   dF e+   Z- e j GdG dH   dH e+   Z. e j GdI dJ   dJ e+   Z/ d S)K    N)BaseSpatialField)Distance)NotSupportedError)
Expression)Lookup	Transform)Queryc               @   s   e  Z d  Z d d   Z d S)RasterBandTransformc             C   s   | j  |  j  S)N)compilelhs)selfcompiler
connection r   F/tmp/pip-build-8lau8j11/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 d d   Z	 d d d  Z
 d	 d
   Z   f d d   Z d d   Z d d   Z   S)	GISLookupNFc                sW   t  | t t f  r | n | g ^ } |  _ t   j | |  i  |  _ |  j   d  S)N)
isinstancelisttuple
rhs_paramssuper__init__template_paramsprocess_rhs_params)r   r   rhs)	__class__r   r   r      s    -	zGISLookup.__init__c             C   s   |  j  rk t |  j   |  j d k r* d n d k r@ |  j   q t |  j   d k r t d |  j   n" t |  j t  r |  j d d  d  S)Nrelate      zTuple too long for lookup %s.only_lhsT)r   lenlookup_nameprocess_band_indices
ValueErrorr   r   r	   )r   r   r   r   r      s    	*zGISLookup.process_rhs_paramsc             C   sp   | r& d |  _  |  j j d |  _ d St |  j t  rN |  j j d |  _ n	 d |  _ |  j ^ |  _  |  _ d S)z
        Extract the lhs band index from the band transform class and the rhs
        band index from the input tuple.
        r"   N)band_rhsr   Z
band_indexband_lhsr   r	   r   )r   r#   r   r   r   r&   '   s    		zGISLookup.process_band_indicesc             C   s   d | j  j |  g f S)Nz%s)opsZAdapter)r   valuer   r   r   r   get_db_prep_lookup:   s    zGISLookup.get_db_prep_lookupc                s   t  |  j t  r% t   j | |  St  |  j t  rO |  j j | j  |  _ t   j | |  \ } } | j j	 |  j
 j |  j |  } | | | f S)N)r   r   r   r   process_rhsr   resolve_expressionqueryr*   Zget_geom_placeholderr   output_field)r   r   r   r   r   placeholder)r   r   r   r-   >   s    !zGISLookup.process_rhsc             C   s   | j  j |  j S)N)r*   gis_operatorsr%   )r   r   r   r   r   r   
get_rhs_opH   s    zGISLookup.get_rhs_opc       	      C   s   |  j  | |  \ } } |  j | |  \ } } | j |  d | d | d d i |  j  } |  j | |  } | j | |  | |  S)Nr   r   r+   z%s)Zprocess_lhsr-   extendr   r3   r   )	r   r   r   Zlhs_sqlZ
sql_paramsrhs_sqlr   r   Zrhs_opr   r   r   r   N   s    !zGISLookup.as_sql)r   r   r   sql_templateZtransform_funcdistancer(   r)   r   r   r&   r,   r-   r3   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.
    Zoverlaps_leftN)r   r   r   __doc__r%   r   r   r   r   r8   \   s   r8   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.
    Zoverlaps_rightN)r   r   r   r9   r%   r   r   r   r   r:   e   s   r:   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.
    Zoverlaps_belowN)r   r   r   r9   r%   r   r   r   r   r;   n   s   r;   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.
    Zoverlaps_aboveN)r   r   r   r9   r%   r   r   r   r   r<   w   s   r<   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   r9   r%   r   r   r   r   r=      s   r=   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   r9   r%   r   r   r   r   r?      s   r?   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.
    Zstrictly_belowN)r   r   r   r9   r%   r   r   r   r   rA      s   rA   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.
    Zstrictly_aboveN)r   r   r   r9   r%   r   r   r   r   rB      s   rB   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.
    Zsame_asN)r   r   r   r9   r%   r   r   r   r   rC      s   rC   exactc               @   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.
    Z
bbcontainsN)r   r   r   r9   r%   r   r   r   r   rE      s   rE   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.
    Z
bboverlapsN)r   r   r   r9   r%   r   r   r   r   rF      s   rF   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.
    Z	containedN)r   r   r   r9   r%   r   r   r   r   rG      s   rG   c               @   s   e  Z d  Z d Z d S)ContainsLookupcontainsN)r   r   r   r%   r   r   r   r   rH      s   rH   c               @   s   e  Z d  Z d Z d S)ContainsProperlyLookupZcontains_properlyN)r   r   r   r%   r   r   r   r   rJ      s   rJ   c               @   s   e  Z d  Z d Z d S)CoveredByLookupZ	coveredbyN)r   r   r   r%   r   r   r   r   rK      s   rK   c               @   s   e  Z d  Z d Z d S)CoversLookupZcoversN)r   r   r   r%   r   r   r   r   rL      s   rL   c               @   s   e  Z d  Z d Z d S)CrossesLookupZcrossesN)r   r   r   r%   r   r   r   r   rM      s   rM   c               @   s   e  Z d  Z d Z d S)DisjointLookupZdisjointN)r   r   r   r%   r   r   r   r   rN      s   rN   c               @   s   e  Z d  Z d Z d S)EqualsLookupequalsN)r   r   r   r%   r   r   r   r   rO      s   rO   c               @   s   e  Z d  Z d Z d S)IntersectsLookupZ
intersectsN)r   r   r   r%   r   r   r   r   rQ      s   rQ   c               @   s   e  Z d  Z d Z d S)OverlapsLookupoverlapsN)r   r   r   r%   r   r   r   r   rR      s   rR   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   |  j  d } | j j |  j } t | d  r? | j |  n3 t | t  sb |  j j	 |  rr t
 d |   t   j | |  \ } } | | | g f S)Nr   check_relate_argumentz)Invalid intersection matrix pattern "%s".)r   r*   r2   r%   hasattrrU   r   strpattern_regexmatchr'   r   r-   )r   r   r   patternZ
backend_opsqlparams)r   r   r   r-     s    #zRelateLookup.process_rhs)	r   r   r   r%   r6   rer
   rX   r-   r   r   )r   r   rT      s   rT   c               @   s   e  Z d  Z d Z d S)TouchesLookupZtouchesN)r   r   r   r%   r   r   r   r   r^     s   r^   c               @   s   e  Z d  Z d Z d S)WithinLookupZwithinN)r   r   r   r%   r   r   r   r   r_     s   r_   c               @   s4   e  Z d  Z d Z d Z d d   Z d d   Z d S)DistanceLookupBaseTz+%(func)s(%(lhs)s, %(rhs)s) %(op)s %(value)sc             C   s   d t  |  j  k o  d k n s; t d |  j   n4 t  |  j  d k ro |  j d d k ro t d   t  |  j  d k r |  j d d k r |  j   d  S)Nr"      z22, 3, or 4-element tuple required for '%s' lookup.r!   spheroidzHFor 4-element tuples the last argument must be the 'spheroid' directive.)r$   r   r'   r%   r&   )r   r   r   r   r     s    %((z%DistanceLookupBase.process_rhs_paramsc             C   s]   |  j  d } t | d  r5 | j | j | j   Sd | j j |  j j |  j  |  j	  f S)Nr   r.   z%s)
r   rV   r
   r.   r/   r*   Zget_distancer   r0   r%   )r   r   r   
dist_paramr   r   r   process_distance&  s    (z#DistanceLookupBase.process_distanceN)r   r   r   r7   r6   r   rd   r   r   r   r   r`     s   
r`   c                   s@   e  Z d  Z d Z d Z   f d d   Z   f d d   Z   S)DWithinLookupZdwithinz%%(func)s(%(lhs)s, %(rhs)s, %(value)s)c                sX   |  j  d } | j j rE t | d  rE t | t  rE t d   t   j | |  S)Nr   r.   zXThis backend does not support expressions for specifying distance in the dwithin lookup.)	r   featuresZsupports_dwithin_distance_exprrV   r   r   r   r   rd   )r   r   r   rc   )r   r   r   rd   4  s    	zDWithinLookup.process_distancec                sN   |  j  | |  \ } } | |  j d <t   j | |  \ } } | | | f S)Nr+   )rd   r   r   r-   )r   r   r   dist_sqldist_paramsr5   r\   )r   r   r   r-   A  s    zDWithinLookup.process_rhs)r   r   r   r%   r6   rd   r-   r   r   )r   r   re   /  s   re   c               @   s   e  Z d  Z d d   Z d S)DistanceLookupFromFunctionc       	      C   s   t  |  j  d k r( |  j d d k p+ d  } | j j |  j |  j d | } | j | j | j   \ } } |  j	 | |  \ } } d d | d |  j
 d | i | | f S)	Nr!   r"   rb   z%(func)s %(op)s %(dist)sfuncopdist)r$   r   r*   Zdistance_expr_for_lookupr   r   r
   r.   r/   rd   rk   )	r   r   r   rb   Zdistance_exprr[   r\   rg   rh   r   r   r   r   I  s    .!!z!DistanceLookupFromFunction.as_sqlN)r   r   r   r   r   r   r   r   ri   H  s   ri   c               @   s   e  Z d  Z d Z d Z d S)DistanceGTLookupZdistance_gt>N)r   r   r   r%   rk   r   r   r   r   rn   T  s   rn   c               @   s   e  Z d  Z d Z d Z d S)DistanceGTELookupZdistance_gtez>=N)r   r   r   r%   rk   r   r   r   r   rp   Z  s   rp   c               @   s   e  Z d  Z d Z d Z d S)DistanceLTLookupZdistance_lt<N)r   r   r   r%   rk   r   r   r   r   rq   `  s   rq   c               @   s   e  Z d  Z d Z d Z d S)DistanceLTELookupZdistance_ltez<=N)r   r   r   r%   rk   r   r   r   r   rs   f  s   rs   )0r]   Z#django.contrib.gis.db.models.fieldsr   Zdjango.contrib.gis.measurer   Z	django.dbr   Zdjango.db.models.expressionsr   Zdjango.db.models.lookupsr   r   Zdjango.db.models.sql.queryr   r	   r   Zregister_lookupr8   r:   r;   r<   r=   r?   rA   rB   rC   rE   rF   rG   rH   rJ   rK   rL   rM   rN   rO   rQ   rR   rT   r^   r_   r`   re   ri   rn   rp   rq   rs   r   r   r   r   <module>   s   L	