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 坪林站


2016年4月28日 星期四

[R] User Agent API 資料接取

無法查看此摘要。請 按這裡查看文章。

2016年4月19日 星期二

[R] Yahoo電影資訊擷取

無法查看此摘要。請 按這裡查看文章。

2016年4月13日 星期三

2016年1月12日 星期二

[R] leafletR package

無法查看此摘要。請 按這裡查看文章。

2016年1月7日 星期四

[R] Open Data合集

library(httr)
library(RCurl)
library(XML)
library(bitops)

###########################################################################################
# 全台寺廟 http://religion.moi.gov.tw/Report/temple.xml
###########################################################################################
url_gen = "http://religion.moi.gov.tw/Report/temple.xml"
data = xmlToDataFrame(url_gen)
#欄位名稱encoding
colnames(data) = iconv(colnames(data),"utf8","big5")

寺廟名稱 主祀神祇 行政區 地址 教別 建別 組織型態 電話 負責人 其他
瑞芳青雲殿 神農大帝 新北市 新北市瑞芳區濱二路海濱里12鄰30號 道教 適用監督寺廟條例寺廟 管理委員會制 02-24972694 李清河  
四腳亭九玄宮 蘇府千歲 新北市 新北市瑞芳區大埔路國宅新村吉安里5鄰17之1號 道教 適用監督寺廟條例寺廟 管理委員會制 02-24575704 陳土金  
新興宮 天上聖母 新北市 新北市瑞芳區鼻頭路鼻頭里3鄰41號之2 道教 適用監督寺廟條例寺廟 管理委員會制 02-24911549 王彥斌  
上天福安宮 福德正神 新北市 新北市瑞芳區滴水子路上天里7鄰29號 道教 適用監督寺廟條例寺廟 管理人(住持)制 02-24578822 陳國中  
金福宮 福德正神 新北市 新北市瑞芳區石山里2鄰5號路253之1號 道教 適用監督寺廟條例寺廟 管理委員會制 02-24961841 吳坤池  
慈音寺 觀世音菩薩 新北市 新北市瑞芳區大寮路上更里5鄰63巷11號 佛教 適用監督寺廟條例寺廟 管理人(住持)制 02-24972027 吳強台  
三學蘭若 準提佛母 新竹縣 新竹縣峨眉鄉湖光村12鄰12寮6-6號 佛教 適用監督寺廟條例寺廟 管理人(住持)制 037-602820 羅金田  
萬佛庵 千手觀音 新竹縣 新竹縣峨眉鄉七星村14鄰6寮59號 佛教 適用監督寺廟條例寺廟 管理人(住持)制 03-5800065 方素珠  
義民廟 義民爺 苗栗縣 苗栗縣獅潭鄉新店村4鄰46號 道教 適用監督寺廟條例寺廟 其他 037-932190 黃宏昌  
玉虛宮 關聖帝君 苗栗縣 苗栗縣獅潭鄉豐林村10鄰24號 道教 適用監督寺廟條例寺廟 其他 037-931547 劉漢龍  

###########################################################################################
# 宗教活動 http://religion.moi.gov.tw/Report/Festival.xml
###########################################################################################
url_gen = "http://religion.moi.gov.tw/Report/Festival.xml"
data = xmlToDataFrame(url_gen)
#欄位名稱encoding
colnames(data) = iconv(colnames(data),"utf8","big5")

慶祭典名稱 宗教團體 類別 日期 區別 說明
放生法會 台北市慈音居士會 6月25日 臺北市中山區 NA
大悲觀音法會 台北市慈音居士會 3月20日 臺北市中山區 NA
浴佛法會 台北市慈音居士會 5月8日 臺北市中山區 NA
宮慶 三玄宮 6月6日 臺北市中山區 NA
祭典 三玄宮 6月1日 臺北市中山區 NA
普度法會 三玄宮 7月18日 臺北市中山區 NA
太上老君聖誕 台北崑崙正興宮 2月15日 臺北市中山區 NA
中元普渡 台北崑崙正興宮 7月19日 臺北市中山區 NA
土地公祭典 有應祠 2月2日 臺北市中山區 NA
中元普度 北投磺港公園三合萬壽祠 7月9日 臺北市北投區 NA

###########################################################################################
# #紫外線預報 http://opendata.epa.gov.tw/ws/Data/{dataId}/?$format={format}&$top={top}
# {dataId}:資料代號 {format}:資料格式 {top}:取最前筆數 {skip}:跳過筆數 {orderby}:排序欄位
###########################################################################################
url_gen = "http://opendata.epa.gov.tw/ws/Data/UVIF/?$orderby=Name&$skip=0&$top=1000&format=xml"
data = xmlToDataFrame(url_gen)

Name Type UVI UVIStatus PublishTime
三仙台 旅遊預報點 3 中量級 2016/1/7 10:30
小琉球 外島預報點 4 中量級 2016/1/7 10:30
中部空品區 空品區 3-4 中量級 2016/1/7 10:30
太平山 旅遊預報點 2 低量級 2016/1/7 10:30
太魯閣 旅遊預報點 2 低量級 2016/1/7 10:30
日月潭 旅遊預報點 3 中量級 2016/1/7 10:30
北部空品區 空品區 1-2 低量級 2016/1/7 10:30

###########################################################################################
# 空氣品質監測 http://opendata.epa.gov.tw/ws/Data/AQX/?format=xml
# 測站名稱(SiteName)、縣市(County)、空氣污染指標(PSI)、指標污染物(MajorPollutant)、
# 狀態(Status)、二氧化硫濃度(SO2)、一氧化碳濃度(CO)、臭氧濃度(O3)、懸浮微粒濃度(PM10)、
# 細懸浮微粒濃度(PM2.5)、二氧化氮濃度(NO2)、風速(WindSpeed)、風向(WindDirec)、發布時間(PublishTime)
###########################################################################################
url_gen = "http://opendata.epa.gov.tw/ws/Data/AQX/?format=xml"
data = xmlToDataFrame(url_gen)

CO County FPMI MajorPollutant NO NO2 NOx O3 PM10 PM2.5 PSI PublishTime SiteName SO2 Status WindDirec WindSpeed
0.36 基隆市 3 0.79 5.8 6.6 43 36 25 37 2016/1/7 16:00 基隆 1.3 良好 74 0.8
0.41 新北市 2 2.84 17 19.99 31 43 24 42 2016/1/7 16:00 汐止 5.2 良好 49 1.4
0.32 新北市 3 懸浮微粒 1.3 5.3 6.55 49 103 33 74 2016/1/7 16:00 萬里 1.6 普通 38 3.1
0.56 新北市 3 3.46 20 23.85 27 45 26 42 2016/1/7 16:00 新店 3.2 良好 72 4.2
0.53 新北市 3 2.19 17 19.51 34 45 28 43 2016/1/7 16:00 土城 4.2 良好 321 1.3
新北市 0.46 21 21.7 34 54 20 0 2016/1/7 16:00 板橋 75 2.8
0.63 新北市 2 2.54 19 21.28 32 47 23 44 2016/1/7 16:00 新莊 3.2 良好 77 4.3
0.53 新北市 2 2.61 15 17.19 28 44 19 41 2016/1/7 16:00 菜寮 1 良好 80 2.3
0.37 新北市 3 6.18 15 21.51 35 51 26 49 2016/1/7 16:00 林口 2 良好 44 3
0.41 新北市 1.12 5.9 7 41 42 0 44 2016/1/7 16:00 淡水 0.7 良好