install.txt 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193
  1. .. _ref-gis-install:
  2. ======================
  3. GeoDjango Installation
  4. ======================
  5. Overview
  6. ========
  7. In general, GeoDjango installation requires:
  8. 1. :ref:`python24` and :ref:`django`
  9. 2. :ref:`spatial_database`
  10. 3. :ref:`geospatial_libs`
  11. Details for each of the requirements and installation instructions
  12. are provided in the sections below. In addition, platform-specific
  13. instructions are available for:
  14. * :ref:`macosx`
  15. * :ref:`ubuntudebian`
  16. * :ref:`windows`
  17. .. admonition:: Use the Source
  18. Because GeoDjango takes advantage of the latest in the open source geospatial
  19. software technology, recent versions of the libraries are necessary.
  20. If binary packages aren't available for your platform,
  21. :ref:`installation from source <build_from_source>`
  22. may be required. When compiling the libraries from source, please follow the
  23. directions closely, especially if you're a beginner.
  24. Requirements
  25. ============
  26. .. _python24:
  27. Python 2.4+
  28. -----------
  29. Because of heavy use of the decorator syntax, Python 2.4 is minimum
  30. version supported by GeoDjango. Python 2.5+ is recommended because the
  31. `ctypes`__ module comes included; otherwise, 2.4 users will need to
  32. `download and install ctypes`__.
  33. __ http://docs.python.org/lib/module-ctypes.html
  34. __ http://sourceforge.net/projects/ctypes/files/
  35. .. _django:
  36. Django
  37. ------
  38. Because GeoDjango is included with Django, please refer to Django's
  39. :ref:`installation instructions <intro-install>` for details on how to install.
  40. .. _spatial_database:
  41. Spatial Database
  42. ----------------
  43. PostgreSQL (with PostGIS), MySQL, Oracle, and SQLite (with SpatiaLite) are
  44. the spatial databases currently supported.
  45. .. note::
  46. PostGIS is recommended, because it is the most mature and feature-rich
  47. open source spatial database.
  48. The geospatial libraries required for a GeoDjango installation depends
  49. on the spatial database used. The following lists the library requirements,
  50. supported versions, and any notes for each of the supported database backends:
  51. ================== ============================== ================== ==========================================================
  52. Database Library Requirements Supported Versions Notes
  53. ================== ============================== ================== ==========================================================
  54. PostgreSQL GEOS, PROJ.4, PostGIS 8.1+ Requires PostGIS.
  55. MySQL GEOS 5.x Not OGC-compliant; limited functionality.
  56. Oracle GEOS 10.2, 11 XE not supported; not tested with 9.
  57. SQLite GEOS, GDAL, PROJ.4, SpatiaLite 3.6.+ Requires SpatiaLite 2.3+, pysqlite2 2.5+, and Django 1.1.
  58. ================== ============================== ================== ==========================================================
  59. .. _geospatial_libs:
  60. Geospatial Libraries
  61. --------------------
  62. GeoDjango uses and/or provides interfaces for the the following open source
  63. geospatial libraries:
  64. ======================== ==================================== ================================ ==========================
  65. Program Description Required Supported Versions
  66. ======================== ==================================== ================================ ==========================
  67. :ref:`GEOS <ref-geos>` Geometry Engine Open Source Yes 3.2, 3.1, 3.0
  68. `PROJ.4`_ Cartographic Projections library Yes (PostgreSQL and SQLite only) 4.7, 4.6, 4.5, 4.4
  69. :ref:`GDAL <ref-gdal>` Geospatial Data Abstraction Library No (but, required for SQLite) 1.7, 1.6, 1.5, 1.4
  70. :ref:`GeoIP <ref-geoip>` IP-based geolocation library No 1.4
  71. `PostGIS`__ Spatial extensions for PostgreSQL Yes (PostgreSQL only) 1.5, 1.4, 1.3
  72. `SpatiaLite`__ Spatial extensions for SQLite Yes (SQLite only) 2.4, 2.3
  73. ======================== ==================================== ================================ ==========================
  74. .. admonition:: Install GDAL
  75. While :ref:`gdalbuild` is technically not required, it is *recommended*.
  76. Some features of GeoDjango (including the :ref:`ref-layermapping` and the geographic
  77. admin) depend on its functionality.
  78. .. note::
  79. The GeoDjango interfaces to GEOS, GDAL, and GeoIP may be used
  80. independently of Django. In other words, no database or settings file
  81. required -- just import them as normal from :mod:`django.contrib.gis`.
  82. .. _PROJ.4: http://trac.osgeo.org/proj/
  83. __ http://postgis.refractions.net/
  84. __ http://www.gaia-gis.it/spatialite/index.html
  85. .. _build_from_source:
  86. Building from Source
  87. ====================
  88. When installing from source on UNIX and GNU/Linux systems, please follow
  89. the installation instructions carefully, and install the libraries in the
  90. given order. If using MySQL or Oracle as the spatial database, only GEOS
  91. is required.
  92. .. note::
  93. On Linux platforms, it may be necessarry to run the ``ldconfig``
  94. command after installing each library. For example::
  95. $ sudo make install
  96. $ sudo ldconfig
  97. .. note::
  98. OS X users are required to install `Apple Developer Tools`_ in order
  99. to compile software from source. This is typically included on your
  100. OS X installation DVDs.
  101. .. _Apple Developer Tools: http://developer.apple.com/tools/xcode/
  102. .. _geosbuild:
  103. GEOS
  104. ----
  105. GEOS is a C++ library for performing geometric operations, and is the default
  106. internal geometry representation used by GeoDjango (it's behind the "lazy"
  107. geometries). Specifically, the C API library is called (e.g., ``libgeos_c.so``)
  108. directly from Python using ctypes.
  109. First, download GEOS 3.2 from the refractions website and untar the source
  110. archive::
  111. $ wget http://download.osgeo.org/geos/geos-3.2.1.tar.bz2
  112. $ tar xjf geos-3.2.1.tar.bz2
  113. Next, change into the directory where GEOS was unpacked, run the configure
  114. script, compile, and install::
  115. $ cd geos-3.2.1
  116. $ ./configure
  117. $ make
  118. $ sudo make install
  119. $ cd ..
  120. Troubleshooting
  121. ^^^^^^^^^^^^^^^
  122. Can't find GEOS Library
  123. ~~~~~~~~~~~~~~~~~~~~~~~
  124. When GeoDjango can't find GEOS, this error is raised::
  125. ImportError: Could not find the GEOS library (tried "geos_c"). Try setting GEOS_LIBRARY_PATH in your settings.
  126. The most common solution is to properly configure your :ref:`libsettings` *or* set
  127. :ref:`geoslibrarypath` in your settings.
  128. If using a binary package of GEOS (e.g., on Ubuntu 8.10), you may need to :ref:`binutils`.
  129. .. _geoslibrarypath:
  130. ``GEOS_LIBRARY_PATH``
  131. ~~~~~~~~~~~~~~~~~~~~~
  132. If your GEOS library is in a non-standard location, or you don't want to
  133. modify the system's library path then the :setting:`GEOS_LIBRARY_PATH` setting
  134. may be added to your Django settings file with the full path to the GEOS
  135. C library. For example::
  136. GEOS_LIBRARY_PATH = '/home/bob/local/lib/libgeos_c.so'
  137. .. note::
  138. The setting must be the *full* path to the **C** shared library; in
  139. other words you want to use ``libgeos_c.so``, not ``libgeos.so``.
  140. .. _proj4:
  141. PROJ.4
  142. ------
  143. `PROJ.4`_ is a library for converting geospatial data to different coordinate
  144. reference systems.
  145. First, download the PROJ.4 source code and datum shifting files [#]_::
  146. $ wget http://download.osgeo.org/proj/proj-4.7.0.tar.gz
  147. $ wget http://download.osgeo.org/proj/proj-datumgrid-1.5.zip
  148. Next, untar the source code archive, and extract the datum shifting files in the
  149. ``nad`` subdirectory. This must be done *prior* to configuration::
  150. $ tar xzf proj-4.7.0.tar.gz
  151. $ cd proj-4.7.0/nad
  152. $ unzip ../../proj-datumgrid-1.5.zip
  153. $ cd ..
  154. Finally, configure, make and install PROJ.4::
  155. $ ./configure
  156. $ make
  157. $ sudo make install
  158. $ cd ..
  159. .. _postgis:
  160. PostGIS
  161. -------
  162. `PostGIS`__ adds geographic object support to PostgreSQL, turning it
  163. into a spatial database. :ref:`geosbuild` and :ref:`proj4` should be
  164. installed prior to building PostGIS.
  165. .. note::
  166. The `psycopg2`_ module is required for use as the database adaptor
  167. when using GeoDjango with PostGIS.
  168. .. _psycopg2: http://initd.org/projects/psycopg2
  169. First download the source archive, and extract::
  170. $ wget http://postgis.refractions.net/download/postgis-1.5.1.tar.gz
  171. $ tar xzf postgis-1.5.1.tar.gz
  172. $ cd postgis-1.5.1
  173. Next, configure, make and install PostGIS::
  174. $ ./configure
  175. Finally, make and install::
  176. $ make
  177. $ sudo make install
  178. $ cd ..
  179. .. note::
  180. GeoDjango does not automatically create a spatial database. Please
  181. consult the section on :ref:`spatialdb_template` for more information.
  182. __ http://postgis.refractions.net/
  183. .. _gdalbuild:
  184. GDAL
  185. ----
  186. `GDAL`__ is an excellent open source geospatial library that has support for
  187. reading most vector and raster spatial data formats. Currently, GeoDjango only
  188. supports :ref:`GDAL's vector data <ref-gdal>` capabilities [#]_.
  189. :ref:`geosbuild` and :ref:`proj4` should be installed prior to building GDAL.
  190. First download the latest GDAL release version and untar the archive::
  191. $ wget http://download.osgeo.org/gdal/gdal-1.7.1.tar.gz
  192. $ tar xzf gdal-1.7.1.tar.gz
  193. $ cd gdal-1.7.1
  194. Configure, make and install::
  195. $ ./configure
  196. $ make # Go get some coffee, this takes a while.
  197. $ sudo make install
  198. $ cd ..
  199. .. note::
  200. Because GeoDjango has it's own Python interface, the preceding instructions
  201. do not build GDAL's own Python bindings. The bindings may be built by
  202. adding the ``--with-python`` flag when running ``configure``. See
  203. `GDAL/OGR In Python`__ for more information on GDAL's bindings.
  204. If you have any problems, please see the troubleshooting section below for
  205. suggestions and solutions.
  206. __ http://trac.osgeo.org/gdal/
  207. __ http://trac.osgeo.org/gdal/wiki/GdalOgrInPython
  208. .. _gdaltrouble:
  209. Troubleshooting
  210. ^^^^^^^^^^^^^^^
  211. Can't find GDAL Library
  212. ~~~~~~~~~~~~~~~~~~~~~~~
  213. When GeoDjango can't find the GDAL library, the ``HAS_GDAL`` flag
  214. will be false::
  215. >>> from django.contrib.gis import gdal
  216. >>> gdal.HAS_GDAL
  217. False
  218. The solution is to properly configure your :ref:`libsettings` *or* set
  219. :ref:`gdallibrarypath` in your settings.
  220. .. _gdallibrarypath:
  221. ``GDAL_LIBRARY_PATH``
  222. ~~~~~~~~~~~~~~~~~~~~~
  223. If your GDAL library is in a non-standard location, or you don't want to
  224. modify the system's library path then the :setting:`GDAL_LIBRARY_PATH`
  225. setting may be added to your Django settings file with the full path to
  226. the GDAL library. For example::
  227. GDAL_LIBRARY_PATH = '/home/sue/local/lib/libgdal.so'
  228. .. _gdaldata:
  229. Can't find GDAL data files (``GDAL_DATA``)
  230. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  231. When installed from source, GDAL versions 1.5.1 and below have an autoconf bug
  232. that places data in the wrong location. [#]_ This can lead to error messages
  233. like this::
  234. ERROR 4: Unable to open EPSG support file gcs.csv.
  235. ...
  236. OGRException: OGR failure.
  237. The solution is to set the ``GDAL_DATA`` environment variable to the location of the
  238. GDAL data files before invoking Python (typically ``/usr/local/share``; use
  239. ``gdal-config --datadir`` to find out). For example::
  240. $ export GDAL_DATA=`gdal-config --datadir`
  241. $ python manage.py shell
  242. If using Apache, you may need to add this environment variable to your configuration
  243. file::
  244. SetEnv GDAL_DATA /usr/local/share
  245. .. _spatialite:
  246. SpatiaLite
  247. ----------
  248. .. versionadded:: 1.1
  249. .. note::
  250. Mac OS X users should follow the instructions in the :ref:`kyngchaos` section,
  251. as it is much easier than building from source.
  252. `SpatiaLite`__ adds spatial support to SQLite, turning it into a full-featured
  253. spatial database. Because SpatiaLite has special requirements, it typically
  254. requires SQLite and pysqlite2 (the Python SQLite DB-API adaptor) to be built from
  255. source. :ref:`geosbuild` and :ref:`proj4` should be installed prior to building
  256. SpatiaLite.
  257. After installation is complete, don't forget to read the post-installation
  258. docs on :ref:`create_spatialite_db`.
  259. __ http://www.gaia-gis.it/spatialite/index.html
  260. .. _sqlite:
  261. SQLite
  262. ^^^^^^
  263. Typically, SQLite packages are not compiled to include the `R*Tree module`__ --
  264. thus it must be compiled from source. First download the latest amalgamation
  265. source archive from the `SQLite download page`__, and extract::
  266. $ wget http://www.sqlite.org/sqlite-amalgamation-3.6.22.tar.gz
  267. $ tar xzf sqlite-amalgamation-3.6.22.tar.gz
  268. $ cd sqlite-3.6.22
  269. Next, run the ``configure`` script -- however the ``CFLAGS`` environment variable
  270. needs to be customized so that SQLite knows to build the R*Tree module::
  271. $ CFLAGS="-DSQLITE_ENABLE_RTREE=1" ./configure
  272. $ make
  273. $ sudo make install
  274. $ cd ..
  275. .. note::
  276. If using Ubuntu, installing a newer SQLite from source can be very difficult
  277. because it links to the existing ``libsqlite3.so`` in ``/usr/lib`` which
  278. many other packages depend on. Unfortunately, the best solution at this time
  279. is to overwrite the existing library by adding ``--prefix=/usr`` to the
  280. ``configure`` command.
  281. __ http://www.sqlite.org/rtree.html
  282. __ http://www.sqlite.org/download.html
  283. .. _spatialitebuild :
  284. SpatiaLite Library (``libspatialite``) and Tools (``spatialite``)
  285. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  286. After SQLite has been built with the R*Tree module enabled, get the latest
  287. SpatiaLite library source and tools bundle from the `download page`__::
  288. $ wget http://www.gaia-gis.it/spatialite/libspatialite-amalgamation-2.3.1.tar.gz
  289. $ wget http://www.gaia-gis.it/spatialite/spatialite-tools-2.3.1.tar.gz
  290. $ tar xzf libspatialite-amalgamation-2.3.1.tar.gz
  291. $ tar xzf spatialite-tools-2.3.1.tar.gz
  292. Prior to attempting to build, please read the important notes below to see if
  293. customization of the ``configure`` command is necessary. If not, then run the
  294. ``configure`` script, make, and install for the SpatiaLite library::
  295. $ cd libspatialite-amalgamation-2.3.1
  296. $ ./configure # May need to modified, see notes below.
  297. $ make
  298. $ sudo make install
  299. $ cd ..
  300. Finally, do the same for the SpatiaLite tools::
  301. $ cd spatialite-tools-2.3.1
  302. $ ./configure # May need to modified, see notes below.
  303. $ make
  304. $ sudo make install
  305. $ cd ..
  306. .. note::
  307. If you've installed GEOS and PROJ.4 from binary packages, you will have to specify
  308. their paths when running the ``configure`` scripts for *both* the library and the
  309. tools (the configure scripts look, by default, in ``/usr/local``). For example,
  310. on Debian/Ubuntu distributions that have GEOS and PROJ.4 packages, the command would be::
  311. $ ./configure --with-proj-include=/usr/include --with-proj-lib=/usr/lib --with-geos-include=/usr/include --with-geos-lib=/usr/lib
  312. .. note::
  313. For Mac OS X users building from source, the SpatiaLite library *and* tools
  314. need to be linked into the existing ``iconv`` library. While this happens
  315. automatically on Linux, the ``configure`` scripts need to know about the
  316. specific location on Mac OS X (via modification of the ``CFLAGS`` and
  317. ``LDFLAGS`` environment variables prior to configuration)::
  318. $ CFLAGS=-I/usr/include LDFLAGS="-L/usr/lib -liconv" ./configure
  319. __ http://www.gaia-gis.it/spatialite/sources.html
  320. .. _pysqlite2:
  321. pysqlite2
  322. ^^^^^^^^^
  323. Because SpatiaLite must be loaded as an external extension, it requires the
  324. ``enable_load_extension`` method, which is only available in versions 2.5+.
  325. Thus, download pysqlite2 2.6, and untar::
  326. $ wget http://pysqlite.googlecode.com/files/pysqlite-2.6.0.tar.gz
  327. $ tar xzf pysqlite-2.6.0.tar.gz
  328. $ cd pysqlite-2.6.0
  329. Next, use a text editor (e.g., ``emacs`` or ``vi``) to edit the ``setup.cfg`` file
  330. to look like the following::
  331. [build_ext]
  332. #define=
  333. include_dirs=/usr/local/include
  334. library_dirs=/usr/local/lib
  335. libraries=sqlite3
  336. #define=SQLITE_OMIT_LOAD_EXTENSION
  337. .. note::
  338. The important thing here is to make sure you comment out the the
  339. ``define=SQLITE_OMIT_LOAD_EXTENSION`` flag and that the ``include_dirs``
  340. and ``library_dirs`` settings are uncommented and set to the appropriate
  341. path if the SQLite header files and libraries are not in ``/usr/include``
  342. and ``/usr/lib``, respectively.
  343. After modifying ``setup.cfg`` appropriately, then run the ``setup.py`` script
  344. to build and install::
  345. $ sudo python setup.py install
  346. Post-Installation
  347. =================
  348. .. _spatialdb_template:
  349. Creating a Spatial Database Template for PostGIS
  350. ------------------------------------------------
  351. Creating a spatial database with PostGIS is different than normal because
  352. additional SQL must be loaded to enable spatial functionality. Because of
  353. the steps in this process, it's better to create a database template that
  354. can be reused later.
  355. First, you need to be able to execute the commands as a privileged database
  356. user. For example, you can use the following to become the ``postgres`` user::
  357. $ sudo su - postgres
  358. .. note::
  359. The location *and* name of the PostGIS SQL files (e.g., from
  360. ``POSTGIS_SQL_PATH`` below) depends on the version of PostGIS.
  361. PostGIS versions 1.3 and below use ``<sharedir>/contrib/lwpostgis.sql``, whereas
  362. versions 1.4 and 1.5 use ``<sharedir>/contrib/postgis-1.4/postgis.sql`` and
  363. ``<sharedir>/contrib/postgis-1.5/postgis.sql``, respectively.
  364. The example below assumes PostGIS 1.5, thus you may need to modify
  365. ``POSTGIS_SQL_PATH`` and the name of the SQL file for the specific
  366. version of PostGIS you are using.
  367. Once you're a database super user, then you may execute the following commands
  368. to create a PostGIS spatial database template. If running Ubuntu :ref:`ibex`
  369. or Debian :ref:`lenny`, please refer to their specific documentation for
  370. modifications to these commands::
  371. $ POSTGIS_SQL_PATH=`pg_config --sharedir`/contrib/postgis-1.5
  372. # Creating the template spatial database.
  373. $ createdb -E UTF8 template_postgis
  374. $ createlang -d template_postgis plpgsql # Adding PLPGSQL language support.
  375. # Allows non-superusers the ability to create from this template
  376. $ psql -d postgres -c "UPDATE pg_database SET datistemplate='true' WHERE datname='template_postgis';"
  377. # Loading the PostGIS SQL routines
  378. $ psql -d template_postgis -f $POSTGIS_SQL_PATH/postgis.sql
  379. $ psql -d template_postgis -f $POSTGIS_SQL_PATH/spatial_ref_sys.sql
  380. # Enabling users to alter spatial tables.
  381. $ psql -d template_postgis -c "GRANT ALL ON geometry_columns TO PUBLIC;"
  382. $ psql -d template_postgis -c "GRANT ALL ON geography_columns TO PUBLIC;"
  383. $ psql -d template_postgis -c "GRANT ALL ON spatial_ref_sys TO PUBLIC;"
  384. These commands may be placed in a shell script for later use; for convenience
  385. the following scripts are available:
  386. =============== ==========================================
  387. PostGIS Version Shell Script
  388. =============== ==========================================
  389. 1.3 `create_template_postgis-1.3.sh`_
  390. 1.4 `create_template_postgis-1.4.sh`_
  391. 1.5 `create_template_postgis-1.5.sh`_
  392. =============== ==========================================
  393. Afterwards, you may create a spatial database by simply specifying
  394. ``template_postgis`` as the template to use (via the ``-T`` option)::
  395. $ createdb -T template_postgis <db name>
  396. .. note::
  397. While the ``createdb`` command does not require database super-user privileges,
  398. it must be executed by a database user that has permissions to create databases.
  399. You can create such a user with the following command::
  400. $ createuser --createdb <user>
  401. .. _create_template_postgis-1.3.sh: http://geodjango.org/docs/create_template_postgis-1.3.sh
  402. .. _create_template_postgis-1.4.sh: http://geodjango.org/docs/create_template_postgis-1.4.sh
  403. .. _create_template_postgis-1.5.sh: http://geodjango.org/docs/create_template_postgis-1.5.sh
  404. .. _create_template_postgis-debian.sh: http://geodjango.org/docs/create_template_postgis-debian.sh
  405. .. _create_spatialite_db:
  406. Creating a Spatial Database for SpatiaLite
  407. -------------------------------------------
  408. After the SpatiaLite library and tools have been installed, it is now possible
  409. to create spatial database for use with GeoDjango. In order to do this, download
  410. the spatial database initialization SQL from the `SpatiaLite Resources`__ page::
  411. $ wget http://www.gaia-gis.it/spatialite/init_spatialite-2.3.sql.gz
  412. $ gunzip init_spatialite-2.3.sql.gz
  413. Now, the ``spatialite`` command can be used to initialize a spatial database::
  414. $ spatialite geodjango.db < init_spatialite-2.3.sql
  415. .. note::
  416. The parameter ``geodjango.db`` is the *filename* of the SQLite database
  417. you want to use. Use the same in the :setting:`DATABASE_NAME`
  418. inside your ``settings.py``.
  419. __ http://www.gaia-gis.it/spatialite/resources.html
  420. Add ``django.contrib.gis`` to ``INSTALLED_APPS``
  421. ------------------------------------------------
  422. Like other Django contrib applications, you will *only* need to add
  423. :mod:`django.contrib.gis` to :setting:`INSTALLED_APPS` in your settings.
  424. This is the so that ``gis`` templates can be located -- if not done, then
  425. features such as the geographic admin or KML sitemaps will not function properly.
  426. .. _addgoogleprojection:
  427. Add Google Projection to ``spatial_ref_sys`` table
  428. --------------------------------------------------
  429. .. versionchanged:: 1.2
  430. .. note::
  431. If running PostGIS 1.4 and above, the entry is already included in the
  432. default ``spatial_ref_sys`` table. You can skip this step.
  433. In order to conduct database transformations to the so-called "Google"
  434. projection (a spherical mercator projection used by Google Maps),
  435. an entry must be added to your spatial database's ``spatial_ref_sys`` table.
  436. Invoke the Django shell from your project and execute the
  437. ``add_srs_entry`` function::
  438. $ python manage shell
  439. >>> from django.contrib.gis.utils import add_srs_entry
  440. >>> add_srs_entry(900913)
  441. .. note::
  442. In Django 1.1 the name of this function is ``add_postgis_srs``.
  443. This adds an entry for the 900913 SRID to the ``spatial_ref_sys`` (or equivalent)
  444. table, making it possible for the spatial database to transform coordinates in
  445. this projection. You only need to execute this command *once* per spatial database.
  446. Troubleshooting
  447. ===============
  448. If you can't find the solution to your problem here then participate in the
  449. community! You can:
  450. * Join the ``#geodjango`` IRC channel on FreeNode (may be accessed on the
  451. web via `Mibbit`__). Please be patient and polite -- while you may not
  452. get an immediate response, someone will attempt to answer your question
  453. as soon as they see it.
  454. * Ask your question on the `GeoDjango`__ mailing list.
  455. * File a ticket on the `Django trac`__ if you think there's a bug. Make
  456. sure to provide a complete description of the problem, versions used,
  457. and specify the component as "GIS".
  458. __ http://www.mibbit.com/?server=irc.freenode.net&channel=%23geodjango
  459. __ http://groups.google.com/group/geodjango
  460. __ http://code.djangoproject.com/simpleticket
  461. .. _libsettings:
  462. Library Environment Settings
  463. ----------------------------
  464. By far, the most common problem when installing GeoDjango is that the
  465. external shared libraries (e.g., for GEOS and GDAL) cannot be located. [#]_
  466. Typically, the cause of this problem is that the operating system isn't aware
  467. of the directory where the libraries built from source were installed.
  468. In general, the library path may be set on a per-user basis by setting
  469. an environment variable, or by configuring the library path for the entire
  470. system.
  471. ``LD_LIBRARY_PATH`` environment variable
  472. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  473. A user may set this environment variable to customize the library paths
  474. they want to use. The typical library directory for software
  475. built from source is ``/usr/local/lib``. Thus, ``/usr/local/lib`` needs
  476. to be included in the ``LD_LIBRARY_PATH`` variable. For example, the user
  477. could place the following in their bash profile::
  478. export LD_LIBRARY_PATH=/usr/local/lib
  479. Setting System Library Path
  480. ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  481. On GNU/Linux systems, there is typically a file in ``/etc/ld.so.conf``, which may include
  482. additional paths from files in another directory, such as ``/etc/ld.so.conf.d``.
  483. As the root user, add the custom library path (like ``/usr/local/lib``) on a
  484. new line in ``ld.so.conf``. This is *one* example of how to do so::
  485. $ sudo echo /usr/local/lib >> /etc/ld.so.conf
  486. $ sudo ldconfig
  487. For OpenSolaris users, the system library path may be modified using the
  488. ``crle`` utility. Run ``crle`` with no options to see the current configuration
  489. and use ``crle -l`` to set with the new library path. Be *very* careful when
  490. modifying the system library path::
  491. # crle -l $OLD_PATH:/usr/local/lib
  492. .. _binutils:
  493. Install ``binutils``
  494. ^^^^^^^^^^^^^^^^^^^^
  495. GeoDjango uses the ``find_library`` function (from the ``ctypes.util`` Python
  496. module) to discover libraries. The ``find_library`` routine uses a program
  497. called ``objdump`` (part of the ``binutils`` package) to verify a shared
  498. library on GNU/Linux systems. Thus, if ``binutils`` is not installed on your
  499. Linux system then Python's ctypes may not be able to find your library even if
  500. your library path is set correctly and geospatial libraries were built perfectly.
  501. The ``binutils`` package may be installed on Debian and Ubuntu systems using the
  502. following command::
  503. $ sudo apt-get install binutils
  504. Similarly, on Red Hat and CentOS systems::
  505. $ sudo yum install binutils
  506. Platform Specific Instructions
  507. ==============================
  508. .. _macosx:
  509. Mac OS X
  510. --------
  511. Because of the variety of packaging systems available for OS X, users have
  512. several different options for installing GeoDjango. These options are:
  513. * :ref:`kyngchaos`
  514. * :ref:`fink`
  515. * :ref:`macports`
  516. * :ref:`build_from_source`
  517. .. note::
  518. Currently, the easiest and recommended approach for installing GeoDjango
  519. on OS X is to use the KyngChaos packages.
  520. This section also includes instructions for installing an upgraded version
  521. of :ref:`macosx_python` from packages provided by the Python Software
  522. Foundation, however, this is not required.
  523. .. _macosx_python:
  524. Python
  525. ^^^^^^
  526. Although OS X comes with Python installed, users can use framework
  527. installers (`2.5`__ and `2.6`__ are available) provided by
  528. the Python Software Foundation. An advantage to using the installer is
  529. that OS X's Python will remain "pristine" for internal operating system
  530. use.
  531. __ http://python.org/ftp/python/2.5.4/python-2.5.4-macosx.dmg
  532. __ http://python.org/ftp/python/2.6.2/python-2.6.2-macosx2009-04-16.dmg
  533. .. note::
  534. You will need to modify the ``PATH`` environment variable in your
  535. ``.profile`` file so that the new version of Python is used when
  536. ``python`` is entered at the command-line::
  537. export PATH=/Library/Frameworks/Python.framework/Versions/Current/bin:$PATH
  538. .. _kyngchaos:
  539. KyngChaos Packages
  540. ^^^^^^^^^^^^^^^^^^
  541. William Kyngesburye provides a number of `geospatial library binary packages`__
  542. that make it simple to get GeoDjango installed on OS X without compiling
  543. them from source. However, the `Apple Developer Tools`_ are still necessary
  544. for compiling the Python database adapters :ref:`psycopg2_kyngchaos` (for PostGIS)
  545. and :ref:`pysqlite2_kyngchaos` (for SpatiaLite).
  546. .. note::
  547. SpatiaLite users should consult the :ref:`spatialite_kyngchaos` section
  548. after installing the packages for additional instructions.
  549. Download the framework packages for:
  550. * UnixImageIO
  551. * PROJ
  552. * GEOS
  553. * SQLite3 (includes the SpatiaLite library)
  554. * GDAL
  555. Install the packages in the order they are listed above, as the GDAL and SQLite
  556. packages require the packages listed before them. Afterwards, you can also
  557. install the KyngChaos binary packages for `PostgreSQL and PostGIS`__.
  558. After installing the binary packages, you'll want to add the following to
  559. your ``.profile`` to be able to run the package programs from the command-line::
  560. export PATH=/Library/Frameworks/UnixImageIO.framework/Programs:$PATH
  561. export PATH=/Library/Frameworks/PROJ.framework/Programs:$PATH
  562. export PATH=/Library/Frameworks/GEOS.framework/Programs:$PATH
  563. export PATH=/Library/Frameworks/SQLite3.framework/Programs:$PATH
  564. export PATH=/Library/Frameworks/GDAL.framework/Programs:$PATH
  565. export PATH=/usr/local/pgsql/bin:$PATH
  566. __ http://www.kyngchaos.com/wiki/software:frameworks
  567. __ http://www.kyngchaos.com/wiki/software:postgres
  568. .. note::
  569. Use of these binaries requires Django 1.0.3 and above. If you are
  570. using a previous version of Django (like 1.0.2), then you will have
  571. to add the the following in your settings::
  572. GEOS_LIBRARY_PATH='/Library/Frameworks/GEOS.framework/GEOS'
  573. GDAL_LIBRARY_PATH='/Library/Frameworks/GDAL.framework/GDAL'
  574. .. _psycopg2_kyngchaos:
  575. psycopg2
  576. ~~~~~~~~
  577. After you've installed the KyngChaos binaries and modified your ``PATH``, as
  578. described above, ``psycopg2`` may be installed using the following command::
  579. $ sudo python easy_install psycopg2
  580. .. note::
  581. To use ``easy_install`` you'll need to install Python's `setuptools`_.
  582. .. _setuptools: http://pypi.python.org/pypi/setuptools
  583. .. _pysqlite2_kyngchaos:
  584. pysqlite2
  585. ~~~~~~~~~
  586. Follow the :ref:`pysqlite2` source install instructions, however,
  587. when editing the ``setup.cfg`` use the following instead::
  588. [build_ext]
  589. #define=
  590. include_dirs=/Library/Frameworks/SQLite3.framework/unix/include
  591. library_dirs=/Library/Frameworks/SQLite3.framework/unix/lib
  592. libraries=sqlite3
  593. #define=SQLITE_OMIT_LOAD_EXTENSION
  594. .. _spatialite_kyngchaos:
  595. SpatiaLite
  596. ~~~~~~~~~~
  597. When :ref:`create_spatialite_db`, the ``spatialite`` program is required.
  598. However, instead of attempting to compile the SpatiaLite tools from source,
  599. download the `SpatiaLite Binaries`__ for OS X, and install ``spatialite`` in a
  600. location available in your ``PATH``. For example::
  601. $ curl -O http://www.gaia-gis.it/spatialite/spatialite-tools-osx-x86-2.3.1.tar.gz
  602. $ tar xzf spatialite-tools-osx-x86-2.3.1.tar.gz
  603. $ cd spatialite-tools-osx-x86-2.3.1/bin
  604. $ sudo cp spatialite /Library/Frameworks/SQLite3.framework/Programs
  605. Finally, for GeoDjango to be able to find the KyngChaos SpatiaLite library,
  606. add the following to your ``settings.py``::
  607. SPATIALITE_LIBRARY_PATH='/Library/Frameworks/SQLite3.framework/SQLite3'
  608. __ http://www.gaia-gis.it/spatialite/binaries.html
  609. .. _fink:
  610. Fink
  611. ^^^^
  612. `Kurt Schwehr`__ has been gracious enough to create GeoDjango packages for users
  613. of the `Fink`__ package system. The following packages are available, depending
  614. on which version of Python you want to use:
  615. * ``django-gis-py26``
  616. * ``django-gis-py25``
  617. * ``django-gis-py24``
  618. __ http://schwehr.org/blog/
  619. __ http://www.finkproject.org/
  620. .. _macports:
  621. MacPorts
  622. ^^^^^^^^
  623. `MacPorts`__ may be used to install GeoDjango prerequisites on Macintosh
  624. computers running OS X. Because MacPorts still builds the software from source,
  625. the `Apple Developer Tools`_ are required.
  626. Summary::
  627. $ sudo port install postgresql83-server
  628. $ sudo port install geos
  629. $ sudo port install proj
  630. $ sudo port install postgis
  631. $ sudo port install gdal
  632. $ sudo port install libgeoip
  633. .. note::
  634. You will also have to modify the ``PATH`` in your ``.profile`` so
  635. that the MacPorts programs are accessible from the command-line::
  636. export PATH=/opt/local/bin:/opt/local/lib/postgresql83/bin
  637. In addition, add the ``FALLBACK_DYLD_LIBRARY_PATH`` setting so that
  638. the libraries can be found by Python::
  639. export FALLBACK_DYLD_LIBRARY_PATH=/opt/local/lib:/opt/local/lib/postgresql83
  640. __ http://www.macports.org/
  641. .. _ubuntudebian:
  642. Ubuntu & Debian GNU/Linux
  643. -------------------------
  644. .. _ubuntu:
  645. Ubuntu
  646. ^^^^^^
  647. .. _heron:
  648. 8.04 and lower
  649. ~~~~~~~~~~~~~~
  650. The 8.04 (and lower) versions of Ubuntu use GEOS v2.2.3 in their binary packages,
  651. which is incompatible with GeoDjango. Thus, do *not* use the binary packages
  652. for GEOS or PostGIS and build some prerequisites from source, per the instructions
  653. in this document; however, it is okay to use the PostgreSQL binary packages.
  654. For more details, please see the Debian instructions for :ref:`etch` below.
  655. .. _ibex:
  656. 8.10
  657. ~~~~
  658. Use the synaptic package manager to install the following packages::
  659. $ sudo apt-get install binutils libgdal1-1.5.0 postgresql-8.3-postgis postgresql-server-dev-8.3 python-psycopg2 python-setuptools
  660. Afterwards, you may install Django with Python's ``easy_install`` script (the
  661. Ubuntu package ``python-django`` uses an older version missing several
  662. important bug fixes for GeoDjango)::
  663. $ sudo easy_install Django
  664. That's it! For the curious, the required binary prerequisites packages are:
  665. * ``binutils``: for ctypes to find libraries
  666. * ``postgresql-8.3``
  667. * ``postgresql-server-dev-8.3``: for ``pg_config``
  668. * ``postgresql-8.3-postgis``: for PostGIS 1.3.3
  669. * ``libgeos-3.0.0``, and ``libgeos-c1``: for GEOS 3.0.0
  670. * ``libgdal1-1.5.0``: for GDAL 1.5.0 library
  671. * ``proj``: for PROJ 4.6.0 -- but no datum shifting files, see note below
  672. * ``python-psycopg2``
  673. * ``python-setuptools``: for ``easy_install``
  674. Optional packages to consider:
  675. * ``libgeoip1``: for :ref:`GeoIP <ref-geoip>` support
  676. * ``gdal-bin``: for GDAL command line programs like ``ogr2ogr``
  677. * ``python-gdal`` for GDAL's own Python bindings -- includes interfaces for raster manipulation
  678. .. note::
  679. The Ubuntu ``proj`` package does not come with the datum shifting files
  680. installed, which will cause problems with the geographic admin because
  681. the ``null`` datum grid is not available for transforming geometries to the
  682. spherical mercator projection. A solution is to download the
  683. datum-shifting files, create the grid file, and install it yourself::
  684. $ wget http://download.osgeo.org/proj/proj-datumgrid-1.4.tar.gz
  685. $ mkdir nad
  686. $ cd nad
  687. $ tar xzf ../proj-datumgrid-1.4.tar.gz
  688. $ nad2bin null < null.lla
  689. $ sudo cp null /usr/share/proj
  690. Otherwise, the Ubuntu ``proj`` package is fine for general use as long as you
  691. do not plan on doing any database transformation of geometries to the
  692. Google projection (900913).
  693. .. note::
  694. The PostGIS SQL files are not placed the PostgreSQL share directory in the
  695. Ubuntu packages. Use the `create_template_postgis-debian.sh`_ script
  696. instead when :ref:`spatialdb_template`.
  697. .. _debian:
  698. Debian
  699. ------
  700. .. _etch:
  701. 4.0 (Etch)
  702. ^^^^^^^^^^
  703. The situation here is the same as that of Ubuntu :ref:`heron` -- in other words,
  704. some packages must be built from source to work properly with GeoDjango.
  705. Binary Packages
  706. ~~~~~~~~~~~~~~~
  707. The following command will install acceptable binary packages, as well as
  708. the development tools necessary to build the rest of the requirements::
  709. $ sudo apt-get install binutils bzip2 gcc g++ flex make postgresql-8.1 postgresql-server-dev-8.1 python-ctypes python-psycopg2 python-setuptools
  710. Required package information:
  711. * ``binutils``: for ctypes to find libraries
  712. * ``bzip2``: for decompressing the source packages
  713. * ``gcc``, ``g++``, ``make``: GNU developer tools used to compile the libraries
  714. * ``flex``: required to build PostGIS
  715. * ``postgresql-8.1``
  716. * ``postgresql-server-dev-8.1``: for ``pg_config``
  717. * ``python-ctypes``: Python 2.4 needs to have ctypes installed separately
  718. * ``python-psycopg2``
  719. * ``python-setuptools``: for ``easy_install``
  720. Optional packages:
  721. * ``libgeoip``: for :ref:`GeoIP <ref-geoip>` support
  722. Source Packages
  723. ~~~~~~~~~~~~~~~
  724. You will still have to install :ref:`geosbuild`, :ref:`proj4`,
  725. :ref:`postgis`, and :ref:`gdalbuild` from source. Please follow the
  726. directions carefully.
  727. .. _lenny:
  728. 5.0 (Lenny)
  729. ^^^^^^^^^^^
  730. This version is comparable to Ubuntu :ref:`ibex`, so the command
  731. is very similar::
  732. $ sudo apt-get install binutils libgdal1-1.5.0 postgresql-8.3 postgresql-8.3-postgis postgresql-server-dev-8.3 python-psycopg2 python-setuptools
  733. This assumes that you are using PostgreSQL version 8.3. Else, replace ``8.3``
  734. in the above command with the appropriate PostgreSQL version.
  735. .. note::
  736. Please read the note in the Ubuntu :ref:`ibex` install documentation
  737. about the ``proj`` package -- it also applies here because the package does
  738. not include the datum shifting files.
  739. .. _post_install:
  740. Post-installation Notes
  741. ~~~~~~~~~~~~~~~~~~~~~~~
  742. If the PostgreSQL database cluster was not initiated after installing, then it
  743. can be created (and started) with the following command::
  744. $ sudo pg_createcluster --start 8.3 main
  745. Afterwards, the ``/etc/init.d/postgresql-8.3`` script should be used to manage
  746. the starting and stopping of PostgreSQL.
  747. In addition, the SQL files for PostGIS are placed in a different location on
  748. Debian 5.0 . Thus when :ref:`spatialdb_template` either:
  749. * Create a symbolic link to these files::
  750. $ sudo ln -s /usr/share/postgresql-8.3-postgis/{lwpostgis,spatial_ref_sys}.sql /usr/share/postgresql/8.3
  751. If not running PostgreSQL 8.3, then replace ``8.3`` in the command above with the correct version.
  752. * Or use the `create_template_postgis-debian.sh`_ to create the spatial database.
  753. .. _windows:
  754. Windows XP
  755. ----------
  756. Python
  757. ^^^^^^
  758. First, download the `Python 2.6 installer`__ from the Python website. Next,
  759. execute the installer and use defaults, e.g., keep 'Install for all users'
  760. checked and the installation path set as ``C:\Python26``.
  761. .. note::
  762. You may already have a version of Python installed in ``C:\python`` as ESRI
  763. products sometimes install a copy there. *You should still install a
  764. fresh version of Python 2.6.*
  765. __ http://python.org/ftp/python/2.6.2/python-2.6.2.msi
  766. PostgreSQL
  767. ^^^^^^^^^^
  768. First, select a mirror and download the latest `PostgreSQL 8.3 installer`__ from
  769. the EnterpriseDB website.
  770. .. note::
  771. PostgreSQL 8.3 is required because PostGIS is not available yet for 8.4.
  772. After downloading, simply click on the installer, follow the
  773. on-screen directions, and keep the default options (e.g., keep the installation
  774. path as ``C:\Program Files\PostgreSQL\8.3``).
  775. .. note::
  776. This PostgreSQL installation process will create both a new windows user to be the
  777. 'postgres service account' and a special 'postgres superuser' to own the database
  778. cluster. You will be prompted to set a password for both users (make sure to write
  779. them down!). To see basic details on the 'service user' account right click on
  780. 'My Computer' and select 'Manage' or go to: Control Panel -> Administrative Tools ->
  781. Computer Management -> System Tools -> Local Users and Groups.
  782. If installed successfully, the PostgreSQL server will run in the background each time
  783. the system as started as a Windows service. When finished, the installer should launch
  784. the Application Stack Builder (ASB) -- use this to install PostGIS, see instructions
  785. below for more details. A 'PostgreSQL 8.3' start menu group should be created that
  786. contains shortcuts for the ASB and 'Command Prompt', which launches a terminal window
  787. in the PostgreSQL directory.
  788. __ http://www.enterprisedb.com/products/pgdownload.do#windows
  789. PostGIS
  790. ^^^^^^^
  791. From the Application Stack Builder (Programs -> PostgreSQL 8.3), select
  792. 'PostgreSQL Database Server 8.3 on port 5432' from the drop down menu. Next,
  793. select 'PostGIS 1.3.6 for PostgreSQL 8.3' from the 'Spatial Extensions' tree
  794. in the list. Select only the default options during install (do not uncheck
  795. the option to create a default PostGIS database).
  796. .. note::
  797. You will be prompted to enter your 'postgres superuser' password in the
  798. 'Database Connection Information' dialog.
  799. psycopg2
  800. ^^^^^^^^
  801. The ``psycopg2`` Python module provides the interface between Python and the
  802. PostgreSQL database. Download the `Windows installer`__ (v2.0.10) and run
  803. using the default settings. [#]_
  804. __ http://www.stickpeople.com/projects/python/win-psycopg/psycopg2-2.0.10.win32-py2.6-pg8.3.7-release.exe
  805. GeoDjango Installer
  806. ^^^^^^^^^^^^^^^^^^^
  807. Download the `GeoDjango Installer`__; this was created [#]_ to simplify the rest
  808. of the process for installing GeoDjango on Windows platforms. The installer
  809. automatically installs Django 1.1, GDAL 1.6.0, PROJ 4.6.1 (including datum grid
  810. files), and configures the necessary environment variables.
  811. Once the installer has completed, log out and log back in so that the
  812. modifications to the system environment variables take effect, and you
  813. should be good to go.
  814. .. note::
  815. The installer modifies the system ``Path`` environment variable to
  816. include ``C:\Program Files\PostgreSQL\8.3\bin`` and
  817. ``C:\Program Files\GeoDjango\bin``. This is required so that Python
  818. may find the GEOS DLL provided by PostGIS and the GDAL DLL provided
  819. by the installer. The installer also sets the ``GDAL_DATA`` and
  820. ``PROJ_LIB`` environment variables.
  821. __ http://geodjango.org/windows/GeoDjango_Installer.exe
  822. .. rubric:: Footnotes
  823. .. [#] The datum shifting files are needed for converting data to and from certain projections.
  824. For example, the PROJ.4 string for the `Google projection (900913) <http://spatialreference.org/ref/epsg/900913/proj4>`_
  825. requires the ``null`` grid file only included in the extra datum shifting files.
  826. It is easier to install the shifting files now, then to have debug a problem caused by their absence later.
  827. .. [#] Specifically, GeoDjango provides support for the `OGR <http://gdal.org/ogr>`_ library, a component of GDAL.
  828. .. [#] See `GDAL ticket #2382 <http://trac.osgeo.org/gdal/ticket/2382>`_.
  829. .. [#] GeoDjango uses the `find_library <http://docs.python.org/library/ctypes.html#finding-shared-libraries>`_
  830. routine from ``ctypes.util`` to locate shared libraries.
  831. .. [#] The ``psycopg2`` Windows installers are packaged and maintained by
  832. `Jason Erickson <http://www.stickpeople.com/projects/python/win-psycopg/>`_.
  833. .. [#] The source code for the installer is available in the `nsis_installer <http://geodjango.org/hg/nsis_installer/>`_
  834. GeoDjango mercurial repository.