R/subset_towns_shapefile_to_CNC.R

## This is an R script to read in the shapefile for towns
## and trim to only those towns in the CNC area for Boston
## and clip the towns ae are half in and half out to include only
## the portion that is in the boston cnc

# packages
library(sf)
library(dplyr)
library(ggplot2)

# read in the towns shapefile
towns <- st_read("data/townssurvey_shp/TOWNSSURVEY_POLY.shp")

# read in sf of cnc_area
cnc_area <- readRDS("data/cnc_area.rds")

# join towns and cnc area
towns_in_cnc_area <- towns %>%
  st_transform(4326) %>%
  st_intersects(cnc_area) %>%
  as.data.frame() %>%
  left_join(., towns %>%
              mutate(row.id=1:nrow(towns)), by="row.id") %>%
  st_as_sf() %>%
  st_transform(4326)

ggplot()+
  geom_sf(data=cnc_area)+
  geom_sf(data=towns_in_cnc_area, fill="orange")+
  theme_classic()

# but this doesn't 'clip' the file to the Boston CNC area
# so will add this step
clipped_towns <- cnc_area %>%
  st_intersection(towns_in_cnc_area) %>%
  mutate(poly_id=1:nrow(.)) %>%
  dplyr::select(poly_id, TOWN, TOWN_ID, ACRES)

ggplot()+
  geom_sf(data=cnc_area)+
  geom_sf(data=clipped_towns, fill="orange")+
  theme_classic()

# then combine the polygons based on the 'town' name
towns_combined <- clipped_towns %>%
  group_by(TOWN) %>%
  summarize()

ggplot()+
  geom_sf(data=cnc_area)+
  geom_sf(data=towns_combined, fill="orange")+
  theme_classic()

# filter for any rows
# with very small area
# which are weird cases I guess
towns_filtered <- clipped_towns %>%
  dplyr::filter(ACRES >500)

ggplot()+
  geom_sf(data=cnc_area)+
  geom_sf(data=towns_filtered, fill="orange")+
  theme_classic()

# then combine the polygons based on the 'town' name
towns_combined2 <- towns_filtered %>%
  group_by(TOWN) %>%
  summarize()

ggplot()+
  geom_sf(data=cnc_area)+
  geom_sf(data=towns_combined2, fill="orange")+
  theme_classic()

# write out the shapefile
st_write(clipped_towns, "data/towns_in_cnc_area/towns_in_cnc_area.shp")
iozeroff/cncpointR documentation built on Feb. 4, 2020, 6:18 p.m.