grid_generation: Generate a grid system

View source: R/grid_generation.R

grid_generationR Documentation

Generate a grid system

Description

This function is used to generate (simulate) a grid system based on metadata. The GridDER package provided an example metadata to simulate some grid systems (2_simulate_grid.Rmd). To access the metadata file using download_demoGrid function.

Usage

grid_generation(
  res_x = 10,
  res_y = 10,
  extent_unit = NULL,
  input_extent = NULL,
  country = NULL,
  crs_num = NULL,
  crs_obj = NULL,
  flag_offset = NULL,
  flag_maskByCountry = FALSE,
  flag_buffer = 2,
  flag_loadCountryPolygon = FALSE
)

Arguments

res_x

A numeric input of longitude resolution.

res_y

A numeric input of latitude resolution.

extent_unit

Character vector. It can be "crs_countryPolygon", "crs_web_extent", "empirical_occ_extent", "shapefile_extent", "shifted_shapefile_extent".

input_extent

A vector of 4 numbers c(xmin,ymin,xmax,ymax).

country

Character vector of country name where the grid system is from, e.g. "Germany", it can be used to extract spatial extent and the final grid could be masked by country polygons.

crs_num

Character, which is a crs number for the grid system, e.g. "4326".

crs_obj

An crs object for the grid system.

flag_offset

Do any adjustment of the grid system, a vector of two numbers, represent adjustment of the origin along x,y.

flag_maskByCountry

Logical. Mask the grid system by country polygon.

flag_buffer

Draw an additional buffer (e.g. 2 additional grids) along the simulated grid system, this can help cover the areas along the coastal line.

flag_loadCountryPolygon

Load Natural Earth 10meter country polygon data (ne_10m_admin_0_countries).

unit

Character vector of unite in arc-minute ("minute") or kilometre ("km").

Value

The function returns a shapefile.

Author(s)

Xiao Feng

Examples

 ## Not run: 
# Will change. Not run!
# Load a shapefile of countries
country_shp <- raster::shapefile("data/0_basemap/ne_10m_admin_0_countries.shp")

#Load metadata information
grid_metadata <- gsheet::gsheet2tbl('https://docs.google.com/spreadsheets/d/1mSad3lUL5eMgtwH311Bo8PUWIqJkvgxUfG99NiuRLjo/edit#gid=166945453') |>
as.data.frame() |>
subset(!grepl("del_", grid_ID))

# Select the Netherlands data
iii= which(grid_metadata$grid_ID==24)
grid_metadata$country_name[iii]
crs_num = grid_metadata$crs_code[iii]
crs_num

if(grid_metadata$extent_unit[iii]=="empirical_occ_extent"){
empirical_occ_extent = gsub('"',"",grid_metadata$extent[[iii]])
empirical_occ_extent = as.numeric(strsplit(empirical_occ_extent,",")[[1]])
}

# Simulate grid system
NL_grid <- GridDER::grid_generation(res_x = as.numeric(grid_metadata$resolution_x[iii]),
                res_y = as.numeric(grid_metadata$resolution_y[iii]),
                unit = grid_metadata$resolution_unit[iii],
                flag_crs=TRUE,
                extent_unit=grid_metadata$extent_unit[iii],
                input_extent=empirical_occ_extent,
                country = grid_metadata$country_name[iii],
                crs_num = grid_metadata$crs_code[iii],
                flag_maskByCountry = T,
                flag_buffer=10
                )

# Load occurrence data
gbif_occ_proj <- load_occ("data-raw/Netherlands/gridID_24/0047643-210914110416597.csv") |>
spTransform(CRS(paste0("+init=epsg:",crs_num)))

# Plot
plot(NL_grid)
plot(NL_grid,
xlim=c(extent(gbif_occ_proj)[1],
       extent(gbif_occ_proj)[1]+3),
       ylim=c(extent(gbif_occ_proj)[3],
              extent(gbif_occ_proj)[3]+3))
plot(gbif_occ_proj,add=T,col="red")


 
## End(Not run)

BiogeographyLab/gridder documentation built on April 21, 2024, 2:32 a.m.