2016年7月21日 星期四

[R] 用LeafletR 畫鐵軌+火車站

# read rail shape file(ref https://gist-map.motc.gov.tw/Complex/MapTopic)
rail = toGeoJSON(data = "F:/WGS84_2.zip", name = "rail", dest = tempdir())

# change file code page to utf8
writeLines(iconv(readLines(rail), from = "big5", to = "utf8"), file(rail, encoding = "UTF-8"))

# make style based on rail path
rail.sty = styleSingle(col = "blue", lwd = 3, alpha = 0.8)

# read station point file(ref https://gist-map.motc.gov.tw/Complex/MapTopic)
railway_station = read.csv("F:/railway_station.csv", stringsAsFactors=FALSE)
station = toGeoJSON(data = railway_station, dest = tempdir(), name = "station", lat.lon = c("lat","lon"))

# change file code page to utf8
writeLines(iconv(readLines(station), from = "big5", to = "utf8"), file(station, encoding = "UTF-8"))

# make style based on station point
station.sty = styleSingle(fill = "red", fill.alpha = 1, rad = 10)

# create map
map = leaflet(data = list(rail,station), dest=tempdir(), style=list(rail.sty,station.sty), popup = "station")
map



2016年7月7日 星期四

[R] 高鐵時刻表

終於做出一個POST網頁的例子,抓取高鐵時刻表

library(RCurl)
library(XML)

##輸入相關查詢Value
url = "https://www.thsrc.com.tw/tw/TimeTable/SearchResult"
html = postForm(url,StartStation = "977abb69-413a-4ccf-a109-0272c24fd490"        ##台北站ID
                                 ,EndStation = "e6e26e66-7dc1-458f-b2f3-71ce65fdc95f"  ##板橋站ID
         ,SearchDate = "2016/07/07"
         ,SearchTime = "11:00"
         ,SearchWay = "DepartureInMandarin")  ##出發


 







html_code = htmlParse(html)

##擷取查詢結果

























thrsc_id = xpathSApply(test, "//*[@class='column1']",xmlValue)
thrsc_leave_time = xpathSApply(test, "//*[@class='column3']",xmlValue)
thrsc_arrival_time = xpathSApply(test, "//*[@class='column3']",xmlValue)





2016年7月4日 星期一

[R] 國道客運站牌API資料擷取

#若有Proxy加上下列指令
Sys.setenv(http_proxy="http://proxy.xxx.com.tw:8080")
Sys.setenv(https_proxy="https://proxy.xxx.com.tw:8080")

#Import library
library(httr)
library(RCurl)
library(XML)
library(jsonlite)

#################################################################################
# 國道客運
#################################################################################
#國道客運API
bus_station_point = function(busid){
url_gen = paste("http://www.taiwanbus.tw/app_api/RouteStop.ashx?routeNo=", busid, sep="")
html_code = getURL(url_gen, encoding = "big5")
bus = fromJSON(html_code)
return(bus$stopResult)
}

bus_station_point(1916)

busid groupid lat lon name
9028 8529 24.98405 121.5413 捷運大坪林站
9028 39871 24.63259 121.7934 冬山國中
9028 41302 24.59528 121.8515 蘇澳站
9028 48017 24.67824 121.7767 羅東站
9028 56243 24.68814 121.7912 五結
9028 56705 24.93718 121.7129 坪林站