【问题标题】:st_crop function not working correctly - produces map with horizontal lines at north polest_crop 功能无法正常工作 - 在北极生成带有水平线的地图
【发布时间】:2021-10-18 17:43:57
【问题描述】:

下面的 R 代码可以在早期版本的 R 和 sf 上运行,但不能在我最近下载 R、RStudio 和必要软件包的另一台笔记本电脑上运行。对世界地图(sf 对象)使用“st_crop”函数会导致地图中只有水平线朝向北极。我只能通过首先将对象转换为“SpatialPolygonsDataFrame”并使用 sp 包中的“crop”函数对其进行裁剪来裁剪它。是否有错误,或者我在这里做错了什么?

版本

R 4.1.0 科幻小说 1.0.3 rnaturalearth 0.1.0 dplyr 1.0.7

这是可重现的代码

library(sp)
library(sf)
library(dplyr)
library(rnaturalearth)
library(raster)
library(ggplot2)

# World map (sf object)
world_sf <- ne_countries(scale = 10, type = "countries", returnclass = "sf") %>%
  filter(!(admin == "Antarctica"))

#This code does NOT work
ext_world1 <- c(xmin = -165,  xmax = 175, ymin= -59.47275, ymax = 83.6341)
world_sf1 <- st_crop(world_sf, ext_world1)
ggplot() + geom_sf(data = world_sf1, color="gray20",  fill = "gray85", lwd = 0.3)

# This code produces the expected result
ext_world2 <- as(raster::extent(-165, 175, -59.47275, 83.6341), "SpatialPolygons")
world_sp <- crop(as_Spatial(world_sf), ext_world2)
world_sf2 <- st_as_sf(world_sp) 
ggplot() + geom_sf(data = world_sf2, color="gray20",  fill = "gray85", lwd = 0.3)

【问题讨论】:

    标签: r crop sf sp


    【解决方案1】:

    st_crop() 工作正常,但您需要确保使用正确的坐标参考系。试试这个...

    world_sf_4326 <- ne_countries(type  = "countries", returnclass = "sf") %>%
      filter(!(admin == "Antarctica")) %>%
      st_transform(crs = 4326)
    
    st_crs(world_sf_4326)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-17
      • 2014-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-06
      相关资源
      最近更新 更多