Search code examples
rprojectionr-sf

Reprojection is not changing the values in the 'geometry' column when converting to a sf dataframe in R


I have POINT_X, POINT_Y, and a z-point column in a .csv file that I'm trying to reproject in R. Originally, it was projected in the EPSG:26777 reference system, but I'm trying to project it back to WGS 84. However, when I import the .csv file into R and try the conversion, it does not change the point values in my "geometry" column. How can I change them to latitude and longitude coordinates?

Here is the data, exported in the original projection:

data <- data.frame(point_x = c(2387371.258,
                               2387411.214,
                               2387451.258,
                               2387529.427,
                               2387411.258,
                               2387191.258,
                               2387491.258,
                               2387331.258,
                               2387116.239,
                               2387291.258,
                               2387371.258,
                               2387431.258,
                               2387331.258,
                               2387271.258,
                               2387511.258,
                               2387331.258,
                               2387211.258,
                               2387351.258,
                               2387112.752,
                               2387311.22,
                               2387411.258,
                               2387191.258,
                               2387271.258,
                               2387491.258,
                               2387131.258,
                               2387231.258,
                               2387291.258,
                               2387371.052,
                               2387491.258,
                               2387171.258,
                               2387131.258,
                               2387371.258,
                               2387113.437,
                               2387211.258,
                               2387531.258,
                               2387331.258,
                               2387371.258,
                               2387311.258,
                               2387291.258,
                               2387511.258,
                               2387211.258,
                               2387491.258,
                               2387391.258,
                               2387530.847,
                               2387542.074,
                               2387491.258,
                               2387511.258,
                               2387271.258,
                               2387161.36,
                               2387112.772,
                               2387451.258,
                               2387431.258,
                               2387511.258,
                               2387127.551,
                               2387531.258,
                               2387251.258,
                               2387151.258,
                               2387231.258,
                               2387231.258,
                               2387191.258,
                               2387511.258,
                               2387171.258,
                               2387231.258,
                               2387431.258,
                               2387531.258,
                               2387231.258,
                               2387211.258,
                               2387112.281,
                               2387191.258,
                               2387391.258,
                               2387182.737,
                               2387351.258,
                               2387271.258,
                               2387371.258,
                               2387451.197,
                               2387171.258,
                               2387451.258,
                               2387431.258,
                               2387114.101,
                               2387491.219,
                               2387151.258,
                               2387451.258,
                               2387171.258,
                               2387528.481,
                               2387251.258,
                               2387251.258,
                               2387451.258,
                               2387112.193,
                               2387391.258,
                               2387151.258,
                               2387211.258,
                               2387171.258,
                               2387351.258,
                               2387131.258,
                               2387511.258,
                               2387411.258,
                               2387351.258,
                               2387411.258,
                               2387231.258,
                               2387211.224,
                               2387311.258,
                               2387191.258,
                               2387351.258,
                               2387351.258,
                               2387351.258,
                               2387171.258,
                               2387491.258,
                               2387371.258,
                               2387171.225,
                               2387251.258,
                               2387118.495,
                               2387231.258,
                               2387471.258,
                               2387171.258,
                               2387131.258,
                               2387191.258,
                               2387371.258,
                               2387151.258,
                               2387131.258,
                               2387411.258,
                               2387112.44,
                               2387191.258,
                               2387524.317,
                               2387391.258,
                               2387371.258,
                               2387251.258,
                               2387451.258,
                               2387371.258,
                               2387191.258,
                               2387231.258,
                               2387211.258,
                               2387331.258,
                               2387391.258,
                               2387311.258,
                               2387191.258,
                               2387431.258,
                               2387271.258,
                               2387271.258,
                               2387323.356,
                               2387231.258,
                               2387351.258,
                               2387191.258,
                               2387211.258,
                               2387231.258,
                               2387431.258,
                               2387191.258,
                               2387351.258,
                               2387491.258,
                               2387431.258),
                   point_y = c(289191.3991,
                               289054.1412,
                               289391.3991,
                               289351.3027,
                               289091.399,
                               289071.399,
                               289251.399,
                               289291.3993,
                               289271.587,
                               289311.3992,
                               289311.3992,
                               289151.3992,
                               289151.3992,
                               289131.3989,
                               289411.399,
                               289391.3991,
                               289071.399,
                               289331.3992,
                               289611.4751,
                               289053.1858,
                               289211.3991,
                               289511.3991,
                               289511.3991,
                               289171.3992,
                               289131.3989,
                               289371.3991,
                               289131.3989,
                               289529.3496,
                               289451.3993,
                               289271.399,
                               289211.3991,
                               289371.3991,
                               289391.4699,
                               289231.399,
                               289251.399,
                               289411.399,
                               289331.3992,
                               289411.399,
                               289531.3991,
                               289371.3991,
                               289331.3992,
                               289431.399,
                               289251.399,
                               289291.3169,
                               289055.4637,
                               289131.3989,
                               289071.399,
                               289231.399,
                               289601.478,
                               289431.4643,
                               289291.3993,
                               289271.399,
                               289131.3989,
                               289626.1925,
                               289131.3989,
                               289291.3993,
                               289231.399,
                               289251.399,
                               289351.3991,
                               289351.3991,
                               289151.3992,
                               289431.399,
                               289111.399,
                               289091.399,
                               289111.399,
                               289491.3992,
                               289311.3992,
                               289571.3209,
                               289191.3991,
                               289071.399,
                               289582.6287,
                               289411.399,
                               289331.3992,
                               289471.3992,
                               289054.5359,
                               289191.3991,
                               289151.3992,
                               289231.399,
                               289351.4766,
                               289511.3581,
                               289071.399,
                               289171.3992,
                               289331.3992,
                               289391.2898,
                               289131.3989,
                               289411.399,
                               289311.3992,
                               289628.765,
                               289471.3992,
                               289411.399,
                               289431.399,
                               289251.399,
                               289471.3992,
                               289471.3992,
                               289451.3993,
                               289391.3991,
                               289151.3992,
                               289111.399,
                               289271.399,
                               289052.2304,
                               289111.399,
                               289231.399,
                               289311.3992,
                               289191.3991,
                               289371.3991,
                               289571.399,
                               289211.3991,
                               289511.3991,
                               289051.8482,
                               289171.3992,
                               289191.7413,
                               289451.3993,
                               289251.399,
                               289351.3991,
                               289231.399,
                               289331.3992,
                               289071.399,
                               289511.3991,
                               289591.399,
                               289451.3993,
                               289451.4619,
                               289491.3992,
                               289487.6817,
                               289091.399,
                               289111.399,
                               289431.399,
                               289231.399,
                               289211.3991,
                               289091.399,
                               289291.3993,
                               289171.3992,
                               289371.3991,
                               289411.399,
                               289391.3991,
                               289211.3991,
                               289491.3992,
                               289251.399,
                               289111.399,
                               289541.5201,
                               289311.3992,
                               289211.3991,
                               289291.3993,
                               289091.399,
                               289231.399,
                               289431.399,
                               289111.399,
                               289251.399,
                               289491.3992,
                               289071.399),
                   point_z = c(0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0,
                               0))

