spatialite.txt 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 modified, see notes below.
  55. $ make
  56. $ sudo make install
  57. $ cd .... _spatialite
  58. Finally, do the same for the SpatiaLite tools::
  59. $ cd spatialite-tools-2.3.1
  60. $ ./configure # May need to modified, see notes below.
  61. $ make
  62. $ sudo make install
  63. $ cd ..
  64. .. note::
  65. If you've installed GEOS and PROJ.4 from binary packages, you will have to specify
  66. their paths when running the ``configure`` scripts for *both* the library and the
  67. tools (the configure scripts look, by default, in ``/usr/local``). For example,
  68. on Debian/Ubuntu distributions that have GEOS and PROJ.4 packages, the command would be::
  69. $ ./configure --with-proj-include=/usr/include --with-proj-lib=/usr/lib --with-geos-include=/usr/include --with-geos-lib=/usr/lib
  70. .. note::
  71. For Mac OS X users building from source, the SpatiaLite library *and* tools
  72. need to have their ``target`` configured::
  73. $ ./configure --target=macosx
  74. __ http://www.gaia-gis.it/gaia-sins/libspatialite-sources/
  75. .. _pysqlite2:
  76. pysqlite2
  77. ^^^^^^^^^
  78. If you are on Python 2.6, you will also have to compile pysqlite2, because
  79. ``SpatiaLite`` must be loaded as an external extension, and the required
  80. ``enable_load_extension`` method is only available in versions 2.5+ of
  81. pysqlite2. Thus, download pysqlite2 2.6, and untar::
  82. $ wget http://pysqlite.googlecode.com/files/pysqlite-2.6.3.tar.gz
  83. $ tar xzf pysqlite-2.6.3.tar.gz
  84. $ cd pysqlite-2.6.3
  85. Next, use a text editor (e.g., ``emacs`` or ``vi``) to edit the ``setup.cfg`` file
  86. to look like the following:
  87. .. code-block:: ini
  88. [build_ext]
  89. #define=
  90. include_dirs=/usr/local/include
  91. library_dirs=/usr/local/lib
  92. libraries=sqlite3
  93. #define=SQLITE_OMIT_LOAD_EXTENSION
  94. or if you are on Mac OS X:
  95. .. code-block:: ini
  96. [build_ext]
  97. #define=
  98. include_dirs=/Library/Frameworks/SQLite3.framework/unix/include
  99. library_dirs=/Library/Frameworks/SQLite3.framework/unix/lib
  100. libraries=sqlite3
  101. #define=SQLITE_OMIT_LOAD_EXTENSION
  102. .. note::
  103. The important thing here is to make sure you comment out the
  104. ``define=SQLITE_OMIT_LOAD_EXTENSION`` flag and that the ``include_dirs``
  105. and ``library_dirs`` settings are uncommented and set to the appropriate
  106. path if the SQLite header files and libraries are not in ``/usr/include``
  107. and ``/usr/lib``, respectively.
  108. After modifying ``setup.cfg`` appropriately, then run the ``setup.py`` script
  109. to build and install::
  110. $ sudo python setup.py install
  111. .. _spatialite_macosx:
  112. Mac OS X-specific instructions
  113. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  114. Mac OS X users should follow the instructions in the :ref:`kyngchaos` section,
  115. as it is much easier than building from source.
  116. When :ref:`create_spatialite_db`, the ``spatialite`` program is required.
  117. However, instead of attempting to compile the SpatiaLite tools from source,
  118. download the `SpatiaLite Binaries`__ for OS X, and install ``spatialite`` in a
  119. location available in your ``PATH``. For example::
  120. $ curl -O http://www.gaia-gis.it/spatialite/spatialite-tools-osx-x86-2.3.1.tar.gz
  121. $ tar xzf spatialite-tools-osx-x86-2.3.1.tar.gz
  122. $ cd spatialite-tools-osx-x86-2.3.1/bin
  123. $ sudo cp spatialite /Library/Frameworks/SQLite3.framework/Programs
  124. Finally, for GeoDjango to be able to find the KyngChaos SpatiaLite library,
  125. add the following to your ``settings.py``:
  126. .. code-block:: python
  127. SPATIALITE_LIBRARY_PATH='/Library/Frameworks/SQLite3.framework/SQLite3'
  128. __ http://www.gaia-gis.it/spatialite-2.3.1/binaries.html
  129. .. _create_spatialite_db:
  130. Creating a spatial database for SpatiaLite
  131. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  132. After you've installed SpatiaLite, you'll need to create a number of spatial
  133. metadata tables in your database in order to perform spatial queries.
  134. If you're using SpatiaLite 2.4 or newer, use the ``spatialite`` utility to
  135. call the ``InitSpatialMetaData()`` function, like this::
  136. $ spatialite geodjango.db "SELECT InitSpatialMetaData();"
  137. the SPATIAL_REF_SYS table already contains some row(s)
  138. InitSpatiaMetaData ()error:"table spatial_ref_sys already exists"
  139. 0
  140. You can safely ignore the error messages shown. When you've done this, you can
  141. skip the rest of this section.
  142. If you're using SpatiaLite 2.3, you'll need to download a
  143. database-initialization file and execute its SQL queries in your database.
  144. First, get it from the `SpatiaLite Resources`__ page::
  145. $ wget http://www.gaia-gis.it/spatialite-2.3.1/init_spatialite-2.3.sql.gz
  146. $ gunzip init_spatialite-2.3.sql.gz
  147. Then, use the ``spatialite`` command to initialize a spatial database::
  148. $ spatialite geodjango.db < init_spatialite-2.3.sql
  149. .. note::
  150. The parameter ``geodjango.db`` is the *filename* of the SQLite database
  151. you want to use. Use the same in the :setting:`DATABASES` ``"name"`` key
  152. inside your ``settings.py``.
  153. __ http://www.gaia-gis.it/spatialite-2.3.1/resources.html