

_V*                 @   sh  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 d  d l m Z i  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- <Gd. d/   d/ e  Z e e d0 <Gd1 d2   d2 e  Z e e d3 <Gd4 d5   d5 e  Z e e d6 <Gd7 d8   d8 e  Z e e d9 <Gd: d;   d; e  Z  e  e d< <Gd= d>   d> e  Z! e! e d? <Gd@ dA   dA e  Z" e" e dB <GdC dD   dD e  Z# e# e dE <GdF dG   dG e  Z$ e$ e dH <GdI dJ   dJ e  Z% e% e dK <GdL dM   dM e  Z& e& e dN <GdO dP   dP e  Z' e' e dQ <GdR dS   dS e  Z( e( e dT <GdU dV   dV e  Z) GdW dX   dX e)  Z* e* e dY <GdZ d[   d[ e)  Z+ e+ e d\ <Gd] d^   d^ e)  Z, e, e d_ <Gd` da   da e)  Z- e- e db <Gdc dd   dd e)  Z. e. e de <d S)f    )unicode_literalsN)FieldDoesNotExist)
LOOKUP_SEP)Col
Expression)Lookup)sixc                   sj   e  Z d  Z d Z d Z d Z e d d    Z d d   Z   f d d   Z	 d	 d
   Z
 d d   Z   S)	GISLookupNFc             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 GeometryField exists according to the given lookup on the model
        options, it will be returned.  Otherwise returns None.
        r   )GeometryFieldFN)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_fld r   F/tmp/pip-build-ghmbqnp_/Django/django/contrib/gis/db/models/lookups.py_check_geo_field   s    
	zGISLookup._check_geo_fieldc             C   sd   t  | t t f  rE | j j | d  g t |  d d   } n | j j |  g } d | f S)Nr      z%s)r   tuplelistopsAdapter)selfvalue
connectionparamsr   r   r   get_db_prep_lookup;   s    0zGISLookup.get_db_prep_lookupc                s  t  t |   j | |  \ } } t |  j d  r= | | f S|  j } t |  j t  r |  j j } t | d  s t d   n  | j	 |  j _	 nI t |  j t
  r t d   n( t |  j t t f  r |  j d } n  | j j |  j j | |  } | | f S)NZ_as_sqlsridz(No geographic field found in expression.z3Complex expressions not supported for GeometryFieldr   )superr	   process_rhshasattrrhsr   r   output_field
ValueErrorr$   r   r   r   r   Zget_geom_placeholderlhs)r   compilerr!   r(   
rhs_paramsZgeomr   )	__class__r   r   r&   D   s    !
	zGISLookup.process_rhsc             C   s   | j  j |  j S)N)r   gis_operatorslookup_name)r   r!   r(   r   r   r   
get_rhs_opZ   s    zGISLookup.get_rhs_opc       	      C   sy   |  j  | |  \ } } |  j | |  \ } } | j |  i | d 6| d 6} |  j | |  } | j | |  | |  S)Nr+   r(   )Zprocess_lhsr&   extendr1   as_sql)	r   r,   r!   Zlhs_sqlZ
sql_paramsZrhs_sqlr-   Ztemplate_paramsZrhs_opr   r   r   r3   `   s    zGISLookup.as_sql)__name__
__module____qualname__sql_templateZtransform_funcdistanceclassmethodr   r#   r&   r1   r3   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)r4   r5   r6   __doc__r0   r   r   r   r   r:   n   s   r:   r;   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)r4   r5   r6   r<   r0   r   r   r   r   r=   w   s   r=   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.
    overlaps_belowN)r4   r5   r6   r<   r0   r   r   r   r   r?      s   r?   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.
    overlaps_aboveN)r4   r5   r6   r<   r0   r   r   r   r   rA      s   rA   rB   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)r4   r5   r6   r<   r0   r   r   r   r   rC      s   rC   rD   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)r4   r5   r6   r<   r0   r   r   r   r   rE      s   rE   rF   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)r4   r5   r6   r<   r0   r   r   r   r   rG      s   rG   rH   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)r4   r5   r6   r<   r0   r   r   r   r   rI      s   rI   rJ   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)r4   r5   r6   r<   r0   r   r   r   r   rK      s   rK   rL   c               @   s   e  Z d  Z d Z d S)ExactLookupexactN)r4   r5   r6   r0   r   r   r   r   rM      s   rM   rN   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)r4   r5   r6   r<   r0   r   r   r   r   rO      s   rO   rP   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)r4   r5   r6   r<   r0   r   r   r   r   rQ      s   rQ   rR   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)r4   r5   r6   r<   r0   r   r   r   r   rS      s   rS   rT   c               @   s   e  Z d  Z d Z d S)ContainsLookupcontainsN)r4   r5   r6   r0   r   r   r   r   rU      s   rU   rV   c               @   s   e  Z d  Z d Z d S)ContainsProperlyLookupcontains_properlyN)r4   r5   r6   r0   r   r   r   r   rW      s   rW   rX   c               @   s   e  Z d  Z d Z d S)CoveredByLookup	coveredbyN)r4   r5   r6   r0   r   r   r   r   rY      s   rY   rZ   c               @   s   e  Z d  Z d Z d S)CoversLookupcoversN)r4   r5   r6   r0   r   r   r   r   r[      s   r[   r\   c               @   s   e  Z d  Z d Z d S)CrossesLookupcrossesN)r4   r5   r6   r0   r   r   r   r   r]      s   r]   r^   c               @   s   e  Z d  Z d Z d S)DisjointLookupdisjointN)r4   r5   r6   r0   r   r   r   r   r_      s   r_   r`   c               @   s   e  Z d  Z d Z d S)EqualsLookupequalsN)r4   r5   r6   r0   r   r   r   r   ra     s   ra   rb   c               @   s   e  Z d  Z d Z d S)IntersectsLookup