Here is what I am trying to do:

#Open packages
library(pacman)
p_load(raster, 
       sf, 
       dplyr, 
       ggplot2, 
       scales, 
       magrittr, 
       gstat, 
       gridExtra, 
       raster,
       sp,
       automap,
       mapview,
       leaflet,
       rgdal)

#Read in data, provided above
data <- read.csv('data.csv')

#Change to a spatial object and reproject to WGS84
data <- st_as_sf(data, coords = c('point_x', 'point_y'), crs = crs('+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs'))

The result of the crs(data) command is:

Coordinate Reference System:
Deprecated Proj.4 representation: +proj=longlat +datum=WGS84 +no_defs 
WKT2 2019 representation:
GEOGCRS["unknown",
    DATUM["World Geodetic System 1984",
        ELLIPSOID["WGS 84",6378137,298.257223563,
            LENGTHUNIT["metre",1]],
        ID["EPSG",6326]],
    PRIMEM["Greenwich",0,
        ANGLEUNIT["degree",0.0174532925199433],
        ID["EPSG",8901]],
    CS[ellipsoidal,2],
        AXIS["longitude",east,
            ORDER[1],
            ANGLEUNIT["degree",0.0174532925199433,
                ID["EPSG",9122]]],
        AXIS["latitude",north,
            ORDER[2],
            ANGLEUNIT["degree",0.0174532925199433,
                ID["EPSG",9122]]]] 

The results above indicate that it has been reprojected successfully. However, running the glimpse(data) command shows that the coordinates are still in the original projection system:

Rows: 300
Columns: 2
$ point_z  <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0~
$ geometry <POINT [°]> POINT (2387371 289191.4), POINT (2387411 289054.1), ~

How can I change these points to latitude and longitude points?


Solution

  • To transform the data to lat/lon, first you need to load it with the original crs value.

    data <- st_as_sf(data, coords = c('point_x', 'point_y'), 
              crs = 26777)
    

    You'll get an sf object of the original data, with original coordinates, but now st_transform will know how to transform it to other coordinate systems:

    data_latlong <- st_transform(data, crs = 4326)
    

    Should give you coordinates in latitude/longitude values.