getCoords.Rd
Function to get the coordinates in "x,y" format for a given set of points
getCoords(
data,
proj = NULL,
x = "x",
y = "y",
z_fix = NULL,
precision = 2,
...
)
An object of class data.table or something coercible to it containing the coordinates needing conversion, or a SpatialPointsDataFrame.
A crs object or character string representing the output
projection. Required unless z_fix
is provided in which case
proj
is ignored.
A character vector representing the column containing the 'x' coordinates.
Required if data
is not SpatialPointsDataFrame.
A character vector representing the column containing the 'y' coordinates.
Required if data
is not SpatialPointsDataFrame.
A SpatRaster with the same origin and resolution as the
z_fix
used to generate the 'world' with makeWorld
.
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
.
A vector containing the requested coordinates in appropriate format in the same order as the input data.
# 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)]
#> x y z
#> 1: 0.01 0.01 1.0146139
#> 2: 0.02 0.01 1.0404641
#> 3: 0.03 0.01 1.0675194
#> 4: 0.04 0.01 1.0958300
#> 5: 0.05 0.01 1.1254477
#> ---
#> 249996: 4.96 5.00 1.0711366
#> 249997: 4.97 5.00 1.0428260
#> 249998: 4.98 5.00 1.0157707
#> 249999: 4.99 5.00 0.9899205
#> 250000: 5.00 5.00 0.9652271
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)