将 `st_bbox()` 的结果转换为其他 CRS

Transform result of `st_bbox()` to other CRS

是否有一种简单的方法可以将简单特征(sf 对象)的边界转换为另一个 CRS?

函数st_bbox()的结果是classbbox。无法使用 st_transform() 将其转换为另一个 CRS。

我正在使用基于 EPSG: 28992 的计算边界框:

sf::st_bbox(xmin = 187470, xmax =194587, 
            ymin = 409753, ymax = 412715,  
            crs = st_crs(28992))

现在我想把这个盒子变成EPSG:4326

一种方法是制作一个中间体 sfc_MULTIPOINT,将其转换为 4326,然后从该对象中提取边界框:

pts <- st_multipoint(rbind(c(187470, 409753), c(194587, 412715))) %>% st_sfc(crs = st_crs(28992))
pts <- pts %>% st_transform(crs = 4326)

st_bbox(pts)

     xmin      ymin      xmax      ymax 
 5.856639 51.675634  5.959866 51.701799 

bbox对象有一个st_as_sfc方法,所以我们可以这样转换bbox

library(sf)

bb = sf::st_bbox(c(xmin = 187470, xmax =194587, 
                   ymin = 409753, ymax = 412715),  
                 crs = st_crs(28992))

bb_ll = st_bbox(
  st_transform(
    st_as_sfc(bb), 
    4326
  )
)

# or pipey
library(magrittr)

bb_ll = bb %>%
  st_as_sfc() %>%
  st_transform(crs = 4326) %>%
  st_bbox()

bb_ll

    xmin      ymin      xmax      ymax 
5.856639 51.675176  5.959866 51.702257

如@spacedman 所述,一个坐标系中的矩形可能不是另一个坐标系中的矩形。有些坐标系比其他坐标系弯曲得多!

为了更安全地变换边界框,您可以使用 st_make_grid(n = whatever) 添加顶点。

# pipey code to show the steps clearly
library(sf)
library(dplyr)
bb_better_reproj = bb_orig %>% 
                      st_make_grid(n=10) %>%  #this also makes it into a polygon
                      st_transform(crs = 4326) %>% 
                      st_bbox()

这不是 100% 安全,但比仅变换矩形多边形(仅变换角顶点,中间不变换)要好。网格顶点太多真的很慢。我发现 ~10-20 在大多数情况下效果很好。

示例(使用与 OP 不同的坐标参考系统 (CRS) 进行说明):

library(sf)
library(dplyr)

bb_orig = st_bbox(c(xmin = 40, xmax =-40, 
                    ymin = 45, ymax = 55), 
                    crs = st_crs(4326)) #lag/long

new_crs = 3995 # arctic polar stereographic

bb_simple_reproj = bb_orig %>% 
                   st_as_sfc() %>% 
                   st_transform(crs=new_crs) %>% 
                   st_bbox

bb_better_reproj = bb_orig %>% 
                   st_make_grid(n=10) %>%  
                   st_transform(crs = new_crs) %>% 
                   st_bbox()

# 100 random points inside the original bounding box (grey)
  sample_from_bb_orig = st_sample(st_as_sfc(bb_orig), 1000) %>%
  st_transform(new_crs)

  plot(st_geometry(sample_from_bb_orig), col="grey")

# if the bbox was only transformed as simple rectangle we would get (red):
  # red points indicate those missed by the simple bbox reprojection -- note that many are missed!
  plot(st_geometry(st_as_sfc(bb_simple_reproj)), border="red", add=TRUE)
  plot(st_difference(sample_from_bb_orig, st_as_sfc(bb_simple_reproj)) , col="red", add=TRUE)

# using 'better' repojection, with more vertices added (blue)
  # blue points indicate those missed by the simple bbox reprojection
  plot(st_geometry(st_as_sfc(bb_better_reproj)), border="blue", add=TRUE)
  plot(st_difference(sample_from_bb_orig, st_as_sfc(bb_better_reproj)) , col="blue", pch="x", add=TRUE)