Visualising Photo Geolocation Data Using Python | by Pavel Cherepansky | May, 2022

Let’s leverage the exif and folium Python packages

Most fashionable GPS-enabled cameras and cell phones file geolocation data when a photograph is taken and this data is saved together with all the opposite metadata.

There are additionally loads of normally web-based functions that assist you to create numerous visualisations utilizing this metadata. However what for those who’re like me and don’t belief your pictures to third-party companies with unknown ranges of information safety? Fortunately, most of those visualisations are comparatively easy to duplicate utilizing Python. On this information, I’ll present the right way to create a journey map utilizing picture GPS knowledge.

For this venture, you will want a couple of packages put in in your Python setting. We are going to use two foremost packages: exif will permit us to extract metadata from pictures and we’ll use folium to create a map and add location markers. Lastly, we may even use color maps from matplotlib, and pandas and numpy for knowledge manipulations. To put in all the mandatory packages merely execute the next in your terminal:

pip set up exif folium pandas numpy matplotlib

We’ll begin with importing all the mandatory packages. Along with those listed above, we may even import jsonand Pathclass from the pathlib library, each of that are part of the usual library.

Extracting and making ready metadata for plotting

Studying metadata with exifcould be very simple — we merely must open a file in a binary format and create an Pictureobject from it. Let’s outline a comfort perform for this:

Relying on the machine a photograph can comprise dozens of varieties of metadata data. To see those who particularly relate to geolocation we will load a single picture and print out all of the metadata classes that start with “gps_”.

If a photograph has embedded geolocation data you need to see the next output:


A few of these classes are comparatively simple to grasp: gps_latitude, gps_longitude and gps_altitude retailer latitude, longitude and altitude data. Altitude is saved as elevation above sea degree, normally in meters. Latitude and longitude are saved as tuples of levels, minutes and seconds of arc.

Classes ending with _ref comprise reference data on how the info needs to be interpreted. Within the case of altitude, it merely informs that the worth is altitude above sea degree, however for latitude and longitude they comprise the knowledge of the reference hemisphere, “E” or “W” for longitude, and “N” or “S” for latitude. These are vital when changing the uncooked knowledge to decimal format since the usual notation has coordinates west of Greenwich and south of the equator as damaging.

Since folium doesn’t perceive coordinates in levels/minutes/seconds we’ll must convert latitudes and longitudes to decimal illustration and since we’ll additionally do it fairly a couple of occasions we will make life simpler and code cleaner by defining a perform that may take a tuple of coordinate and a reference worth and return its binary illustration. For this, we convert minutes and seconds to fractions of a level by dividing by 60 and 3600, respectively. The signal is then determined primarily based on the reference knowledge:

This perform will convert latitudes and longitudes to decimal format and we don’t must do something with altitudes. With this, we will outline one other helper perform that may take an exif.Picture object and return a tuple of decimal coordinates within the format (latitude, longitude, altitude).

Lastly, we are going to add one other perform that may recursively undergo all subfolders of a specific folder, learn spatial data from all of the picture recordsdata and return a dictionary of file names and coordinates.

Now that that is completed we will simply learn the situation knowledge from all recordsdata throughout the picture folder:

Printing the ensuing dictionary ought to end in an output just like mine:

print(json.dumps(res, indent=4))Out:     
"latitude": -37.79912566666666,
"longitude": 144.9850463611111,
"altitude": 0.0,
"timestamp": "2014:04:12 18:14:59",
"latitude": -37.79912566666666,
"longitude": 144.9850463611111,
"altitude": 0.0,
"timestamp": "2014:04:12 18:15:36",

Plotting the info

To simplify future operations we will convert the ensuing dictionary to a pandas DataFrame. We then additionally convert the timestamp column to a datetime format and type values by date in ascending order.

With a purpose to colour-code our knowledge factors we will get one of many color maps that come as part of the matplotlib package deal. Along with this, we additionally must create a Normalize object. This object will assign numerical values to particular person colors in a colourmap between 0 and 1 and we’ll have the ability to name any color within the colourspace by its numerical worth. And for this, we are going to add a column to our dataframe utilizing numpy.linspace() .

One small situation is that our colors shall be in RGBA format nevertheless it doesn’t work with folium so we may even must convert the color to its hexadecimal illustration which is definitely completed:

Now let’s lastly it’s time to visualise our knowledge!

The primary a part of the folium package deal is Map object and easily creating an occasion through folium.Map() will end in a map of the complete world.

Default folium map.

That is effective however I choose to have the map routinely zoom to the extent that every one the markers will be seen. We are able to obtain this utilizing fit_bounds() methodology of the ensuing object and offering coordinates of the southwest and northeast corners of the map extents.

We are able to calculate the coordinates of the southwest nook by taking the utmost latitude and minimal longitude from our knowledge. Equally, the north-eastern nook coordinates are the minimal latitude and the utmost longitude. Within the under code I’ve additionally adjusted the bounding field by 3 levels in every route in order that the info suits neatly throughout the window.

So now lastly we’re able to create a map exhibiting the places of the pictures colour-coded by the date taken.

In my case, the end result exhibits a bunch of places round Victoria, Australia with purple dots representing earlier footage and brighter crimson and yellow dots — newer pictures:

Right here we solely touched on a tiny a part of all of the alternatives that picture metadata provides. With this knowledge, you’ll be able to create your personal databases of pictures permitting you to simply categorise and handle them. It’s also possible to develop using the geolocation knowledge to with the ability to seek for pictures by location inside your picture folders and plenty of different issues can be found too.

The whole code for this venture will be downloaded from my GitHub on the following location:

More Posts