Author avatar

Omotayo Aina

Building GeoPlots with Geoplotlib

Omotayo Aina

  • May 21, 2020
  • 6 Min read
  • 1,502 Views
  • May 21, 2020
  • 6 Min read
  • 1,502 Views
Data
Geoplotlib

Introduction

Geoplotlib is an open-source Python toolbox for visualizing geographical data. It supports the development of hardware-accelerated interactive visualizations in pure Python and provides implementations of dot maps, kernel density estimation, spatial graphs, Voronoi tesselation, shapefiles, and many more common spatial visualizations.

Geoplotlib can be used to make a variety of maps, such as equivalent area maps, heat maps, and point density maps. There are also several extended modules:

  • geoplotlib
  • geoplotlib.layers
  • geoplotlib.utils
  • geoplotlib.core
  • geoplotlib.colors

The diagram below shows an overview of the geoplotlib architecture, which was built on top of numpy, scipy, and OpenGL/pyglet.

geoplotlib arch

In this guide, you'll learn how to build a geoplot with geoplotlib on Windows Operating System. We will start by installing the required packages for geoplotlib.

List of required packages:

  • pip
  • numpy >= 1.12
  • pyglet >= 1.2.4

Optional requirements:

  • matplotlib for colormaps
  • scipy for some layers
  • pyshp for reading .shp files

Installing Required Packages

Installing Pip

Installing Pip on Windows operating system can be a little bit tricky, but it is also very simple.

Step 1: Download and install the Microsoft Visual C++ Redistributable for Visual Studio 2015, 2017, and 2019 using the link below.

1
https://aka.ms/vs/16/release/vc_redist.x64.exe
Windows

eleven

eleven

Step 2: Enable long paths on your Windows operating system by hitting the Windows key, typing gpedit.msc, and pressing Enter.

twelve

Navigate to Local Computer Policy > Computer Configuration > Administrative Templates > System > Filesystem > NTFS.

Double click the Enable NTFS long paths option, as shown below, and enable it. thirteen

Select the Enable radio button, as shown below. fourteen

Click OK to see the enabled page, as shown below. fifteen

Step 3a: Download and install Python 3 using the link below.

1
https://www.python.org/downloads/release/python-382/
Windows

sixteen

Step 3b: Check Add Python to PATH and click on Install Now. seventeen

Step 4: Verify the pip version with the command as shown below on command prompt, and if the version is 19 or higher, skip step 5. Otherwise, proceed.

eighteen

Step 5: Upgrade to the latest version using the command below.

1
pip3 install  --upgrade pip
Windows

nineteen

Step 5: Verify the pip version install using the following command.

1
pip3 --version
Windows

twenty

Installing Numpy

Use pip package as shown below:

1
pip3 install numpy 
Windows

Installing Pyglet

1
pip3 install Pyglet
Windows

Installing Geoplotlib

1
pip3 install geoplotlib
Windows

Creating a Dot Density Map

Step 1: Download a simple geospatial dataset (containing latitude and longitude values) from LatLong.net.

Step 2: Highlight the table, copy and paste it into Excel, and save it as a CSV file.

Step 3: Copy or type the script below in your Python editor.

1
2
3
4
5
6
import geoplotlib
from geoplotlib.utils import read_csv

data = read_csv("C:\\Users\\Omotayo\\Desktop\\nigeria_cities.csv")  #replace path with your file path
geoplotlib.dot(data,point_size=3)
geoplotlib.show()
Windows

Output:

dotmap

You've created a dot density map of cities in Nigeria.

Creating a Spatial Graph

Step 1: Download a simple geospatial dataset (containing latitude and longitude values) from andrea-cuttone on github brain.

Step 2: Copy or type the script below in your Python editor.

1
2
3
4
5
6
import geoplotlib
from geoplotlib.utils import read_csv

data = read_csv("C:\\Users\\Omotayo\\Desktop\\flight.csv") #replace path with your file path
geoplotlib.graph(data, src_lat='lat_departure', src_lon='lon_departure', dest_lat='lat_arrival', dest_lon='lon_arrival', color='hot_r', alpha=16, linewidth=2)
geoplotlib.show()
Windows

Output:

spatial map

You've created a spatial graph of airport locations, where each node represents an airport and each edge represent a flight connection.

Conclusion

In this guide, you learned how to use geoplotlib, a Python toolbox for generating geopraphical visualizations. We demonstrated how geoplotlib is used for dot density maps and spatial graphs. Geoplotlib is a powerful API that can be used for various types of map representations, such as voronio tesselation, delaunay triangulation, markers, and so on using the various modules highlighted in the introduction section.

This guide won't be complete if we don't give special thanks to the creators of the geoplotlib package, as well as Andrea Cuttone, Sune Lehmann, Jakob Eg Larsen, and LatLong.net.

To learn more, please visit Andrea Cuttone's github page.

5