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)

Arguments

df

A data.frame with a x and y coordinate column.

col_x, col_y

Column names or positions of the x and y column. They are passed to vars_pull. These arguments are passed by expression and support quasiquotation (you can unquote column names or column positions).

crs_input

Projection string of class CRS-class (sp compatible) or crs-class (sf compatible) defining the current CRS.

crs_output

Projection string of class CRS-class (sp compatible) or crs-class (sf compatible) defining the CRS to convert to.

Value

A data.frame with the same columns, but transformed coordinates for the x and y column values.

See also

Other GIS_utilities: guess_crs(), plot_coordinates_on_map()

Examples

library(sf)
#> Linking to GEOS 3.10.2, GDAL 3.4.1, PROJ 8.2.1; sf_use_s2() is TRUE
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
  )
}
#> Loading required namespace: sp
#> Warning: GDAL Message 1: +init=epsg:XXXX syntax is deprecated. It might return a CRS with a non-EPSG compliant axis order.
#>   id     lat      lon
#> 1  1 6662134 565946.1
#> 2  2 6580588 426308.3
# 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
)
#>   id     lat      lon
#> 1  1 6662134 565946.1
#> 2  2 6580588 426308.3

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
  )
}
#>   id     lat      lon
#> 1  1 6662134 565946.1
#> 2  2 6580588 426308.3

# 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
)
#>   id     lat      lon
#> 1  1 6662134 565946.1
#> 2  2 6580588 426308.3
# use NSE of x-y columns
transform_coordinates(data_pts,
  col_x = lon, col_y = lat,
  crs_input = sf_crs1, crs_output = sf_crs2
)
#>   id     lat      lon
#> 1  1 6662134 565946.1
#> 2  2 6580588 426308.3
# use position of x-y columns
transform_coordinates(data_pts,
  col_x = 3, col_y = 2,
  crs_input = sf_crs1, crs_output = sf_crs2
)
#>   id     lat      lon
#> 1  1 6662134 565946.1
#> 2  2 6580588 426308.3