Setup and Deploy Geodjango App on Alibaba Cloud Part 2: Completing the App

Developing Our Application Locally

Usually when developing an application that should deployed on the cloud, you develop it locally, make edits, test it and then deploy to the cloud. We tested our application on an Alibaba Cloud Elastic Compute Service (ECS) instance, and now we can go ahead and download the project’s folder for local development and later update the server copy with the changes.

(geodjango) $ pip freeze > requirements.txt
scp -r user@your_server_ip:/coffeeshops /home/user/Desktop/

Creating the Geographic Application Component

We are now ready to start building the main part of the application . First we will need to create a django app inside our project. To do that, type the following inside our project’s root folder:

(geodjango) :~/home/user/Desktop/coffeeshops/$ djang-admin startapp geo

Creating a Coffee Shops Model

Now that we have our app setup, we will create a model to represent the data that each coffee shop will contain. To make it simple , each coffee shop will contain the following information:

  • name — the name of the coffee shop
  • location — the geographic coordinates of the coffee shop
geo/models.pyfrom django.contrib.gis.db import modelsclass CoffeeShop(models.Model):
name = models.CharField(max_length=50)
location = models.PointField(srid=4326)

# Returns the string representation of the model.
def __str__(self):
(geodjango) $ python makemigrations
(geodjango) $ python migrate

Loading Data with Layer Mapping

We now need to load data to our created table. We are going to use GeoDjango’s layer mapping data import utility to quickly import our data into the database table.

geo/load.pyimport os
from django.contrib.gis.utils import LayerMapping
from django.conf import settings
from .models import CoffeeShop
coffeeshop_mapping = {
coffeeshops_file = os.path.join(settings.BASE_DIR, 'data', 'cafes.geojson')def run(verbose=True):
lm = LayerMapping(CoffeeShop, coffeeshops_file, coffeeshop_mapping, transform=False), verbose=verbose)
(geodjango) $ python shell
>>> from geo import load
from django.contrib import admin
from django.contrib.gis.admin import OSMGeoAdmin
from .models import CoffeeShop
# Register your models here., OSMGeoAdmin)
ALLOWED_HOSTS = ['your_server_ip','localhost']
(geodjango) $ python createsuperuser
(geodjango) $ python runserver

Perfoming Geospatial Queries

You can perform geospatial queries on a Geodjango model. In our model, this includes queries like finding a cafe that is within some distance from a given point, or sorting the results by the distance from a point.

(geodjango) $ python shell
>>> from django.contrib.gis.geos import Point
>>> pnt = Point(36.8145,-1.2890)
>>> from geo.models import CoffeeShop
>>> from django.contrib.gis.measure import D
>>> CoffeeShop.objects.filter(location__distance_lte=(pnt,D(km=1)))[:5]
(geodjango) $ python shell
>>> from django.contrib.gis.geos import Point
>>> pnt = Point(36.8145,-1.2890)
>>> from geo.models import CoffeeShop
>>> from django.contrib.gis.db.models.functions import Distance
>>> CoffeeShop.objects.annotate(distance=Distance('location',pnt)).order_by('distance')[:5]

Updating the Project Folder on Alibaba Cloud ECS

We have made quite some changes to our project, and now we need to update the server project to match the local project folder. There are many approaches of making the changes. You can use git and push the changes to a remote, and then clone or pull the project on the server. But for simplicity, we are going to copy the local project to the server, and overwrite the existing one using the scp command line tool:

scp -r /home/user/Desktop/coffeeshops user@your_server_ip:~/
workon geodjango
(geodjango) user@server_ip:~/coffeeshops$ python migrate
(geodjango) user@server_ip:~/coffeeshops$ python shell
>>> from geo import load
(geodjango) user@server_ip:~/coffeeshops$ python runserver


And there we are! We have successfully set up a GeoDjango application and seen the kind of queries that can be performed on Geographic Django models. An Alibaba Cloud ECS instance allowed us to install Geospatial libraries that we needed, the Postgres/PostGIS database and the Python Environment to run our application.



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: