find_cells | R Documentation |
This function identifies the mesh cells which surround inputted coordinates. Coordinates can be inputted as lat
and long
vectors, a dataframe (dat
) or a SpatialPoints-class
object (xysp
). The function can return a dataframe or vector of mesh IDs for each pair of coordinates or each unique pair of coordinates inputted.
find_cells(
lat,
long,
dat = NULL,
xysp = NULL,
mesh,
proj = sp::CRS("+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"),
f = NULL,
return = 1
)
lat |
A numeric vector of latitudes (in decimal degrees). Alternatively, |
long |
A numeric vector of longitudes (in decimal degrees). Alternatively, |
dat |
A dataframe with columns 'lat' and 'long' (in decimal degrees) at which mesh IDs will be identified. Alternatively, |
xysp |
A |
mesh |
A mesh, created by |
proj |
A projection string of class CRS-class. The default is World Geodetic System 84 (WGS84). This should be the same as for |
f |
A function to process mesh IDs after identification. For example, |
return |
A number (1, 2, 3 or 4) which specifies the format of the return output. |
Ensure that the coordinate system of inputted coordinates, the mesh and the projection are identical (whether directly specified, e.g. if coordinates are specified via a SpatialPoints-class
object or otherwise, e.g. if coordinates are supplied via lat
and long
vectors.
The function returns a vector or a dataframe, depending on the input to return
(see above).
Edward Lavender
#### Define some coordinates for which to identify the mesh cell ID
# In this hypothetical example, we'll choose coordinates for which
# ... we know the mesh ID in advance, so we can verify the function
# ... returns the correct outputs:
set.seed(1)
xy <- sp::coordinates(dat_mesh_around_nodes)
select <- sample(1:nrow(xy), 10)
xy <- xy[select, ]
selected_nodes <- as.numeric(rownames(xy))
lat <- as.numeric(xy[, 2])
long <- as.numeric(xy[, 1])
#### Set the mesh projection appropriately:
proj <- sp::CRS("+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0")
raster::crs(dat_mesh_around_nodes) <- proj
#### Example 1: Find cells from lat/long input:
cells <- find_cells(lat = lat,
long = long,
mesh = dat_mesh_around_nodes,
f = function(x) as.numeric(as.character(x)),
return = 1)
cells
identical(cells$mesh_ID, selected_nodes)
#### Example 2: Find cells from dat input:
cells <- find_cells(dat = data.frame(lat = lat, long = long),
mesh = dat_mesh_around_nodes,
f = function(x) as.numeric(as.character(x)),
return = 1)
identical(cells$mesh_ID, selected_nodes)
#### Example 3: Find cells from SpatialPoints input:
cells <- find_cells(xysp = sp::SpatialPoints(xy, proj4string = proj),
mesh = dat_mesh_around_nodes,
f = function(x) as.numeric(as.character(x)),
return = 1)
identical(cells$mesh_ID, selected_nodes)
#### Example 4: Find cells for unique coordinates or all coordinates in a dataframe:
# Imagine we have duplicate coordinates:
xy <- rbind(xy, xy)
rownames(xy) <- NULL
# Loop over each return type (1:4) and compare outputs:
cells_ls <-
lapply(1:4, function(i){
cells <- find_cells(xysp = sp::SpatialPoints(xy, proj4string = proj),
long = long,
mesh = dat_mesh_around_nodes,
f = function(x) as.numeric(as.character(x)),
return = i)
return(cells)
})
utils::str(cells_ls)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.