PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

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