【问题标题】:Transform result of `st_bbox()` to other CRS将 `st_bbox()` 的结果转换为其他 CRS
【发布时间】:2019-07-29 17:54:56
【问题描述】:

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

函数st_bbox() 的结果属于bbox 类。无法使用st_transform() 将其转换为另一个 CRS。

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

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

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

【问题讨论】:

  • 请注意,一个坐标系中的矩形可能不是另一个坐标系中的矩形。如果您真的想投影盒子的完整形状而不仅仅是四个角,那么您需要创建一个边上有更多顶点的矩形多边形,然后投影 that

标签: r transform geospatial sf


【解决方案1】:

一种方法是制作一个中间 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 

【讨论】:

    【解决方案2】:

    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
    

    【讨论】:

      【解决方案3】:

      正如@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)
      
      

      【讨论】:

        猜你喜欢
        • 2018-08-29
        • 2018-11-07
        • 1970-01-01
        • 1970-01-01
        • 2013-08-23
        • 1970-01-01
        • 1970-01-01
        • 2021-04-20
        • 1970-01-01
        相关资源
        最近更新 更多