-
Notifications
You must be signed in to change notification settings - Fork 0
/
process-scooter-od-pairs.R
151 lines (123 loc) · 4.96 KB
/
process-scooter-od-pairs.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
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
#' 10/12/19
#' @author David Pedrick
#' @description This script takes an Origin-Destination pair and returns a likely
#' route to travel in between the Origin-Destination.
#'
#' To calculate the route, I use the HERE api. It returns a json
#' object of the route as well as meta data that says time, distance, etc.
#'
#'
#'
library(tidyverse)
library(stringr)
library(sf)
library(raster)
library(sp)
library(mapview)
library(tmap)
library(tidycensus)
library(tigris)
library(lehdr)
library(httr)
library(jsonlite)
library(viridis)
library(ggpubr)
library(rgdal)
library(xml2)
library(curl)
library(maptools)
library(GISTools)
get_distance <- function(origin, destination, mode = 'bicycle', app_id = '', app_code = ''){
#' distance
url = paste0('https://route.api.here.com/routing/7.2/calculateroute.json?',
'app_id=', app_id,
'&app_code=', app_code,
'&waypoint0=geo!', origin,
'&waypoint1=geo!', destination,
'&mode=fastest;', mode,
';traffic:disabled',
'&routeattributes=sh')
text_response <- url %>%
GET() %>%
content(as = "text") %>%
fromJSON()
return(text_response)
}
## Read in scooter o-d pairs.
## Use Googles crs for this
#scooter_df <- read_csv("C:/Users/dpedrick/OneDrive/ScooterData/sample_od.csv")
scooter_df <- read_csv("C:/Users/dpedrick/OneDrive/ScooterData/O-D Data/missing-days - 27.csv")
#' iterate over the rows of the dataframe
#' assemble lat,lon pairs to use as waypoints in the route query
#' scooter_df == time, id, company, o-lat, o-lon, departure, arrival, d-lat, d-lon
#'
#'
#' Final spdf === scooter_df, distance, duration, speed, geometry
scooter_length <- nrow(scooter_df)
scooter_df <- tibble::rowid_to_column(scooter_df, "ID")
#' initialize the spdf
origin <- paste0(scooter_df[1,]$'o-lat',',',scooter_df[1,]$'o-lon')
destination <- paste0(scooter_df[1,]$'d-lat',',', scooter_df[1,]$'d-lon')
id2 <- scooter_df[1,]$ID
shape <- get_distance(origin, destination, 'bicycle', app_id = 'Mtp0R3CkrTuuXGy0HvrX', app_code = 'MmRLt66MX33Jl1c6RF3F0Q')
coords2 <- shape$response$route$shape
data_df <- data.frame(NA, NA, NA, NA)
names(data_df) <- c('id', 'lat', 'lon', 'num')
length <-length(coords2[[1]])
for(i in seq(1,length,by=1)){
str <- strsplit(coords2[[1]][i],",")
lat <- as.numeric(str[[1]][1])
lon <- as.numeric(str[[1]][2])
data_df2 <- data.frame(id2, lat, lon, i)
names(data_df2) <- c('id', 'lat', 'lon', 'num')
data_df <- rbind(data_df, data_df2)
}
data_df <- na.omit(data_df)
coordinates(data_df) <- c("lon","lat")
lines <- SpatialLines(list(Lines(list(Line(data_df)), "id")))
lines <- st_as_sf(lines)
lines$id <- id2
for(i in seq(2,scooter_length,by=1)){
print(i)
origin <- paste0(scooter_df[i,]$'o-lat',',',scooter_df[i,]$'o-lon')
destination <- paste0(scooter_df[i,]$'d-lat',',', scooter_df[i,]$'d-lon')
id2 <- scooter_df[i,]$ID
shape <- get_distance(origin, destination, 'bicycle', app_id = 'Mtp0R3CkrTuuXGy0HvrX', app_code = 'MmRLt66MX33Jl1c6RF3F0Q')
#' I need the shapefile, time, distance, route
#' convert the scooter_df to a spdf and then add the results to that.
coords2 <- shape$response$route$shape
data_df <- data.frame(NA, NA, NA)
names(data_df) <- c('lat', 'lon', 'num')
length <-length(coords2[[1]])
if(length>1){
for(i in seq(1,length,by=1)){
str <- strsplit(coords2[[1]][i],",")
lat <- as.numeric(str[[1]][1])
lon <- as.numeric(str[[1]][2])
data_df_d <- data.frame(lat, lon, i)
names(data_df_d) <- c('lat', 'lon', 'num')
data_df <- rbind(data_df, data_df_d)
}
data_df <- na.omit(data_df)
coordinates(data_df) <- c("lon","lat")
lines2 <- SpatialLines(list(Lines(list(Line(data_df)),"id")))
lines2 <- st_as_sf(lines2)
lines2$id <- id2
#View(lines)
lines <- rbind(lines, lines2)
}
}
scooterRoutes <- merge(lines, scooter_df, by.x = "id", by.y = "ID")
#' write to file to do spatial join in QGIS
st_write(scooterRoutes, "C:/Users/dpedrick/Desktop/scooter-rides-27.shp")
###########################################################################################
###########################################################################################
###########################################################################################
###########################################################################################
###########################################################################################
###########################################################################################
###########################################################################################
###########################################################################################
###########################################################################################
###########################################################################################
##########################################