Migrate spatial backend from {raster} to {terra}
The goal of this task is to replace the legacy {raster} package with the modern {terra} package to improve performance, memory management, and long-term maintainability of the application.
Key Changes
-
Data Loading (
global.R): Replaceraster::brick()withterra::rast()and update CRS assignment usingterra::crs() -
Spatial Operations (
server.R):- Replace
raster::crop()andraster::extent()withterra::crop()andterra::ext() - Switch from
raster::rasterToPolygons()toterra::as.polygons() -
Note: Use
aggregate = FALSEinas.polygons()to maintain 1:1 parity with the legacy pixel-based grid
- Replace
-
Coordinate Transformation: Standardize on
sf::st_transform()for WGS84 (EPSG:4326) projections required by Leaflet
Implementation Logic
To ensure a safe transition, a side-by-side validation was performed using suffix 2 variables (e.g., X12, raster_tmp2) to compare:
- Bounding Boxes: Ensuring perfect geographic alignment
- Extracted Values: Verifying that parameter values match between packages
Benefits
- Significant speed increase in polygon generation
- Reduced memory footprint during cropping operations
- Compatibility with modern R-Spatial standards