PageRenderTime 26ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/django/contrib/gis/db/backends/spatialite/operations.py

https://github.com/postmates/django
Python | 342 lines | 291 code | 18 blank | 33 comment | 14 complexity | 863957bf60587715d98fb551e1fda472 MD5 | raw file
  1. import re
  2. from decimal import Decimal
  3. from django.contrib.gis.db.backends.base import BaseSpatialOperations
  4. from django.contrib.gis.db.backends.util import SpatialOperation, SpatialFunction
  5. from django.contrib.gis.db.backends.spatialite.adapter import SpatiaLiteAdapter
  6. from django.contrib.gis.geometry.backend import Geometry
  7. from django.contrib.gis.measure import Distance
  8. from django.core.exceptions import ImproperlyConfigured
  9. from django.db.backends.sqlite3.base import DatabaseOperations
  10. from django.db.utils import DatabaseError
  11. class SpatiaLiteOperator(SpatialOperation):
  12. "For SpatiaLite operators (e.g. `&&`, `~`)."
  13. def __init__(self, operator):
  14. super(SpatiaLiteOperator, self).__init__(operator=operator)
  15. class SpatiaLiteFunction(SpatialFunction):
  16. "For SpatiaLite function calls."
  17. def __init__(self, function, **kwargs):
  18. super(SpatiaLiteFunction, self).__init__(function, **kwargs)
  19. class SpatiaLiteFunctionParam(SpatiaLiteFunction):
  20. "For SpatiaLite functions that take another parameter."
  21. sql_template = '%(function)s(%(geo_col)s, %(geometry)s, %%s)'
  22. class SpatiaLiteDistance(SpatiaLiteFunction):
  23. "For SpatiaLite distance operations."
  24. dist_func = 'Distance'
  25. sql_template = '%(function)s(%(geo_col)s, %(geometry)s) %(operator)s %%s'
  26. def __init__(self, operator):
  27. super(SpatiaLiteDistance, self).__init__(self.dist_func,
  28. operator=operator)
  29. class SpatiaLiteRelate(SpatiaLiteFunctionParam):
  30. "For SpatiaLite Relate(<geom>, <pattern>) calls."
  31. pattern_regex = re.compile(r'^[012TF\*]{9}$')
  32. def __init__(self, pattern):
  33. if not self.pattern_regex.match(pattern):
  34. raise ValueError('Invalid intersection matrix pattern "%s".' % pattern)
  35. super(SpatiaLiteRelate, self).__init__('Relate')
  36. # Valid distance types and substitutions
  37. dtypes = (Decimal, Distance, float, int, long)
  38. def get_dist_ops(operator):
  39. "Returns operations for regular distances; spherical distances are not currently supported."
  40. return (SpatiaLiteDistance(operator),)
  41. class SpatiaLiteOperations(DatabaseOperations, BaseSpatialOperations):
  42. compiler_module = 'django.contrib.gis.db.backends.spatialite.compiler'
  43. name = 'spatialite'
  44. spatialite = True
  45. version_regex = re.compile(r'^(?P<major>\d)\.(?P<minor1>\d)\.(?P<minor2>\d+)')
  46. valid_aggregates = dict([(k, None) for k in ('Extent', 'Union')])
  47. Adapter = SpatiaLiteAdapter
  48. Adaptor = Adapter # Backwards-compatibility alias.
  49. area = 'Area'
  50. centroid = 'Centroid'
  51. contained = 'MbrWithin'
  52. difference = 'Difference'
  53. distance = 'Distance'
  54. envelope = 'Envelope'
  55. intersection = 'Intersection'
  56. length = 'GLength' # OpenGis defines Length, but this conflicts with an SQLite reserved keyword
  57. num_geom = 'NumGeometries'
  58. num_points = 'NumPoints'
  59. point_on_surface = 'PointOnSurface'
  60. scale = 'ScaleCoords'
  61. svg = 'AsSVG'
  62. sym_difference = 'SymDifference'
  63. transform = 'Transform'
  64. translate = 'ShiftCoords'
  65. union = 'GUnion' # OpenGis defines Union, but this conflicts with an SQLite reserved keyword
  66. unionagg = 'GUnion'
  67. from_text = 'GeomFromText'
  68. from_wkb = 'GeomFromWKB'
  69. select = 'AsText(%s)'
  70. geometry_functions = {
  71. 'equals' : SpatiaLiteFunction('Equals'),
  72. 'disjoint' : SpatiaLiteFunction('Disjoint'),
  73. 'touches' : SpatiaLiteFunction('Touches'),
  74. 'crosses' : SpatiaLiteFunction('Crosses'),
  75. 'within' : SpatiaLiteFunction('Within'),
  76. 'overlaps' : SpatiaLiteFunction('Overlaps'),
  77. 'contains' : SpatiaLiteFunction('Contains'),
  78. 'intersects' : SpatiaLiteFunction('Intersects'),
  79. 'relate' : (SpatiaLiteRelate, basestring),
  80. # Returns true if B's bounding box completely contains A's bounding box.
  81. 'contained' : SpatiaLiteFunction('MbrWithin'),
  82. # Returns true if A's bounding box completely contains B's bounding box.
  83. 'bbcontains' : SpatiaLiteFunction('MbrContains'),
  84. # Returns true if A's bounding box overlaps B's bounding box.
  85. 'bboverlaps' : SpatiaLiteFunction('MbrOverlaps'),
  86. # These are implemented here as synonyms for Equals
  87. 'same_as' : SpatiaLiteFunction('Equals'),
  88. 'exact' : SpatiaLiteFunction('Equals'),
  89. }
  90. distance_functions = {
  91. 'distance_gt' : (get_dist_ops('>'), dtypes),
  92. 'distance_gte' : (get_dist_ops('>='), dtypes),
  93. 'distance_lt' : (get_dist_ops('<'), dtypes),
  94. 'distance_lte' : (get_dist_ops('<='), dtypes),
  95. }
  96. geometry_functions.update(distance_functions)
  97. def __init__(self, connection):
  98. super(DatabaseOperations, self).__init__(connection)
  99. # Determine the version of the SpatiaLite library.
  100. try:
  101. vtup = self.spatialite_version_tuple()
  102. version = vtup[1:]
  103. if version < (2, 3, 0):
  104. raise ImproperlyConfigured('GeoDjango only supports SpatiaLite versions '
  105. '2.3.0 and above')
  106. self.spatial_version = version
  107. except ImproperlyConfigured:
  108. raise
  109. except Exception, msg:
  110. raise ImproperlyConfigured('Cannot determine the SpatiaLite version for the "%s" '
  111. 'database (error was "%s"). Was the SpatiaLite initialization '
  112. 'SQL loaded on this database?' %
  113. (self.connection.settings_dict['NAME'], msg))
  114. # Creating the GIS terms dictionary.
  115. gis_terms = ['isnull']
  116. gis_terms += self.geometry_functions.keys()
  117. self.gis_terms = dict([(term, None) for term in gis_terms])
  118. def check_aggregate_support(self, aggregate):
  119. """
  120. Checks if the given aggregate name is supported (that is, if it's
  121. in `self.valid_aggregates`).
  122. """
  123. agg_name = aggregate.__class__.__name__
  124. return agg_name in self.valid_aggregates
  125. def convert_geom(self, wkt, geo_field):
  126. """
  127. Converts geometry WKT returned from a SpatiaLite aggregate.
  128. """
  129. if wkt:
  130. return Geometry(wkt, geo_field.srid)
  131. else:
  132. return None
  133. def geo_db_type(self, f):
  134. """
  135. Returns None because geometry columnas are added via the
  136. `AddGeometryColumn` stored procedure on SpatiaLite.
  137. """
  138. return None
  139. def get_distance(self, f, value, lookup_type):
  140. """
  141. Returns the distance parameters for the given geometry field,
  142. lookup value, and lookup type. SpatiaLite only supports regular
  143. cartesian-based queries (no spheroid/sphere calculations for point
  144. geometries like PostGIS).
  145. """
  146. if not value:
  147. return []
  148. value = value[0]
  149. if isinstance(value, Distance):
  150. if f.geodetic(self.connection):
  151. raise ValueError('SpatiaLite does not support distance queries on '
  152. 'geometry fields with a geodetic coordinate system. '
  153. 'Distance objects; use a numeric value of your '
  154. 'distance in degrees instead.')
  155. else:
  156. dist_param = getattr(value, Distance.unit_attname(f.units_name(self.connection)))
  157. else:
  158. dist_param = value
  159. return [dist_param]
  160. def get_geom_placeholder(self, f, value):
  161. """
  162. Provides a proper substitution value for Geometries that are not in the
  163. SRID of the field. Specifically, this routine will substitute in the
  164. Transform() and GeomFromText() function call(s).
  165. """
  166. def transform_value(value, srid):
  167. return not (value is None or value.srid == srid)
  168. if hasattr(value, 'expression'):
  169. if transform_value(value, f.srid):
  170. placeholder = '%s(%%s, %s)' % (self.transform, f.srid)
  171. else:
  172. placeholder = '%s'
  173. # No geometry value used for F expression, substitue in
  174. # the column name instead.
  175. return placeholder % '%s.%s' % tuple(map(self.quote_name, value.cols[value.expression]))
  176. else:
  177. if transform_value(value, f.srid):
  178. # Adding Transform() to the SQL placeholder.
  179. return '%s(%s(%%s,%s), %s)' % (self.transform, self.from_text, value.srid, f.srid)
  180. else:
  181. return '%s(%%s,%s)' % (self.from_text, f.srid)
  182. def _get_spatialite_func(self, func):
  183. """
  184. Helper routine for calling SpatiaLite functions and returning
  185. their result.
  186. """
  187. cursor = self.connection._cursor()
  188. try:
  189. try:
  190. cursor.execute('SELECT %s' % func)
  191. row = cursor.fetchone()
  192. except:
  193. # Responsibility of caller to perform error handling.
  194. raise
  195. finally:
  196. cursor.close()
  197. return row[0]
  198. def geos_version(self):
  199. "Returns the version of GEOS used by SpatiaLite as a string."
  200. return self._get_spatialite_func('geos_version()')
  201. def proj4_version(self):
  202. "Returns the version of the PROJ.4 library used by SpatiaLite."
  203. return self._get_spatialite_func('proj4_version()')
  204. def spatialite_version(self):
  205. "Returns the SpatiaLite library version as a string."
  206. return self._get_spatialite_func('spatialite_version()')
  207. def spatialite_version_tuple(self):
  208. """
  209. Returns the SpatiaLite version as a tuple (version string, major,
  210. minor, subminor).
  211. """
  212. # Getting the SpatiaLite version.
  213. try:
  214. version = self.spatialite_version()
  215. except DatabaseError:
  216. # The `spatialite_version` function first appeared in version 2.3.1
  217. # of SpatiaLite, so doing a fallback test for 2.3.0 (which is
  218. # used by popular Debian/Ubuntu packages).
  219. version = None
  220. try:
  221. tmp = self._get_spatialite_func("X(GeomFromText('POINT(1 1)'))")
  222. if tmp == 1.0: version = '2.3.0'
  223. except DatabaseError:
  224. pass
  225. # If no version string defined, then just re-raise the original
  226. # exception.
  227. if version is None: raise
  228. m = self.version_regex.match(version)
  229. if m:
  230. major = int(m.group('major'))
  231. minor1 = int(m.group('minor1'))
  232. minor2 = int(m.group('minor2'))
  233. else:
  234. raise Exception('Could not parse SpatiaLite version string: %s' % version)
  235. return (version, major, minor1, minor2)
  236. def spatial_aggregate_sql(self, agg):
  237. """
  238. Returns the spatial aggregate SQL template and function for the
  239. given Aggregate instance.
  240. """
  241. agg_name = agg.__class__.__name__
  242. if not self.check_aggregate_support(agg):
  243. raise NotImplementedError('%s spatial aggregate is not implmented for this backend.' % agg_name)
  244. agg_name = agg_name.lower()
  245. if agg_name == 'union': agg_name += 'agg'
  246. sql_template = self.select % '%(function)s(%(field)s)'
  247. sql_function = getattr(self, agg_name)
  248. return sql_template, sql_function
  249. def spatial_lookup_sql(self, lvalue, lookup_type, value, field, qn):
  250. """
  251. Returns the SpatiaLite-specific SQL for the given lookup value
  252. [a tuple of (alias, column, db_type)], lookup type, lookup
  253. value, the model field, and the quoting function.
  254. """
  255. alias, col, db_type = lvalue
  256. # Getting the quoted field as `geo_col`.
  257. geo_col = '%s.%s' % (qn(alias), qn(col))
  258. if lookup_type in self.geometry_functions:
  259. # See if a SpatiaLite geometry function matches the lookup type.
  260. tmp = self.geometry_functions[lookup_type]
  261. # Lookup types that are tuples take tuple arguments, e.g., 'relate' and
  262. # distance lookups.
  263. if isinstance(tmp, tuple):
  264. # First element of tuple is the SpatiaLiteOperation instance, and the
  265. # second element is either the type or a tuple of acceptable types
  266. # that may passed in as further parameters for the lookup type.
  267. op, arg_type = tmp
  268. # Ensuring that a tuple _value_ was passed in from the user
  269. if not isinstance(value, (tuple, list)):
  270. raise ValueError('Tuple required for `%s` lookup type.' % lookup_type)
  271. # Geometry is first element of lookup tuple.
  272. geom = value[0]
  273. # Number of valid tuple parameters depends on the lookup type.
  274. if len(value) != 2:
  275. raise ValueError('Incorrect number of parameters given for `%s` lookup type.' % lookup_type)
  276. # Ensuring the argument type matches what we expect.
  277. if not isinstance(value[1], arg_type):
  278. raise ValueError('Argument type should be %s, got %s instead.' % (arg_type, type(value[1])))
  279. # For lookup type `relate`, the op instance is not yet created (has
  280. # to be instantiated here to check the pattern parameter).
  281. if lookup_type == 'relate':
  282. op = op(value[1])
  283. elif lookup_type in self.distance_functions:
  284. op = op[0]
  285. else:
  286. op = tmp
  287. geom = value
  288. # Calling the `as_sql` function on the operation instance.
  289. return op.as_sql(geo_col, self.get_geom_placeholder(field, geom))
  290. elif lookup_type == 'isnull':
  291. # Handling 'isnull' lookup type
  292. return "%s IS %sNULL" % (geo_col, (not value and 'NOT ' or ''))
  293. raise TypeError("Got invalid lookup_type: %s" % repr(lookup_type))
  294. # Routines for getting the OGC-compliant models.
  295. def geometry_columns(self):
  296. from django.contrib.gis.db.backends.spatialite.models import GeometryColumns
  297. return GeometryColumns
  298. def spatial_ref_sys(self):
  299. from django.contrib.gis.db.backends.spatialite.models import SpatialRefSys
  300. return SpatialRefSys