rgisrasterr-sfncdf4

How to change map projection from pacific to Atlantic centered?


library(sf)
library(tidyverse)

I have an sf object with

geometry type:point

projected over pacific

bbox: xmin: 0 ymin: -78 xmax: 359 ymax: 0 (WGS 84)

I want to re-project the sf object to Atlantic centered (-180,180) from pacific view (0,360). I found in sf package a function that allows to go from Atlantic to Pacific view (i.e.) st_shift_longitude(x). But what I want is the opposite ...

Help? Thanks


Solution

  • Tough to tell for sure without some hint at what the data looks like, but here's one way to do it:

    I'm assuming the original sf object of points doesn't have a crs set, since 0-360 for longitude is unusual. The code below makes up some data points using -78-0 for latitude & 0-360 for longitude. A (somewhat unusual) crs of "+proj=longlat +ellps=WGS84 +pm=-360 +datum=WGS84 +no_defs" is set, and then transformed to the usual 4326 for lon/lat data.

    library(sf)
    #> Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1; sf_use_s2() is TRUE
    library(tidyverse)
    
    set.seed(42)  # for reporducibility
    points <- tibble(x = sample(0:359, size = 20, replace = T),
                     y = sample(-78:0, size = 20, replace = T)) %>%
      st_as_sf(coords = c("x", "y"), remove = F)
    
    # Set a crs that understands 0-360 longitude
    points <- st_set_crs(points, "+proj=longlat +ellps=WGS84 +pm=-360 +datum=WGS84 +no_defs")
    
    # reproject to epsg 4326, the usual lon/lat crs
    points_4326 <- points %>% st_transform(4326)
    
    mapview::mapview(points_4326)
    

    Head of the sample data showing both old (0-360) longitude, and new (-180 to 180) longitude:

    > head(points_4326)
    Simple feature collection with 6 features and 2 fields
    Geometry type: POINT
    Dimension:     XY
    Bounding box:  xmin: -166 ymin: -66 xmax: 88 ymax: -5
    Geodetic CRS:  WGS 84
    # A tibble: 6 × 3
          x     y    geometry
      <int> <int> <POINT [°]>
    1   240   -66  (-120 -66)
    2    23   -66    (23 -66)
    3    46    -5     (46 -5)
    4    88   -10    (88 -10)
    5    17   -61    (17 -61)
    6   194   -13  (-166 -13)
    

    Created on 2022-04-19 by the reprex package (v2.0.1)