Create regional lat, long dataframe from group of states

和自甴很熟 提交于 2021-01-05 08:59:40

问题


I have been using urbnmapr for all of my mapmaking and it contains nice borders for mapping US states. I now need the outer border of a few states to make a regional map and am looking for a way to filter the dataframe to include only the outer borders.

I will use this outer border to mask a raster image. The region I am working with is

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

Open to any ideas that will give me an object I can use to mask a raster of this region.


回答1:


Here is some example data, first with 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)

As you can see, you do not need the outer borders to mask, but if you want them, you can use

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

Now with 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")



回答2:


Using urbnmapr and sf.

Using st_union() on the sf object of only the states you want will give you the exterior borders.

I think sf objects can be used to mask/crop raster objects.

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)

Created on 2020-12-10 by the reprex package (v0.3.0)



来源:https://stackoverflow.com/questions/65224252/create-regional-lat-long-dataframe-from-group-of-states

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!