rm(list = ls())
setwd("D:/R")
library(rjson);library(jsonlite);library(dplyr)

GetStop <- fromJSON("D:/地理系大二/空間分析/期末報告/data/GetStop")
GetStop.attr = data.frame(GetStop)

GetRoute <- fromJSON("D:/R/GetRoute")
GetRoute.attr = data.frame(GetRoute)
getstop <- GetStop.attr[,c(-1,-2,-3,-4)]
getroute <- GetRoute.attr[,c(-1,-2,-3,-4,-10,-14,-15,-16,-17,-18,-20,-38,-39,-40)]

getroute$BusInfo.goLastBusTime_hour <-substr(getroute$BusInfo.goLastBusTime , 1, 2)
getroute$BusInfo.goLastBusTime_min <-substr(getroute$BusInfo.goLastBusTime , 3, 4)

getroute$BusInfo.goFirstBusTime_hour <- substr(getroute$BusInfo.goFirstBusTime , 1, 2)
getroute$BusInfo.goFirstBusTime_min <- substr(getroute$BusInfo.goFirstBusTime , 3, 4)

getroute$BusInfo.goLastBusTime_hour[getroute$BusInfo.goLastBusTime_hour =="00"]<- "24"
getroute$BusInfo.goFirstBusTime_hour[getroute$BusInfo.goFirstBusTime_hour =="00"]<- "24"

for (i in 27:30) {
  getroute[,i] <- as.numeric(as.character(getroute[,i]))
}
getroute$BusInfo.realSequence <- as.numeric(as.character(getroute$BusInfo.realSequence))#總次數

getroute$hour <- getroute$BusInfo.goLastBusTime_hour-getroute$BusInfo.goFirstBusTime_hour
getroute$min <- (getroute$BusInfo.goLastBusTime_min-getroute$BusInfo.goFirstBusTime_min)/60
getroute$period <- getroute$hour+getroute$min
getroute$times_hour <- getroute$BusInfo.realSequence/getroute$period
route <- getroute[,c(1,4,6,7,33,34)]
route.dd <- duplicated(route[,1])
route.dd.attr <- route[!route.dd,]
route.dd.attr$times_hour[is.infinite(route.dd.attr$times_hour)] <-(1/18)
#write.csv(route.dd.attr,file = "D:/地理系大二/空間分析/期末報告/平日公車.csv")
head(route.dd.attr)
##   BusInfo.Id BusInfo.nameZh BusInfo.pathAttributeId
## 1      16184            680                  157886
## 2      11031           棕10                   11031
## 3      11051            小1                  110510
## 4      11052           小12                  157460
## 6      11055            小2                  110550
## 7      11062          小1區                  110630
##   BusInfo.pathAttributeName   period times_hour
## 1                       680 13.08333  7.5668790
## 2                      棕10 17.00000  1.7647059
## 3                       小1 13.00000  1.3846154
## 4            小12往富德公墓 11.16667  0.6268657
## 6                       小2 15.50000  0.3870968
## 7               小1區經康寧 15.58333  1.6684492
station_times <- right_join(getstop, route.dd.attr, by = c("BusInfo.routeId"="BusInfo.Id"))

station_times_xtabs = xtabs(times_hour ~ BusInfo.nameZh.x ,station_times)
stop_w <- data.frame(station_times_xtabs)
head(stop_w)
##   BusInfo.nameZh.x       Freq
## 1  101國際購物中心  40.465112
## 2         48號公園   2.560000
## 3      823紀念公園  25.512892
## 4     一女中(公園) 112.865248
## 5     一女中(重慶)   8.524038
## 6     一女中(貴陽)  45.016643
getroute$BusInfo.holidayGoLastBusTime_min <-substr(getroute$BusInfo.holidayGoLastBusTime , 3, 4)
getroute$BusInfo.holidayGoLastBusTime_hour <-substr(getroute$BusInfo.holidayGoLastBusTime , 1, 2)

getroute$BusInfo.holidayGoFirstBusTime_min <- substr(getroute$BusInfo.holidayGoFirstBusTime , 3, 4)
getroute$BusInfo.holidayGoFirstBusTime_hour <- substr(getroute$BusInfo.holidayGoFirstBusTime , 1, 2)

