Using a web map is a flexible way of presenting your GIS project and I wanted to use R to create a Leaflet web map. This R script will:
- Download current earthquake data from USGS (United States Geological Survey)
- Convert the earthquake data to geometry (points)
- Download tectonic plates data from GitHub
- Create an interactive web map using Leaflet
- Export the web map to an HTML file
To run the script, you need R and I recommend RStudio for R development.
Code:
# Set working directory
setwd("C:\\R\\")
# Load libraries
library(htmlwidgets)
library(leaflet)
library(sf)
# Read earthquake csv file from USGS
quakescsv <- read.csv("https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/4.5_month.csv")
# Earthquake coordinates -> spatial data (EPSG = 4326)
earthquakes_points <- st_as_sf(quakescsv, coords = c("longitude", "latitude"), crs = 4326)
# Create description
earthquakes_points$description = paste("Magnitude", earthquakes_points$mag)
# Read tectonic plates boundaries
tectonic_plates <- st_read("https://raw.githubusercontent.com/fraxen/tectonicplates/master/GeoJSON/PB2002_boundaries.json")
# Create Leaflet web map
earthquakemap = leaflet(earthquakes_points) %>%
# Set map center
setView(lng = 0, lat = 0, zoom = 2) %>%
# Base groups
addProviderTiles(providers$CartoDB.Positron, group = "Positron") %>%
addTiles(group = "OSM") %>%
# Overlay groups
# Tectonic plates
addPolylines(data=tectonic_plates, group = "Tectonic plates", weight = 2)%>%
# Earthquakes
addCircleMarkers(
data = earthquakes_points,
group = "Earthquakes",
popup = ~as.character(description),
label = ~as.character(mag),
radius = ~(mag)*2,
color = "RED",
stroke = FALSE, fillOpacity = 0.2
) %>%
# Layers control
addLayersControl(
baseGroups = c("Positron", "OSM"),
overlayGroups = c("Earthquakes", "Tectonic plates"),
options = layersControlOptions(collapsed = TRUE)
)
# Render Leaflet map
earthquakemap
# Save map to disk
saveWidget(widget = earthquakemap, file = "Earthquakemap.html")
More on Leaflet/R at https://rstudio.github.io/leaflet/.