@@ -87,7 +87,7 @@ get_cellindex <- function(grid_filename, extent = NULL, coordinates = NULL, shap
8787
8888 # Check if extent values are within the longitude and latitude range in the cells
8989 if (! is.null(extent )) {
90- cells $ cellindex <- as.numeric(row.names(cells ))
90+ cells $ cellindex <- as.numeric(row.names(cells )) + 1
9191
9292 out_of_bounds_lon <- extent [c(1 , 2 )][extent [c(1 , 2 )] < lon_range [1 ] |
9393 extent [c(1 , 2 )] > lon_range [2 ]]
@@ -107,12 +107,15 @@ get_cellindex <- function(grid_filename, extent = NULL, coordinates = NULL, shap
107107 cells $ lon < = extent [2 ] &
108108 cells $ lat > = extent [3 ] & cells $ lat < = extent [4 ], ]
109109
110- grid_cell <- transform(grid_lonlat , " lon_lat" )
111-
112- cells <- grid_cell $ subset(coordinates = lapply(X = list (lon = cells $ lon ,
113- lat = cells $ lat ),
114- FUN = as.character ))
110+ if (! simplify ) {
111+ grid_cell <- transform(grid_lonlat , " lon_lat" )
115112
113+ cells <- grid_cell $ subset(coordinates = lapply(X = list (lon = cells $ lon ,
114+ lat = cells $ lat ),
115+ FUN = as.character ))
116+ } else {
117+ cells <- cells $ cellindex
118+ }
116119 }
117120
118121 # Check if coordinates are within the longitude and latitude range in the cells
@@ -149,18 +152,20 @@ get_cellindex <- function(grid_filename, extent = NULL, coordinates = NULL, shap
149152 }
150153
151154 if (! is.null(shape )) {
155+ grid_lonlat <- grid_lonlat $ transform(" lon_lat" )
156+
152157 cell_coords <- grid_lonlat | >
153158 as_terra() | >
154159 terra :: mask(shape ) | >
155160 terra :: as.data.frame(xy = TRUE ) | >
156161 dplyr :: select(" x" , " y" )
157162
158- cells <- grid_lonlat $ transform( " lon_lat " ) | >
163+ cells <- grid_lonlat | >
159164 subset(coordinates = lapply(list (lon = cell_coords $ x , lat = cell_coords $ y ),
160165 FUN = as.character ))
161166 }
162167
163- if (simplify ) {
168+ if (simplify && is.null( extent ) ) {
164169 cells <- c(stats :: na.omit(c(cells $ data + 1 )))
165170 }
166171
0 commit comments