Geo-Spatial Rest Backend Using GeoDjango
dj

I am Nayan Das!
@_themadnerd
- Backend Developer by day, devops engineer by night
- Have experience in Nodejs, Django, Reactjs etc
- In my free time, I love to read books and manga
- Cooks tasty API’s and web services
@NayanJD


What is Geographic Information System?

Geographic Map Data
+

Map Analysis and Queries
What is Geospatial?
-
If we summarise GIS and GeoSpatial, geo spatial is GIS - the whole map data.
Only Map Analysis and Queries

Spatial Reference System ID (SRID)
-
Measuring unit for geometry types.
-
It specifies which units (degrees/metres) are going to be used to represent the geometry types.
-
We are going to use SRID 4326
SRID 900913
(in metres)
SRID 4326
(in degrees)
Bengaluru/ Coordinates
8637791 m, 1456487 m
Bengaluru/ Coordinates
12.9716 °N, 77.5946 °E
...a small caveat
-
Longitude is x
-
Latitude is y
-
...not the other way around
(30°,15°)
(x, y)
(lat, long)



Geometries Representations
GeoJson
Geographic Mark-up Language
Well Known Text (WKT)
GeoJson

Geographic Mark-up Language

Well Known Text (WKT)

Geometry Types
-
Point
-
LineString
-
Polygon
Simple Types
Collection Types
-
MultiPoint
-
MultiLineString
-
MultiPolygon
-
GeometryCollection
Point as GeoJson

e.g. GeekyAnts On Maps

Linestring as GeoJson

e.g. route along road between two points

Polygon as GeoJson

e.g. an area of sub-locality of BTM

Tech stack used
Python
Django
Django Rest Framework
Postgres (with postgis extension)

Geos API
- Stands for Geometry Engine - Open Source
- Is an open source c++ library
- GeoDjango has its own implementation of the GEOS library.

GEOS Functions
1 .contains
- Checks if a geometry is inside another geometry.

-
Invoked as polygon.contains(point)

GEOS methods & properties
2 .distance
-
Returns the distance between the closest point in this geometry and the other geometry

- Invoked as polygon.distance(point)

GIS ORM
- Extends Django ORM
- Provides spatial lookup and queries
- Takes advantages of database procedures by postgis and others
Retrieve factories which covers a given point
Using GEOS API
Using ORM
GEOS API would have to collect all rows of database in memory


GIS ORM would do the query right in database. Thus, efficient.
GIS ORM (QuerySet) API Ops
contains

intersects



More GIS ORM Ops
-
overlaps
-
bboverlaps
-
bbcontains
-
etc.
PROS
1. Fully supports GIS Standards
2. Provides Spatial ORM
3. Low learning curve
4. Plug & Play nature
CONS
1. Is overkill if geometries do not have to be stored
2. Has limited support for MySQL
References
1. https://www.gislounge.com/what-is-gis/
2. https://www.django-rest-framework.org/api-guide/serializers/
3. https://docs.djangoproject.com/en/3.1/ref/contrib/
gis/geoquerysets/
4. https://docs.djangoproject.com/en/3.2/ref/contrib/gis/geos/
Thank You
Geo-Spatial REST Service using GeoDjango
By Nayan Jyoti Das
Geo-Spatial REST Service using GeoDjango
- 241