Skip to content

Commit

Permalink
Fix incompatibilities with sp and raster
Browse files Browse the repository at this point in the history
  • Loading branch information
caiohamamura committed Apr 1, 2024
1 parent 3fecd90 commit e1b1119
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 83 deletions.
3 changes: 3 additions & 0 deletions R/clipLevel1BGeo.R
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ clipLevel1BGeoGeometry <- function(level1BGeo, polygon, split_by = "id") {
if (nrow(level1BGeo) == 0) {
print("The polygon does not overlap the GEDI data")
} else {
.I <- data.table::.I
level1BGeo[, id := .I]

points <- sf::st_as_sf(
level1BGeo,
coords = c("longitude_bin0", "latitude_bin0"),
Expand Down
4 changes: 3 additions & 1 deletion R/clipLevel2AM.R
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,10 @@ clipLevel2AMGeometry <- function(level2AM, polygon, split_by = "id") {
if (nrow(level2adt) == 0) {
print("The polygon does not overlap the GEDI data")
} else {
.I <- data.table::.I
level2adt[, id := .I]
points <- sf::st_as_sf(
level2AM,
level2adt,
coords = c("lon_lowestmode", "lat_lowestmode"),
crs = sf::st_crs(polygon)
)
Expand Down
4 changes: 3 additions & 1 deletion R/clipLevel2BPAIProfile.R
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,10 @@ clipLevel2BPAIProfileGeometry <- function(level2BPAIProfile, polygon, split_by =
if (nrow(level2bdt) == 0) {
print("The polygon does not overlap the GEDI data")
} else {
.I <- data.table::.I
level2bdt[, id := .I]
points <- sf::st_as_sf(
level2BPAIProfile,
level2bdt,
coords = c("lon_lowestmode", "lat_lowestmode"),
crs = sf::st_crs(polygon)
)
Expand Down
5 changes: 4 additions & 1 deletion R/clipLevel2BPAVDProfile.R
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,11 @@ clipLevel2BPAVDProfileGeometry <- function(level2BPAVDProfile, polygon, split_by
if (nrow(level2bdt) == 0) {
print("The polygon does not overlap the GEDI data")
} else {
.I <- data.table::.I
level2bdt[, id := .I]

points <- sf::st_as_sf(
level2BPAVDProfile,
level2bdt,
coords = c("lon_lowestmode", "lat_lowestmode"),
crs = sf::st_crs(polygon)
)
Expand Down
4 changes: 3 additions & 1 deletion R/clipLevel2BVPM.R
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,10 @@ clipLevel2BVPMGeometry <- function(level2BVPM, polygon, split_by = NULL) {
if (nrow(level2bdt) == 0) {
print("The polygon does not overlap the GEDI data")
} else {
.I <- data.table::.I
level2bdt[, id := .I]
points <- sf::st_as_sf(
level2BVPM,
level2bdt,
coords = c("longitude_bin0", "latitude_bin0"),
crs = sf::st_crs(polygon)
)
Expand Down
32 changes: 14 additions & 18 deletions R/gridStatsLevel2AM.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,31 +75,27 @@ gridStatsLevel2AM <- function(level2AM, func, res = 0.5) {

call <- lazy_call(func)

sf <- sf::st_as_sf(level2AM, coords = c("lon_lowestmode", "lat_lowestmode"), crs = "epsg:4326")
bbox <- terra::ext(sf)
layout <- terra::rast(bbox, resolution = res, vals = NA, crs = "epsg:4326")

sf <- sf::st_as_sf(level2AM, coords = c("lon_lowestmode", "lat_lowestmode"))
layout <- sf::st_bbox(sf) %>%
stars::st_as_stars(dx = res, dy = res, values = NA, crs = "epsg:4326")

level2AM[, cells := stars::st_cells(layout, sf)]
level2AM[, cells := terra::cells(layout, terra::vect(sf))[, 2]]
metrics <- lazy_apply_dt_call(level2AM, call, group.by = "by = cells")
metrics <- metrics[cells > -1]
n_metrics <- ncol(metrics) - 1
output <- sf::st_bbox(sf) %>%
stars::st_as_stars(
dx = res,
dy = res,
values = as.numeric(NA),
nz = n_metrics,
crs = "epsg:4326"
)

dim_data <- stars::st_dimensions(output) %>%
setNames(c("x", "y", "bands"))
dim_data$bands$values <- names(metrics)[-1]
output <- terra::rast(
bbox,
resolution = res,
vals = as.numeric(NA),
nlyr = n_metrics,
crs = "epsg:4326"
)

stars::st_dimensions(output) <- dim_data
names(output) <- names(metrics)[-1]

for (metric in seq_along(names(metrics)[-1])) {
output[[1]][, , metric][metrics$cells] <- metrics[[metric]]
output[[metric]][metrics$cells] <- metrics[[metric + 1]]
}

return(output)
Expand Down
29 changes: 13 additions & 16 deletions R/gridStatsLevel2BVPM.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,29 +74,26 @@ gridStatsLevel2BVPM <- function(level2BVPM, func, res) {


sf <- sf::st_as_sf(level2BVPM, coords = c("longitude_bin0", "latitude_bin0"))
layout <- sf::st_bbox(sf) %>%
stars::st_as_stars(dx = res, dy = res, values = NA, crs = "epsg:4326")
bbox <- terra::ext(sf)
layout <- terra::rast(bbox, resolution = res, vals = NA, crs = "epsg:4326")

level2BVPM[, cells := stars::st_cells(layout, sf)]
level2BVPM[, cells := terra::cells(layout, terra::vect(sf))[, 2]]
metrics <- lazy_apply_dt_call(level2BVPM, call, group.by = "by = cells")
metrics <- metrics[cells > -1]
n_metrics <- ncol(metrics) - 1
output <- sf::st_bbox(sf) %>%
stars::st_as_stars(
dx = res,
dy = res,
values = as.numeric(NA),
nz = n_metrics,
crs = "epsg:4326"
)

dim_data <- stars::st_dimensions(output) %>%
setNames(c("x", "y", "bands"))
dim_data$bands$values <- names(metrics)[-1]
output <- terra::rast(
bbox,
resolution = res,
vals = as.numeric(NA),
nlyr = n_metrics,
crs = "epsg:4326"
)

stars::st_dimensions(output) <- dim_data
names(output) <- names(metrics)[-1]

for (metric in seq_along(names(metrics)[-1])) {
output[[1]][, , metric][metrics$cells] <- metrics[[metric]]
output[[metric]][metrics$cells] <- metrics[[metric + 1]]
}

return(output)
Expand Down
Loading

0 comments on commit e1b1119

Please sign in to comment.