Skip to content

Commit 4b4ffef

Browse files
authored
Optimize Raster Grid Generation (Speed & Memory)
2 parents a88c689 + 050d16e commit 4b4ffef

File tree

3 files changed

+25
-24
lines changed

3 files changed

+25
-24
lines changed

R/backend_sequential.R

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,12 +199,22 @@ inspire_grid_from_extent_internal <- function(
199199

200200
# 3. Generate IDs and Assign as Levels (Factor Raster)
201201
if (id_format != "none") {
202-
# xyFromCell returns centroids
203-
coords <- terra::xyFromCell(r, seq_len(terra::ncell(r)))
202+
# xyFromCell returns centroids. For large rasters, this is memory-intensive.
203+
# We replicate the logic using efficient vector generation.
204+
# Rows (Y) are outer loop (slowest changing), Columns (X) are inner loop (fastest).
205+
206+
nrows <- terra::nrow(r)
207+
ncols <- terra::ncol(r)
204208

205-
# Calculate LLC for make_ids
206-
x_llc <- coords[, 1] - (cellsize_m / 2)
207-
y_llc <- coords[, 2] - (cellsize_m / 2)
209+
# Calculate LLC for make_ids directly
210+
# X LLC: seq from xmin
211+
x_llc_seq <- seq.int(from = xmin, by = cellsize_m, length.out = ncols)
212+
# Y LLC: seq from ymax - cellsize (top-left Y LLC) down to ymin
213+
y_llc_seq <- seq.int(from = ymax - cellsize_m, by = -cellsize_m, length.out = nrows)
214+
215+
# Create full vectors
216+
x_llc <- rep(x_llc_seq, times = nrows)
217+
y_llc <- rep(y_llc_seq, each = ncols)
208218

209219
# Generate ID strings (using existing helper)
210220
ids_list <- make_ids(

R/stream_grid_raster.R

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -127,17 +127,7 @@ stream_grid_raster_terra <- function(
127127
)
128128

129129
cells <- first_cell:last_cell
130-
coords <- terra::xyFromCell(r_template, cells)
131-
132-
x_vals <- coords[, 1]
133-
y_vals <- coords[, 2]
134-
135-
# Compute grid cell IDs (linear addressing, row-major order)
136-
# Column: floor((x - xmin) / cellsize) + 1
137-
# Row: floor((ymax - y) / cellsize) + 1 (Y axis is inverted)
138-
col_idx <- floor((x_vals - xmin) / cellsize_m) + 1
139-
row_idx <- floor((ymax - y_vals) / cellsize_m) + 1
140-
cell_ids <- (row_idx - 1) * ncols + col_idx
130+
cell_ids <- cells
141131

142132
# Write chunk values
143133
terra::writeValues(

R/stream_grid_raster_parallel.R

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,15 @@ NULL
4141
#' @keywords internal
4242
#' @noRd
4343
.compute_raster_chunk <- function(start_row, nrows, ncols) {
44-
# Generate row/col indices for the chunk
45-
# Generate continuous sequence of cell IDs directly (row-major order)
46-
# Optimization: Avoid allocating large row/col vectors
47-
start_id <- (start_row - 1) * ncols + 1
48-
end_id <- start_id + (nrows * ncols) - 1
49-
cell_ids <- start_id:end_id
50-
51-
list(values = as.integer(cell_ids), start = start_row, nrows = nrows)
44+
# Compute first cell ID for this linear chunk
45+
# Cell IDs are 1-based, row-major
46+
first_cell <- (start_row - 1) * ncols + 1
47+
48+
# Create sequence directly
49+
# Since we are processing full rows, the cell IDs are contiguous
50+
values <- seq.int(from = first_cell, by = 1L, length.out = nrows * ncols)
51+
52+
list(values = values, start = start_row, nrows = nrows)
5253
}
5354

5455

0 commit comments

Comments
 (0)