Skip to contents

Transform between Coordinate Reference Systems

Usage

transform_crs(data, coord, in_crs, out_crs, append = TRUE)

transform_LV95_to_WSG84(data, coord = c(lon = "x", lat = "y"), append = TRUE)

transform_WSG84_to_LV95(data, coord = c(x = "lon", y = "lat"), append = TRUE)

transform_LV03_to_WSG84(data, coord = c(lon = "x", lat = "y"), append = TRUE)

transform_WSG84_to_LV03(data, coord = c(x = "lon", y = "lat"), append = TRUE)

Arguments

data

input data

coord

mappping of columns for Conversion

in_crs

source coordinate reference system as sp::CRS

out_crs

target coordinate reference system as sp::CRS

append

append data in new Columns

Value

tibble with transformed coordinates

WSG84 (EPSG 4326) vs Web Pseudo Mercator (EPSG 3857)

See Tiles à la Google Maps for a detailed explanation. In Short:

EPSG:4326 uses a coordinate system on the surface of a sphere or ellipsoid of reference.

EPSG:3857 uses a coordinate system PROJECTED from the surface of the sphere or ellipsoid to a flat surface.

Usually Packages and Webservices expects coordinates for Elements in EPSG:4326, one Exception is leaflet::addRasterImage()

Known Issues

R spatial is migrating to PROJ6+ and gdal3+. At the moment the sp::spTransform isn't converted to use the WKT comments, and rgdal raises some Warnings. The transformation are done correctly.

Examples

fn <- system.file("extdata", "meta_smn.rds", package = "rOstluft.data")
data <- readRDS(fn)
data <- dplyr::distinct(data, site, x, y)
transform_crs(
  data = data,
  coord = c(lon = "x", lat = "y"),
  in_crs = sf::st_crs("EPSG:2056"),
  out_crs = sf::st_crs("EPSG:4326")
)
#> # A tibble: 22 × 5
#>    site                     x       y   lon   lat
#>  * <chr>                <dbl>   <dbl> <dbl> <dbl>
#>  1 Aadorf/Tänikon     2710517 1259824  8.90  47.5
#>  2 Altenrhein         2760382 1261386  9.57  47.5
#>  3 BadRagaz           2756910 1209350  9.50  47.0
#>  4 Chur               2759465 1193152  9.53  46.9
#>  5 Ebnat-Kappel       2726347 1237176  9.11  47.3
#>  6 Glarus             2723755 1210567  9.07  47.0
#>  7 Hörnli             2713516 1247756  8.94  47.4
#>  8 Lägern             2672250 1259460  8.40  47.5
#>  9 Oberriet/Kriessern 2764170 1249582  9.61  47.4
#> 10 Pilatus            2661904 1203410  8.25  47.0
#> # ℹ 12 more rows

transform_crs(
  data = data,
  coord = c(lon = "x", lat = "y"),
  in_crs = sf::st_crs("EPSG:2056"),
  out_crs = sf::st_crs("EPSG:4326"),
  append = FALSE
)
#> # A tibble: 22 × 3
#>    site                 lon   lat
#>  * <chr>              <dbl> <dbl>
#>  1 Aadorf/Tänikon      8.90  47.5
#>  2 Altenrhein          9.57  47.5
#>  3 BadRagaz            9.50  47.0
#>  4 Chur                9.53  46.9
#>  5 Ebnat-Kappel        9.11  47.3
#>  6 Glarus              9.07  47.0
#>  7 Hörnli              8.94  47.4
#>  8 Lägern              8.40  47.5
#>  9 Oberriet/Kriessern  9.61  47.4
#> 10 Pilatus             8.25  47.0
#> # ℹ 12 more rows

transform_LV95_to_WSG84(data)
#> # A tibble: 22 × 5
#>    site                     x       y   lon   lat
#>  * <chr>                <dbl>   <dbl> <dbl> <dbl>
#>  1 Aadorf/Tänikon     2710517 1259824  8.90  47.5
#>  2 Altenrhein         2760382 1261386  9.57  47.5
#>  3 BadRagaz           2756910 1209350  9.50  47.0
#>  4 Chur               2759465 1193152  9.53  46.9
#>  5 Ebnat-Kappel       2726347 1237176  9.11  47.3
#>  6 Glarus             2723755 1210567  9.07  47.0
#>  7 Hörnli             2713516 1247756  8.94  47.4
#>  8 Lägern             2672250 1259460  8.40  47.5
#>  9 Oberriet/Kriessern 2764170 1249582  9.61  47.4
#> 10 Pilatus            2661904 1203410  8.25  47.0
#> # ℹ 12 more rows