getCoords: Get "x,y" coordinates in appropriate format

View source: R/getCoords.R

getCoordsR Documentation

Get "x,y" coordinates in appropriate format

Description

Function to get the coordinates in "x,y" format for a given set of points

Usage

getCoords(
  data,
  proj = NULL,
  x = "x",
  y = "y",
  z_fix = NULL,
  precision = 2,
  ...
)

Arguments

data

An object of class data.table or something coercible to it containing the coordinates needing conversion, or a SpatialPointsDataFrame.

proj

A crs object or character string representing the output projection. Required unless z_fix is provided in which case proj is ignored.

x

A character vector representing the column containing the 'x' coordinates. Required if data is not SpatialPointsDataFrame.

y

A character vector representing the column containing the 'y' coordinates. Required if data is not SpatialPointsDataFrame.

z_fix

A SpatRaster with the same origin and resolution as the z_fix used to generate the 'world' with makeWorld.

precision

An integer representing the number of decimals to retain in the x and y directions. For grid sizes with nice, round numbers precisions can be low. This factor is controled by rast and must be the same as the one used to generate the 'world' with makeWorld. Default is 2.

...

Additional arguments to pass to fix_z.

Value

A vector containing the requested coordinates in appropriate format in the same order as the input data.

Examples

# Generate a DEM
n <- 5
dem <- expand.grid(list(x = 1:(n * 100),
                        y = 1:(n * 100))) / 100
dem <- as.data.table(dem)
dem[, z := 250 * exp(-(x - n/2)^2) + 
      250 * exp(-(y - n/2)^2)]
dem <- rast(dem)
ext(dem) <- c(10000, 20000, 30000, 40000)
crs(dem) <- "+proj=lcc +lat_1=48 +lat_2=33 +lon_0=-100 +datum=WGS84"

# Generate five random points that fall within the DEM
points <- data.table(x = runif(5, ext(dem)[1], ext(dem)[2]),
                     y = runif(5, ext(dem)[3], ext(dem)[4]))

# Get the coordinates
points$Cell <- getCoords(points, z_fix = dem)

andresgmejiar/lbmech documentation built on Feb. 2, 2025, 12:37 a.m.