使用 R 在县上创建均匀间隔的折线

jsi*_*sno 6 r geospatial r-sf rgeo-shapefile

我想创建从北到南的均匀间隔多段线,每条线之间的间距为 50 英里,长度为 10 英里。不确定这是否可以使用 sf 包。在下面的示例中,我希望线条填充整个华盛顿州的县。


library(tigris)
library(leaflet)

states <- states(cb = TRUE)

counties<-counties(cb=TRUE)

counties<- counties%>%filter(STATEFP==53)

states<- states%>%filter(NAME=="Washington")

leaflet(states) %>%
  addProviderTiles("CartoDB.Positron") %>%
  addPolygons(fillColor = "white",
              color = "black",
              weight = 0.5) %>%
  addPolygons(data=counties,color='red',fillColor = 'white')%>%
  setView(-120.5, 47.3, zoom=8)
Run Code Online (Sandbox Code Playgroud)

我已更新以包含我想在下面执行的操作的图像。在此处输入图片说明

Ric*_*loo 3

您可以multilinestring通过指定坐标从头开始创建 sf 对象。

您可以从extent华盛顿的(边界框)获取这些坐标,但您可能也有兴趣了解如何创建网格,我将在下面演示,因为它可能会有所帮助。

复制并粘贴此可重现的示例:

library(tidyverse)
library(tigris)
library(leaflet)
library(sf)
library(raster)

states <- states(cb = TRUE)

# subset for WA and transform to a meter-based CRS 
states <- states %>% 
  filter(NAME == "Washington") %>% 
  st_transform(crs = 3857) # Mercator

# fifty miles in meters
fm <- 80467.2

# subset for Washington
states_sp <- as(states, "Spatial")

# create a grid, convert it to polygons to plot
grid <- raster(extent(states_sp), 
               resolution = c(fm, fm), 
               crs = proj4string(states_sp))
grid <- rasterToPolygons(grid)
plot(states_sp)
plot(grid, add = TRUE)

# find the top y coordinate and calculate 50 mile intervals moving south
ty <- extent(grid)[4]  # y coordinate along northern WA edge
ty <- ty - (fm * 0:7)  # y coordinates moving south at 10 mile intervals

# create a list of sf linestring objects
l <- vector("list", length(ty))
for(i in seq_along(l)){
  l[[i]]  <- 
    st_linestring(
      rbind(
        c(extent(grid)[1], ty[i]),
        c(extent(grid)[2], ty[i])
      )
    )
}

# create the multilinestring, which expects a list of linestrings
ml <- st_multilinestring(l) 

plot(states_sp)
plot(as(ml, "Spatial"), add = TRUE, col = "red")
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,我使用函数和在sf和对象之间来回切换。使用它们将数据转换为您的需求。spas(sf_object, "Spatial")st_as_sf(sp_object)

在此输入图像描述