getroute$BusInfo.holidayGoLastBusTime_hour[getroute$BusInfo.holidayGoLastBusTime_hour =="00"]<- "24"
getroute$BusInfo.holidayGoFirstBusTime_hour[getroute$BusInfo.holidayGoFirstBusTime_hour =="00"]<- "24"

for (i in 35:38) { getroute[,i] <- as.numeric(as.character(getroute[,i]))}

getroute$hour_holiday <- getroute$BusInfo.holidayGoLastBusTime_hour-getroute$BusInfo.holidayGoFirstBusTime_hour
getroute$min_holiday <- (getroute$BusInfo.holidayGoLastBusTime_min-getroute$BusInfo.holidayGoFirstBusTime_min)/60
getroute$period_holiday <- getroute$hour_holiday+getroute$min_holiday
getroute$times_hour_holiday <- getroute$BusInfo.realSequence/getroute$period_holiday
route_holiday <- getroute[,c(1,4,6,7,41,42)]
route.dd_holiday <- duplicated(route_holiday[,1])
route.dd.attr_holiday<- route_holiday[!route.dd_holiday,]
route.dd.attr_holiday[is.na(route.dd.attr_holiday)] <- 0
#write.csv(route.dd.attr_holiday,file = "D:/地理系大二/空間分析/期末報告/假日公車.csv")
head(route.dd.attr_holiday)
##   BusInfo.Id BusInfo.nameZh BusInfo.pathAttributeId
## 1      16184            680                  157886
## 2      11031           棕10                   11031
## 3      11051            小1                  110510
## 4      11052           小12                  157460
## 6      11055            小2                  110550
## 7      11062          小1區                  110630
##   BusInfo.pathAttributeName period_holiday times_hour_holiday
## 1                       680        9.50000         10.4210526
## 2                      棕10       17.00000          1.7647059
## 3                       小1       13.00000          1.3846154
## 4            小12往富德公墓       11.16667          0.6268657
## 6                       小2        0.00000          0.0000000
## 7               小1區經康寧       15.58333          1.6684492
station_times_holiday <- right_join(getstop, route.dd.attr_holiday, by = c("BusInfo.routeId"="BusInfo.Id"))

station_times_xtabs_holiday = xtabs(times_hour_holiday ~ BusInfo.nameZh.x ,station_times_holiday)
stop_w_holiday <- data.frame(station_times_xtabs_holiday)
stop_w_holiday <- data.frame(BusInfo.nameZh.x=stop_w_holiday$BusInfo.nameZh.x, Freq_holiday =stop_w_holiday$Freq)

getstop.dd <- duplicated(getstop[,3])
getstop.dd.attr<- getstop[!getstop.dd,]

getstop_we <- right_join(getstop.dd.attr, stop_w, by = c("BusInfo.nameZh"="BusInfo.nameZh.x"))
getstop_we <- right_join(getstop_we, stop_w_holiday, by = c("BusInfo.nameZh"="BusInfo.nameZh.x"))
for (i in c(15,16,23,24)) {
  getroute[,i][getroute[,i]=="10"] <- "1010"; getroute[,i][getroute[,i]=="12"] <- "1212"
  getroute[,i][getroute[,i]=="15"] <- "1515"; getroute[,i][getroute[,i]=="20"] <- "2020"
  getroute[,i][getroute[,i]=="30"] <- "3030"; getroute[,i][getroute[,i]=="45"] <- "4545"
}

getroute$holidayPeak_1 <-substr(getroute$BusInfo.holidayPeakHeadway , 1, 2)
getroute$holidayPeak_2 <-substr(getroute$BusInfo.holidayPeakHeadway , 3, 4)

getroute$holidayOffPeak_1 <-substr(getroute$BusInfo.holidayOffPeakHeadway , 1,2)
getroute$holidayOffPeak_2 <-substr(getroute$BusInfo.holidayOffPeakHeadway , 3,4)

getroute$Peak_1 <- substr(getroute$BusInfo.peakHeadway , 1, 2)
getroute$Peak_2 <- substr(getroute$BusInfo.peakHeadway , 3, 4)

getroute$OffPeak_1 <- substr(getroute$BusInfo.offPeakHeadway , 1, 2)
getroute$OffPeak_2 <- substr(getroute$BusInfo.offPeakHeadway , 3, 4)

for (i in 43:50) {
  getroute[,i] <- as.numeric(as.character(getroute[,i]))
}

