29/6/2021

Packages and general options

library(tidyverse) # Package for data manipulation
library(httr) # Package for data connection by an API
library(jsonlite) # Package for manipulating JSON files
library(leaflet) # Package for mapping data

Connecting by API and reading the data

Through a specific API, we conncected to the data from the Open Data Webservice of the Catalonian Government. You can see the data here.

# Connecting to the data by an API
accAPI = GET(
    "https://analisi.transparenciacatalunya.cat/resource/fsum-2k6e.json")

# Convert JSON data to a data.frame, modifying the encoding to UTF-8
acc_char <- rawToChar(accAPI$content)
Encoding(acc_char) <- "UTF-8"
dataAcc <- fromJSON(acc_char)

Cleaning the data (1)

We make different filtering and cleaning operations: Coercing lat. and long. to numeric format; eliminating rows with longitud o latitud NA, eliminating a row with incorrect latitude (0); creating a categorical variable Rescue_type in English, equal to tal_nom_alarma; creating a color variable colRescue to be used for the legend in the map.

# Set as numeric geolocalization data
dataAcc$latitud <- as.numeric(dataAcc$latitud)
dataAcc$longitud <- as.numeric(dataAcc$longitud)

dataAcc <- dataAcc %>%
    filter(!is.na(latitud)) %>% # Eliminating NAs "latitud" ("Latitude")
    filter(!is.na(longitud)) %>%  # Eliminating NAs "longitud" ("Longitude")
    filter(!latitud == 0) # Eliminating incorrect "latitude" 
                                # (latitude equal to 0 is equator!)

Cleaning the data (2)

# The variable "tal_nom_alarma" identify the type of rescue
# Create a variable in English equal to tal_nom_alarma 
dataAcc<- dataAcc %>% mutate(Rescue_type= case_when( 
        tal_nom_alarma == "Rescat de muntanya" ~ "Mountain rescue",
        tal_nom_alarma == 
            "Recerca i/o rescat en medi fluvial (rius, barrancs, llacs)" ~ 
            "River and lake rescue",
        tal_nom_alarma == "Recerca i/o rescat en medi marítim" ~ 
            "Sea rescue",
        tal_nom_alarma == "Recerca persones perdudes" ~ "Missing persons",
        tal_nom_alarma == "Rescat en coves i pous" ~ "Cave rescue")) %>%
    mutate(colRescue = case_when( # A color to every Type of rescue
        Rescue_type == "Mountain rescue" ~ "red",
        Rescue_type == "River and lake rescue" ~ "green",
        Rescue_type == "Sea rescue" ~ "blue",
        Rescue_type == "Missing persons" ~ "orange",
        Rescue_type == "Cave rescue" ~ "black")) %>% 
    filter(any == "2021") # Filter rescues of year ("any") 2021

Map of mountain rescues (code)

LatLongRescues <- data.frame(
    lat = dataAcc$latitud,
    lng = dataAcc$longitud)

Map_Rescues <- LatLongRescues %>%
leaflet() %>%
addTiles() %>%
addCircleMarkers(color = dataAcc$colRescue,
                 popup = paste("Location:", dataAcc$nom_poblacio,
                               "<br>",
                               "Month:", dataAcc$mes
                               ),
                 clusterOptions = markerClusterOptions()) %>% 
    addLegend(labels = c("Mountain rescue",
                         "River and lake rescue", "Sea rescue",
                         "Missing persons", "Cave rescue"),
              colors= c("red", "green", "blue", "orange", "black"
                        ))

Wilderness rescues in Catalonia (jan-jun 2021) - 2021-06-30