top of page
Writer's picture Midhun S

Deploying Geospatial Web Applications: A Comprehensive Guide Using Django, PostGIS, and Cloud Technologies

Updated: Oct 13

Introduction: The Power of Web Maps and Geospatial Data Centers

Web maps and geospatial data centers are crucial for industries like urban planning, environmental management, transportation, and emergency services. They offer real-time, interactive access to spatial data that helps governments, businesses, and organizations make informed decisions. With the rise of cloud computing and web technologies, it has never been easier to create and host these powerful tools.

Cover image

In this guide, we’ll walk you through the step-by-step process of setting up a web mapping platform and building a geospatial data center. This blog is designed for geospatial professionals, GIS developers, and data scientists who want to bring their data online, create interactive maps, and manage their geospatial data in an organized, scalable manner. We’ll showcase how we at Avakaza Geoscience successfully implemented this, and you can view the live example here: Manchitra Prajna Web Mapping Platform.


Technologies Covered:

  • Python & Django: Backend framework for building web applications, including geospatial services.

  • PostGIS: An extension to PostgreSQL that adds support for geographic objects, enabling spatial queries.

  • Leaflet.js: A lightweight, open-source JavaScript library for creating interactive web maps.

  • Mapbox: A platform for customizable map creation with powerful styling options.

  • PyDeck & Deck.gl: Libraries for 3D terrain visualization and advanced map rendering.

  • AWS EC2 & RDS: Cloud hosting infrastructure for scalable and reliable geospatial services.

  • GDAL/OGR: Tools for processing and converting geospatial data formats

  • Django REST Framework with GIS Extensions: For building APIs to expose geospatial data.


Step 1: Setting Up the Development Environment

1.1 Install Django and Create Your Project

-Start by setting up a Python virtual environment and installing Django:


>>Create and activate a virtual environment

python3 -m venv myenv

source myenv/bin/activate

>>Install Django

pip install django

-Create a new Django project and app:

django-admin startproject myproject

cd myproject

django-admin startapp myapp


1.2 Set Up PostgreSQL and PostGIS

-PostGIS extends PostgreSQL to handle geospatial data. Install PostgreSQL and PostGIS:

sudo apt-get install postgresql postgis


-In PostgreSQL, create your geospatial database:

-CREATE DATABASE geospatial_db;

\c geospatial_db

-CREATE EXTENSION postgis;


-Next, configure the Django settings to use PostGIS in settings.py:


DATABASES = {

'default': {

'ENGINE': 'django.contrib.gis.db.backends.postgis',

'NAME': 'geospatial_db',

'USER': 'postgres',

'PASSWORD': 'your_password',

'HOST': 'localhost',

'PORT': '5432',

}

}


1.3 Install GDAL and Geospatial Tools

GDAL is an essential tool for working with geospatial data. Install GDAL on your system:


sudo apt-get install gdal-bin


Set up the path to GDAL in your settings.py file:


import os

GDAL_LIBRARY_PATH = os.getenv('GDAL_LIBRARY_PATH', '/usr/local/lib/libgdal.so')


Step 2: Building the Geospatial Data Backend

2.1 Creating Geospatial Models


Django’s django.contrib.gis module allows you to work with spatial data types. Let’s define a model to store point data (like locations) using the PointField:


from django.contrib.gis.db import models


class Location(models.Model):

name = models.CharField(max_length=100)

point = models.PointField()


def _str_(self):

return self.name


2.2 Creating Geospatial APIs

To expose your geospatial data via APIs, use Django Rest Framework and the GIS extension:


pip install djangorestframework djangorestframework-gis


Now, create a serializer and view for the Location model:


from rest_framework_gis.serializers import GeoFeatureModelSerializer

from .models import Location


class LocationSerializer(GeoFeatureModelSerializer):

class Meta:

model = Location

geo_field = 'point'

fields = ['name']


And create an API view to expose the geospatial data:


from rest_framework import viewsets

from .models import Location

from .serializers import LocationSerializer