getroute$Peak <- 60/((getroute$Peak_1+getroute$Peak_2)/2)
getroute$OffPeak <- 60/((getroute$OffPeak_1+getroute$OffPeak_2)/2)
getroute$holidayPeak <- 60/((getroute$holidayPeak_1+getroute$holidayPeak_2)/2)
getroute$holidayOffPeak <- 60/((getroute$holidayOffPeak_1+getroute$holidayOffPeak_2)/2)
peak_offpeak <- getroute[,c(1,4,6,7,51,52,53,54)]
peak_offpeak.dd <- duplicated(peak_offpeak[,1])
peak_offpeak.dd.attr<- peak_offpeak[!peak_offpeak.dd,]
peak_offpeak.dd.attr[is.na(peak_offpeak.dd.attr)] <- 0
#write.csv(peak_offpeak.dd.attr,file = "D:/地理系大二/空間分析/期末報告/尖峰公車.csv")
head(peak_offpeak.dd.attr)
##   BusInfo.Id BusInfo.nameZh BusInfo.pathAttributeId
## 1      16184            680                  157886
## 2      11031           棕10                   11031
## 3      11051            小1                  110510
## 4      11052           小12                  157460
## 6      11055            小2                  110550
## 7      11062          小1區                  110630
##   BusInfo.pathAttributeName Peak OffPeak holidayPeak holidayOffPeak
## 1                       680    0       0           0              0
## 2                      棕10    0       0           0              0
## 3                       小1    0       0           0              0
## 4            小12往富德公墓    0       0           0              0
## 6                       小2    0       0           0              0
## 7               小1區經康寧    0       0           0              0
station_times_peak <- right_join(getstop, peak_offpeak.dd.attr, by = c("BusInfo.routeId"="BusInfo.Id"))

station_times_xtabs_peak = xtabs(Peak ~ BusInfo.nameZh.x ,station_times_peak)
station_times_xtabs_offpeak = xtabs(OffPeak ~ BusInfo.nameZh.x ,station_times_peak)
station_times_xtabs_holidaypeak = xtabs(holidayPeak ~ BusInfo.nameZh.x ,station_times_peak)
station_times_xtabs_holidayoffpeak = xtabs(holidayOffPeak ~ BusInfo.nameZh.x ,station_times_peak)

a <- data.frame(station_times_xtabs_peak)
b <- data.frame(station_times_xtabs_offpeak)
c <- data.frame(station_times_xtabs_holidaypeak)
d <- data.frame(station_times_xtabs_holidayoffpeak)

stop_w_peak <- data.frame(BusInfo.nameZh.x=a$BusInfo.nameZh.x, peak = a$Freq,offpeak = b$Freq, holidaypeak = c$Freq, holidayoffpeak = d$Freq)
setwd("D:/地理系大二/空間分析/期末報告")
final= read.csv("尖峰與普通.csv", sep = ",", header = T)

station_times_peak <- right_join(getstop, final, by = c("BusInfo.routeId"="BusInfo.Id"))

station_times_xtabs_peak = xtabs(Peak ~ BusInfo.nameZh.x ,station_times_peak)
station_times_xtabs_offpeak = xtabs(OffPeak ~ BusInfo.nameZh.x ,station_times_peak)
station_times_xtabs_holidaypeak = xtabs(holidayPeak ~ BusInfo.nameZh.x ,station_times_peak)
station_times_xtabs_holidayoffpeak = xtabs(holidayOffPeak ~ BusInfo.nameZh.x ,station_times_peak)

a <- data.frame(station_times_xtabs_peak)
b <- data.frame(station_times_xtabs_offpeak)
c <- data.frame(station_times_xtabs_holidaypeak)
d <- data.frame(station_times_xtabs_holidayoffpeak)

stop_w_peak_nor <- data.frame(BusInfo.nameZh.x=a$BusInfo.nameZh.x, peak = a$Freq,offpeak = b$Freq)
stop_w_peak_hol <- data.frame(BusInfo.nameZh.x=c$BusInfo.nameZh.x, holidaypeak = c$Freq, holidayoffpeak = d$Freq)

getstop.dd <- duplicated(getstop[,3])
getstop.dd.attr<- getstop[!getstop.dd,]
#write.csv(getstop.dd.attr,file = "D:/地理系大二/空間分析/期末報告/getstop.dd.attr.csv")

