Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create evenly spaced polylines over counties using R

I would like to create evenly spaced polylines going North to South with 50 mile spacing between each line and 10 miles long. Not sure if this is possible using sf package. In the example below, I would like to have the lines filling the counties across the state of Washington.


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)

I've updated to include an image of what I'd like to do below.enter image description here

like image 512
jsimpsno Avatar asked Oct 23 '20 23:10

jsimpsno


1 Answers

You can create a multilinestring sf object from scratch by specifying coordinates.

You can get these coordinates from the extent (bounding box) of Washington, but you may also be interested in knowing how to create a grid, which I will demonstrate below because it may be helpful.

Copy and paste this reproducible example:

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

As you can see, I switch back and forth between sf and sp objects using the functions as(sf_object, "Spatial") and st_as_sf(sp_object). Use these to transform the data to your needs.

enter image description here

like image 94
Rich Pauloo Avatar answered Oct 30 '22 15:10

Rich Pauloo