-
Notifications
You must be signed in to change notification settings - Fork 0
/
preparing_isochrones.Rmd
161 lines (123 loc) · 4.83 KB
/
preparing_isochrones.Rmd
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
---
title: "Untitled"
description: |
A new article created using the Distill format.
author:
- name: Nora Jones
url: https://example.com/norajones
affiliation: Spacely Sprockets
affiliation_url: https://example.com/spacelysprokets
date: "`r Sys.Date()`"
output: distill::distill_article
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
```{r}
library(sf)
library(openrouteservice)
library(tidyverse)
library(tmap)
```
```{r}
origins <- st_read("data/network_redesign_travel_time_map_locations.geojson")
tmap_mode("view")
tm_shape(origins) +
tm_dots()
create_iso <- function(location, profile_1 = "e-bike", range_1){
print(range_1)
iso <- ors_isochrones(locations = st_coordinates(location),
profile = ors_profile(mode = profile_1),
range = range_1,
output = "sf")
iso$geometry
}
as.data.frame(origins_2[1:2,]) %>%
mutate(iso = ors_isochrones(locations = st_coordinates(geometry),
#profile = ors_profile(mode = bike_type),
range = time,
output = "sf"))
origins_2[1,] %>%
mutate(iso = create_iso(geometry, bike_type, time))
# maybe mutate is not the right thing for this. Could try map somehow? But mutate should work!!!
# so the error has to
# let's try with map and see where we get
map_dfr(1:nrow(origins_2), ~create_iso(origins_2[.,]))
library(sf)
library(dplyr)
library(openrouteservice)
dd <- data.frame(x = c(-89.33044, -89.33044, -89.33044, -89.33044, -89.55, -89.55, -89.55, -89.55),
y = c(43.12018, 43.12018, 43.12018, 43.12018, 43.12019, 43.12019, 43.12019, 43.12019),
bike_type = c("bike", "bike", "e-bike", "e-bike", "bike", "bike", "e-bike", "e-bike"),
time = c(300, 600, 300, 600, 300, 600, 300, 600))
df <- st_as_sf(dd, coords = c("x","y"))
df
create_iso <- function(location, profile_1 = "e-bike", range_1){
print(range_1)
iso <- ors_isochrones(locations = st_coordinates(location),
profile = ors_profile(mode = profile_1),
range = range_1,
output = "sf")
iso$geometry
}
x <- apply(df, MARGIN = 1, function(r){
r$iso <- create_iso(r$geometry, r$bike_type, r$time)
})
x <- df[4:7,] %>%
mutate(iso = create_iso(geometry, bike_type, time))
tm_shape(x[2,]$iso) +
tm_polygons()
dd <- data.frame(x = c(-89.33044, -89.33044, -89.33044, -89.33044, -89.33045, -89.33045, -89.33045, -89.33045),
y = c(43.12018, 43.12018, 43.12018, 43.12018, 43.12019, 43.12019, 43.12019, 43.12019),
bike_type = c("bike", "bike", "e-bike", "e-bike", "bike", "bike", "e-bike", "e-bike"),
time = c(300, 600, 300, 600, 300, 600, 300, 600))
df <- st_as_sf(dd, coords = c("x","y"))
df
sf <- sf::st_as_sf(dd, coords = c("x","y"))
sf
```
What I want my output dataframe to look like:
```{r}
origins_2 <- origins %>%
mutate(mode_ebike = "e-bike", mode_bike = "bike", time_15 = 900, time_30 = 1800, time_45 = 2700) %>%
pivot_longer(starts_with("mode"), names_to = "mode_var", values_to = "bike_type") %>% pivot_longer(starts_with("time_"), names_to = "time_var", values_to = "time") %>%
select(-c(mode_var, time_var, id))
```
```{r}
bike_isochrones <- origins_2 %>%
rowwise() %>%
mutate(iso = create_iso(geometry, bike_type, time))
bike_isochrones2 <- bike_isochrones %>%
mutate(name = case_when(name == "Atwood Dunning" ~ "Atwood at Dunning",
T ~ name))
saveRDS(bike_isochrones2, file = "how-far-can-you-go/data/bike_isochrones.RDS")
tmap_mode("view")
tmap_options(check.and.fix = TRUE)
bike_isochrones %>%
mutate(time = as.factor(time)) %>%
filter(bike_type == "bike") %>%
st_set_geometry(., "iso") %>%
tm_shape() +
tm_polygons(col = "time", alpha = .2) +
tm_facets(by = "name", sync = T)
```
```{r download-maps}
library(magick)
download.file("https://madison.legistar.com/View.ashx?M=F&ID=10441390&GUID=ECA1CC37-55E7-48B3-9B26-37D4A6ADF11C", destfile = "data/travel_time_maps.pdf", mode = "wb")
download.file("https://www.cityofmadison.com/metro/documents/network-redesign/AdditionalIsochrones.pdf",
destfile = "data/additional_travel_time_maps.pdf")
```
```{r}
magick::image_read_pdf("data/travel_time_maps.pdf")
read_page <- function(page, pdf_name){
p <- image_read_pdf(pdf_name, pages = page, density = 600)
location <- p %>% image_crop(geometry = "2400x200+60+385") %>%
image_ocr()
path_name = paste0(str_sub(location, end = -2), ".png")
image_write(p, path = path_name)
}
read_page(2)
library(pdftools)
map(2:pdf_info("data/travel_time_maps.pdf")$pages, read_page)
map(1:pdf_info("data/AdditionalIsochrones.pdf")$pages, ~ read_page(page = ., pdf_name = "data/AdditionalIsochrones.pdf"))
```