Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add normalize argument to st_crop #686

Merged
merged 1 commit into from
May 16, 2024
Merged

add normalize argument to st_crop #686

merged 1 commit into from
May 16, 2024

Conversation

btupper
Copy link
Contributor

@btupper btupper commented May 16, 2024

As per #685 this pull request adds a normalize argument (default is FALSE) to the st_crop function.

Here's a demonstration of the package with the changes.

suppressPackageStartupMessages({
  library(sf)
  library(stars)
  library(dplyr)
})
## Warning: package 'sf' was built under R version 4.3.2
# read in data
filename= system.file(paste0("netcdf/", "avhrr-only-v2.19810901.nc"), package = "starsdata")
orig = read_stars(filename, sub = "sst", quiet = TRUE, proxy = FALSE)
bb = st_bbox(c(xmin = 270, ymin = 20, xmax = 300, ymax = 50), crs = st_crs(orig))
crop = st_crop(orig, bb, normalize = TRUE)
x = st_sample(bb, 10)

Compare the maximum cell index with the output of st_cells. Note the cells indices are less than the maximum possible cell index.

iorig = st_cells(orig, x)
iorig
##  [1] 330876 353902 283381 289158 294893 264667 303586 343905 276140 270388
mx = prod(dim(orig))
cat("max orig index:", mx, "\n")
## max orig index: 1036800
iorig <= mx
##  [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE

And now the same for the cropped.

icrop = st_cells(crop, x)
icrop
##  [1]  8316 10222  4381  4878  5333  2827  6106  9465  3740  3268
mx = prod(dim(crop))
cat("max cropped index:", mx, "\n")
## max cropped index: 14400
icrop <= mx
##  [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE

@edzer edzer merged commit 1dbd5d2 into r-spatial:main May 16, 2024
7 checks passed
@edzer
Copy link
Member

edzer commented May 16, 2024

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants