In this post we will learn about the basic steps required to setup geodjango
Code repository : https://github.com/tayyabsayyad/treemapping
Create a virtual Envirnment ( Install Virtualenv tool in you system if not installed)
python3 -m venv env
Activate the envirnment
source env/bin/activate
Install Django ( Install pip if not installed in system)
pip install django
Create Project
django-admin.py startproject tree
Configure the posgreSQL database
DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': 'database_name',
'USER': 'user_name_database',
'PASSWORD': 'password',
'HOST': 'localhost',
'PORT': '5432'
}
}
Install Required libraries
pip install psycopg2-binary
pip install geos
pip install pyproj
pip install geoip
pip install django-leaflet
To install the postgis and geoserver refer Post : Installation of PostGIS
Add geodjango framework in the setting.py file
INSTALLED_APPS = [
# [...]
'django.contrib.gis'
]
Now we will make Create the treemapping app, we will add its model
, migrate
the database, add view
function and finally a template
for frount end
Create Application as treemapping
python manage.py startapp shops
Add treemapping app into INSTALLED_APPS in setting.py
INSTALLED_APPS = [
# [.....]
'django.contrib.gis'
'treemapping',
]
Creating a model for tree mapping application
add following to the treemapping/models
class TreeData(models.Model):
treeID = models.IntegerField(default=0)
location = models.PointField()
commonName = models.CharField(max_length=100)
scientificName = models.CharField(max_length=100)
trunkDiameter = models.IntegerField(default=0)
treeHeight = models.IntegerField(default=0)
datePlanted = models.DateTimeField(auto_now=True)
condition = models.CharField(max_length=100)
treeStewardship = models.CharField(max_length=100)
address = models.CharField(max_length=100)
city = models.CharField(max_length=100)
pincode = models.IntegerField(default=413405)
Creating Tables in the database
python manage.py makemigrations
python manage.py migrate
Add super user to your application
python manage.py createsuperuser
Register Admin Module on treemapping/admin.py
from django.contrib import admin
from .models import TreeData
admin.site.register(TreeData)
By default it will use openlayer for displaying map, if you want to use the openstreetmap then use folllwoing way to register admin Module
from django.contrib.gis.admin import OSMGeoAdmin
from django.contrib.gis import admin
from .models import TreeData
@admin.register(TreeData)
class TreeAdmin(OSMGeoAdmin):
list_display = ('location', 'city')
Finally run django server
python manage.py runserver