getstop_we <- right_join(getstop.dd.attr,stop_w_peak_nor, by = c("BusInfo.nameZh"="BusInfo.nameZh.x"))
getstop_we_hol <- right_join(getstop.dd.attr,stop_w_peak_hol, by = c("BusInfo.nameZh"="BusInfo.nameZh.x"))
getstop_we$peak <- getstop_we$peak*5/7
getstop_we$offpeak <- getstop_we$offpeak*5/7
normalday <- getstop_we[,c(-2,-5,-6,-9,-11,-14)]
#write.csv(normalday,file = "D:/地理系大二/空間分析/期末報告/平日.csv")

getstop_we_hol$holidaypeak <- getstop_we_hol$holidaypeak *2/7
getstop_we_hol$holidayoffpeak <- getstop_we_hol$holidayoffpeak *2/7
holiday <- getstop_we_hol[,c(-2,-5,-6,-9,-11,-14)]
#write.csv(normalday,file = "D:/地理系大二/空間分析/期末報告/假日.csv")

head(normalday)
##   BusInfo.Id  BusInfo.nameZh                            BusInfo.nameEn
## 1      35449 101國際購物中心                                Taipei 101
## 2     148814        48號公園                                   48 Park
## 3      45347     823紀念公園                         823 Memorial Park
## 4      35546    一女中(公園)  Taipei First Girls High School(Gongyuan)
## 5      40541    一女中(重慶) Taipei First Girls High School(Chongqing)
## 6      27140    一女中(貴陽)  Taipei First Girls High School(Guiyang )
##   BusInfo.longitude BusInfo.latitude
## 1        121.565536        25.034635
## 2   121.51220691788  25.142618161451
## 3        121.515168        25.001785
## 4  121.515136595156 25.0368285357308
## 5        121.512577         25.03755
## 6         121.51452        25.038687
##                                BusInfo.address  BusInfo.showLon
## 1                                       松智路       121.565536
## 2 登山路與泉源路65-1號前方橋梁路口東側15公尺處      121.5122069
## 3                            安樂路248-1號同向      121.5151052
## 4                                 公園路21號旁 121.515136595156
## 5                                 重慶南路一段      121.5128083
## 6                               貴陽街一段對向 121.514498656168
##    BusInfo.showLat       peak   offpeak
## 1        25.034635  43.127616 28.940145
## 2      25.14261816   1.828571  1.828571
## 3      25.00190917  18.973589 16.787648
## 4 25.0368285357308 103.327661 72.871745
## 5      25.03780821   9.297052  5.601366
## 6 25.0386560843846  44.628256 35.687080
head(holiday)
##   BusInfo.Id  BusInfo.nameZh                            BusInfo.nameEn
## 1      35449 101國際購物中心                                Taipei 101
## 2     148814        48號公園                                   48 Park
## 3      45347     823紀念公園                         823 Memorial Park
## 4      35546    一女中(公園)  Taipei First Girls High School(Gongyuan)
## 5      40541    一女中(重慶) Taipei First Girls High School(Chongqing)
## 6      27140    一女中(貴陽)  Taipei First Girls High School(Guiyang )
##   BusInfo.longitude BusInfo.latitude
## 1        121.565536        25.034635
## 2   121.51220691788  25.142618161451
## 3        121.515168        25.001785
## 4  121.515136595156 25.0368285357308
## 5        121.512577         25.03755
## 6         121.51452        25.038687
##                                BusInfo.address  BusInfo.showLon
## 1                                       松智路       121.565536
## 2 登山路與泉源路65-1號前方橋梁路口東側15公尺處      121.5122069
## 3                            安樂路248-1號同向      121.5151052
## 4                                 公園路21號旁 121.515136595156
## 5                                 重慶南路一段      121.5128083
## 6                               貴陽街一段對向 121.514498656168
##    BusInfo.showLat holidaypeak holidayoffpeak
## 1        25.034635  10.0542007     10.0542007
## 2      25.14261816   0.7314286      0.7314286
## 3      25.00190917   6.6833133      6.6833133
## 4 25.0368285357308  22.1752701     20.4273709
## 5      25.03780821   2.6433180      2.6433180
## 6 25.0386560843846   5.5700336      5.5700336