-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbus-stops-example.R
55 lines (46 loc) · 1.29 KB
/
bus-stops-example.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
library(sf)
library(geos)
library(dplyr)
library(arcgis)
library(ggplot2)
# install.packages("arcgis", repos = c("https://r-arcgis.r-universe.dev", "https://cloud.r-project.org"))
# get the stop locations
stops_url <- "https://arcgis.metc.state.mn.us/server/rest/services/EnterpriseLibrary/TransitStops/FeatureServer/0"
# read the data into memory
stops <- arc_read(stops_url)
# create a vector of addresses to geocode
locations <- c(
"677 Transfer Road St Paul, MN 55114",
"560 Sixth Avenue Nortt Minneapolis, MN 55411"
)
# geocode the addresses
geocoded <- find_address_candidates(
locations,
crs = sf::st_crs(stops),
max_locations = 1
)
# create a spatial index to query against
tree <- geos_basic_strtree(stops)
# buffer our query geometry by 2 miles (10 miles was wayyy too big)
buffer <- sf::st_buffer(
geocoded$geometry,
units::set_units(2, "miles")
)
# find all of the locations that are within 2 miles
locations_within_10_miles <- geos_basic_strtree_query_filtered(
tree,
buffer,
stops$geometry,
geos_intersects
)
# create a simple visualization
locations_within_10_miles |>
mutate(
geometry = stops$geometry[tree],
address = locations[x]
) |>
st_as_sf() |>
ggplot(aes(color = address)) +
geom_sf(alpha = 0.2) +
theme_minimal() +
theme(legend.position = "bottom")