Mapping street art in Athens with leaflet and R

My colleague and fellow doctoral student at Lund University, George Stampoulidis, is documenting political street art in Athens as part of his research project. The data consist of images and other variables describing each piece of street art. In this post, we’ll do the following:

  1. use R to extract geotagged data from the images’ exif files
  2. use the leaflet package to map locations
  3. customise our map and add popup with images and various information

Installing packages

I sometimes use the ‘pacman’ package, which installs (if necessary) and loads multiple packages.

if(!require("pacman")) install.packages("pacman")
## Loading required package: pacman
pacman::p_load(exifr,dplyr,leaflet,mapview,magrittr)

Extracting geodata from exif files

The geodata has already been included in the data file. But this is an easy way of creating a data frame with coordinates for each image file. We’ll do this in R. You can also do on the command line if you install ‘exiftool’ and use the following line of code:
exiftool -filename -gpslatitude -gpslongitude -T -n NAME_OF_DIRECTORY > out.txt

exifData <- read_exif("imageFolderName",recursive=T)

exifData <- select(exifData,FileName,
               GPSLongitude, GPSLatitude)

write.csv2(exifData, 'exifData.csv',sep=",",
          row.names = F)

Loading and handling data

Since we already have all the variables we need in the data file, there’s no need to merge it with the exif data. We simply do some minor cleaning of the data and add a variable for a remote repository in which all image files are stored.

df <- read.csv2("streetArtUpdate1.csv",stringsAsFactors = F,header = T)

# change coordinates to numeric values
df$Longitude <- as.numeric(df$Longitude)
df$Latitude  <- as.numeric(df$Latitude)

# add remote path for image files
df$remotePath <- paste0("http://dev.humlab.lu.se/tmp/img/",df$Filename)

# remove one data point with erroneous geodata
df <- df[df$Title!="The Greek health system",] 

Basic mapping with leaflet

Let’s first have a look at how easy it is to create a leaflet map with markers for each location in our data.

#Create basic world map
athensMap <- leaflet() %>% 
  addTiles()           %>%
  # add points
  addMarkers(lng = df$Longitude,lat = df$Latitude)

athensMap

Customizing our map with popups etc.

Let’s add the following elements to our map:
1. A different tile layer
2. Markers with popup capability
3. Include popup images and multiple variables containing information
4. Cluster our markers

Please note that for some reason, you might need to click more than once on a marker to see images.

tile       <- 'https://cartodb-basemaps-{s}.global.ssl.fastly.net/light_all/{z}/{x}/{y}.png'
athensMap2 <- leaflet() %>%
  # Add a base layer
  addTiles(tile)        %>%
  # add markers
  addAwesomeMarkers(lng=df$Longitude, lat=df$Latitude,
      # add popup with images and variables as text
      popup=paste(popupImage(df$remotePath, src = c("remote")),"<br>",
                                "Title: ",paste("<a href=",df$remotePath,">",df$Title,"</a>"),"<br>",
                                "Creator: ",df$Name_of_the_creator,"<br>",
                                "Type: ",df$Pictorial_verbopictorial,"<br>",
                                "Text: ",paste("'",df$Text,"'")),
      # organise points as clusters
      clusterOptions = markerClusterOptions(),
      label = df$Title) %>%
      # include minimap
      addMiniMap()

athensMap2
comments powered by Disqus