This assignment reads GIS data for US national parks from https://data-oema.opendata.arcgis.com and illustrate them in a map using the R package “leaflet”.
Here we only consider the park categories:“National Park”,“National Battlefield Park”,“National Monument”,“National Historic Site”,“National Memorial”, “National Recreation Area”.
The map uses the “layers control” option to allow users to switch between the above park categories.
library(leaflet)
#load national parks dsata
data_url = "https://data-oema.opendata.arcgis.com/datasets/c54be84491364a04a0caecc837ab492a_0.csv"
df_parks <- read.csv(url(data_url), na.strings=c("NA","#DIV/0!",""))
#rename columns
colnames(df_parks)[1] <- "Longitude"
colnames(df_parks)[2] <- "Latitude"
#subset only few types
categories= c("National Park","National Battlefield Park","National Monument","National Historic Site","National Memorial", "National Recreation Area")
df_parks<- df_parks[df_parks$UNIT_TYPE %in% categories, ]
df_parks$UNIT_TYPE <- as.factor(df_parks$UNIT_TYPE)
#groups based on park type
groupColors = colorFactor(palette = "Dark2", domain = df_parks$UNIT_TYPE )
groups = as.character(unique(df_parks$UNIT_TYPE))
map = leaflet(df_parks,width = "100%") %>% addTiles(group = "OpenStreetMap")
for(g in groups){
d = df_parks[df_parks$UNIT_TYPE == g, ]
map = map %>% addCircleMarkers(data = d,
lng = ~Longitude,
lat = ~Latitude,
popup = ~paste(
'Name: <strong>', UNIT_NAME, '</strong><br>',
'Type: <strong>', UNIT_TYPE, '</strong><br>',
'Longitude: <strong>', Longitude, '</strong><br>',
'Latitude: <strong>', Latitude, '</strong><br>',
'State: <strong>', STATE, '</strong><br>',
"<a href='", METADATA, "'> Click for More Information </a><br>"),
color = ~groupColors(UNIT_TYPE),
clusterOptions = markerClusterOptions(),
group = g)
}
map %>% addLayersControl(overlayGroups = groups) %>%
addLegend(
position = 'bottomleft',
pal = groupColors, values = ~UNIT_TYPE,
title = 'Type of Parks'
)
Use the icon at the top right coner of the map, to switch between the park categories.