In Class Exercise 7

Author

Zachary Wong

Published

March 9, 2024

Modified

March 10, 2024

Getting Started

Launching R Packages

Code
pacman::p_load(sf, terra, gstat, tmap,
               viridis, tidyverse)

Dataset Loading and Wrangling

Code
rfstations <- read_csv("data/aspatial/RainfallStation.csv")
Code
rfdata <- read_csv("data/aspatial/DAILYDATA_202402.csv") %>%
  select(c(1,5)) %>%
  group_by(Station) %>%
  summarise(MONTHSUM = sum(`Daily Rainfall Total (mm)`) )%>%
  ungroup()
Code
rfdata <- rfdata %>%
  left_join(rfstations)
Code
rfdata_sf <- st_as_sf(rfdata,
                      coords = c("Longitude",
                                 "Latitude"),
                      crs = 4326) %>%
  st_transform(crs = 3414)
Code
mpsz2019 <- st_read(dsn = "data/geospatial",
                    layer = "MPSZ-2019") %>%
  st_transform(crs = 3414)

Using tmap Function

Code
tmap_options(check.and.fix = TRUE)
tmap_mode("view")
tm_shape(mpsz2019) +
  tm_borders() +
  tm_shape(rfdata_sf) +
  tm_dots(col = 'MONTHSUM')
tmap_mode("plot")

Applying Interpolation

Code
grid <- terra::rast(mpsz2019,
                    nrows = 690,
                    ncols = 1075)
xy <- terra::xyFromCell(grid,
                        1:ncell(grid))
Code
coop <- st_as_sf(as.data.frame(xy),
                 coords = c(x,y))