Nowadays, there are a lot of geotagged photos in our mobile phones and that might be the natural place to start when trying to find a method for extracting the coordinates and creating a web map with the geotagged images. If you are anything like me, you have a lot of photos of the neighborhood cats, deer, squirrels and hedgehoges in your phone. No collection of web maps is complete without a cat edition, but somehow, I found a large collection of geotagged images at Kaggle. There are about 4.2 million geotagged photos, divided over 142 zip files. Just imagine how many cat images there might be in there! Using 4.2 million files might be a bit over the top for the first try, but each file contains only 30 000 images. Manageable.
With some help from Claude, I made an R script that does the following:
- reads the downloaded .msg file
- extracts the images
- writes the metadata to a csv file
- creates a simple Leaflet web map with the locations
As you can see, this includes some Python as well:
# Set directory
setwd("C:/Eget/R")
#Sys.setenv(CURL_CA_BUNDLE = "")
#Sys.setenv(SSL_CERT_FILE = "")
# Load libraries
library(leaflet)
library(reticulate)
# Python for reading and extracting images
py_run_string("
import msgpack
import os
# Create output folder
os.makedirs('extracted_images', exist_ok=True)
# Read .msg file
with open('C:/Eget/R/Geotagged images/shard_24.msg', 'rb') as f:
unpacker = msgpack.Unpacker(f, raw=False)
# Create file with GPS data
with open('gps_data.csv', 'w') as gps_file:
gps_file.write('filename,latitude,longitude\\n')
for i, record in enumerate(unpacker):
# Save images
img_id = record['id'].replace('/', '_')
with open(f'extracted_images/{img_id}', 'wb') as img_file:
img_file.write(record['image'])
# Save GPS data
with open('gps_data.csv', 'a') as gps_file:
gps_file.write(f\"{img_id},{record['latitude']},{record['longitude']}\\n\")
if i % 1000 == 0:
print(f'Processed {i} images...')
print('Done! Extracted all images.')
")
# Read file with GPS data
gps_data <- read.csv("gps_data.csv")
# Create Leaflet map
map <- leaflet(gps_data) %>%
addTiles() %>%
addCircleMarkers(
lng = ~longitude,
lat = ~latitude,
radius = 3,
color = "red",
fillOpacity = 0.5,
popup = ~paste("Fil:", filename, "<br>",
"Lat:", latitude, "<br>",
"Lon:", longitude)
)
# Show map
map

30 000 images is a respectable amount of data for showing in a web map like this and Leaflet has some trouble serving the map. There are of course ways of solving this, such as reading a subset of the images or using another tool for the map.

Europe and the US seem well documented. So, what do the images contain? A lot of buildings, people, plants…and cats. This might be a task for image recognition in a later post.