class LocationViewSet(viewsets.ModelViewSet):

queryset = Location.objects.all()

serializer_class = LocationSerializer


In your urls.py:


from django.urls import path, include

from rest_framework.routers import DefaultRouter

from .views import LocationViewSet


router = DefaultRouter()

router.register(r'locations', LocationViewSet)


urlpatterns = [

path('api/', include(router.urls)),

]


Step 3: Adding Interactive Web Maps

3.1 Setting Up Leaflet.js


Leaflet.js is a simple yet powerful library for creating interactive maps. Add Leaflet.js to your Django template:


<head>

<link rel="stylesheet" href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css" />

</head>


<body>

<div id="map" style="height: 400px;"></div>


<script>

var map = L.map('map').setView([51.505, -0.09], 13);

L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {

maxZoom: 19

}).addTo(map);

</script>

</body>


3.2 Integrating Mapbox for Custom Maps

Mapbox offers high-quality map visualizations. Add it to your template:



<script>

mapboxgl.accessToken = 'your_mapbox_access_token';

var map = new mapboxgl.Map({

container: 'map',

style: 'mapbox://styles/mapbox/streets-v11',

center: [77.5946, 12.9716], // Coordinates of Bengaluru, India

zoom: 12

});

</script>


3.3 Building 3D Terrain Models with PyDeck

For advanced 3D visualizations, use PyDeck with Deck.gl. Here’s an example of a simple 3D column layer:

import pydeck as pdk


# Define the map view

view_state = pdk.ViewState(

latitude=12.9716,

longitude=77.5946,

zoom=12,

pitch=45

)


# Define a 3D column layer

layer = pdk.Layer(

'ColumnLayer',

data=[{'lat': 12.9716, 'lon': 77.5946, 'elevation': 100}],

get_position=['lon', 'lat'],

get_elevation='elevation',

get_fill_color='[255, 0, 0]',

radius=200,

elevation_scale=100

)


# Render the deck.gl map

r = pdk.Deck(layers=[layer], initial_view_state=view_state)

r.to_html('terrain_model.html')


Step 4: Deploying the Application on AWS

4.1 Hosting the Application with AWS EC2


Create an AWS EC2 instance to host your application. After launching the instance, connect to it via SSH:


ssh -i your-key.pem ec2-user@your-ec2-public-ip


-Set up the environment by installing required packages:


sudo apt-get update

sudo apt-get install python3-pip python3-venv nginx postgresql postgis


-Clone your project and set up the virtual environment:-


cd your-project

python3 -m venv venv

source venv/bin/activate

pip install -r requirements.txt


4.2 Setting Up PostgreSQL and PostGIS on AWS RDS

You can also use AWS RDS for your database. Set up an RDS instance with PostgreSQL and PostGIS, then update your settings.py:


DATABASES = {

'default': {

'ENGINE': 'django.contrib.gis.db.backends.postgis',

'NAME': 'postgres',

'USER': 'your_rds_username',

'PASSWORD': 'your_rds_password',

'HOST': 'your_rds_endpoint',

'PORT': '5432',

}

}


4.3 Configuring Gunicorn and Nginx

Set up Gunicorn to serve your Django application:

gunicorn --workers 3 --bind 0.0.0.0:8000 myproject.wsgi:application


Then configure Nginx to reverse proxy to Gunicorn:

server {

listen 80;

server_name your_domain.com;


location / {

proxy_pass http://127.0.0.1:8000;

}

}


Enable the configuration and restart Nginx:


sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled

sudo systemctl restart nginx



Conclusion

By following this guide, you’ll be able to set up a comprehensive web mapping platform and geospatial data center. At Avakaza Geoscience, we specialize in delivering geospatial solutions, from data center development to advanced mapping platforms. For more information or personalized training, visit our website at Avakaza Geoscience Research Technologies (AGSRT)- https://www.agsrt.com/

Or explore our live demo at Manchitra Prajna Private Limited:- https://manchitraprajna.com/

Comentarios


bottom of page