spatialite.txt 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. .. _spatialite:
  2. =====================
  3. Installing Spatialite
  4. =====================
  5. `SpatiaLite`__ adds spatial support to SQLite, turning it into a full-featured
  6. spatial database.
  7. Check first if you can install Spatialite from system packages or binaries. For
  8. example, on Debian-based distributions, try to install the ``spatialite-bin``
  9. package. For Mac OS X, follow the
  10. :ref:`specific instructions below<spatialite_macosx>`. For Windows, you may
  11. find binaries on `Gaia-SINS`__ home page. In any case, you should always
  12. be able to :ref:`install from source<spatialite_source>`.
  13. When you are done with the installation process, skip to :ref:`create_spatialite_db`.
  14. __ https://www.gaia-gis.it/fossil/libspatialite
  15. __ http://www.gaia-gis.it/gaia-sins/
  16. .. _spatialite_source:
  17. Installing from source
  18. ~~~~~~~~~~~~~~~~~~~~~~
  19. :ref:`GEOS and PROJ.4<geospatial_libs>` should be installed prior to building
  20. SpatiaLite.
  21. SQLite
  22. ^^^^^^
  23. Check first if SQLite is compiled with the `R*Tree module`__. Run the sqlite3
  24. command line interface and enter the following query::
  25. sqlite> CREATE VIRTUAL TABLE testrtree USING rtree(id,minX,maxX,minY,maxY);
  26. If you obtain an error, you will have to recompile SQLite from source. Otherwise,
  27. just skip this section.
  28. To install from sources, download the latest amalgamation source archive from
  29. the `SQLite download page`__, and extract::
  30. $ wget http://sqlite.org/sqlite-amalgamation-3.6.23.1.tar.gz
  31. $ tar xzf sqlite-amalgamation-3.6.23.1.tar.gz
  32. $ cd sqlite-3.6.23.1
  33. Next, run the ``configure`` script -- however the ``CFLAGS`` environment variable
  34. needs to be customized so that SQLite knows to build the R*Tree module::
  35. $ CFLAGS="-DSQLITE_ENABLE_RTREE=1" ./configure
  36. $ make
  37. $ sudo make install
  38. $ cd ..
  39. __ http://www.sqlite.org/rtree.html
  40. __ http://www.sqlite.org/download.html
  41. .. _spatialitebuild:
  42. SpatiaLite library (``libspatialite``) and tools (``spatialite``)
  43. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  44. Get the latest SpatiaLite library source and tools bundle from the
  45. `download page`__::
  46. $ wget http://www.gaia-gis.it/gaia-sins/libspatialite-sources/libspatialite-amalgamation-2.4.0-5.tar.gz
  47. $ wget http://www.gaia-gis.it/gaia-sins/spatialite-tools-sources/spatialite-tools-2.4.0-5.tar.gz
  48. $ tar xzf libspatialite-amalgamation-2.4.0-5.tar.gz
  49. $ tar xzf spatialite-tools-2.4.0-5.tar.gz
  50. Prior to attempting to build, please read the important notes below to see if
  51. customization of the ``configure`` command is necessary. If not, then run the
  52. ``configure`` script, make, and install for the SpatiaLite library::
  53. $ cd libspatialite-amalgamation-2.3.1
  54. $ ./configure # May need to be modified, see notes below.
  55. $ make
  56. $ sudo make install
  57. $ cd ..
  58. .. _spatialite_tools:
  59. Finally, do the same for the SpatiaLite tools::
  60. $ cd spatialite-tools-2.3.1
  61. $ ./configure # May need to be modified, see notes below.
  62. $ make
  63. $ sudo make install
  64. $ cd ..
  65. .. note::
  66. If you've installed GEOS and PROJ.4 from binary packages, you will have to specify
  67. their paths when running the ``configure`` scripts for *both* the library and the
  68. tools (the configure scripts look, by default, in ``/usr/local``). For example,
  69. on Debian/Ubuntu distributions that have GEOS and PROJ.4 packages, the command would be::
  70. $ ./configure --with-proj-include=/usr/include --with-proj-lib=/usr/lib --with-geos-include=/usr/include --with-geos-lib=/usr/lib
  71. .. note::
  72. For Mac OS X users building from source, the SpatiaLite library *and* tools
  73. need to have their ``target`` configured::
  74. $ ./configure --target=macosx
  75. __ http://www.gaia-gis.it/gaia-sins/libspatialite-sources/
  76. .. _pysqlite2:
  77. pysqlite2
  78. ^^^^^^^^^
  79. If you've decided to use a :ref:`newer version of pysqlite2
  80. <using-newer-versions-of-pysqlite>` instead of the ``sqlite3`` Python stdlib
  81. module, then you need to make sure it can load external extensions (i.e. the
  82. required ``enable_load_extension`` method is available so ``SpatiaLite`` can be
  83. loaded).
  84. This might involve building it yourself. For this, download pysqlite2 2.6, and
  85. untar::
  86. $ wget https://pypi.python.org/packages/source/p/pysqlite/pysqlite-2.6.3.tar.gz
  87. $ tar xzf pysqlite-2.6.3.tar.gz
  88. $ cd pysqlite-2.6.3
  89. Next, use a text editor to edit the ``setup.cfg`` file to look like the
  90. following:
  91. .. code-block:: ini
  92. [build_ext]
  93. #define=
  94. include_dirs=/usr/local/include
  95. library_dirs=/usr/local/lib
  96. libraries=sqlite3
  97. #define=SQLITE_OMIT_LOAD_EXTENSION
  98. or if you are on Mac OS X:
  99. .. code-block:: ini
  100. [build_ext]
  101. #define=
  102. include_dirs=/Library/Frameworks/SQLite3.framework/unix/include
  103. library_dirs=/Library/Frameworks/SQLite3.framework/unix/lib
  104. libraries=sqlite3
  105. #define=SQLITE_OMIT_LOAD_EXTENSION
  106. .. note::
  107. The important thing here is to make sure you comment out the
  108. ``define=SQLITE_OMIT_LOAD_EXTENSION`` flag and that the ``include_dirs``
  109. and ``library_dirs`` settings are uncommented and set to the appropriate
  110. path if the SQLite header files and libraries are not in ``/usr/include``
  111. and ``/usr/lib``, respectively.
  112. After modifying ``setup.cfg`` appropriately, then run the ``setup.py`` script
  113. to build and install::
  114. $ sudo python setup.py install
  115. .. _spatialite_macosx:
  116. Mac OS X-specific instructions
  117. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  118. Mac OS X users should follow the instructions in the :ref:`kyngchaos` section,
  119. as it is much easier than building from source.
  120. When :ref:`create_spatialite_db`, the ``spatialite`` program is required.
  121. However, instead of attempting to compile the SpatiaLite tools from source,
  122. download the `SpatiaLite Binaries`__ for OS X, and install ``spatialite`` in a
  123. location available in your ``PATH``. For example::
  124. $ curl -O http://www.gaia-gis.it/spatialite/spatialite-tools-osx-x86-2.3.1.tar.gz
  125. $ tar xzf spatialite-tools-osx-x86-2.3.1.tar.gz
  126. $ cd spatialite-tools-osx-x86-2.3.1/bin
  127. $ sudo cp spatialite /Library/Frameworks/SQLite3.framework/Programs
  128. Finally, for GeoDjango to be able to find the KyngChaos SpatiaLite library,
  129. add the following to your ``settings.py``:
  130. .. code-block:: python
  131. SPATIALITE_LIBRARY_PATH='/Library/Frameworks/SQLite3.framework/SQLite3'
  132. __ http://www.gaia-gis.it/spatialite-2.3.1/binaries.html
  133. .. _create_spatialite_db:
  134. Creating a spatial database for SpatiaLite
  135. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  136. After you've installed SpatiaLite, you'll need to create a number of spatial
  137. metadata tables in your database in order to perform spatial queries.
  138. If you're using SpatiaLite 2.4 or newer, use the ``spatialite`` utility to
  139. call the ``InitSpatialMetaData()`` function, like this::
  140. $ spatialite geodjango.db "SELECT InitSpatialMetaData();"
  141. the SPATIAL_REF_SYS table already contains some row(s)
  142. InitSpatiaMetaData ()error:"table spatial_ref_sys already exists"
  143. 0
  144. You can safely ignore the error messages shown. When you've done this, you can
  145. skip the rest of this section.
  146. If you're using SpatiaLite 2.3, you'll need to download a
  147. database-initialization file and execute its SQL queries in your database.
  148. First, get it from the `SpatiaLite Resources`__ page::
  149. $ wget http://www.gaia-gis.it/spatialite-2.3.1/init_spatialite-2.3.sql.gz
  150. $ gunzip init_spatialite-2.3.sql.gz
  151. Then, use the ``spatialite`` command to initialize a spatial database::
  152. $ spatialite geodjango.db < init_spatialite-2.3.sql
  153. .. note::
  154. The parameter ``geodjango.db`` is the *filename* of the SQLite database
  155. you want to use. Use the same in the :setting:`DATABASES` ``"name"`` key
  156. inside your ``settings.py``.
  157. __ http://www.gaia-gis.it/spatialite-2.3.1/resources.html