index.txt 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. .. _ref-gis-install:
  2. ======================
  3. GeoDjango Installation
  4. ======================
  5. .. highlight:: console
  6. Overview
  7. ========
  8. In general, GeoDjango installation requires:
  9. 1. :ref:`Python and Django <django>`
  10. 2. :ref:`spatial_database`
  11. 3. :ref:`geospatial_libs`
  12. Details for each of the requirements and installation instructions
  13. are provided in the sections below. In addition, platform-specific
  14. instructions are available for:
  15. * :ref:`macosx`
  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, installation from
  21. source may be required. When compiling the libraries from source, please
  22. follow the directions closely, especially if you're a beginner.
  23. Requirements
  24. ============
  25. .. _django:
  26. Python and Django
  27. -----------------
  28. Because GeoDjango is included with Django, please refer to Django's
  29. :ref:`installation instructions <installing-official-release>` for details on
  30. how to install.
  31. .. _spatial_database:
  32. Spatial database
  33. ----------------
  34. PostgreSQL (with PostGIS), MySQL (mostly with MyISAM engine), Oracle, and SQLite
  35. (with SpatiaLite) are the spatial databases currently supported.
  36. .. note::
  37. PostGIS is recommended, because it is the most mature and feature-rich
  38. open source spatial database.
  39. The geospatial libraries required for a GeoDjango installation depends
  40. on the spatial database used. The following lists the library requirements,
  41. supported versions, and any notes for each of the supported database backends:
  42. ================== ============================== ================== =========================================
  43. Database Library Requirements Supported Versions Notes
  44. ================== ============================== ================== =========================================
  45. PostgreSQL GEOS, PROJ.4, PostGIS 8.4+ Requires PostGIS.
  46. MySQL GEOS 5.x Not OGC-compliant; :ref:`limited functionality <mysql-spatial-limitations>`.
  47. Oracle GEOS 10.2, 11 XE not supported; not tested with 9.
  48. SQLite GEOS, GDAL, PROJ.4, SpatiaLite 3.6.+ Requires SpatiaLite 2.3+, pysqlite2 2.5+
  49. ================== ============================== ================== =========================================
  50. See also `this comparison matrix`__ on the OSGeo Wiki for
  51. PostgreSQL/PostGIS/GEOS/GDAL possible combinations.
  52. __ http://trac.osgeo.org/postgis/wiki/UsersWikiPostgreSQLPostGIS
  53. Installation
  54. ============
  55. Geospatial libraries
  56. --------------------
  57. .. toctree::
  58. :maxdepth: 1
  59. geolibs
  60. Database installation
  61. ---------------------
  62. .. toctree::
  63. :maxdepth: 1
  64. postgis
  65. spatialite
  66. Add ``django.contrib.gis`` to :setting:`INSTALLED_APPS`
  67. -------------------------------------------------------
  68. Like other Django contrib applications, you will *only* need to add
  69. :mod:`django.contrib.gis` to :setting:`INSTALLED_APPS` in your settings.
  70. This is the so that ``gis`` templates can be located -- if not done, then
  71. features such as the geographic admin or KML sitemaps will not function properly.
  72. .. _addgoogleprojection:
  73. Add Google projection to ``spatial_ref_sys`` table
  74. --------------------------------------------------
  75. .. note::
  76. If you're running PostGIS 1.4 or above, you can skip this step. The entry
  77. is already included in the default ``spatial_ref_sys`` table.
  78. In order to conduct database transformations to the so-called "Google"
  79. projection (a spherical mercator projection used by Google Maps),
  80. an entry must be added to your spatial database's ``spatial_ref_sys`` table.
  81. Invoke the Django shell from your project and execute the
  82. ``add_srs_entry`` function:
  83. .. code-block:: pycon
  84. $ python manage.py shell
  85. >>> from django.contrib.gis.utils import add_srs_entry
  86. >>> add_srs_entry(900913)
  87. This adds an entry for the 900913 SRID to the ``spatial_ref_sys`` (or equivalent)
  88. table, making it possible for the spatial database to transform coordinates in
  89. this projection. You only need to execute this command *once* per spatial database.
  90. Troubleshooting
  91. ===============
  92. If you can't find the solution to your problem here then participate in the
  93. community! You can:
  94. * Join the ``#geodjango`` IRC channel on FreeNode. Please be patient and polite
  95. -- while you may not get an immediate response, someone will attempt to answer
  96. your question as soon as they see it.
  97. * Ask your question on the `GeoDjango`__ mailing list.
  98. * File a ticket on the `Django trac`__ if you think there's a bug. Make
  99. sure to provide a complete description of the problem, versions used,
  100. and specify the component as "GIS".
  101. __ https://groups.google.com/d/forum/geodjango
  102. __ https://code.djangoproject.com/newticket
  103. .. _libsettings:
  104. Library environment settings
  105. ----------------------------
  106. By far, the most common problem when installing GeoDjango is that the
  107. external shared libraries (e.g., for GEOS and GDAL) cannot be located. [#]_
  108. Typically, the cause of this problem is that the operating system isn't aware
  109. of the directory where the libraries built from source were installed.
  110. In general, the library path may be set on a per-user basis by setting
  111. an environment variable, or by configuring the library path for the entire
  112. system.
  113. ``LD_LIBRARY_PATH`` environment variable
  114. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  115. A user may set this environment variable to customize the library paths
  116. they want to use. The typical library directory for software
  117. built from source is ``/usr/local/lib``. Thus, ``/usr/local/lib`` needs
  118. to be included in the ``LD_LIBRARY_PATH`` variable. For example, the user
  119. could place the following in their bash profile::
  120. export LD_LIBRARY_PATH=/usr/local/lib
  121. Setting system library path
  122. ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  123. On GNU/Linux systems, there is typically a file in ``/etc/ld.so.conf``, which may include
  124. additional paths from files in another directory, such as ``/etc/ld.so.conf.d``.
  125. As the root user, add the custom library path (like ``/usr/local/lib``) on a
  126. new line in ``ld.so.conf``. This is *one* example of how to do so::
  127. $ sudo echo /usr/local/lib >> /etc/ld.so.conf
  128. $ sudo ldconfig
  129. For OpenSolaris users, the system library path may be modified using the
  130. ``crle`` utility. Run ``crle`` with no options to see the current configuration
  131. and use ``crle -l`` to set with the new library path. Be *very* careful when
  132. modifying the system library path::
  133. # crle -l $OLD_PATH:/usr/local/lib
  134. .. _binutils:
  135. Install ``binutils``
  136. ^^^^^^^^^^^^^^^^^^^^
  137. GeoDjango uses the ``find_library`` function (from the ``ctypes.util`` Python
  138. module) to discover libraries. The ``find_library`` routine uses a program
  139. called ``objdump`` (part of the ``binutils`` package) to verify a shared
  140. library on GNU/Linux systems. Thus, if ``binutils`` is not installed on your
  141. Linux system then Python's ctypes may not be able to find your library even if
  142. your library path is set correctly and geospatial libraries were built perfectly.
  143. The ``binutils`` package may be installed on Debian and Ubuntu systems using the
  144. following command::
  145. $ sudo apt-get install binutils
  146. Similarly, on Red Hat and CentOS systems::
  147. $ sudo yum install binutils
  148. Platform-specific instructions
  149. ==============================
  150. .. _macosx:
  151. Mac OS X
  152. --------
  153. Because of the variety of packaging systems available for OS X, users have
  154. several different options for installing GeoDjango. These options are:
  155. * :ref:`homebrew`
  156. * :ref:`kyngchaos`
  157. * :ref:`fink`
  158. * :ref:`macports`
  159. * :ref:`build_from_source`
  160. .. note::
  161. Currently, the easiest and recommended approach for installing GeoDjango
  162. on OS X is to use the KyngChaos packages.
  163. This section also includes instructions for installing an upgraded version
  164. of :ref:`macosx_python` from packages provided by the Python Software
  165. Foundation, however, this is not required.
  166. .. _macosx_python:
  167. Python
  168. ^^^^^^
  169. Although OS X comes with Python installed, users can use framework
  170. installers (`2.7`__, `3.2`__ and `3.3`__ are available) provided by
  171. the Python Software Foundation. An advantage to using the installer is
  172. that OS X's Python will remain "pristine" for internal operating system
  173. use.
  174. __ http://python.org/ftp/python/2.7.5/
  175. __ http://python.org/ftp/python/3.2.5/
  176. __ http://python.org/ftp/python/3.3.2/
  177. .. note::
  178. You will need to modify the ``PATH`` environment variable in your
  179. ``.profile`` file so that the new version of Python is used when
  180. ``python`` is entered at the command-line::
  181. export PATH=/Library/Frameworks/Python.framework/Versions/Current/bin:$PATH
  182. .. _homebrew:
  183. Homebrew
  184. ^^^^^^^^
  185. `Homebrew`__ provides "recipes" for building binaries and packages from source.
  186. It provides recipes for the GeoDjango prerequisites on Macintosh computers
  187. running OS X. Because Homebrew still builds the software from source, the
  188. `Apple Developer Tools`_ are required.
  189. Summary::
  190. $ brew install postgresql
  191. $ brew install postgis
  192. $ brew install gdal
  193. $ brew install libgeoip
  194. __ http://brew.sh/
  195. .. _Apple Developer Tools: https://developer.apple.com/technologies/tools/
  196. .. _kyngchaos:
  197. KyngChaos packages
  198. ^^^^^^^^^^^^^^^^^^
  199. William Kyngesburye provides a number of `geospatial library binary packages`__
  200. that make it simple to get GeoDjango installed on OS X without compiling
  201. them from source. However, the `Apple Developer Tools`_ are still necessary
  202. for compiling the Python database adapters :ref:`psycopg2_kyngchaos` (for PostGIS)
  203. and :ref:`pysqlite2` (for SpatiaLite).
  204. .. note::
  205. SpatiaLite users should consult the :ref:`spatialite_macosx` section
  206. after installing the packages for additional instructions.
  207. Download the framework packages for:
  208. * UnixImageIO
  209. * PROJ
  210. * GEOS
  211. * SQLite3 (includes the SpatiaLite library)
  212. * GDAL
  213. Install the packages in the order they are listed above, as the GDAL and SQLite
  214. packages require the packages listed before them.
  215. Afterwards, you can also install the KyngChaos binary packages for `PostgreSQL
  216. and PostGIS`__.
  217. After installing the binary packages, you'll want to add the following to
  218. your ``.profile`` to be able to run the package programs from the command-line::
  219. export PATH=/Library/Frameworks/UnixImageIO.framework/Programs:$PATH
  220. export PATH=/Library/Frameworks/PROJ.framework/Programs:$PATH
  221. export PATH=/Library/Frameworks/GEOS.framework/Programs:$PATH
  222. export PATH=/Library/Frameworks/SQLite3.framework/Programs:$PATH
  223. export PATH=/Library/Frameworks/GDAL.framework/Programs:$PATH
  224. export PATH=/usr/local/pgsql/bin:$PATH
  225. __ http://www.kyngchaos.com/software/frameworks
  226. __ http://www.kyngchaos.com/software/postgres
  227. .. _psycopg2_kyngchaos:
  228. psycopg2
  229. ~~~~~~~~
  230. After you've installed the KyngChaos binaries and modified your ``PATH``, as
  231. described above, ``psycopg2`` may be installed using the following command::
  232. $ sudo pip install psycopg2
  233. .. note::
  234. If you don't have ``pip``, follow the :ref:`installation instructions
  235. <installing-official-release>` to install it.
  236. .. _fink:
  237. Fink
  238. ^^^^
  239. `Kurt Schwehr`__ has been gracious enough to create GeoDjango packages for users
  240. of the `Fink`__ package system. The following packages are available, depending
  241. on which version of Python you want to use:
  242. * ``django-gis-py26``
  243. * ``django-gis-py25``
  244. * ``django-gis-py24``
  245. __ http://schwehr.org/blog/
  246. __ http://www.finkproject.org/
  247. .. _macports:
  248. MacPorts
  249. ^^^^^^^^
  250. `MacPorts`__ may be used to install GeoDjango prerequisites on Macintosh
  251. computers running OS X. Because MacPorts still builds the software from source,
  252. the `Apple Developer Tools`_ are required.
  253. Summary::
  254. $ sudo port install postgresql83-server
  255. $ sudo port install geos
  256. $ sudo port install proj
  257. $ sudo port install postgis
  258. $ sudo port install gdal +geos
  259. $ sudo port install libgeoip
  260. .. note::
  261. You will also have to modify the ``PATH`` in your ``.profile`` so
  262. that the MacPorts programs are accessible from the command-line::
  263. export PATH=/opt/local/bin:/opt/local/lib/postgresql83/bin
  264. In addition, add the ``DYLD_FALLBACK_LIBRARY_PATH`` setting so that
  265. the libraries can be found by Python::
  266. export DYLD_FALLBACK_LIBRARY_PATH=/opt/local/lib:/opt/local/lib/postgresql83
  267. __ http://www.macports.org/
  268. .. _windows:
  269. Windows
  270. -------
  271. Proceed through the following sections sequentially in order to install
  272. GeoDjango on Windows.
  273. .. note::
  274. These instructions assume that you are using 32-bit versions of
  275. all programs. While 64-bit versions of Python and PostgreSQL 9.x
  276. are available, 64-bit versions of spatial libraries, like
  277. GEOS and GDAL, are not yet provided by the :ref:`OSGeo4W` installer.
  278. Python
  279. ^^^^^^
  280. First, download the latest `Python 2.7 installer`__ from the Python Web site.
  281. Next, run the installer and keep the defaults -- for example, keep
  282. 'Install for all users' checked and the installation path set as
  283. ``C:\Python27``.
  284. .. note::
  285. You may already have a version of Python installed in ``C:\python`` as ESRI
  286. products sometimes install a copy there. *You should still install a
  287. fresh version of Python 2.7.*
  288. __ http://python.org/download/
  289. PostgreSQL
  290. ^^^^^^^^^^
  291. First, download the latest `PostgreSQL 9.x installer`__ from the
  292. `EnterpriseDB`__ Web site. After downloading, simply run the installer,
  293. follow the on-screen directions, and keep the default options unless
  294. you know the consequences of changing them.
  295. .. note::
  296. The PostgreSQL installer creates both a new Windows user to be the
  297. 'postgres service account' and a ``postgres`` database superuser
  298. You will be prompted once to set the password for both accounts --
  299. make sure to remember it!
  300. When the installer completes, it will ask to launch the Application Stack
  301. Builder (ASB) on exit -- keep this checked, as it is necessary to
  302. install :ref:`postgisasb`.
  303. .. note::
  304. If installed successfully, the PostgreSQL server will run in the
  305. background each time the system as started as a Windows service.
  306. A :menuselection:`PostgreSQL 9.x` start menu group will created
  307. and contains shortcuts for the ASB as well as the 'SQL Shell',
  308. which will launch a ``psql`` command window.
  309. __ http://www.enterprisedb.com/products-services-training/pgdownload
  310. __ http://www.enterprisedb.com
  311. .. _postgisasb:
  312. PostGIS
  313. ^^^^^^^
  314. From within the Application Stack Builder (to run outside of the installer,
  315. :menuselection:`Start --> Programs --> PostgreSQL 9.x`), select
  316. :menuselection:`PostgreSQL Database Server 9.x on port 5432` from the drop down
  317. menu. Next, expand the :menuselection:`Categories --> Spatial Extensions` menu
  318. tree and select :menuselection:`PostGIS 1.5 for PostgreSQL 9.x`.
  319. After clicking next, you will be prompted to select your mirror, PostGIS
  320. will be downloaded, and the PostGIS installer will begin. Select only the
  321. default options during install (e.g., do not uncheck the option to create a
  322. default PostGIS database).
  323. .. note::
  324. You will be prompted to enter your ``postgres`` database superuser
  325. password in the 'Database Connection Information' dialog.
  326. psycopg2
  327. ^^^^^^^^
  328. The ``psycopg2`` Python module provides the interface between Python and the
  329. PostgreSQL database. Download the latest `Windows installer`__ for your version
  330. of Python and PostgreSQL and run using the default settings. [#]_
  331. __ http://www.stickpeople.com/projects/python/win-psycopg/
  332. .. _osgeo4w:
  333. OSGeo4W
  334. ^^^^^^^
  335. The `OSGeo4W installer`_ makes it simple to install the PROJ.4, GDAL, and GEOS
  336. libraries required by GeoDjango. First, download the `OSGeo4W installer`_,
  337. and run it. Select :menuselection:`Express Web-GIS Install` and click next.
  338. In the 'Select Packages' list, ensure that GDAL is selected; MapServer and
  339. Apache are also enabled by default, but are not required by GeoDjango and
  340. may be unchecked safely. After clicking next, the packages will be
  341. automatically downloaded and installed, after which you may exit the
  342. installer.
  343. .. _OSGeo4W installer: http://trac.osgeo.org/osgeo4w/
  344. Modify Windows environment
  345. ^^^^^^^^^^^^^^^^^^^^^^^^^^
  346. In order to use GeoDjango, you will need to add your Python and OSGeo4W
  347. directories to your Windows system ``Path``, as well as create ``GDAL_DATA``
  348. and ``PROJ_LIB`` environment variables. The following set of commands,
  349. executable with ``cmd.exe``, will set this up:
  350. .. code-block:: bat
  351. set OSGEO4W_ROOT=C:\OSGeo4W
  352. set PYTHON_ROOT=C:\Python27
  353. set GDAL_DATA=%OSGEO4W_ROOT%\share\gdal
  354. set PROJ_LIB=%OSGEO4W_ROOT%\share\proj
  355. set PATH=%PATH%;%PYTHON_ROOT%;%OSGEO4W_ROOT%\bin
  356. reg ADD "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path /t REG_EXPAND_SZ /f /d "%PATH%"
  357. reg ADD "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v GDAL_DATA /t REG_EXPAND_SZ /f /d "%GDAL_DATA%"
  358. reg ADD "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v PROJ_LIB /t REG_EXPAND_SZ /f /d "%PROJ_LIB%"
  359. For your convenience, these commands are available in the executable batch
  360. script, :download:`geodjango_setup.bat`.
  361. .. note::
  362. Administrator privileges are required to execute these commands.
  363. To do this, right-click on :download:`geodjango_setup.bat` and select
  364. :menuselection:`Run as administrator`. You need to log out and log back in again
  365. for the settings to take effect.
  366. .. note::
  367. If you customized the Python or OSGeo4W installation directories,
  368. then you will need to modify the ``OSGEO4W_ROOT`` and/or ``PYTHON_ROOT``
  369. variables accordingly.
  370. Install Django and set up database
  371. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  372. Finally, :ref:`install Django <installing-official-release>` on your system.
  373. .. rubric:: Footnotes
  374. .. [#] GeoDjango uses the :func:`~ctypes.util.find_library` routine from
  375. ``ctypes.util`` to locate shared libraries.
  376. .. [#] The ``psycopg2`` Windows installers are packaged and maintained by
  377. `Jason Erickson <http://www.stickpeople.com/projects/python/win-psycopg/>`_.