Search code examples
rcoordinatesr-sf

Change coordinate system in a CSV file


I have a csv file. It has x and y columns for longitude and latitude. The coordinate system is WGS84. The following picture shows a part of my table.
CSV file with lat and long columns

I need to add two new columns to my csv file and change the coordinate system from WGS84 to NAD83/BC Albers and put the results in the two new columns. Please tell me how I can do it in R? Thanks for your help


Solution

  • This is straight forward using the sf package:

    #create some data
    set.seed(1)
    df <- data.frame(ID=1:10, y=runif(10, 48, 50), x= runif(10, -122, -118))
    
    #convert
    library(sf)
    newcoords <- sf_project(from = st_crs(4326), 
               to = st_crs(3005), df[, c("x", "y")]) |> as.data.frame()
    
            V1       V2
    1  1356700 402722.2
    2  1346440 425753.3
    3  1492117 482291.2
    4  1398129 548942.2
    5  1524425 402642.4
    6  1430914 549426.5
    7  1493005 565419.1
    8  1578512 510951.0
    9  1401795 486993.0
    10 1529783 371986.7
    
    cbind(df[ ,c("ID", "x", "y")], newcoords)
    

    Referenced: https://epsg.io to determine the proper CRS number. Another possible CRS is 3153 (provides very similar results, apparently better accuracy).