GeoDjango is an extension of Django that adds support for geographic data. It leverages the PostGIS extension for PostgreSQL, allowing you to perform complex geographic queries right within your Django models. If you’re building applications that involve maps, location-based services, or spatial data, GeoDjango is the perfect solution.
In this blog post, we’ll cover how to set up a GeoDjango project, define geospatial models, and perform a variety of geospatial queries using Django’s ORM.
GeoDjango is a powerful addition to the Django web framework that allows you to manage geospatial data. By integrating with PostGIS, a PostgreSQL extension that supports spatial data, GeoDjango allows you to:
GeoDjango supports fields such as PointField, PolygonField, LineStringField, and more. It also provides a variety of queryset methods to work with spatial data, allowing you to efficiently query geographic features.
Before you begin working with GeoDjango, you need to set up a Django project with the appropriate configuration. Follow these steps to get your environment ready:
First, ensure you have Python, PostgreSQL, and the PostGIS extension installed on your system. Then, install the necessary Python packages using pip:
pip install Django psycopg2 djangorestframework
Create a PostgreSQL database with the PostGIS extension enabled:
createdb my_geodjango_project
psql my_geodjango_project -c "CREATE EXTENSION postgis;"
Related read: Implementation of Django APScheduler with PostgreSQL
In your settings.py file, ensure that ‘django.contrib.gis’ is included in your INSTALLED_APPS, and configure the database connection to use PostGIS:
INSTALLED_APPS = [
'django.contrib.gis',
'rest_framework',
# other apps
]
DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': 'my_geodjango_project',
'USER': 'your_username',
'PASSWORD': 'your_password',
'HOST': 'localhost',
'PORT': '5432',
}
}
GeoDjango provides specialized fields for storing spatial data. For example, we can create a Location model that stores points (latitude and longitude) and a CityBoundary model that stores polygons (representing city boundaries).
This model will represent a geographic point (latitude and longitude).
from django.contrib.gis.db import models
class Location(models.Model):
name = models.CharField(max_length=100)
point = models.PointField()
This model will represent a city boundary as a polygon.
from django.contrib.gis.db import models
class CityBoundary(models.Model):
name = models.CharField(max_length=100)
boundary = models.PolygonField()
Once the models are set up, we can use Django’s ORM to perform various geospatial queries. Below are some of the most common geospatial queries you’ll need:
➡️ Nearest Location
GeoDjango provides the ability to find the nearest locations to a given point. You can use the distance method to calculate distances between geographic points.
from django.contrib.gis.geos import Point
# Define the user's location (latitude, longitude)
user_location = Point(-73.935242, 40.730610, srid=4326) # New York City coordinates
# Query for locations within 5 km of the user's location
nearest_locations = Location.objects.annotate(distance=user_location.distance('point')) \
.filter(distance__lte=5000) # Filter locations within 5 km
nearest_locations = nearest_locations.order_by('distance')
In this example, we use Point() to create a point object for the user’s location. The query annotates the Location model with the distance between the user’s location and each location in the database. The results are filtered by distance and ordered by proximity.
➡️ Locations within a Bounding Box
Bounding boxes are used to define rectangular regions of geographic space. You can use the Polygon.from_bbox() method to create a polygon from a set of coordinates, and then use the within lookup to filter locations within that polygon.
from django.contrib.gis.geos import Polygon
# Define the coordinates for a bounding box (min_lon, min_lat, max_lon, max_lat)
bounding_box = Polygon.from_bbox([-74.1, 40.5, -73.5, 40.9])
# Query locations within the bounding box
locations_within_bbox = Location.objects.filter(point__within=bounding_box)
Here, Polygon.from_bbox() takes four arguments: the minimum and maximum latitudes and longitudes. The point__within lookup is used to filter locations that lie within the bounding box.
➡️ Locations within a Distance
This query allows you to filter locations that are within a certain distance from a given point. The distance_lte lookup allows you to filter based on the distance between two geometries.
from django.contrib.gis.measure import D
from django.contrib.gis.geos import Point
# User's location
user_location = Point(-73.935242, 40.730610, srid=4326)
# Query locations within 1 km of the user's location
locations_within_distance = Location.objects.filter(point__distance_lte=(user_location, D(km=1)))
Here, D(km=1) specifies a distance of 1 kilometer. The point__distance_lte filter retrieves all locations whose distance from the user_location is less than or equal to 1 km.
➡️ Locations Within City Boundaries
You can filter locations based on whether they fall within a specific city boundary. This can be done using the within lookup on a PolygonField.
# Assume we have a city boundary (polygon) object
city_boundary = CityBoundary.objects.get(name="New York")
# Query locations within the city boundary
locations_in_city = Location.objects.filter(point__within=city_boundary.boundary)
In this case, the point__within lookup checks whether the location’s point lies within the city’s boundary polygon.
➡️ Locations Near a City Center
To find locations near a city center, you can use the distance_lte lookup along with a predefined center point. For example, consider New York City’s coordinates as the city center.
# Define city center (latitude, longitude)
city_center = Point(-73.935242, 40.730610, srid=4326) # New York City center
# Query for locations within 5 km of the city center
locations_near_city_center = Location.objects.filter(point__distance_lte=(city_center, D(km=5)))
This query filters locations that are within 5 kilometers of New York City’s center.
GeoDjango is an incredibly powerful tool for building geospatial applications with Django. It provides seamless integration with PostGIS to store and query geographic data, enabling you to build applications that involve maps, locations, and geographic analysis.
In this guide, we’ve covered how to set up a GeoDjango project, create geospatial models, and perform a variety of spatial queries using Django’s ORM. Whether you’re building a map-based service or a location-aware application, GeoDjango gives you all the tools you need to handle spatial data efficiently.
With the geospatial capabilities provided by GeoDjango, the possibilities for your applications are endless. From simple proximity searches to complex spatial analytics, GeoDjango opens up a world of possibilities for developers building location-based services.
The team at Mindbowser was highly professional, patient, and collaborative throughout our engagement. They struck the right balance between offering guidance and taking direction, which made the development process smooth. Although our project wasn’t related to healthcare, we clearly benefited...
Founder, Texas Ranch Security
Mindbowser played a crucial role in helping us bring everything together into a unified, cohesive product. Their commitment to industry-standard coding practices made an enormous difference, allowing developers to seamlessly transition in and out of the project without any confusion....
CEO, MarketsAI
I'm thrilled to be partnering with Mindbowser on our journey with TravelRite. The collaboration has been exceptional, and I’m truly grateful for the dedication and expertise the team has brought to the development process. Their commitment to our mission is...
Founder & CEO, TravelRite
The Mindbowser team's professionalism consistently impressed me. Their commitment to quality shone through in every aspect of the project. They truly went the extra mile, ensuring they understood our needs perfectly and were always willing to invest the time to...
CTO, New Day Therapeutics
I collaborated with Mindbowser for several years on a complex SaaS platform project. They took over a partially completed project and successfully transformed it into a fully functional and robust platform. Throughout the entire process, the quality of their work...
President, E.B. Carlson
Mindbowser and team are professional, talented and very responsive. They got us through a challenging situation with our IOT product successfully. They will be our go to dev team going forward.
Founder, Cascada
Amazing team to work with. Very responsive and very skilled in both front and backend engineering. Looking forward to our next project together.
Co-Founder, Emerge
The team is great to work with. Very professional, on task, and efficient.
Founder, PeriopMD
I can not express enough how pleased we are with the whole team. From the first call and meeting, they took our vision and ran with it. Communication was easy and everyone was flexible to our schedule. I’m excited to...
Founder, Seeke
We had very close go live timeline and Mindbowser team got us live a month before.
CEO, BuyNow WorldWide
If you want a team of great developers, I recommend them for the next project.
Founder, Teach Reach
Mindbowser built both iOS and Android apps for Mindworks, that have stood the test of time. 5 years later they still function quite beautifully. Their team always met their objectives and I'm very happy with the end result. Thank you!
Founder, Mindworks
Mindbowser has delivered a much better quality product than our previous tech vendors. Our product is stable and passed Well Architected Framework Review from AWS.
CEO, PurpleAnt
I am happy to share that we got USD 10k in cloud credits courtesy of our friends at Mindbowser. Thank you Pravin and Ayush, this means a lot to us.
CTO, Shortlist
Mindbowser is one of the reasons that our app is successful. These guys have been a great team.
Founder & CEO, MangoMirror
Kudos for all your hard work and diligence on the Telehealth platform project. You made it possible.
CEO, ThriveHealth
Mindbowser helped us build an awesome iOS app to bring balance to people’s lives.
CEO, SMILINGMIND
They were a very responsive team! Extremely easy to communicate and work with!
Founder & CEO, TotTech
We’ve had very little-to-no hiccups at all—it’s been a really pleasurable experience.
Co-Founder, TEAM8s
Mindbowser was very helpful with explaining the development process and started quickly on the project.
Executive Director of Product Development, Innovation Lab
The greatest benefit we got from Mindbowser is the expertise. Their team has developed apps in all different industries with all types of social proofs.
Co-Founder, Vesica
Mindbowser is professional, efficient and thorough.
Consultant, XPRIZE
Very committed, they create beautiful apps and are very benevolent. They have brilliant Ideas.
Founder, S.T.A.R.S of Wellness
Mindbowser was great; they listened to us a lot and helped us hone in on the actual idea of the app. They had put together fantastic wireframes for us.
Co-Founder, Flat Earth
Ayush was responsive and paired me with the best team member possible, to complete my complex vision and project. Could not be happier.
Founder, Child Life On Call
The team from Mindbowser stayed on task, asked the right questions, and completed the required tasks in a timely fashion! Strong work team!
CEO, SDOH2Health LLC
Mindbowser was easy to work with and hit the ground running, immediately feeling like part of our team.
CEO, Stealth Startup
Mindbowser was an excellent partner in developing my fitness app. They were patient, attentive, & understood my business needs. The end product exceeded my expectations. Thrilled to share it globally.
Owner, Phalanx
Mindbowser's expertise in tech, process & mobile development made them our choice for our app. The team was dedicated to the process & delivered high-quality features on time. They also gave valuable industry advice. Highly recommend them for app development...
Co-Founder, Fox&Fork