
 Xl)                 @   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 Gd d   d e e  Z d S)a  
SQL functions reference lists:
http://www.gaia-gis.it/spatialite-3.0.0-BETA/spatialite-sql-3.0.0.html
https://web.archive.org/web/20130407175746/http://www.gaia-gis.it/gaia-sins/spatialite-sql-4.0.0.html
http://www.gaia-gis.it/gaia-sins/spatialite-sql-4.2.1.html
    N)BaseSpatialOperations)SpatiaLiteAdapter)SpatialOperator)
aggregates)Geometry)Distance)ImproperlyConfigured)DatabaseOperations)six)cached_propertyc                   s  e  Z d  Z d Z d 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/  d0 6e% d d  d1 6e% d d2  d3 6e% d d4  d5 6e% d d  d6 6e% d d  d7 6e% d d	 d8 d9  d: 6e% d d	 d8 d;  d< 6e% d d	 d8 d=  d> 6e% d d	 d8 d?  d@ 6Z& e' j( f Z) e* dA dB    Z+ e* dC dD    Z, e* dE dF    Z- dG dH   Z. dI dJ   Z/ dK dL   Z0 dM dN   Z1 dO dP   Z2 dQ dR   Z3 dS dT   Z4 dU dV   Z5 dW dX   Z6 dY dZ   Z7 d[ d\   Z8 d] d^   Z9 d_ d`   Z: da db   Z;   f dc dd   Z< de df   Z=   S)gSpatiaLiteOperations
spatialiteTz/^(?P<major>\d)\.(?P<minor1>\d)\.(?P<minor2>\d+)ZAreaZCentroidZCollectZ	MbrWithinZ
Differencer   ZEnvelopeZExtentZ	AsGeoJSONZAsGMLZIntersectionZAsKMLZGLengthZMakeLineZNumGeometriesZ	NumPointsZPointOnSurfaceScaleCoordsZAsSVGZSymDifferenceZ	TransformShiftCoordsZGUnionZGeomFromTextZGeomFromWKBz
AsText(%s)funcZEqualsequalsZDisjointZdisjointZTouchesZtouchesZCrossesZcrossesZWithinZwithinZOverlapsZoverlapsZContainscontainsZ
IntersectsZ
intersectsZRelateZrelate	containedZMbrContainsZ
bbcontainsZMbrOverlapsZ
bboverlapsZsame_asexactop>Zdistance_gtz>=Zdistance_gte<Zdistance_ltz<=Zdistance_ltec             C   s<   i d d 6d d 6d d 6|  j  d k r- d
 n d d 6d d 6S)NZ	ST_LengthZLengthZ
ST_ReverseReverser   ZScale      r   ZST_Translater   Z	TranslateZST_UnionUnion)r   r   r   )spatial_version)self r   b/home/ubuntu/projects/ifolica/build/django/django/contrib/gis/db/backends/spatialite/operations.pyfunction_namesW   s    z#SpatiaLiteOperations.function_namesc             C   sy   d d d d d h } |  j  d k  r4 | j d	  n  |  j  d k  rY | j d d h  n |  j   su | j d  n  | S)NZBoundingCircleZForceRHRZIsValidZ	MakeValidZMemSizer   r   r   Z
SnapToGrid   Z	Perimeterr   ZGeoHash)r   r   r   )r!   r   r   )r   addupdatelwgeom_version)r   Zunsupportedr   r   r   unsupported_functionsa   s    z*SpatiaLiteOperations.unsupported_functionsc             C   s   y |  j    d d  } Wnb t k
 r~ } zB d |  j j d | f } t j t t |  t j   d  WYd d } ~ Xn X| d	 k  r t d   n  | S)
z0Determine the version of the SpatiaLite library.r   NzCannot determine the SpatiaLite version for the "%s" database (error was "%s").  Was the SpatiaLite initialization SQL loaded on this database?NAME   r   r   z<GeoDjango only supports SpatiaLite versions 3.0.0 and above.)r   r   r   )	spatialite_version_tuple	Exception
connectionZsettings_dictr
   reraiser   sysexc_info)r   versionmsgZnew_msgr   r   r   r   l   s    6z$SpatiaLiteOperations.spatial_versionc             C   sf   | d k r d St  | |  j } | d d d  \ } } | d d d  \ } } | | | | f S)zV
        Convert the polygon data received from SpatiaLite to min/max values.
        Nr   r'   )r   shell)r   Zboxsridr0   ZxminZyminZxmaxZymaxr   r   r   convert_extent{   s    z#SpatiaLiteOperations.convert_extentc             C   s   | r t  | | j  Sd Sd S)zM
        Converts geometry WKT returned from a SpatiaLite aggregate.
        N)r   r1   )r   ZwktZ	geo_fieldr   r   r   convert_geom   s    z!SpatiaLiteOperations.convert_geomc             C   s   d S)z
        Returns None because geometry columns are added via the
        `AddGeometryColumn` stored procedure on SpatiaLite.
        Nr   )r   fr   r   r   geo_db_type   s    z SpatiaLiteOperations.geo_db_typec             K   sx   | s
 g  S| d } t  | t  rk | j |  j  rD t d   qq t | t j | j |  j    } n | } | g S)a  
        Returns the distance parameters for the given geometry field,
        lookup value, and lookup type.  SpatiaLite only supports regular
        cartesian-based queries (no spheroid/sphere calculations for point
        geometries like PostGIS).
        r   zSpatiaLite does not support distance queries on geometry fields with a geodetic coordinate system. Distance objects; use a numeric value of your distance in degrees instead.)
