1 __all__ = ['create_test_spatial_db', 'get_geo_where_clause', 'SpatialBackend'] |
|
2 |
|
3 from ctypes.util import find_library |
|
4 from django.conf import settings |
|
5 from django.db.backends.signals import connection_created |
|
6 |
|
7 from django.contrib.gis.db.backend.base import BaseSpatialBackend |
|
8 from django.contrib.gis.db.backend.spatialite.adaptor import SpatiaLiteAdaptor |
|
9 from django.contrib.gis.db.backend.spatialite.creation import create_test_spatial_db |
|
10 from django.contrib.gis.db.backend.spatialite.field import SpatiaLiteField |
|
11 from django.contrib.gis.db.backend.spatialite.models import GeometryColumns, SpatialRefSys |
|
12 from django.contrib.gis.db.backend.spatialite.query import * |
|
13 |
|
14 # Here we are figuring out the path to the SpatiLite library (`libspatialite`). |
|
15 # If it's not in the system PATH, it may be set manually in the settings via |
|
16 # the `SPATIALITE_LIBRARY_PATH` setting. |
|
17 spatialite_lib = getattr(settings, 'SPATIALITE_LIBRARY_PATH', find_library('spatialite')) |
|
18 if spatialite_lib: |
|
19 def initialize_spatialite(sender=None, **kwargs): |
|
20 """ |
|
21 This function initializes the pysqlite2 connection to enable the |
|
22 loading of extensions, and to load up the SpatiaLite library |
|
23 extension. |
|
24 """ |
|
25 from django.db import connection |
|
26 connection.connection.enable_load_extension(True) |
|
27 connection.cursor().execute("SELECT load_extension(%s)", (spatialite_lib,)) |
|
28 connection_created.connect(initialize_spatialite) |
|
29 else: |
|
30 # No SpatiaLite library found. |
|
31 raise Exception('Unable to locate SpatiaLite, needed to use GeoDjango with sqlite3.') |
|
32 |
|
33 SpatialBackend = BaseSpatialBackend(name='spatialite', spatialite=True, |
|
34 area=AREA, |
|
35 centroid=CENTROID, |
|
36 contained=CONTAINED, |
|
37 difference=DIFFERENCE, |
|
38 distance=DISTANCE, |
|
39 distance_functions=DISTANCE_FUNCTIONS, |
|
40 envelope=ENVELOPE, |
|
41 from_text=GEOM_FROM_TEXT, |
|
42 gis_terms=SPATIALITE_TERMS, |
|
43 intersection=INTERSECTION, |
|
44 length=LENGTH, |
|
45 num_geom=NUM_GEOM, |
|
46 num_points=NUM_POINTS, |
|
47 point_on_surface=POINT_ON_SURFACE, |
|
48 scale=SCALE, |
|
49 select=GEOM_SELECT, |
|
50 svg=ASSVG, |
|
51 sym_difference=SYM_DIFFERENCE, |
|
52 transform=TRANSFORM, |
|
53 translate=TRANSLATE, |
|
54 union=UNION, |
|
55 unionagg=UNIONAGG, |
|
56 Adaptor=SpatiaLiteAdaptor, |
|
57 Field=SpatiaLiteField, |
|
58 GeometryColumns=GeometryColumns, |
|
59 SpatialRefSys=SpatialRefSys, |
|
60 ) |
|