intersectsN)r4   r5   r6   r0   r   r   r   r   rc     s   rc   rd   c               @   s   e  Z d  Z d Z d S)OverlapsLookupoverlapsN)r4   r5   r6   r0   r   r   r   r   re     s   re   rf   c                   s=   e  Z d  Z d Z d Z e j d  Z   f d d   Z   S)RelateLookuprelatez%(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)N   z!relate must be passed a two-tuplecheck_relate_argumentr   z)Invalid intersection matrix pattern "%s".)r   r*   r   r/   r0   r'   rj   r   r   string_typespattern_regexmatchr%   rg   r#   )r   r    r!   Z
backend_oppattern)r.   r   r   r#     s    
&zRelateLookup.get_db_prep_lookup)	r4   r5   r6   r0   r7   recompilerl   r#   r   r   )r.   r   rg     s   rg   rh   c               @   s   e  Z d  Z d Z d S)TouchesLookuptouchesN)r4   r5   r6   r0   r   r   r   r   rq   %  s   rq   rr   c               @   s   e  Z d  Z d Z d S)WithinLookupwithinN)r4   r5   r6   r0   r   r   r   r   rs   *  s   rs   rt   c                   s.   e  Z d  Z d Z d Z   f d d   Z   S)DistanceLookupBaseTz%%(func)s(%(lhs)s, %(rhs)s) %(op)s %%sc                s   t  | t t f  r d t |  k o2 d k n sM t d |  j   n  | j j | d  g } | | j j |  j	 j
 | d d   |  j  7} d | f St t |   j | |  Sd  S)Nri      z.2 or 3-element tuple required for '%s' lookup.r   r   z%s)r   r   r   r   r*   r0   r   r   Zget_distancer+   r)   r%   ru   r#   )r   r    r!   r"   )r.   r   r   r#   3  s    "/
z%DistanceLookupBase.get_db_prep_lookup)r4   r5   r6   r8   r7   r#   r   r   )r.   r   ru   /  s   ru   c               @   s   e  Z d  Z d Z d Z d S)DWithinLookupdwithinz%(func)s(%(lhs)s, %(rhs)s, %%s)N)r4   r5   r6   r0   r7   r   r   r   r   rw   ?  s   rw   rx   c               @   s   e  Z d  Z d Z d S)DistanceGTLookupdistance_gtN)r4   r5   r6   r0   r   r   r   r   ry   E  s   ry   rz   c               @   s   e  Z d  Z d Z d S)DistanceGTELookupdistance_gteN)r4   r5   r6   r0   r   r   r   r   r{   J  s   r{   r|   c               @   s   e  Z d  Z d Z d S)DistanceLTLookupdistance_ltN)r4   r5   r6   r0   r   r   r   r   r}   O  s   r}   r~   c               @   s   e  Z d  Z d Z d S)DistanceLTELookupdistance_lteN)r4   r5   r6   r0   r   r   r   r   r   T  s   r   r   )/
__future__r   ro   Zdjango.core.exceptionsr   Zdjango.db.models.constantsr   Zdjango.db.models.expressionsr   r   Zdjango.db.models.lookupsr   Zdjango.utilsr   Zgis_lookupsr	   r:   r=   r?   rA   rC   rE   rG   rI   rK   rM   rO   rQ   rS   rU   rW   rY   r[   r]   r_   ra   rc   re   rg   rq   rs   ru   rw   ry   r{   r}   r   r   r   r   r   <module>   s   `




























