The world’s leading publication for data science, AI, and ML professionals.

Simple GPS data visualization using Python and Open Street Maps

There is an easy way of visualizing the GPS data on the maps.

Photo by Timo Wielink on Unsplash
Photo by Timo Wielink on Unsplash

Visualization of the GPS data can be challenging when using programming languages. Some interesting Python packages can be used for such purposes. But, it can be hard to install and use them in some cases, especially if you have only a simple task to do. This article will show the simple but effective GPS records visualization method using Python and Open Street Maps (OSM). If you want to skip straight to the code, visit https://github.com/tisljaricleo/GPS-visualization-Python.


Before we continue, give me a second of your time. If you want to support me in my work, you can do it using this secure PayPal link. Thank you 🙂

Pay Leo Tisljaric using PayPal.Me


The goal

The image below shows the goal of this method. There are three main elements that need to be included:

  1. Map image – map in some image format like .png, .jpg, etc.
  2. GPS records – records that consist of (latitude, longitude) pairs.
  3. Geographical coordinates – conversion from pixels to geographical coordinates.
The final result of the GPS visualization method (Image by: Author)
The final result of the GPS visualization method (Image by: Author)

Setup & Data

The easiest way to use code below is to install VS Code or some other IDE for editing Python code. Python Jupyter notebook users could encounter some problems or additional setup. The easiest way is to run main.py script directly from one of the IDEs.

For this article, a small GPS dataset is used collected with the Columbus V-990 logger that I’ve carried in my pocket for two days. Data is available at the github repository.

It is a simple (latitude, longitude) dataset with around 35000 records and looks like this:

Used dataset (Image by: Author)
Used dataset (Image by: Author)

Important note: To successfully run a code, data must be formatted in exact way as shown in the image above. Otherwise, the code will result in error!

Methods

Here, we will go through all the steps needed for this method to work. For the full code, visit the github repository.

1. Get the map

The map can be downloaded from https://www.openstreetmap.org/export. Just choose the part of the map that you need by clicking "Manually select a different area." Write down the coordinates of the upper left, and lower right corners in this case (45.8357, 15.9645) and (45.6806, 16.1557).

Exporting the OSM map (Image by: Author)
Exporting the OSM map (Image by: Author)

Then export it by using the "Share" button on the right side. Don’t forget to choose the option "Set custom dimensions" and align the view to the selected area.

2. Python code

Importing packages and data:

To draw geographic coordinates on the image, coordinates must be converted to image pixels. Method _scale_toimg(gps_coordinate, (image_height, image_width)) takes the GPS coordinates and converts it to image coordinates (pixels) based on the image width and height.

The image below shows the need of converting the geographic coordinates to image coordinates:

Difference between geographic coordinates and image coordinates (Image by: Author)
Difference between geographic coordinates and image coordinates (Image by: Author)

Right now, we have drawn the GPS records on the map! If you look at the image below, it will look better if there are some other elements, like grid and geographic coordinates.

Visualization of the GPS coordinates on OSM (Image by: Author)
Visualization of the GPS coordinates on OSM (Image by: Author)

Those extra elements will be added using the matplotlib package:

The final result is here:

The final result of the GPS records visualization using Python and OSM (Image by: Author)
The final result of the GPS records visualization using Python and OSM (Image by: Author)

Conclusion

Visualization of the GPS data can be challenging when using a programming language to automate it. There are some interesting Python packages that can be used for such purposes. But, in some cases, it can be hard to install and use them, especially if you have only a simple task to do.

With this article, the simple but effective method for visualization of the GPS dataset was presented. We used Python and the OSM to build a framework. In the article, all the steps were shown, but you can find the full code here: https://github.com/tisljaricleo/GPS-visualization-Python

If you have any questions or suggestions, feel free to comment or contact me!

Linkedin: https://www.linkedin.com/in/leo-tisljaric-28a56b123/

References


Related Articles