View source: R/transform_coordinates.R
transform_coordinates | R Documentation |
data.frame
columns.This function extracts x-y coordinates from a data.frame
by means of the
given coordinate reference system (CRS
), transforms them to the new CRS
and assign them back to the data.frame
columns.
transform_coordinates(df, col_x, col_y, crs_input, crs_output)
df |
A |
col_x, col_y |
Column names or positions of the x and y
column. They are passed to |
crs_input |
Projection string of class |
crs_output |
Projection string of class |
A data.frame
with the same columns, but transformed coordinates for
the x and y column values.
Other GIS_utilities:
guess_crs()
,
plot_coordinates_on_map()
library(sf)
data_pts <- data.frame(
id = c(1, 2),
lat = c(51.23031, 50.76931),
lon = c(5.083980, 3.829593),
stringsAsFactors = FALSE
)
# CRS-class (use sp package)
if (requireNamespace("sp")) {
sp_crs1 <- sp::CRS("+init=epsg:4269")
sp_crs2 <- sp::CRS("+init=epsg:3857")
transform_coordinates(data_pts,
col_x = "lon", col_y = "lat",
crs_input = sp_crs1, crs_output = sp_crs2
)
}
# crs-class (use sf package)
sf_crs1 <- st_crs(4269)
sf_crs2 <- st_crs(3857)
transform_coordinates(data_pts,
col_x = "lon", col_y = "lat",
crs_input = sf_crs1, crs_output = sf_crs2
)
if (requireNamespace("sp")) {
# input projection is CRS-class (sp) and output projection crs-class (sf)
transform_coordinates(data_pts,
col_x = "lon", col_y = "lat",
crs_input = sp_crs1, crs_output = sf_crs2
)
# input projection is crs-class (spf and output projection CRS-class (sp)
transform_coordinates(data_pts,
col_x = "lon", col_y = "lat",
crs_input = sf_crs1, crs_output = sp_crs2
)
}
# use names (character) of x-y columns
transform_coordinates(data_pts,
col_x = "lon", col_y = "lat",
crs_input = sf_crs1, crs_output = sf_crs2
)
# use NSE of x-y columns
transform_coordinates(data_pts,
col_x = lon, col_y = lat,
crs_input = sf_crs1, crs_output = sf_crs2
)
# use position of x-y columns
transform_coordinates(data_pts,
col_x = 3, col_y = 2,
crs_input = sf_crs1, crs_output = sf_crs2
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.