Setting up 🌏 Geodjango 3 on Mac OS X 🖥️ under 15 minutes!
I’m a huge fan of Django Framework because it is by far one of the easiest , secured and fun 😉 way to create really complicated project. I work in GIS Industry where I deal with geospatial enable data all the time. Which lot of the time includes cleaning data, performing some basic analysis and then consume in the web app and since we live in an era where python provides solutions for almost everything, it is an obvious decision to vote for django above the php frameworks like laravel, zend ,etc.
What is Django ?
Django is a python based MTV(model, template, view) framework designed to create a simple, elegant, secure, fast project
You can read everything about Django 👉🏻 here 👈🏻 .
What is GeoDjango ?
Geodjango is an extension to the existing django framework where developer can leverage world class framework features available in django with the goodness of 🌏 Geospatial Data 🌎 .Geodjango supports all databases such as PostgreSQL (PostGIS), MySQL , SQLite (SpatiaLite), Oracle, etc.
This enables us to use geometry data in the way it is intended, allowing user to do geospatial analysis (buffer, finding nearest points, etc.)
Installing GeoDjango on MacOS X 🖥️
It’s little bit trickier to install Geodjango on MacOS compared with Windows because some of the geospatial libraries are not available in a executable .dmg
In this Blog we’ll be working with macOS Catelina ( 10.15.7) , Postgres 10, pgAdmin 4, anconda env.
🚀 Let’s get started 🚀
Step 1: Create environment to work with
It is important to keep the python libraries used in specific project separate from the universal libraries, hence it’s always a good choice to spin up new env. Put the following code in anaconda terminal
conda create -n geodjangoenv django=3.1
Step 2: Installing Home Brew for mac
Home Brew is a package manager for mac, most of the softwares or packages for which .dmg or any executable file is not available, home brew can help installing them. More information can be found 👉🏻 here 👈🏻.
put the following command in terminal
/bin/bash -c “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
Step 3: Installing Geospatial Database
As mentioned we’ll be sticking with PostgreSQL (PostGIS) for this blog, feel free to copy the steps for other databases as well.
PostgreSQL can be installed in two ways here:
Street 1 : Installing PostgreSQL can related components via executables by downloading it from 👉🏻 here 👈🏻.
Street 2: Installing PostgreSQL can related components via Home brew
brew install postgres
brew install postgis
Honestly, I like to keep my life simple, so I’ll be sticking with Street 1. Once you go the download page , find the stable version (I’m downloading 10.x for this blog)
Step 3.1: Creating PostgreSQL Database
Once the database is installed succesfully, open it by opening the pgAdmin Application to open postgreSQL interface. Then login with the default credentials
username = postgres ; password = postgres (Pretty lame , right?? 🙄)
then create a new database by right clicking on “database” , enter the desired name (postgresdb in my case).
Once the database is created, right click on name and open query tool
write the following command in the query window and click on run, this command will install postgis extension in the database, so that database can understand and run spatial queries
create extension postgis;
Step 4: Installing Geospatial Libraries
This is where we see true super powers of home brew. We’ll install gdal, libgeoip using terminal .
Open terminal and type
brew install gdal
brew install libgeoip
🎉 Installations complete 👏🏻
Creating GeoDjango project 🚀️
Creating geodjango project is exactly similar to creating django project.
Step 1 : Open folder in the terminal where you want to create project
Step 2: Make sure that the geodjangoenv (or whatever name that you gave) is activated
Step 3: Type following in command line to create new django project
django-admin startproject geodjangoproject
Step 4 (optional) : You can create new app inside the django folder according to your need.
Step 5 : Go to the settings.py file in the project and add following in INSTALLED_APPS array as following
INSTALLED_APPS = [
***,
django.contrib.gis ]
Step 6 : Modify the Database configuration in settings.py to connect django project with the database
DATABASES = {
“default”: {
“ENGINE”: “django.contrib.gis.db.backends.postgis”,
“NAME”: “postgresDb”, #Database name
“USER”: “postgres”, #PostgreSQL Username
“PASSWORD”: “postgres”, #PostgreSQL password
“HOST”: “localhost”,
“PORT”: “5432”,
}
}
Creating GeoDjango model 📚
Generally we create database table via creating entry in model.py file, which looks like this
which is now slightly changed in order to add geometry fields as well.
Support in GeoDjango model 📚
Geodjango model supports all typed of geometries i.e. Point, Linestring, Polygon, Multi Polygon,etc. More about it can be found 👉🏻 here 👈🏻. Once the geodjango model is created successfully, then based on the geometry type used, user can login to admin panel and add geometry to the database.
Further development in GeoDjango framework 📈
Once the geodjango model is setup properly, you can add and extract geospatial data successfully from database using the views available in django project. Once the data is saved successfully, geodjango api allows us to perform geospatial analysis on this data. Make sure you subscribe to the publication for further blogs on how to use geodjango framework.
Follow the publications ‘Random GIS talks’ for my upcoming blogs on :
1. Adding and showing the data on opensource Mapping application using geodjango as a framework.
2. Getting started with Geoserver
and many more 😍…
Read the existing blogs on :
1. Installation of geoserver on mac / windows
2. Creating Dynamic legend on mapbox
and many more 😍…
About me
Hi, I’m Krishna Lodha, I’m a full stack Web GIS developer. I’m really enthusiastic about the power of locations and what story it tells us. Read more about me at http://krishnaglodha.com , to get more information about this code or suggest a better way to do it. ping me on LinkedIn at https://www.linkedin.com/in/krishnaglodha/
Adios!