<s> import re <EOL> from decimal import Decimal <EOL> from django . contrib . gis . db . backends . base import BaseSpatialOperations <EOL> from django . contrib . gis . db . backends . util import SpatialOperation , SpatialFunction <EOL> from django . contrib . gis . db . backends . spatialite . adapter import SpatiaLiteAdapter <EOL> from django . contrib . gis . geometry . backend import Geometry <EOL> from django . contrib . gis . measure import Distance <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> from django . db . backends . sqlite3 . base import DatabaseOperations <EOL> from django . db . utils import DatabaseError <EOL> class SpatiaLiteOperator ( SpatialOperation ) : <EOL> "<STR_LIT>" <EOL> def __init__ ( self , operator ) : <EOL> super ( SpatiaLiteOperator , self ) . __init__ ( operator = operator ) <EOL> class SpatiaLiteFunction ( SpatialFunction ) : <EOL> "<STR_LIT>" <EOL> def __init__ ( self , function , ** kwargs ) : <EOL> super ( SpatiaLiteFunction , self ) . __init__ ( function , ** kwargs ) <EOL> class SpatiaLiteFunctionParam ( SpatiaLiteFunction ) : <EOL> "<STR_LIT>" <EOL> sql_template = '<STR_LIT>' <EOL> class SpatiaLiteDistance ( SpatiaLiteFunction ) : <EOL> "<STR_LIT>" <EOL> dist_func = '<STR_LIT>' <EOL> sql_template = '<STR_LIT>' <EOL> def __init__ ( self , operator ) : <EOL> super ( SpatiaLiteDistance , self ) . __init__ ( self . dist_func , <EOL> operator = operator ) <EOL> class SpatiaLiteRelate ( SpatiaLiteFunctionParam ) : <EOL> "<STR_LIT>" <EOL> pattern_regex = re . compile ( r'<STR_LIT>' ) <EOL> def __init__ ( self , pattern ) : <EOL> if not self . pattern_regex . match ( pattern ) : <EOL> raise ValueError ( '<STR_LIT>' % pattern ) <EOL> super ( SpatiaLiteRelate , self ) . __init__ ( '<STR_LIT>' ) <EOL> dtypes = ( Decimal , Distance , float , int , long ) <EOL> def get_dist_ops ( operator ) : <EOL> "<STR_LIT>" <EOL> return ( SpatiaLiteDistance ( operator ) , ) <EOL> class SpatiaLiteOperations ( DatabaseOperations , BaseSpatialOperations ) : <EOL> compiler_module = '<STR_LIT>' <EOL> name = '<STR_LIT>' <EOL> spatialite = True <EOL> version_regex = re . compile ( r'<STR_LIT>' ) <EOL> valid_aggregates = dict ( [ ( k , None ) for k in ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> Adapter = SpatiaLiteAdapter <EOL> Adaptor = Adapter <EOL> area = '<STR_LIT>' <EOL> centroid = '<STR_LIT>' <EOL> contained = '<STR_LIT>' <EOL> difference = '<STR_LIT>' <EOL> distance = '<STR_LIT>' <EOL> envelope = '<STR_LIT>' <EOL> intersection = '<STR_LIT>' <EOL> length = '<STR_LIT>' <EOL> num_geom = '<STR_LIT>' <EOL> num_points = '<STR_LIT>' <EOL> point_on_surface = '<STR_LIT>' <EOL> scale = '<STR_LIT>' <EOL> svg = '<STR_LIT>' <EOL> sym_difference = '<STR_LIT>' <EOL> transform = '<STR_LIT>' <EOL> translate = '<STR_LIT>' <EOL> union = '<STR_LIT>' <EOL> unionagg = '<STR_LIT>' <EOL> from_text = '<STR_LIT>' <EOL> from_wkb = '<STR_LIT>' <EOL> select = '<STR_LIT>' <EOL> geometry_functions = { <EOL> '<STR_LIT>' : SpatiaLiteFunction ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : SpatiaLiteFunction ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : SpatiaLiteFunction ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : SpatiaLiteFunction ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : SpatiaLiteFunction ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : SpatiaLiteFunction ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : SpatiaLiteFunction ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : SpatiaLiteFunction ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : ( SpatiaLiteRelate , basestring ) , <EOL> '<STR_LIT>' : SpatiaLiteFunction ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : SpatiaLiteFunction ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : SpatiaLiteFunction ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : SpatiaLiteFunction ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : SpatiaLiteFunction ( '<STR_LIT>' ) , <EOL> } <EOL> distance_functions = { <EOL> '<STR_LIT>' : ( get_dist_ops ( '<STR_LIT:>>' ) , dtypes ) , <EOL> '<STR_LIT>' : ( get_dist_ops ( '<STR_LIT>' ) , dtypes ) , <EOL> '<STR_LIT>' : ( get_dist_ops ( '<STR_LIT:<>' ) , dtypes ) , <EOL> '<STR_LIT>' : ( get_dist_ops ( '<STR_LIT>' ) , dtypes ) , <EOL> } <EOL> geometry_functions . update ( distance_functions ) <EOL> def __init__ ( self , connection ) : <EOL> super ( DatabaseOperations , self ) . __init__ ( connection ) <EOL> try : <EOL> vtup = self . spatialite_version_tuple ( ) <EOL> version = vtup [ <NUM_LIT:1> : ] <EOL> if version < ( <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:0> ) : <EOL> raise ImproperlyConfigured ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . spatial_version = version <EOL> except ImproperlyConfigured : <EOL> raise <EOL> except Exception , msg : <EOL> raise ImproperlyConfigured ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> ( self . connection . settings_dict [ '<STR_LIT>' ] , msg ) ) <EOL> gis_terms = [ '<STR_LIT>' ] <EOL> gis_terms += self . geometry_functions . keys ( ) <EOL> self . gis_terms = dict ( [ ( term , None ) for term in gis_terms ] ) <EOL> if version >= ( <NUM_LIT:2> , <NUM_LIT:4> , <NUM_LIT:0> ) : <EOL> try : <EOL> self . _get_spatialite_func ( "<STR_LIT>" ) <EOL> self . gml = '<STR_LIT>' <EOL> self . kml = '<STR_LIT>' <EOL> except DatabaseError : <EOL> pass <EOL> def check_aggregate_support ( self , aggregate ) : <EOL> """<STR_LIT>""" <EOL> agg_name = aggregate . __class__ . __name__ <EOL> return agg_name in self . valid_aggregates <EOL> def convert_geom ( self , wkt , geo_field ) : <EOL> """<STR_LIT>""" <EOL> if wkt : <EOL> return Geometry ( wkt , geo_field . srid ) <EOL> else : <EOL> return None <EOL> def geo_db_type ( self , f ) : <EOL> """<STR_LIT>""" <EOL> return None <EOL> def get_distance ( self , f , value , lookup_type ) : <EOL> """<STR_LIT>""" <EOL> if not value : <EOL> return [ ] <EOL> value = value [ <NUM_LIT:0> ] <EOL> if isinstance ( value , Distance ) : <EOL> if f . geodetic ( self . connection ) : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> else : <EOL> dist_param = getattr ( value , Distance . unit_attname ( f . units_name ( self . connection ) ) ) <EOL> else : <EOL> dist_param = value <EOL> return [ dist_param ] <EOL> def get_geom_placeholder ( self , f , value ) : <EOL> """<STR_LIT>""" <EOL> def transform_value ( value , srid ) : <EOL> return not ( value is None or value . srid == srid ) <EOL> if hasattr ( value , '<STR_LIT>' ) : <EOL> if transform_value ( value , f . srid ) : <EOL> placeholder = '<STR_LIT>' % ( self . transform , f . srid ) <EOL> else : <EOL> placeholder = '<STR_LIT:%s>' <EOL> return placeholder % '<STR_LIT>' % tuple ( map ( self . quote_name , value . cols [ value . expression ] ) ) <EOL> else : <EOL> if transform_value ( value , f . srid ) : <EOL> return '<STR_LIT>' % ( self . transform , self . from_text , value . srid , f . srid ) <EOL> else : <EOL> return '<STR_LIT>' % ( self . from_text , f . srid ) <EOL> def _get_spatialite_func ( self , func ) : <EOL> """<STR_LIT>""" <EOL> cursor = self . connection . _cursor ( ) <EOL> try : <EOL> try : <EOL> cursor . execute ( '<STR_LIT>' % func ) <EOL> row = cursor . fetchone ( ) <EOL> except : <EOL> raise <EOL> finally : <EOL> cursor . close ( ) <EOL> return row [ <NUM_LIT:0> ] <EOL> def geos_version ( self ) : <EOL> "<STR_LIT>" <EOL> return self . _get_spatialite_func ( '<STR_LIT>' ) <EOL> def proj4_version ( self ) : <EOL> "<STR_LIT>" <EOL> return self . _get_spatialite_func ( '<STR_LIT>' ) <EOL> def spatialite_version ( self ) : <EOL> "<STR_LIT>" <EOL> return self . _get_spatialite_func ( '<STR_LIT>' ) <EOL> def spatialite_version_tuple ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> version = self . spatialite_version ( ) <EOL> except DatabaseError : <EOL> version = None <EOL> try : <EOL> tmp = self . _get_spatialite_func ( "<STR_LIT>" ) <EOL> if tmp == <NUM_LIT:1.0> : version = '<STR_LIT>' <EOL> except DatabaseError : <EOL> pass <EOL> if version is None : raise <EOL> m = self . version_regex . match ( version ) <EOL> if m : <EOL> major = int ( m . group ( '<STR_LIT>' ) ) <EOL> minor1 = int ( m . group ( '<STR_LIT>' ) ) <EOL> minor2 = int ( m . group ( '<STR_LIT>' ) ) <EOL> else : <EOL> raise Exception ( '<STR_LIT>' % version ) <EOL> return ( version , major , minor1 , minor2 ) <EOL> def spatial_aggregate_sql ( self , agg ) : <EOL> """<STR_LIT>""" <EOL> agg_name = agg . __class__ . __name__ <EOL> if not self . check_aggregate_support ( agg ) : <EOL> raise NotImplementedError ( '<STR_LIT>' % agg_name ) <EOL> agg_name = agg_name . lower ( ) <EOL> if agg_name == '<STR_LIT>' : agg_name += '<STR_LIT>' <EOL> sql_template = self . select % '<STR_LIT>' <EOL> sql_function = getattr ( self , agg_name ) <EOL> return sql_template , sql_function <EOL> def spatial_lookup_sql ( self , lvalue , lookup_type , value , field , qn ) : <EOL> """<STR_LIT>""" <EOL> alias , col , db_type = lvalue <EOL> geo_col = '<STR_LIT>' % ( qn ( alias ) , qn ( col ) ) <EOL> if lookup_type in self . geometry_functions : <EOL> tmp = self . geometry_functions [ lookup_type ] <EOL> if isinstance ( tmp , tuple ) : <EOL> op , arg_type = tmp <EOL> if not isinstance ( value , ( tuple , list ) ) : <EOL> raise ValueError ( '<STR_LIT>' % lookup_type ) <EOL> geom = value [ <NUM_LIT:0> ] <EOL> if len ( value ) != <NUM_LIT:2> : <EOL> raise ValueError ( '<STR_LIT>' % lookup_type ) <EOL> if not isinstance ( value [ <NUM_LIT:1> ] , arg_type ) : <EOL> raise ValueError ( '<STR_LIT>' % ( arg_type , type ( value [ <NUM_LIT:1> ] ) ) ) <EOL> if lookup_type == '<STR_LIT>' : <EOL> op = op ( value [ <NUM_LIT:1> ] ) <EOL> elif lookup_type in self . distance_functions : <EOL> op = op [ <NUM_LIT:0> ] <EOL> else : <EOL> op = tmp <EOL> geom = value <EOL> return op . as_sql ( geo_col , self . get_geom_placeholder ( field , geom ) ) <EOL> elif lookup_type == '<STR_LIT>' : <EOL> return "<STR_LIT>" % ( geo_col , ( not value and '<STR_LIT>' or '<STR_LIT>' ) ) <EOL> raise TypeError ( "<STR_LIT>" % repr ( lookup_type ) ) <EOL> def geometry_columns ( self ) : <EOL> from django . contrib . gis . db . backends . spatialite . models import GeometryColumns <EOL> return GeometryColumns <EOL> def spatial_ref_sys ( self ) : <EOL> from django . contrib . gis . db . backends . spatialite . models import SpatialRefSys <EOL> return SpatialRefSys </s>
