spatialite.txt 7.0 KB

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