Author avatar

Dániel Szabó

Building GeoPlots with Python

Dániel Szabó

  • Jul 19, 2019
  • 7 Min read
  • 71 Views
  • Jul 19, 2019
  • 7 Min read
  • 71 Views
Languages Frameworks and Tools
C#

Introduction

Geoplot is a plotting library, a high-level API on top of cartopy and matplotlib. The idea behind it is to make it easier for end-users to create nice looking geospatial maps which are populated with data.

The maps

There are three main components:

  1. High-level plotting API
  2. Native projection support
  3. Compatibility with matplotlib

The high-level API gives you access to the standard-bearer maps which you may already be familiar with from your geography books.

The native projection support solves the problem of unrolling a sphere onto a flat surface and maps your points accurately.

Matplotlib, by default, is not really fit for geospatial plotting. However, with an interface, it can be incorporated nicely into the existing application.

In order to dig deeper, you may want to visit the documentation of geopandas and the geoplot to get the full picture of its capabilities.

The Setup

Although the package is available on pypi. The official documentation says that the following command may or may not work.

1
pip install geoplot
bash

This package is platform-independent, so it should work on Windows/Linux or macOS.

In my experience it does not work.

Here is what works. If you don’t already have anaconda installed, you should go and install it first. The following demos that I will show you were created on a CentOS 7 machine. First download the x64 installer for Linux, then issue the following command:

1
sudo ./Anaconda3-2019.03-Linux-x86_64.sh
bash

Accepted the defaults and then, after the installation completed, open a new shell and issue the following command as root:

1
conda config --set channel_priority strict
bash

This fixes a mixed channel problem in anaconda. Then edit the ~/.condarc file which will look like this after the edit:

1
2
3
4
channel_priority: strict
channels:
  - conda-forge
  - defaults 
bash

Finally, we can install the geoplot pacakage. Use the root user to install it because it will have to access places where your standard user does not have access.

1
conda install geoplot -c conda-forge
bash

This will take some time, but it will be worth your while. After the installation is complete we can jump right into it.

The Example

By default, geoplot comes with some datasets that you can try out. In my experience, jupyter is the easiest approach to this. Let's spin up a notebook and create our first example. Issue the following command:

1
jupyter notebook
bash

By default, your browser should open a new window with a blank page.

The jupyter

On the right-hand side, you will find a new button that allows you to start a new instance of a notebook.

The blank

This is the new blank notebook we would like to populate.

The Continental USA cities

Let's dissect this example.

1
%matplotlib inline
bash

This is a line-oriented magic function in jupyter.

1
2
import geopandas as gpd
import geoplot as gplt
python

These are special import statements in python; the result of those import lines so that you can reference the functionalities provided by the modules with a shorter name. For example instead of geopandas.read_file() you can say gpd.read_file(). This is common in python, and popular modules also provide examples like this.

1
2
cities = gpd.read_file(gplt.datasets.get_path('usa_cities'))
gplt.pointplot(cities)
python

The cities variable holds all the USA city coordinates. This is a nested function call. First, we get the dataset with the gplt.datasets.get_path('usa_cities') and then they are passed to the gpd.read_file() function.

We’ll also want to project our points to our map. Here is how we do it. First, we create our map:

The map

We use the same method as for getting the cities dataset. Then we use the polyplot to make it visible.

Projected

This time, we pass the cities dataframe to the pointplot function, and we specify the ax argument to be the frame we have created called contiguous_usa.

The Round Earth

This sample will show you how you can use the spherical earth as your map:

The round earth

We use the named import of the crs submodule of geoplot. The crs refers to the Coordinate Reference System. It's a wrapper for the cartopy.crs module and meant to be a parameter for the projection parameters of the geoplot outputs. We create a new column with the name called fdp_pp, then we use the ployplot function to plot it. The figsize is a grid-like argument which specifies how many inches the plotsize should be and it takes a tuple as an argument. The outline_patch.set_visible(True) creates a nice outline of our planet.

About Projections

Projections will define how your map looks when you render it with either of the available plotting functions. When you choose the projection type, you need to make sure that the data you are working with is appropriate. The data that you are working with will come in GeoJSON format which is a standard for representing simple geographical features with their non-spatial attributes. So, basically, it allows you to describe a mountain, a reef, a village, or whatever you feel like representing. The projections in geoplot are prepared to handle only data which is appropriate for that type; meaning that you will possibly face an error if the GeoJSON data you provide is not in the correct format. For example, if we replace the projection with gcrs.OSNI(), the following error will occur.

The projection error

This means that the data processor is expecting a different format than it received. There are many projection types and many formats that they expect, it takes time to experiment and get to the right format.

GeoJSON Sources

List of popular sources:

Conclusion

The geoplot library with the GeoJSON data provides a nice and easy to learn a technique that allows you to build kickass maps and visualize the data you would like to present. In my experience, practice makes perfect, there are pretty good tutorials for more specific applications of the geoplot which I could not cover in this guide. Digging deeper into this application, there are dozens of ways one can utilize it. I personally use it in my daily work because we have devices with coordinates that we need to put on a map and the default app of the manufacturer is sluggish and sometimes even provides false information, but this module came to the rescue!

0