3
(ht6                 @   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G dd	 d	eZG d
d deZG dd deZeed< G dd deZeed< G dd deZeed< G dd deZeed< G dd deZeed< G dd deZeed< G dd deZeed < G d!d" d"eZeed#< G d$d% d%eZeed&< G d'd( d(eZeed)< G d*d+ d+eZeed,< G d-d. d.eZeed/< G d0d1 d1eZeed2< G d3d4 d4eZ e ed5< G d6d7 d7eZ!e!ed8< G d9d: d:eZ"e"ed;< G d<d= d=eZ#e#ed>< G d?d@ d@eZ$e$edA< G dBdC dCeZ%e%edD< G dEdF dFeZ&e&edG< G dHdI dIeZ'e'edJ< G dKdL dLeZ(e(edM< G dNdO dOeZ)e)edP< G dQdR dReZ*e*edS< G dTdU dUeZ+e+edV< G dWdX dXeZ,e,edY< G dZd[ d[eZ-G d\d] d]e-Z.e.ed^< G d_d` d`e-Z/e/eda< G dbdc dce-Z0e0edd< G dedf dfe-Z1e1edg< G dhdi die-Z2e2e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   H/tmp/pip-install-q3hcpn_q/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                   sj   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  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 }y4|j|}x$t|rV|jj	j
}|j|j }q4W W n ttfk
rr   dS X t||r|S dS d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S t| jtr8| jjd | _nd| _| jd | _ t| jdkrf| 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   sH   t |ttfr2|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    $zGISLookup.get_db_prep_lookupc                s*  t | jdrtt| j||S | j}t| jtrV| jj}t |dsJtd|j	| j_	nt| jt
rltdnt| jttfr| jd }t| jdkr| jdk st| jdkr| jdkr| j  qt| jdkrtd	| j nt| jtr| jd
d 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.T)r1   )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| ||dd}|j| j | j||}|j|| ||S )Nz%s)r   r0   r7   )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)F)r   r   r   sql_templateZtransform_funcdistancer.   r/   r   classmethodr+   r2   r9   r>   rE   r   __classcell__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   rM      s   rM   rN   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   rO   rA   r   r   r   r   rP      s   rP   rQ   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   rO   rA   r   r   r   r   rR      s   rR   rS   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   rO   rA   r   r   r   r   rT      s   rT   rU   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   rO   rA   r   r   r   r   rV      s   rV   rW   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   rO   rA   r   r   r   r   rX      s   rX   rY   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   rO   rA   r   r   r   r   rZ      s   rZ   r[   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   rO   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   rO   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`   ra   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   rO   rA   r   r   r   r   rb      s   rb   rc   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   rO   rA   r   r   r   r   rd      s   rd   re   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   rO   rA   r   r   r   r   rf     s   rf   rg   c               @   s   e Zd ZdZdS )ContainsLookupcontainsN)r   r   r   rA   r   r   r   r   rh     s   rh   ri   c               @   s   e Zd ZdZdS )ContainsProperlyLookupcontains_properlyN)r   r   r   rA   r   r   r   r   rj     s   rj   rk   c               @   s   e Zd ZdZdS )CoveredByLookup	coveredbyN)r   r   r   rA   r   r   r   r   rl     s   rl   rm   c               @   s   e Zd ZdZdS )CoversLookupcoversN)r   r   r   rA   r   r   r   r   rn     s   rn   ro   c               @   s   e Zd ZdZdS )CrossesLookupcrossesN)r   r   r   rA   r   r   r   r   rp   $  s   rp   rq   c               @   s   e Zd ZdZdS )DisjointLookupdisjointN)r   r   r   rA   r   r   r   r   rr   )  s   rr   rs   c               @   s   e Zd ZdZdS )EqualsLookupequalsN)r   r   r   rA   r   r   r   r   rt   .  s   rt   ru   c               @   s   e Zd ZdZdS )IntersectsLookup
intersectsN)r   r   r   rA   r   r   r   r   rv   3  s   rv   rw   c               @   s   e Zd ZdZdd ZdS )IsValidLookupisvalidc             C   sZ   | j jjdkrtd|jj| j }| j||\}}d|j|d }| j	sRd| }||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   rz   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   rx   8  s   rx   ry   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  Z	S )RelateLookupr;   z%(func)s(%(lhs)s, %(rhs)s, %%s)z^[012TF\*]{9}$c                s~   t |dkrtd|jj| j }t|dr<|j|d  n0|d }t|tj	 s`| j
j| rltd| 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   rL   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   s4  t | jttf s2dt| j  ko*dkn   rBtd| j n&t| jdkrh| jd dk rhtdt| jdkr| jd dk r| j  |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   F)Zhandle_spheroid)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$    2


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   rM   rP   rR   rT   rV   rX   rZ   r\   r^   r`   rb   rd   rf   rh   rj   rl   rn   rp   rr   rt   rv   rx   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   s     