Store and display GeoJSON data using Flask, MongoDB & Leaflet
Flask is a lightweight Python framework (that can somehow be compared to Slim framework for PHP). Additional functionalities can be added by installing Flask extensions. GeoJSON is a format that allows us to encode geolocation data as JSON. You can read more about the specifications here and play with it a bit on this very well made website.
The usage of MongoDB is not arbitrary here, as it allows us to store geolocation data easily. You might need to install it on your system before following this article.
Let's set up a new virtual environment using virtualenv
We can now install the required packages
Our first website using Flaskπ
Building a plain simple website using Flask is pretty straight forward as we don't have to spend hours configuring stuff that we might never use.
=
return
Let's run our WSGI application using python3 main.py and head to https://127.0.0.1:5000/ to see our beautiful website.
Flask uses Jinja2 as a templates engine, it allow us to render HTML templates. It uses the templates directory by default, so let's create that and add a simple HTML file.
GeoJSON + Flask + MongoDB
Let's display some nice maps here!
We can use the render_template function to render an HTML file which will render (pretty obvious from the function name) and create a response from that HTML content.
=
return
Pymongo, the MongoDB Python APIπ
Pymongo is the Python implementation of the MongoDB API, that can allow us to communicate with mongodb using Python. The mongdb daemon must be running during that process. If it's your first time with your pymongo, have a look at the official documentation.
Connecting to a MongoDB database is done by accessing the database name attribute from the client object.
=
=
Let's add some geospatial data that we can display later.
=
GeoJSON supports multiple objects types like Point, LineString, Polygon... You can find the list of those and how to store them on a MongoDB collection here or by reading the GeoJSON specs.
Now that we have some data stored on our collection, we need to provide a simple API to fetch that data so we can display it later on our HTML page, we will see the reason later. Our API endpoint should return a JSON object with all the points coordinates we have stored in our collection.
=
return
Flask provides a jsonify method which converts our dict object to a JSON object and adds the correct content-type header.
Displaying the data with OpenStreetMap & Leafletπ
In order to display our map, we will be using Leaflet, which is a JS library that allows us to interact with maps easily. We won't be using any JS build system here, if you are using VueJS in your project, you can use Vue2Leaflet.
Let's add the JS & CSS files to our HTML file
We can now create a new map using Leaflet and pass the center position and the zoom level.
If you want to center the map on the user's current location, you can use this, which will ask the user to allow your website to access their current geolocation and update the center to that position.
;
if navigator.geolocation
If we run the current code, nothing will be displayed on our map as we need to specify a tile provider first. We will be using Open Street Map
'http://{s}.tile.osm.org/{z}/{x}/{y}.png',
map;

Yay, our map is finally showing up! Let's fetch the data from that simple API endpoint we have made before and add those to our Leaflet map.
Letβs fetch the data from that simple API endpoint we have made before and add those to our Leaflet map. We can use a simple XMLHTTPRequest, but I prefer using axios, which is a simple HTTP client.
We can fetch the data from our API endpoint using axios.get
'http://127.0.0.1:5000/points'
;
Leaflet supports creating a GeoJSON layer and display it on the map
response.data, map;
As you can see, the advantages of using Leaflet to display the data we have stored on our MongoDB pretty easy, as we won't need to change much in our code in the future if we want to display in kind of GeoJSON features.
Conclusionπ
I wrote this article after doing some research on how to handle geospatial data and how can I display them pretty easily on a project I'm working on. You should by now have an idea of how to
-
Get started with simple Flask web pages
-
GeoJSON, what's and how to store the data in a proper database like MongoDB
-
How to fetch and display the data using axios and Leaflet.
You can find the final code here with a few tweaks to allow showing whatever type of GeoJSON features.
=
=
=
=
=
return
return
GeoJSON + Flask + MongoDB
Let's display some nice maps here!