Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
So you have used the excellent exiftool to extract all of the GPS-related information from a directory of photos in JPG format and write to a CSV file:
exiftool '-*GPS*' -ext jpg -csv . > outfile.csv
You’ve used R/leaflet to plot coordinates (latitude and longitude) before, but what about that tag named GPSImgDirection? It would be nice to have some kind of marker which indicates the direction in which you were facing when the photo was taken.
For me, a Google search provided hints but not one single, obvious straightforward solution to this problem (the generative AI effect? time will tell…), so here’s what I’ve put together from several sources, in particular this StackOverflow post.
The key points are:
- use awesomeIcons() to create a directional icon which can be rotated
- add the icons to your map using addAwesomeMarkers()
Here’s some example code which uses the Font Awesome icon long-arrow-up. Since “up” (north) corresponds to zero degrees, applying a rotation corresponding to GPSImgDirection should result in the correct orientation for the marker. The GPS-related tags in this case come from an iPhone 13.
library(readr)
library(leaflet)
library(sp)
outfile <- read_csv("outfile.csv")
# create dataset
# ugly but it works
dataset <- outfile %>%
mutate(GPSLatitude = str_replace(GPSLatitude, " deg", "d"),
GPSLatitude = GPSLatitude %>%
char2dms() %>%
as.numeric(),
GPSLongitude = str_replace(GPSLongitude, " deg", "d"),
GPSLongitude = GPSLongitude %>%
char2dms() %>%
as.numeric(),
GPSHPositioningError = str_replace(GPSHPositioningError, " m", ""),
GPSHPositioningError = GPSHPositioningError %>%
as.numeric()) %>%
select(latitude = GPSLatitude,
longitude = GPSLongitude,
GPSTimeStamp,
GPSImgDirection,
GPSHPositioningError)
# create the marker icons
icons <- awesomeIcons(iconRotate = dataset$GPSImgDirection,
icon = "long-arrow-up",
library = "fa",
markerColor = "white",
squareMarker = TRUE)
# create map
# can filter on positioning error if desired
leaflet(data = dataset %>%
addProviderTiles(provider = providers$CartoDB.Positron) %>%
addAwesomeMarkers(icon = icons, label = ~GPSTimeStamp)
Here’s a screenshot of the resulting interactive map.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
