In-class Exercise 7

Author

Rizqi Febriansyah

Published

March 8, 2024

Modified

March 9, 2024

Load Packages

Code chunk below loads the packages:

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

Import dataset

Code chunk below imports the dataset:

rfstations <- read_csv("data/aspatial/RainfallStation.csv")

Plotting IsoMap

rfdata <- read_csv("data/aspatial/DAILYDATA_202402.csv") %>%
  select(c(1,5)) %>%
  group_by(Station) %>%
  summarise(MONTHSUM = sum(`Daily Rainfall Total (mm)`)) %>%
  ungroup()
rfdata <- rfdata %>%
  left_join(rfstations)

Joining with ‘by = join_by(Station)’

rfdata_sf <- st_as_sf(rfdata,
                      coords = c("Longitude", "Latitude"),
                      crs = 4326) %>%
  st_transform(crs = 3414)
mpsz2019 <- st_read(dsn = "data/geospatial",
                    layer = "MPSZ-2019") %>%
  st_transform(crs = 3414)
Reading layer `MPSZ-2019' from data source 
  `/Users/rizfebriansyah/GitHub/ISSS608-VAA/In-class_Ex/In-class_Ex03/data/geospatial' 
  using driver `ESRI Shapefile'
Simple feature collection with 332 features and 6 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: 103.6057 ymin: 1.158699 xmax: 104.0885 ymax: 1.470775
Geodetic CRS:  WGS 84
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")
grid <- terra::rast(mpsz2019,
                    nrows = 690,
                    ncols = 1075)

xy <- terra::xyFromCell(grid,
                        1:ncell(grid))