【问题标题】:Set coordinate reference system to a polygon created in R将坐标参考系设置为在 R 中创建的多边形
【发布时间】:2019-05-08 08:00:09
【问题描述】:

我正在尝试在使用 ggplot2::geom_sf() 绘制的地图中创建一个放大的正方形(多边形) 因此,我需要多边形是一个简单的特征,这样我就可以将正方形和地图绘制在一起。 我可以使用以下代码创建多边形。

zoom_square_1<- rbind(c(-10.821079,-68.403542),
                  c(-10.821079,-68.367060),
                  c(-10.862918,-68.367060),
                  c(-10.862918,-68.403542),
                  c(-10.821079,-68.403542)) # add the first point again to it wrap up.
zoom_square_1_pol <- st_polygon(list(zoom_square_1))
plot(zoom_square_1_pol) # it returns the polygon.

但是,当我尝试为其分配坐标参考系 (CRS) 时,它不起作用。这些是我到目前为止尝试过的代码。

sp::proj4string(zoom_square_1_pol) <- CRS("+proj=longlat +datum=WGS84 +no_defs")
zoom_square_1_pol_ex1 <- st_as_sf(x = zoom_square_1_pol[[1]],
                              crs = "+proj=longlat +datum=WGS84 +no_defs")
zoom_square_1_pol_ex2 <- st_transform(x = zoom_square_1_pol,
                                  crs = "+proj=longlat +datum=WGS84 +no_defs")

欢迎任何帮助。

在 Chris 发表评论之后,我实际上可以使用 ggplot2::geom_sf() 绘制多边形,并意识到我的纬度和经度坐标自乞讨以来处于错误的位置。所以,这是代码的最终版本:

 zoom_square_1<- rbind(c(-68.403542,-10.821079),
                       c(-68.367060,-10.821079),
                       c(-68.367060,-10.862918),
                       c(-68.403542,-10.862918),
                       c(-68.403542,-10.821079)) #add the first point again to it wrap up.
zoom_square_1_pol <- st_polygon(list(zoom_square_1))
zoom_square_1_pol_CRS<- st_sfc(x = zoom_square_1_pol,
                               crs = "+proj=longlat +datum=WGS84 +no_defs")

【问题讨论】:

  • 你想要st_sfc()
  • 谢谢,这实际上工作得很好。我也刚刚意识到我的纬度坐标混淆了。

标签: r ggplot2 sf


【解决方案1】:

我可以通过将正方形作为列表提供给 st_as_sfc 来设置 crs:

res <- st_as_sfc(list(zoom_square_1_pol), 
                 crs = "+proj=longlat +datum=WGS84 +no_defs")

【讨论】:

  • st_sfc() 会消除您作为列表传递的需要吗?
猜你喜欢
  • 2011-12-10
  • 2021-02-19
  • 2014-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-24
  • 2015-08-22
相关资源
最近更新 更多