
0_c           @   s   d  Z  d d l Z 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 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 d e f d     YZ d e e f d     YZ d S(   s   
SQL functions reference lists:
https://web.archive.org/web/20130407175746/https://www.gaia-gis.it/gaia-sins/spatialite-sql-4.0.0.html
https://www.gaia-gis.it/gaia-sins/spatialite-sql-4.2.1.html
iN(   t   BaseSpatialOperations(   t   SpatiaLiteAdapter(   t   SpatialOperator(   t
   aggregates(   t   Geometry(   t   Distance(   t   ImproperlyConfigured(   t   DatabaseOperations(   t   six(   t   cached_propertyt   SpatiaLiteDistanceOperatorc           B   s   e  Z d    Z RS(   c         C   s   | j  j j |  r d } | j i |  j d 6| j j d  d 6 | j d t | j	  d k os | j	 d d k  | | | f St
 t |   j | | | |  S(	   Ns=   COALESCE(%(func)s(%(lhs)s, %(rhs)s, %%s), 0) %(op)s %(value)st   opR   t   funci   i   it   spheroid(   t   lhst   output_fieldt   geodetict   updateR   t   opst   spatial_function_namet   insertt   lent   rhst   superR
   t   as_sql(   t   selft
   connectiont   lookupt   template_paramst
   sql_paramst   sql_template(    (    sV   /tmp/pip-unpacked-wheel-BAJOf3/django/contrib/gis/db/backends/spatialite/operations.pyR      s    	
2(   t   __name__t
   __module__R   (    (    (    sV   /tmp/pip-unpacked-wheel-BAJOf3/django/contrib/gis/db/backends/spatialite/operations.pyR
      s   t   SpatiaLiteOperationsc           B   s  e  Z d  Z e Z e j d  Z e Z	 d Z
 d Z d Z d Z d Z d Z d Z d	 Z d
 Z d Z d Z d Z d Z d Z d Z d Z d Z d Z d Z d Z d Z d Z d Z  d Z! d Z" d Z# d Z$ i e% d d  d 6e% d d  d  6e% d d!  d" 6e% d d#  d$ 6e% d d%  d& 6e% d d'  d( 6e% d d)  d* 6e% d d+  d, 6e% d d-  d. 6e% d d/  d0 6e% d d  d1 6e% d d2  d3 6e% d d4  d5 6e% d d  d6 6e% d d  d7 6e% d d8  d9 6e& d d d: d;  d< 6e& d d d: d=  d> 6e& d d d: d?  d@ 6e& d d d: dA  dB 6Z' e( j) f Z* e+ dC    Z, e+ dD    Z- e+ dE    Z. dF   Z/ dG   Z0 dH   Z1 dI   Z2 dJ   Z3 dK   Z4 dL   Z5 dM   Z6 dN   Z7 dO   Z8 dP   Z9 dQ   Z: dR   Z; dS   Z< dT   Z= RS(U   t
   spatialites/   ^(?P<major>\d)\.(?P<minor1>\d)\.(?P<minor2>\d+)t   Areat   Centroidt   Collectt	   MbrWithint
   DifferenceR   t   Envelopet   Extentt	   AsGeoJSONt   AsGMLt   Intersectiont   AsKMLt   GLengtht   MakeLinet   NumGeometriest	   NumPointst   PointOnSurfacet   ScaleCoordst   AsSVGt   SymDifferencet	   Transformt   ShiftCoordst   GUniont   GeomFromTextt   GeomFromWKBs
   AsText(%s)R   t   IsValidt   isvalidt   Equalst   equalst   Disjointt   disjointt   Touchest   touchest   Crossest   crossest   Withint   withint   Overlapst   overlapst   Containst   containst
   Intersectst
   intersectst   Relatet   relatet	   containedt   MbrContainst
   bbcontainst   MbrOverlapst
   bboverlapst   same_ast   exactt   PtDistWithint   dwithinR   t   >t   distance_gts   >=t   distance_gtet   <t   distance_lts   <=t   distance_ltec         C   s'   i d d 6d d 6d d 6d d 6d	 d
 6S(   Nt	   ST_Lengtht   Lengtht
   ST_Reverset   ReverseR3   t   Scalet   ST_Translatet	   Translatet   ST_Uniont   Union(    (   R   (    (    sV   /tmp/pip-unpacked-wheel-BAJOf3/django/contrib/gis/db/backends/spatialite/operations.pyt   function_namesi   s    c         C   s5   d d d h } |  j    s1 | d d d h O} n  | S(   Nt   BoundingCirclet   ForceRHRt   MemSizet   GeoHashR;   t	   MakeValid(   t   lwgeom_version(   R   t   unsupported(    (    sV   /tmp/pip-unpacked-wheel-BAJOf3/django/contrib/gis/db/backends/spatialite/operations.pyt   unsupported_functionss   s    c         C   s   y |  j    d } WnP t k
 rf } d |  j j d | f } t j t t |  t j   d  n X| d k  r t d   n  | S(	   s0   Determine the version of the SpatiaLite library.i   s   Cannot determine the SpatiaLite version for the "%s" database (error was "%s").  Was the SpatiaLite initialization SQL loaded on this database?t   NAMEi   i   i    s<   GeoDjango only supports SpatiaLite versions 4.0.0 and above.(   i   i    i    (	   t   spatialite_version_tuplet	   ExceptionR   t   settings_dictR   t   reraiseR   t   syst   exc_info(   R   t   versiont   msgt   new_msg(    (    sV   /tmp/pip-unpacked-wheel-BAJOf3/django/contrib/gis/db/backends/spatialite/operations.pyt   spatial_versionz   s    'c         C   sZ   | d k r d St | |  j } | d d  \ } } | d d  \ } } | | | | f S(   sV   
        Convert the polygon data received from SpatiaLite to min/max values.
        i    i   N(   t   NoneR   t   shell(   R   t   boxt   sridR|   t   xmint   ymint   xmaxt   ymax(    (    sV   /tmp/pip-unpacked-wheel-BAJOf3/django/contrib/gis/db/backends/spatialite/operations.pyt   convert_extent   s    c         C   s   d S(   s   
        Returns None because geometry columns are added via the
        `AddGeometryColumn` stored procedure on SpatiaLite.
        N(   R{   (   R   t   f(    (    sV   /tmp/pip-unpacked-wheel-BAJOf3/django/contrib/gis/db/backends/spatialite/operations.pyt   geo_db_type   s    c         K   s   | s
 g  S| d } t  | t  r | j |  j  r\ | d k rP t d   n  | j } q t | t j | j |  j    } n | } | g S(   sv   
        Returns the distance parameters for the given geometry field,
        lookup value, and lookup type.
        i    RW   sN   Only numeric values of degree units are allowed on geographic DWithin queries.(	   t
   isinstanceR   R   R   t
   ValueErrort   mt   getattrt   unit_attnamet
   units_name(   R   R   t   valuet   lookup_typet   kwargst
   dist_param(    (    sV   /tmp/pip-unpacked-wheel-BAJOf3/django/contrib/gis/db/backends/spatialite/operations.pyt   get_distance   s    
'c         C   s   d   } t  | d  rf | | | j  rC d |  j | j f } n d } | j |  \ } } | | S| | | j  r d |  j |  j | j | j f Sd |  j | j f Sd S(   s   
        Provides a proper substitution value for Geometries that are not in the
        SRID of the field.  Specifically, this routine will substitute in the
        Transform() and GeomFromText() function call(s).
        c         S   s   |  d  k p |  j | k S(   N(   R{   R~   (   R   R~   (    (    sV   /tmp/pip-unpacked-wheel-BAJOf3/django/contrib/gis/db/backends/spatialite/operations.pyt   transform_value   s    R   s   %s(%%s, %s)s   %ss   %s(%s(%%s,%s), %s)s
   %s(%%s,%s)N(   t   hasattrR~   t	   transformt   compilet	   from_text(   R   R   R   t   compilerR   t   placeholdert   sqlt   _(    (    sV   /tmp/pip-unpacked-wheel-BAJOf3/django/contrib/gis/db/backends/spatialite/operations.pyt   get_geom_placeholder   s    	 c         C   sF   |  j  j   } z! | j d |  | j   } Wd | j   X| d S(   s   
        Helper routine for calling SpatiaLite functions and returning
        their result.
        Any error occurring in this method should be handled by the caller.
        s	   SELECT %sNi    (   R   t   _cursort   executet   fetchonet   close(   R   R   t   cursort   row(    (    sV   /tmp/pip-unpacked-wheel-BAJOf3/django/contrib/gis/db/backends/spatialite/operations.pyt   _get_spatialite_func   s    c         C   s   |  j  d  S(   s;   Returns the version of GEOS used by SpatiaLite as a string.s   geos_version()(   R   (   R   (    (    sV   /tmp/pip-unpacked-wheel-BAJOf3/django/contrib/gis/db/backends/spatialite/operations.pyt   geos_version   s    c         C   s   |  j  d  S(   s=   Returns the version of the PROJ.4 library used by SpatiaLite.s   proj4_version()(   R   (   R   (    (    sV   /tmp/pip-unpacked-wheel-BAJOf3/django/contrib/gis/db/backends/spatialite/operations.pyt   proj4_version   s    c         C   s   |  j  d  S(   s8   Return the version of LWGEOM library used by SpatiaLite.s   lwgeom_version()(   R   (   R   (    (    sV   /tmp/pip-unpacked-wheel-BAJOf3/django/contrib/gis/db/backends/spatialite/operations.pyRm      s    c         C   s   |  j  d  S(   s3   Returns the SpatiaLite library version as a string.s   spatialite_version()(   R   (   R   (    (    sV   /tmp/pip-unpacked-wheel-BAJOf3/django/contrib/gis/db/backends/spatialite/operations.pyt   spatialite_version   s    c         C   s   |  j    } |  j j |  } | rf t | j d   } t | j d   } t | j d   } n t d |   | | | | f S(   sm   
        Returns the SpatiaLite version as a tuple (version string, major,
        minor, subminor).
        t   majort   minor1t   minor2s-   Could not parse SpatiaLite version string: %s(   R   t   version_regext   matcht   intt   groupRr   (   R   Rw   R   R   R   R   (    (    sV   /tmp/pip-unpacked-wheel-BAJOf3/django/contrib/gis/db/backends/spatialite/operations.pyRq      s    c         C   s1   | j    d k r d n	 | j    } t |  |  S(   ss   
        Returns the spatial aggregate SQL template and function for the
        given Aggregate instance.
        t   uniont   unionagg(   t   lowerR   (   R   t   agg_name(    (    sV   /tmp/pip-unpacked-wheel-BAJOf3/django/contrib/gis/db/backends/spatialite/operations.pyt   spatial_aggregate_name   s    $c         C   s   d d l  m } | S(   Ni(   t   SpatialiteGeometryColumns(   t0   django.contrib.gis.db.backends.spatialite.modelsR   (   R   R   (    (    sV   /tmp/pip-unpacked-wheel-BAJOf3/django/contrib/gis/db/backends/spatialite/operations.pyt   geometry_columns  s    c         C   s   d d l  m } | S(   Ni(   t   SpatialiteSpatialRefSys(   R   R   (   R   R   (    (    sV   /tmp/pip-unpacked-wheel-BAJOf3/django/contrib/gis/db/backends/spatialite/operations.pyt   spatial_ref_sys  s    c         C   sA   t  t |   j |  } t | j d  r= | j |  j  n  | S(   Nt	   geom_type(   R   R!   t   get_db_convertersR   R   t   appendt   convert_geometry(   R   t
   expressiont
   converters(    (    sV   /tmp/pip-unpacked-wheel-BAJOf3/django/contrib/gis/db/backends/spatialite/operations.pyR   	  s    c         C   s5   | r1 t  |  } d | k r1 | d | _ q1 n  | S(   Nt   transformed_srid(   R   R~   (   R   R   R   R   t   context(    (    sV   /tmp/pip-unpacked-wheel-BAJOf3/django/contrib/gis/db/backends/spatialite/operations.pyR     s
    (>   R   R    t   namet   TrueR"   t   reR   R   R   t   Adaptert   areat   centroidt   collectRO   t
   differencet   distancet   envelopet   extentt   geojsont   gmlt   intersectiont   kmlt   lengtht   makelinet   num_geomt
   num_pointst   point_on_surfacet   scalet   svgt   sym_differenceR   t	   translateR   R   R   t   from_wkbt   selectR   R
   t   gis_operatorsR   t   Extent3Dt   disallowed_aggregatesR	   Rg   Ro   Rz   R   R   R   R   R   R   R   Rm   R   Rq   R   R   R   R   R   (    (    (    sV   /tmp/pip-unpacked-wheel-BAJOf3/django/contrib/gis/db/backends/spatialite/operations.pyR!   %   s   
															(   t   __doc__R   Ru   t.   django.contrib.gis.db.backends.base.operationsR    t1   django.contrib.gis.db.backends.spatialite.adapterR   t$   django.contrib.gis.db.backends.utilsR   t   django.contrib.gis.db.modelsR   t#   django.contrib.gis.geometry.backendR   t   django.contrib.gis.measureR   t   django.core.exceptionsR   t%   django.db.backends.sqlite3.operationsR   t   django.utilsR   t   django.utils.functionalR	   R
   R!   (    (    (    sV   /tmp/pip-unpacked-wheel-BAJOf3/django/contrib/gis/db/backends/spatialite/operations.pyt   <module>   s   