googledjango

Get started with GeoDjango

GeoDjango intends to be a world-class geographic Web framework. Its goal is to make it as easy as possible to build GIS (Geographic information system) Web applications. GeoDjango is an included contrib module for Django.

PostGIS is included in all stacks that ship PostgreSQL.

Create a database template with PostGIS support

NOTE: The following section assumes that you are working with Django 1.5+ which already supports PostGIS 2.0+. It is based on the official GeoDjango documentation.

Create a database template by following the steps below. In this example, the template will be named template_postgis.

  • Create a template:

      $ createdb template_postgis
    
      # Allows non-superusers the ability to create from this template
      $ psql -d postgres -c "UPDATE pg_database SET datistemplate='true' WHERE datname='template_postgis';"
    
      $ psql template_postgis -c "create extension postgis"
      $ psql template_postgis -c "create extension postgis_topology"
      $ psql template_postgis -f /opt/bitnami/postgresql/share/contrib/postgis-2.0/legacy.sql
    
      # Enabling users to alter spatial tables.
      $ psql -d template_postgis -c "GRANT ALL ON geometry_columns TO PUBLIC;"
      $ psql -d template_postgis -c "GRANT ALL ON geography_columns TO PUBLIC;"
      $ psql -d template_postgis -c "GRANT ALL ON spatial_ref_sys TO PUBLIC;"
    

    To check the version of PostGIS and its dependencies, execute the following command:

      $ psql template_postgis -c "SELECT PostGIS_Full_Version()"
    

    NOTE: Although the official documentation suggests loading legacy.sql only “if really necessary”, we have found that it is required for installing Tiger Geocoder and running the OGC tests suite. Also, it is necessary when restoring data from an old database. Once these tasks are done, you can always uninstall it using uninstall_legacy.sql.

  • Create a new database from the template with PostGIS support using this command:

      $ createdb -T template_postgis geocoder
    

    Or, from the database console:

      CREATE DATABASE geocoder TEMPLATE template_postgis;
    

Create a geographic web application using GeoDjango

NOTE: The following section assumes that you are working with Django 1.5+ which already supports PostGIS 2.0+. It is based on the official GeoDjango documentation.

Follow these steps:

  • Create a spatial database:

      $ createdb -T template_postgis geodjango
    
  • Create a new project:

      $ django-admin.py startproject geodjango
      $ cd geodjango
      $ python3 manage.py startapp world
    
  • Configure database settings and add a few variables so GEOS and GDAL components are found. Edit the project settings file geodjango/settings.py and update it to look like this:

      DATABASES = {
        'default': {
            'ENGINE': 'django.contrib.gis.db.backends.postgis',
            'NAME': 'geodjango',  # Name of your spatial database
            'USER': 'postgres',   # Database user
            'PASSWORD': 'bitnami',# Database password
            'HOST': 'SERVER-IP',
            'PORT': '5432',
        }
      }
    
      GEOS_LIBRARY_PATH = '/opt/bitnami/postgresql/lib/libgeos_c.so'
      GDAL_LIBRARY_PATH = '/opt/bitnami/postgresql/lib/libgdal.so'
      GDAL_DATA = '/opt/bitnami/postgresql/share/gdal'
    
  • In the same file, include the django.contrib.admin.gis and world applications in the INSTALLED_APPS list:

      INSTALLED_APPS = (
          'django.contrib.auth',
          'django.contrib.contenttypes',
          'django.contrib.sessions',
          'django.contrib.sites',
          'django.contrib.messages',
          'django.contrib.staticfiles',
          'django.contrib.admin',
          'django.contrib.gis',
          'world'
      )
    

Now that the environment is set, follow the official GeoDjango tutorial, starting from the “Geographic Data” section.

Last modification December 21, 2022