从一组州创建区域纬度、经度数据框

Create regional lat, long dataframe from group of states

我一直在使用 urbnmapr 制作所有地图,它包含用于绘制美国各州地图的漂亮边界。我现在需要一些州的外边界来制作区域地图,并且正在寻找一种方法来过滤数据框以仅包含外边界。

我将使用此外边框来遮盖光栅图像。我工作的地区是

filter(urbnmapr::states, state_name %in% c("South Dakota", "Nebraska", "Iowa", "Minnesota",
                                              "Missouri", "Michigan", "Indiana", "Illinois",
                                              "Wisconsin", "Kansas", "Ohio", "North Dakota"))

接受任何能给我一个对象的想法,我可以用它来掩盖这个区域的光栅。

使用 urbnmaprsf

仅在您想要的状态的 sf 对象上使用 st_union() 将为您提供外部边框。

我认为 sf 个对象可以用于 mask/crop 个栅格对象。

library(urbnmapr)
library(tidyverse)
library(sf)
#> Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1

states_all <- get_urbn_map(map = 'states', sf = TRUE)
my_states_vec <-  c("South Dakota", "Nebraska", "Iowa", "Minnesota",
                                           "Missouri", "Michigan", "Indiana", "Illinois",
                                           "Wisconsin", "Kansas", "Ohio", "North Dakota")
my_states <- states_all %>% 
  filter(state_name %in% my_states_vec) %>%
  st_union()

head(my_states)
#> Geometry set for 1 feature 
#> geometry type:  MULTIPOLYGON
#> dimension:      XY
#> bbox:           xmin: -340177.8 ymin: -950695.4 xmax: 1627432 ymax: 498098.3
#> projected CRS:  US National Atlas Equal Area
#> MULTIPOLYGON (((1422979 -227154.2, 1423979 -225...

ggplot(my_states) + 
  geom_sf(fill = NA)

reprex package (v0.3.0)

于 2020 年 12 月 10 日创建

这是一些示例数据,首先是 raster/sp

library(raster)
us <- getData("GADM", level=1, country="USA")
us <- us[!(us$NAME_1 %in% c("Alaska", "Hawaii")), ]
r <- raster(us, res=1)
values(r) <- 1:ncell(r)
states <- c("South Dakota", "Nebraska", "Iowa", "Minnesota", "Missouri", "Michigan", "Indiana", "Illinois", "Wisconsin", "Kansas", "Ohio", "North Dakota")

s <- us[us$NAME_1 %in% states, ]
m <- mask(r, s)
plot(m)
lines(us)
lines(s, col="red", lwd=2)

如您所见,您不需要遮盖外边框,但如果需要,可以使用

d <- aggregate(s)
lines(d, col="blue", lwd=3) 

现在有了 urbanmapr / sf

#remotes::install_github("UrbanInstitute/urbnmapr")
library(urbnmapr)
library(sf) 
us2 <- get_urbn_map(map="states", sf = TRUE)
us2 <- sf::st_transform(us2, crs(r))
s2 <- subset(us2, state_name %in% states)
r <- raster(us, res=1)
values(r) <- 1:ncell(r)

m2 <- mask(r, s2)
plot(m2)
lines(as(us2, "Spatial"))
lines(as(s2, "Spatial"), col="red")