Skip to content

Commit b975def

Browse files
authored
Merge pull request #9 from sarahhurley/main
add neighboring towns script
2 parents e09a7bb + 5e84851 commit b975def

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# This script will create lists of neighboring towns in Connecticut.
2+
# Author: Sarah Hurley
3+
4+
# Packages
5+
library(tidyverse)
6+
library(sf)
7+
library(arcgis)
8+
9+
# Install packages
10+
# install.packages("tidyverse")
11+
# install.packages("sf")
12+
# remotes::install_github("r-arcgis/arcgis", dependencies = TRUE)
13+
14+
# ============================================================================
15+
16+
# Read in CT Municipalities layer (ArcGIS REST URL)
17+
town_url <- "https://services1.arcgis.com/FCaUeJ5SOVtImake/arcgis/rest/services/CT_Municipalities/FeatureServer/0"
18+
towns <- arc_open(town_url) %>% arc_select()
19+
20+
plot(towns["Municipality"])
21+
22+
23+
# Create a tiny buffer around each towns geometry so we can capture the neighbors
24+
towns2 <- st_buffer(towns, dist = 0.001)
25+
26+
# Get a list of lists of the overlapping neighbors
27+
neighbors <- st_intersects(towns2,
28+
towns2)
29+
30+
31+
# A function returning data frame of neighbors of a given town
32+
nbr_pairs <- function(idx) {
33+
data.frame(Town = rep(towns$Municipality[idx], length(neighbors[[idx]])),
34+
Neighbor_town = towns$Municipality[neighbors[[idx]]])
35+
}
36+
37+
38+
# Apply the function to list of indices (the neighbors list)
39+
pairs_of_names <- purrr::map_dfr(seq_along(neighbors),
40+
nbr_pairs)
41+
42+
43+
grouped_list <- pairs_of_names %>%
44+
mutate(same = if_else(Town == Neighbor_town, TRUE, FALSE)) %>%
45+
filter(same == FALSE) %>%
46+
select(-same) %>%
47+
group_by(Town) %>%
48+
mutate(Neighbor_index = row_number()) %>%
49+
ungroup()
50+
51+
52+
53+
## Create 3 new data sets from the grouped list
54+
55+
# 1. Number of neighboring towns for each town
56+
summary_towns <- grouped_list %>%
57+
group_by(Town) %>%
58+
summarise(Count = n())
59+
60+
# 2. Wide data set: columns are # of neighboring towns
61+
wide_list <- grouped_list %>%
62+
pivot_wider(names_from = Neighbor_index,
63+
values_from = Neighbor_town,
64+
names_prefix = "Neighbor_") %>%
65+
left_join(summary_towns, by = "Town") %>%
66+
select(Town, Count, everything())
67+
68+
# 3. Long data set: two columns, one for town and another for a neighboring town
69+
long_list <- grouped_list %>%
70+
select(Town, Neighbor_town)
71+
72+
73+
74+
75+
# write_csv(wide_list, "CT_Adjacent_Towns_WideFormat.csv")
76+
# write_csv(long_list, "CT_Adjacent_Towns_LongList.csv")
77+
78+
79+
80+
81+
82+
### Resources ###
83+
# 1. Connecticut Neighboring Towns data set
84+
## https://data.ct.gov/Local-Government/Connecticut-Neighboring-Towns/3u6x-c464/about_data
85+
86+
# 2. Wide vs. Long data
87+
## "Getting wide and long with data" by Stephen Skalicky: https://tinyurl.com/musuvthk
88+
89+
# 3. R-ArcGIS Packages
90+
## https://github.com/R-ArcGIS/arcgislayers
91+
92+
93+

0 commit comments

Comments
 (0)