官术网_书友最值得收藏!

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:

  1. 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
    
  2. Add this line to the file, save, and then close it:
    $ deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main
    
  3. Now, run the wget command for add the key:
    $ wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | \ sudo apt-key add -
    
  4. Run the update command to actualize your installer packages:
    $ sudo apt-get update
    
  5. Run the install command to actually install PostgreSQL 9.3:
    $ sudo apt-get install postgresql-9.3
    
  6. To install PostGIS 2.1, we will have one unmet dependency, libgdal1, so go ahead and install it:
    $ sudo apt-get install libgdal1
    
  7. Now we can install PostGIS 2.1 for PostgreSQL 9.3 on our machine:
    $ sudo apt-get install postgresql-9.3-postgis-2.1
    
  8. Install the PostgreSQL header files:
    $ sudo apt-get install libpq-dev
    
  9. Finally, install the contrib module with contributions:
    $ sudo apt-get install postgresql-contrib
    
  10. Install the Python database adapter, psycopg2, to connect to your PostgreSQL database from Python:
    $ sudo apt-get install python-psycopg2
    
  11. Now we can create a standard PostgreSQL database as follows:
    (pygeoan_cb)mdiener@mdiener-VirtualBox:~$ createdb [NewDatabaseName]
    
  12. 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;"
    
  13. Moving on, we can finally install Django in one line directly in our activated virtual environment:
    $ pip install django
    
  14. 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:

主站蜘蛛池模板: 布尔津县| 新宾| 紫阳县| 任丘市| 昌乐县| 藁城市| 牡丹江市| 开封市| 巢湖市| 高青县| 达尔| 罗山县| 连平县| 麻栗坡县| 博乐市| 巩留县| 桑植县| 泰兴市| 高邮市| 墨玉县| 天水市| 福贡县| 北碚区| 翁牛特旗| 娱乐| 中山市| 邳州市| 青州市| 藁城市| 正阳县| 成安县| 民权县| 高阳县| 五指山市| 铁岭市| 屯昌县| 华池县| 南宫市| 龙游县| 安福县| 广东省|