Skip to content

Instantly share code, notes, and snippets.

@jshannon75
Created November 2, 2022 14:01
Show Gist options
  • Save jshannon75/dde53ada97bbd85b2df60a1e76241888 to your computer and use it in GitHub Desktop.
Save jshannon75/dde53ada97bbd85b2df60a1e76241888 to your computer and use it in GitHub Desktop.
library(tidyverse)
library(tmap)
library(sf)
ctytax<-read_csv("countyinflow1920.csv") %>%
mutate(ctyfips1=paste(y1_statefips,y1_countyfips,sep=""),
ctyfips2=paste(y2_statefips,y2_countyfips,sep=""))
counties<-tigris::counties()
county_point<-st_centroid(counties)
county_point1<-county_point %>%
bind_cols(data.frame(st_coordinates(county_point))) %>%
select(GEOID,X,Y) %>%
st_set_geometry(NULL)
tm_shape(county_point1)+tm_dots()
ctytax1<-ctytax %>%
left_join(county_point1,by=c("ctyfips1"="GEOID")) %>%
rename(X1=X,Y1=Y) %>%
left_join(county_point1,by=c("ctyfips2"="GEOID")) %>%
rename(X2=X,Y2=Y) %>%
filter(is.na(X1)==FALSE & is.na(X2)==FALSE) %>%
mutate(ID=row_number())
rows <- split(ctytax1, seq(nrow(ctytax1)))
cty_lines<-lapply(rows, function(row) {
lmat <- matrix(unlist(row[12:15]), ncol = 2, byrow = TRUE)
st_linestring(lmat)
})
cty_lines1 <- st_sfc(cty_lines)
cty_lines_sf <- st_sf('ID' = ctytax1$ID, 'geometry' = cty_lines1)
cty_lines_all<-cty_lines_sf %>%
left_join(ctytax1)
st_write(cty_lines_all,"taxlines.gpkg")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment