spatialite.txt 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. To install the SpatiaLite library and tools, Mac OS X users can choose between
  118. :ref:`kyngchaos` and `Homebrew`_.
  119. KyngChaos
  120. ^^^^^^^^^
  121. First, follow the instructions in the :ref:`kyngchaos` section.
  122. When :ref:`create_spatialite_db`, the ``spatialite`` program is required.
  123. However, instead of attempting to compile the SpatiaLite tools from source,
  124. download the `SpatiaLite Binaries`__ for OS X, and install ``spatialite`` in a
  125. location available in your ``PATH``. For example::
  126. $ curl -O http://www.gaia-gis.it/spatialite/spatialite-tools-osx-x86-2.3.1.tar.gz
  127. $ tar xzf spatialite-tools-osx-x86-2.3.1.tar.gz
  128. $ cd spatialite-tools-osx-x86-2.3.1/bin
  129. $ sudo cp spatialite /Library/Frameworks/SQLite3.framework/Programs
  130. Finally, for GeoDjango to be able to find the KyngChaos SpatiaLite library,
  131. add the following to your ``settings.py``::
  132. SPATIALITE_LIBRARY_PATH='/Library/Frameworks/SQLite3.framework/SQLite3'
  133. __ http://www.gaia-gis.it/spatialite-2.3.1/binaries.html
  134. Homebrew
  135. ^^^^^^^^
  136. `Homebrew`_ handles all the SpatiaLite related packages on your behalf,
  137. including SQLite3, SpatiaLite, PROJ, and GEOS. Install them like this::
  138. $ brew update
  139. $ brew install spatialite-tools
  140. $ brew install gdal
  141. Finally, for GeoDjango to be able to find the SpatiaLite library, add the
  142. following to your ``settings.py``::
  143. SPATIALITE_LIBRARY_PATH='/usr/local/lib/mod_spatialite.dylib'
  144. .. _Homebrew: http://brew.sh/
  145. .. _create_spatialite_db:
  146. Creating a spatial database for SpatiaLite
  147. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  148. When running ``manage.py migrate`` with a SQLite or SpatiaLite database, the
  149. database file will be automatically created if it doesn't exist. Django will
  150. also ensure that the spatial metadata are initialized in the database.
  151. .. versionchanged:: 1.8
  152. Prior to Django 1.8, you had to intialize spatial metadata tables yourself
  153. by manually running the "SELECT InitSpatialMetaData();" query.