99# ' to correct it.
1010# '
1111# ' @param grid_filename A string representing the grid file name.
12- # ' @param extent A numeric vector (lonmin, lonmax, latmin, latmax) containing the
13- # ' longitude and latitude boundaries between which values included in the subset.
14- # ' @param coordinates A list of two named (lon, lat) numeric vectors representing the coordinates.
15- # ' @param shape A terra SpatVector object in the WGS 84 coordinate reference system
16- # ' representing the shape to subset the grid.
17- # ' @param simplify A logical indicating whether to simplify the output to a vector.
12+ # ' @param extent A numeric vector (lonmin, lonmax, latmin, latmax) containing
13+ # ' the longitude and latitude boundaries between which values included in the
14+ # ' subset.
15+ # ' @param coordinates A list of two named (lon, lat) numeric vectors
16+ # ' representing the coordinates.
17+ # ' @param shape A terra SpatVector object in the WGS 84 coordinate reference
18+ # ' system representing the shape to subset the grid.
19+ # ' @param simplify A logical indicating whether to simplify the output to a
20+ # ' vector.
1821# '
1922# '
2023# ' @return Either an LPJmLData object containing the grid or a vector subsetted
2528# ' \dontrun{
2629# ' get_cellindex(
2730# ' grid_filename = "my_grid.bin.json",
28- # ' extent = c(-123.25, -122.75, 49.25, 49.75) # (lonmin, lonmax, latmin, latmax)
31+ # ' extent = c(-123.25, -122.75, 49.25, 49.75) # (lonmin, lonmax, latmin,
32+ # ' latmax)
2933# ' )
3034# ' get_cellindex(
3135# ' grid_filename = "my_grid.bin.json",
3943# '
4044# ' If an `extent` is provided, the function filters the cells to include only
4145# ' those within the specified longitude and latitude range. The `extent` should
42- # ' be a numeric vector of length 4 in the form c(lonmin, lonmax, latmin, latmax).
46+ # ' be a numeric vector of length 4 in the form c(lonmin, lonmax, latmin,
47+ # ' latmax).
4348# '
4449# ' If a list of `coordinates` is provided, the function filters the cells to
4550# ' include only those that match the specified coordinates. The `coordinates`
5863# ' specific error messages for different error conditions. For example, it
5964# ' checks if the `grid_filename` exists, if the `extent` vector has the correct
6065# ' length, and if the `coordinates` list contains two vectors of equal length.
61- get_cellindex <- function (grid_filename , extent = NULL , coordinates = NULL , shape = NULL , simplify = TRUE ) {
66+ get_cellindex <- function (grid_filename , extent = NULL ,
67+ coordinates = NULL ,
68+ shape = NULL ,
69+ simplify = TRUE ) {
6270 # check if filepath is valid
6371 check_filepath(grid_filename )
6472 # check if (only) one of extent or coordinates is provided
@@ -85,7 +93,8 @@ get_cellindex <- function(grid_filename, extent = NULL, coordinates = NULL, shap
8593 lon_range <- range(cells $ lon )
8694 lat_range <- range(cells $ lat )
8795
88- # Check if extent values are within the longitude and latitude range in the cells
96+ # Check if extent values are within the longitude and latitude range in the
97+ # cells
8998 if (! is.null(extent )) {
9099 cells $ cellindex <- as.numeric(row.names(cells )) + 1
91100
@@ -118,7 +127,8 @@ get_cellindex <- function(grid_filename, extent = NULL, coordinates = NULL, shap
118127 }
119128 }
120129
121- # Check if coordinates are within the longitude and latitude range in the cells
130+ # Check if coordinates are within the longitude and latitude range in the
131+ # cells
122132 if (! is.null(coordinates )) {
123133 out_of_bounds <- coordinates [[1 ]] < lon_range [1 ] |
124134 coordinates [[1 ]] > lon_range [2 ] |
@@ -161,7 +171,8 @@ get_cellindex <- function(grid_filename, extent = NULL, coordinates = NULL, shap
161171 dplyr :: select(" x" , " y" )
162172
163173 cells <- grid_lonlat | >
164- subset(coordinates = lapply(list (lon = cell_coords $ x , lat = cell_coords $ y ),
174+ subset(coordinates = lapply(list (lon = cell_coords $ x ,
175+ lat = cell_coords $ y ),
165176 FUN = as.character ))
166177 }
167178
@@ -189,7 +200,8 @@ check_extent <- function(extent) {
189200 stop(" extent must be a numeric vector of length 4." )
190201 }
191202
192- # check if character vector present and convert to numeric (to be consistent with coordinates) # nolint
203+ # check if character vector present and convert to numeric (to be consistent
204+ # with coordinates)
193205 if (is.character(extent )) {
194206 extent <- as.numeric(extent )
195207 }
@@ -207,7 +219,8 @@ check_extent <- function(extent) {
207219# Check if both extent and coordinates are provided
208220check_multiple <- function (extent , coordinates , shape ) {
209221 if (is.null(extent ) && is.null(coordinates ) && is.null(shape )) {
210- warning(" Neither extent, coordinates or shape provided. Full grid will be returned." )
222+ warning(" Neither extent, coordinates or shape provided." ,
223+ " Full grid will be returned." )
211224 }
212225 if ((! is.null(extent ) && ! is.null(coordinates )) ||
213226 (! is.null(extent ) && ! is.null(shape )) ||
@@ -236,15 +249,17 @@ correct_extent <- function(extent) {
236249 extent
237250}
238251
239- # Check if the coordinates are a list of two numeric vectors and of equal length
252+ # Check if the coordinates are a list of two numeric vectors and of equal
253+ # length
240254check_coordinates <- function (coordinates ) {
241255 if (! is.list(coordinates ) || length(coordinates ) != 2 ) {
242256 stop(" coordinates must be a list of two vectors." )
243257 }
244258
245259 coordinates <- lapply(coordinates , function (coord ) {
246260 if (! is.numeric(coord )) {
247- warning(" Non-numeric coordinates detected, attempting to convert to numeric." )
261+ warning(" Non-numeric coordinates detected," ,
262+ " attempting to convert to numeric." )
248263 coord <- as.numeric(coord )
249264 if (any(is.na(coord ))) {
250265 stop(" Unable to convert all coordinates to numeric." )
0 commit comments