How to Connect Geodjango App with ApsaraDB RDS for PostgreSQL

  1. Django model fields for OGC geometries and raster data.
  2. Extensions to Django’s ORM for querying and manipulating spatial data.
  3. Loosely-coupled, high-level Python interfaces for GIS geometry and raster operations and data manipulation in different formats.
  4. Editing geometry fields from the admin.

Prerequisites

For you to successfully complete this tutorial. you will need to have:

  1. A valid Alibaba Cloud account. If you don’t have one already, sign up to the Free Trial to enjoy up to $300 worth in Alibaba Cloud products.
  2. An ECS instance running Ubuntu 16.04. You can select your preferred region and configurations; this will not affect the outcome of the server setup.
  3. A root password for your server.
  4. Geodjango Setup on your ECS instance.
  5. An ApsaraDB RDS for PostgreSQL instance.

Step 1: Create a Virtual Environment

The main purpose of Python virtual environments is to create an isolated environment for Python projects. This means that each project can have its own dependencies, regardless of what dependencies every other project has.

$ sudo -H pip3 install --upgrade pip$ sudo -H pip3 install virtualenv
$ mkdir geodjango$ cd geodjango
$ virtualenv geodjangoenv
$ source geodjangoenv/bin/activate

Step 2: Install Python Packages

Next we will install python packages that will be need for the application we will create.

$ pip3 install django psycopg2
$ pip freeze > requirements.txt

Step 3:Create the Geodjango Application

Start by creating a directory geoadjango where your project will be housed.Then move into the directory and create the project.

$ mkdir geodjango && cd geodjango$ django-admin startproject myapp
myapp/
manage.py
myapp/
__init__.py
settings.py
urls.py
wsgi.py
$ myapp/manage.py startapp worldapp
worldapp/
__init__.py
admin.py
apps.py
migrations/
__init__.py
models.py
tests.py
views.py

Step 4: Configure Settings.py

Open settings.py file and modify the INSTALLED_APPS setting to include django.contrib.admin, django.contrib.gis, and worldapp (your newly created application):

$ nano myapp/settings.pyINSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.gis',
'worldapp',
]

Step 5: Connecting to ApsaraDB RDS for PostgreSQL

When you connect to an RDS instance through a client, choose to use an intranet or Internet address as follows:

  • Use the intranet IP address when your client is installed on the ECS that is located in the same region and the same network type as the RDS instance to be connected.
  • Use the Internet IP address for the other situations.
DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': '<database name>',
'USER': '<user>',
'PASSWORD':'<password>'
'HOST':'<database host>'
'PORT':'<database port>'
},
}

Step 6: Download Geospatial Datasets

We will be using world borders for this tutorial. Create a directory called data in your worldapp directory, download the borders and unzip the file:

$ mkdir worldapp/data$ cd worldapp/data$ wget https://thematicmapping.org/downloads/TM_WORLD_BORDERS-0.3.zip$ unzip TM_WORLD_BORDERS-0.3.zip$ cd ../..
  • .shp: Holds the vector data for the world borders geometries.
  • .shx: Spatial index file for geometries stored in the .shp.
  • .dbf: Database file for holding non-geometric attribute data (e.g., integer and character fields).
  • .prj: Contains the spatial reference information for the geographic data stored in the shapefile.
$ ogrinfo -so world/data/TM_WORLD_BORDERS-0.3.shp TM_WORLD_BORDERS-0.3
INFO: Open of `world/data/TM_WORLD_BORDERS-0.3.shp'
using driver `ESRI Shapefile' successful.
Layer name: TM_WORLD_BORDERS-0.3
Geometry: Polygon
Feature Count: 246
Extent: (-180.000000, -90.000000) - (180.000000, 83.623596)
Layer SRS WKT:
GEOGCS["GCS_WGS_1984",
DATUM["WGS_1984",
SPHEROID["WGS_1984",6378137.0,298.257223563]],
PRIMEM["Greenwich",0.0],
UNIT["Degree",0.0174532925199433]]
FIPS: String (2.0)
ISO2: String (2.0)
ISO3: String (3.0)
UN: Integer (3.0)
NAME: String (50.0)
AREA: Integer (7.0)
POP2005: Integer (10.0)
REGION: Integer (3.0)
SUBREGION: Integer (3.0)
LON: Real (8.3)
LAT: Real (7.3)

Step 7: Define Geographic Models

Open worldapp/models.py and add the following:

$ nano worldapp/models.pyfrom django.contrib.gis.db import modelsclass WorldBorder(models.Model):
# Regular Django fields corresponding to the attributes in the
# world borders shapefile.
name = models.CharField(max_length=50)
area = models.IntegerField()
pop2005 = models.IntegerField('Population 2005')
fips = models.CharField('FIPS Code', max_length=2)
iso2 = models.CharField('2 Digit ISO', max_length=2)
iso3 = models.CharField('3 Digit ISO', max_length=3)
un = models.IntegerField('United Nations Code')
region = models.IntegerField('Region Code')
subregion = models.IntegerField('Sub-Region Code')
lon = models.FloatField()
lat = models.FloatField()
# GeoDjango-specific: a geometry field (MultiPolygonField)
mpoly = models.MultiPolygonField()
# Returns the string representation of the model.
def __str__(self):
return self.name

Step 8: Sync Models with Database

After defining the models, you will need to sync them with the database by typing:

$ myapp/manage.py makemigrations
$ myapp/manage.py migrate

Step 9: Layer Mapping the Spatial Data

We will import the world borders shapefile into the database via GeoDjango models using the LayerMapping data import utility.

import os
from django.contrib.gis.utils import LayerMapping
from .models import WorldBorder
world_mapping = {
'fips' : 'FIPS',
'iso2' : 'ISO2',
'iso3' : 'ISO3',
'un' : 'UN',
'name' : 'NAME',
'area' : 'AREA',
'pop2005' : 'POP2005',
'region' : 'REGION',
'subregion' : 'SUBREGION',
'lon' : 'LON',
'lat' : 'LAT',
'mpoly' : 'MULTIPOLYGON',
}
world_shp = os.path.abspath(
os.path.join(os.path.dirname(__file__), 'data', 'TM_WORLD_BORDERS-0.3.shp'),
)
def run(verbose=True):
lm = LayerMapping(WorldBorder, world_shp, world_mapping, transform=False)
lm.save(strict=True, verbose=verbose)
$ myapp/manage.py shell
>>> from worldapp import load
>>> load.run()
...
Saved: Antigua and Barbuda
Saved: Algeria
Saved: Azerbaijan
Saved: Albania
Saved: Armenia
...

Step 10: Interact with the Geographic Admin

Let’s add the following to admin.py file inside the worldapp directory:

from django.contrib.gis import admin
from .models import WorldBorder
admin.site.register(WorldBorder, admin.GeoModelAdmin)
from django.contrib.gis import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
]
$ myapp/manage.py createsuperuser$ myapp/manage.py runserver 0.0.0.0:8000
http://server_domain_or_IP:8000

Conclusion

With everything in place you should now be able to connect your ApsaraDB RDS for PostgreSQL with your Geodjango Application. I hope this tutorial came in handy.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alibaba Cloud

Alibaba Cloud

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com