【问题标题】:adding polygon over map in R, CRS issues在 R 中的地图上添加多边形,CRS 问题
【发布时间】:2021-09-08 20:20:56
【问题描述】:

我正在尝试绘制瑞典地图,然后绘制我在其上创建的假形状。我有一个绘制瑞典自治市的脚本:

library(ggplot2)
library(sf)
library(smoothr)
library(jsonlite)


tmp <- tempfile()
download.file("http://api.thenmap.net/v2/se-7/geo/2020-06-06", destfile = tmp)

mun_name <- fromJSON("http://api.thenmap.net/v2/se-7/data/2020-06-06?language=en&data_props=name|shapeid|is_in") %>% 
  unnest(is_in) %>% 
  rename(county = is_in)

mun <- read_sf(tmp) %>% 
  left_join(mun_name, by = c("id" = "shapeid"))

ggplot(mun) +
  geom_sf(color="grey20",fill="grey95",size=.3) +
  theme_bw()

然后我有一个脚本,我用st_polygon 制作一个多边形:

# make a polygon that is mostly inside sweden
sweden_polygon <-
  # create list of matrices and the first point same as last point
  list(
    matrix(
      c(14, 62, 
        12, 63, 
        14, 64, 
        16, 66, 
        20, 68,  
        20, 66,
        19, 65, 
        18, 63, 
        14, 62),
      ncol=2, byrow=T
    )
  ) 

# Create an sf polygon
sweden_polygon <-  sf::st_polygon(sweden_polygon)
# smooth the polygon
smooth_sweden_polygon <- smooth(sweden_polygon, method = "chaikin")

我可以用看似相同的坐标分别绘制它们,但是当我将它们绘制在一起时,它不起作用,因为多边形没有与瑞典匹配的 CRS。

# this works:
ggplot() +
  geom_sf(data=mun,color="grey20",fill="grey95",size=.3) +
  theme_bw()

# this works:
ggplot() +
  geom_sf(data=smooth_sweden_polygon) +
  theme_bw()

# this don't work:
ggplot() +
  geom_sf(data=mun,color="grey20",fill="grey95",size=.3) +
  geom_sf(data=smooth_sweden_polygon) +
  theme_bw()

我从st_crs(mun) 得知瑞典的坐标系是 WGS 84,但我不知道如何将其分配给我的多边形。

【问题讨论】:

    标签: r gis spatial sf ggmap


    【解决方案1】:

    由于某种原因,我在安装 smoothr 时遇到了问题,所以这个答案与未平滑的多边形有关。

    poly <- st_as_sfc(list(sweden_polygon), crs = 4326)
    

    现在 poly 在同一个 CRS 中。那么,

    ggplot(mun) +
      geom_sf(color="grey20",fill="grey95",size=.3) +
      theme_bw() +
      geom_sf(data = poly) 
    

    给予:

    这就是你所追求的吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-12
      • 2015-06-11
      • 1970-01-01
      • 2012-04-18
      • 1970-01-01
      • 2017-08-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多