- Python Geospatial Analysis Cookbook
- Michael Diener
- 661字
- 2021-07-30 10:13:22
Installing GeoDjango and PostgreSQL with PostGIS
This is our final installation recipe and if you have followed along so far, you are ready for a simple, straightforward start to Django. Django is a web framework for professionals with deadlines, according to the Django homepage. The spatial part of it can be found in GeoDjango. GeoDjango is a contrib module installed with every Django installation therefore, you only need to install Django to get GeoDjango running. Of course, "geo" has its dependencies that were met in the previous sections. For reference purposes, take a look at this great documentation on the Django homepage at
https://docs.djangoproject.com/en/dev/ref/contrib/gis/install/#ref-gis-install.
We will use PostgreSQL and PostGIS since they are the open source industry go-to spatial databases. The installations are not 100% necessary, but without them there is no real point because you then limit your operations, and they're definitely needed if you plan to store your spatial data in a spatial database. The combination of PostgreSQL and PostGIS is the most common spatial database setup for GeoDjango. This installation is definitely more involved and can lead to some hook-ups depending on your system.
Getting ready
To use GeoDjango, we will need to have a spatial database installed, and in our case, we will be using PostgreSQL with the PostGIS extension. GeoDjango also supports Oracle, Spatialite, and MySQL. The dependencies of PostGIS include GDAL, GEOS, PROJ.4, LibXML2, and JSON-C.
Start up your Python virtual environment as follows:
mdiener@mdiener-VirtualBox:~$ workon pygeoan_cb (pygeoan_cb)mdiener@mdiener-VirtualBox:~$
Tip
Downloading the example code
You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
How to do it...
Follow these steps. These are taken from the PostgreSQL homepage for Ubuntu Linux:
- Create a new file called
pgdg.list
using the standard gedit text editor. This stores the command to fire up your Ubuntu installer package:$ sudo gedit /etc/apt/sources.list.d/pgdg.list
- Add this line to the file, save, and then close it:
$ deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main
- Now, run the
wget
command for add the key:$ wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | \ sudo apt-key add -
- Run the
update
command to actualize your installer packages:$ sudo apt-get update
- Run the
install
command to actually install PostgreSQL 9.3:$ sudo apt-get install postgresql-9.3
- To install PostGIS 2.1, we will have one unmet dependency,
libgdal1
, so go ahead and install it:$ sudo apt-get install libgdal1
- Now we can install PostGIS 2.1 for PostgreSQL 9.3 on our machine:
$ sudo apt-get install postgresql-9.3-postgis-2.1
- Install the PostgreSQL header files:
$ sudo apt-get install libpq-dev
- Finally, install the
contrib
module with contributions:$ sudo apt-get install postgresql-contrib
- Install the Python database adapter,
psycopg2
, to connect to your PostgreSQL database from Python:$ sudo apt-get install python-psycopg2
- Now we can create a standard PostgreSQL database as follows:
(pygeoan_cb)mdiener@mdiener-VirtualBox:~$ createdb [NewDatabaseName]
- Using the
psql
command-line tool, we can create a PostGIS extension to our newly created database to give it all the PostGIS functions as follows:(pygeoan_cb)mdiener@mdiener-VirtualBox:~$ psql -d [NewDatabaseName] -c "CREATE EXTENSION postgis;"
- Moving on, we can finally install Django in one line directly in our activated virtual environment:
$ pip install django
- Test out your install of Django and GDAL and, as always, try to import them as follows:
>>> from django.contrib.gis import gdal >>> gdal.HAS_GDAL True
Windows users should be directed to the PostgreSQL Windows (http://www.postgresql.org/download/windows/) binaries provided by EnterpriseDB (http://www.enterprisedb.com/products-services-training/pgdownload#windows). Download the correct version and follow the installer instructions. PostGIS is also included in the list of extensions that you can directly install using the installer.
How it works...
Installations using the apt-get Ubuntu installer and the Windows installers are simple enough in order to have PostgreSQL, PostGIS, and Django up and running. However, the inner workings of the installers are beyond the scope of this book.
There's more...
To summarize all the installed libraries, take a look at this table:

- Java 開發從入門到精通(第2版)
- Boost C++ Application Development Cookbook(Second Edition)
- Learning Chef
- Nginx Essentials
- Android 應用案例開發大全(第3版)
- Python完全自學教程
- Elasticsearch for Hadoop
- Android底層接口與驅動開發技術詳解
- 軟件供應鏈安全:源代碼缺陷實例剖析
- Raspberry Pi Robotic Projects(Third Edition)
- Visual Basic程序設計實驗指導及考試指南
- Visual C++從入門到精通(第2版)
- Visual C++程序設計全程指南
- LabVIEW數據采集(第2版)
- Django 3 Web Development Cookbook