-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutilities_grid.R
More file actions
31 lines (23 loc) · 926 Bytes
/
Copy pathutilities_grid.R
File metadata and controls
31 lines (23 loc) · 926 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
library(terra)
classify_locations_into_gridcells <- function(locations, gridtype) {
# Read grid
if (gridtype=="10x10"){
grid <- vect("Input_grids/assessment_grid_10_countries.shp")[, "CellCode"]
}else{
grid <- vect("Input_grids/assessment_grid_100_20_country.shp")[, "GRIDCODE"]
names(grid) <- "CellCode"
}
# Identify invalid geometries
is.valid(grid)
# Make invalid geometries valid
grid <- makeValid(grid)
# Transform projection into UTM33N
grid <- terra::project(grid, "EPSG:4326")
# Make locations spatial keeping original longitude/latitude
locations <- vect(as.data.frame(locations), geom = c("Longitude", "Latitude"), crs = "EPSG:4326", keepgeom= T)
# Classify stations into the gridcells
locations <- terra::intersect(grid, locations)
# Remove spatial column in order to merge station samples
locations <-as.data.table(locations)
return(locations)
}