isinstancer   Zgeodeticr*   
ValueErrorgetattrZunit_attnameZ
units_name)r   r4   valueZlookup_typekwargsZ
dist_paramr   r   r   get_distance   s    
'z!SpatiaLiteOperations.get_distancec             C   s   d d   } t  | d  ri | | | j  rF d |  j | j f } n d } | j |  \ } } | | S| | | j  r d |  j |  j | j | j f Sd |  j | j f Sd S)	z
        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)r1   )r9   r1   r   r   r   transform_value   s    zBSpatiaLiteOperations.get_geom_placeholder.<locals>.transform_valueZas_sqlz%s(%%s, %s)z%sz%s(%s(%%s,%s), %s)z
%s(%%s,%s)N)hasattrr1   	transformcompile	from_text)r   r4   r9   compilerr<   placeholderZsql_r   r   r   get_geom_placeholder   s     z)SpatiaLiteOperations.get_geom_placeholderc          
   C   sF   |  j  j   } z! | j d |  | j   } Wd | j   X| d S)z
        Helper routine for calling SpatiaLite functions and returning
        their result.
        Any error occurring in this method should be handled by the caller.
        z	SELECT %sNr   )r*   Z_cursorexecuteZfetchoneclose)r   r   cursorrowr   r   r   _get_spatialite_func   s    z)SpatiaLiteOperations._get_spatialite_funcc             C   s   |  j  d  S)z;Returns the version of GEOS used by SpatiaLite as a string.zgeos_version())rI   )r   r   r   r   geos_version   s    z!SpatiaLiteOperations.geos_versionc             C   s   |  j  d  S)z=Returns the version of the PROJ.4 library used by SpatiaLite.zproj4_version())rI   )r   r   r   r   proj4_version   s    z"SpatiaLiteOperations.proj4_versionc             C   s   |  j  d  S)z8Return the version of LWGEOM library used by SpatiaLite.zlwgeom_version())rI   )r   r   r   r   r$      s    z#SpatiaLiteOperations.lwgeom_versionc             C   s   |  j  d  S)z3Returns the SpatiaLite library version as a string.zspatialite_version())rI   )r   r   r   r   spatialite_version   s    z'SpatiaLiteOperations.spatialite_versionc             C   s   |  j    } |  j j |  } | rf t | j d   } t | j d   } t | j d   } n t d |   | | | | f S)zm
        Returns the SpatiaLite version as a tuple (version string, major,
        minor, subminor).
        majorminor1minor2z-Could not parse SpatiaLite version string: %s)rL   version_regexmatchintgroupr)   )r   r.   mrM   rN   rO   r   r   r   r(      s    z-SpatiaLiteOperations.spatialite_version_tuplec             C   s1   | j    d k r d n	 | j    } t |  |  S)zs
        Returns the spatial aggregate SQL template and function for the
        given Aggregate instance.
        unionunionagg)lowerr8   )r   Zagg_namer   r   r   spatial_aggregate_name   s    $z+SpatiaLiteOperations.spatial_aggregate_namec             C   s   d d l  m } | S)Nr   )SpatialiteGeometryColumns)0django.contrib.gis.db.backends.spatialite.modelsrY   )r   rY   r   r   r   geometry_columns   s    z%SpatiaLiteOperations.geometry_columnsc             C   s   d d l  m } | S)Nr   )SpatialiteSpatialRefSys)rZ   r\   )r   r\   r   r   r   spatial_ref_sys   s    z$SpatiaLiteOperations.spatial_ref_sysc                sA   t  t |   j |  } t | j d  r= | j |  j  n  | S)NZ	geom_type)superr   get_db_convertersr=   Zoutput_fieldappendconvert_geometry)r   
expressionZ
converters)	__class__r   r   r_     s    z&SpatiaLiteOperations.get_db_convertersc             C   s5   | r1 t  |  } d | k r1 | d | _ q1 n  | S)NZtransformed_srid)r   r1   )r   r9   rb   r*   contextr   r   r   ra   
  s
    z%SpatiaLiteOperations.convert_geometry)>__name__
__module____qualname__namer   rer?   rP   r   ZAdapterareaZcentroidZcollectr   
differenceZdistanceZenvelopeZextentZgeojsonZgmlintersectionZkmllengthZmakelineZnum_geomZ
num_pointsZpoint_on_surfaceZscalesvgZsym_differencer>   	translaterU   rV   r@   Zfrom_wkbselectr   Zgis_operatorsr   ZExtent3DZdisallowed_aggregatesr   r    r%   r   r2   r3   r5   r;   rD   rI   rJ   rK   r$   rL   r(   rX   r[   r]   r_   ra   r   r   )rc   r   r      s   
		r   )__doc__ri   r,   Z.django.contrib.gis.db.backends.base.operationsr   Z1django.contrib.gis.db.backends.spatialite.adapterr   Z$django.contrib.gis.db.backends.utilsr   Zdjango.contrib.gis.db.modelsr   Z#django.contrib.gis.geometry.backendr   Zdjango.contrib.gis.measurer   Zdjango.core.exceptionsr   Z%django.db.backends.sqlite3.operationsr	   Zdjango.utilsr
   Zdjango.utils.functionalr   r   r   r   r   r   <module>   s   