Lab 4: Part B

Matthew Sommer

2024-03-21


Introduction

It is often useful to have interactive maps as a display of an analysis. Leaflet is an open source JavaScript library with a corresponding R interface. To demonstrate how this might be useful, I have made a map of all of the rooftop solar installations in San Juan County, Washington.

The first step is to import the necessary libraries including sf for manipulating spatial data, leaflet for interactive mapping, and tidyverse, here, and janitor for data cleaning. Then two data sets are read into R to create the visualization.

# import libraries
library(leaflet)
library(sf)
library(tidyverse)
library(here)
library(janitor)
library(leafpop)

### read in data

# all electric points with associated data. will be narrowed and
#    anonymized
electric_pnts <- st_read(here("data", "points_3212024.shp"))

# subset of all electric points with associated usage data
#   only unique ID will be used from this data set
solar <- read_csv(here("data", "solar_data_test.csv"))

The first data set is the geographic data for all electric meters and the second data set represents solar panel installations. The two data sets contain far more variables than are necessary for this demonstration, so some data cleaning will be done to remove extraneous variables and subset the data. The two data sets will be joined using a unique identifier by an inner join. The results will be points representing all solar installations that are connected to the electric grid in San Juan County. These points will then be mapped in an interactive map using leaflet.

# narrow fields to anonymize data and keep only relevant
#    fields
all_mtrs <- electric_pnts %>%
  select(gs_servi_1, gs_service)

# extract solar installations IDs as characters
#    so they can be joined to the meter points
solar_id <- solar %>%
  clean_names() %>% 
  select(service_location) %>% 
  distinct(service_location) %>% 
  rename(gs_service = service_location) %>% 
  mutate(gs_service = as.character(gs_service))

# join the two data frames to get only points
#    with solar installations
solar_pts <- all_mtrs %>% 
  inner_join(solar_id, by = "gs_service")

Now that the data has been cleaned, the result is a data frame with 726 points, two identifiers, and a point geometry. The geometry is in the local state plane projection. To use this spatial data frame in the leaflet map, it will be transformed into EPSG 4326, which is a common web map coordinate reference system.

# transform spatial reference to web map friendly crs
solar_pts <- st_transform(solar_pts, 4326)

Now a leaflet map can be created and the point data can be added. I am calling this ‘basic map’ as a starting point to demonstrate additional map features that can be added to it. It uses a base map from Carto and the solar interconnect points as markers.

basic_map <- leaflet() %>% 
  addProviderTiles(providers$CartoDB) %>% 
  addMarkers(data = solar_pts)
  
basic_map

This map is very cluttered, so I am going to use leaflet’s built in clustering function to generalize the point. Also, I’ve changed the base map just for fun. I’ve also changed the marker to an orange circle marker set against the dark base map. I put a legend in the bottom right corner to show the solar interconnects. In this instance it isn’t totally necessary because it is the only feature in the map, but it does add some context to what is being shown.

I’ve also added a little information as a pop up. This pop up shows some identifiers that would allow a map user to find some more information. If this map were going to be used professionally, more information could be added including customer information. One feature of the pop ups is that graphs are available. This could come in handy for use with analysis results. In this case, showing some generation results over time could be added to the pop up.

leaflet(data = solar_pts) %>% 
  addProviderTiles(providers$CartoDB.DarkMatter) %>% 
  addCircleMarkers(color = 'orange',
                   stroke = FALSE,
                   fillOpacity = 0.75,
                   radius = 8,
                   clusterOptions = markerClusterOptions(),
             popup = leafpop::popupTable(st_drop_geometry(solar_pts),
                                         feature.id = FALSE,
                                         row.numbers = FALSE)) %>% 
  addLegend(position = "bottomright",
            colors = 'orange',
            labels = "Solar Interconnect")

```

Conclusion

Working through this exercise gave me a good head start on some of the techniques that may be helpful for visualizing my results for my term project. Through this exercise, I successfully mapped the solar installations in the county by tying data from a non-spatial database to a spatial one and creating an interactive map from that data. The map could be enhanced by different classifications including network relationships. Likewise, adding some information describing generation capacity or array size at each site could be helpful.