open data intro
TRANSCRIPT
Open Data IntroUsing C# and XML
About me
• Lorex L. Yang
• 你可以叫我 L ,但請切記我不認識夜神月,也看不懂死亡筆記本。• [email protected]
• https://github.com/Lorex (一堆無意義的 Repo…… )
• Sinta Design 學生資訊社群 / 副社長• 立志中學電腦與資訊研究社 / 講師• 高雄市議員 / 資訊顧問• 正太控一枚
其實我只是個高中生
只不過高中讀了五年
在苦讀五年之後
我休學了 XDDD
Open Data歐噴.爹塔
WTF is Open Data ?
!!
Before we get started…
先來句經典格言
“ 我好帥 ”
- Lorex L. Yang, SintaDesign 學生資訊社群 副團長
“ Information is the currency of democracy. ”
- Thomas Jefferson, the 3rd President of the U.S.
So.
WTF is Open Data ?
Freely to use.
Freely to republic republish.
開什麼玩笑! 要不要 Republic 要交給 13 億人民來決定才對!!
Without restrictions from copyright.
Available to everyone.
Why Open Data ?
Transparency
Democratic control
Participation
Innovation
Government x Open Data
Why government ?
我們都知道……
政府真的很清廉……
於是我們要求政府建 Open Data
Another reason
我們都知道……
以下為 參考 裝飾用。
想打 1999……
「請問誤點的是哪個班次呢?」「請問誤點的駕駛是哪位呢?」
「請問誤點的車牌號碼是甚麼呢?」
如果政府有 Open Data……
交通局 你
有數據可以婊,爽!
Example
交通局-捷運動態資訊系統
氣象局-水庫洩洪警戒
內政部-政治獻金紀錄…… . 恩,不意外。
How to use ?
等等!
地下管線資料不開放
知道 Open Data 的重要性了吧
How to use ?
以高雄市公車動態資訊系統為例
先來看一下政府做的介面……http://122.146.229.210/bus/Dybus.aspx#
什麼?找不到到站歷史紀錄?
政府:
呵呵,自幹吧你 (́C◕ლ ◞ ◟◕‵౪ )ლGovernment says go f*** yourself
C# + XML
What is XML ?
eXtensible Markup Language
Example
<?xml version=“1.0?>
< 腫花皿鍋 >
< 政治人物 >
< 馬芙丸 seat=“ 區長” class=“ 水母” alias=“bumbler” />
< 祭止兀 seat=“ 立猥” class=“ 盜圖” alias=“” />
< 靈異室 seat=“ 秘書長” class=“3323” alias=“014” />
</ 政治人物 >
< 腫花皿鍋 >
開始自幹Go F*** Yourself.
Repohttps://github.com/Lorex/KH-Bus-Agent
Open Data used
• 公車路線表• http://122.146.229.210/xmlbus2/StaticData/GetRoute.xml
• 公車到站時間• http://122.146.229.210/xmlbus2/GetEstimateTime.xml?routeIds={ 路
線 }
XML 欄位介紹
Concept. 自己寫兩個 Function
• 讀取並更新所有公車的路線• private void RefRoute()
• 讀取並更新到站資訊• private void RefEstimate(string Route,bool History,bool AutoRef10s)
• Route • 要查詢的路線
• History : • True :正在更新的是所有路線在這時間的到站資訊• False :正在更新的是單一路線的所有站點資訊
• AutoRef10s :• True :正在更新的是每 10 秒自動觸發的更新事件
首先宣告XmlDocument doc = new XmlDocument();
Part 1. 讀取公車路線private void RefRoute()
{ //ComboBox 清空列表 comboBox1.Items.Clear();
//載入 XML 文件 doc.Load("http://122.146.229.210/xmlbus2/StaticData/GetRoute.xml");
//選擇節點列表 XmlNodeList NodeLists = doc.SelectNodes("BusDynInfo/BusInfo/Route");
// 讀取節點 foreach (XmlNode Single in NodeLists) { // 寫入到 ComboBox 選單裡面 comboBox1.Items.Add(Single.Attributes["ID"].Value); }}
Part 2-1. 顯示選擇路線的詳細資訊 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{ //載入 doc.Load("http://122.146.229.210/xmlbus2/StaticData/GetRoute.xml");
// 讀取節點清單 XmlNodeList NodeLists = doc.SelectNodes("BusDynInfo/BusInfo/Route");
// 讀取節點 foreach (XmlNode Single in NodeLists) { if (Single.Attributes["ID"].Value == comboBox1.Text)
{ // 開始拉節點屬性 label5.Text = Single.Attributes["ddesc"].Value; label6.Text = Single.Attributes["departureZh"].Value; label7.Text = Single.Attributes["destinationZh"].Value; } }
// 讀取到站時間 RefEstimate(comboBox1.Text,false,false);}
Part 2-2. 讓他自動更新private void timer1_Tick(object sender, EventArgs e)
{ RefEstimate(comboBox1.Text,false,true);}
private void timer2_Tick(object sender, EventArgs e)
{ timer1.Enabled = false; foreach(string line in comboBox1.Items)
{ RefEstimate(line,true,false); } timer1.Enabled = true;}
在開始顯示到站資訊之前…
DataGridView 控制項
可以看成是一張表格Column = 欄 = 直排
Row = 列 = 橫排
編輯欄位
Part 3. 開始實作到站資訊更新 - Review
private void RefEstimate(string Route,bool History,bool AutoRef10s) {}
• Route • 要查詢的路線
• History : • True :正在更新的是所有路線在這時間的到站資訊• False :正在更新的是單一路線的所有站點資訊
• AutoRef10s :• True :正在更新的是每 10 秒自動觸發的更新事件
Part 3-1. 判斷輸入if (Route == "") return;
string link = "http://122.146.229.210/xmlbus2/GetEstimateTime.xml?routeIds=" + Route;
Part 3-2. XML處理// 讀取 XMLdoc.Load(link);
// 讀取節點列表XmlNodeList NodeLists = doc.SelectNodes("BusDynInfo/BusInfo/Route/EstimateTime");
if (History == false) dataGridView1.Rows.Clear();
// 讀取各節點foreach(XmlNode Single in NodeLists)
{…}
Part 3-3. 拆 XML
foreach(XmlNode Single in NodeLists)
{ string GoBack = (Single.Attributes["GoBack"].Value == "1") ? "去程 " : "返程 "; string StopName = Single.Attributes["StopName"].Value; string Value = Single.Attributes["Value"].Value; string CarID = (Single.Attributes["carId"].Value == "") ? "未發車 " : Single.Attributes["carId"].Value;
DateTime dt = DateTime.Now; dt = dt.AddMinutes((Single.Attributes["Value"].Value == "null") ? 0 : Convert.ToDouble(Single.Attributes["Value"].Value));
string ArriveTime = (Single.Attributes["Value"].Value == "null") ? "未發車 " : dt.ToString("HH : mm");
...
Part 3-4. 時間處理switch (Value)
{case "null": Value = "未發車 "; break;
case "0": Value = "進站中 "; if (!AutoRef10s) dataGridView2.Rows.Add(Route,GoBack, StopName, ArriveTime, CarID); break;
default : Value = Value + " 分鐘 "; break;
}
…
Part 3-5. 寫入 DataGridView
if (History == false) dataGridView1.Rows.Add(GoBack,StopName,Value,ArriveTime,CarID);
休息一下Q&A Time
實作歷史紀錄一天一個檔案
Concept. 這邊要自己寫一個 Function
•把歷史資料寫入 XML 檔• private void XMLWrite()
Part 4-1. 當歷史資料表更新的時候…private void XMLWrite()private void dataGridView2_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{ //顯示到最後一列 dataGridView2.FirstDisplayedScrollingRowIndex = dataGridView2.Rows.Count - 1;
// 寫入 XML XMLWrite();}
Part 4-2. 讓使用者選擇存檔資料夾 private void button1_Click(object sender, EventArgs e)
{ folderBrowserDialog1.ShowDialog(); if (folderBrowserDialog1.SelectedPath == "") folderBrowserDialog1.SelectedPath = @"D:\Logs"; textBox1.Text = folderBrowserDialog1.SelectedPath;}
Part 4-3-1. 實作寫入到 XML
private void XMLWrite()
{ XmlDocument doc = new XmlDocument();
//檢驗檔案是否存在 string path = textBox1.Text; if (path == "") path = @"D:\Logs";
string filename = DateTime.Now.ToString("yyyy-MM-dd") + ".xml";
…
Part 4-3-2. 建立 XML
// 如果檔案不存在if (!System.IO.File.Exists(System.IO.Path.Combine(path, filename)))
{ // 建立根節點 XmlElement root = doc.CreateElement(“CarHistory”); doc.AppendChild(root);
// 建立子節點 XmlElement sec = doc.CreateElement(“List”); sec.SetAttribute(“Date”,DateTime.Now.ToString(“yyyyy-MM-dd”)); root.AppendChild(sec);
// 建立資料夾(如果不存在的話) if (!System.IO.Directory.Exists(path)) System.IO.Directory.CreateDirectory(path);
System.IO.File.Create(filename); doc.Save(System.IO.Path.Combine(path, filename));}
Part 4-3-3. 載入 XML
//載入 XMLdoc.Load(System.IO.Path.Combine(path, filename));XmlNode node = doc.SelectSingleNode("CarHistory/List");
if (node == null) return;
Part 4-3-4. 將歷史資料寫入節點// 開始建立節點XmlElement main = doc.CreateElement("History");
main.SetAttribute("Line", dataGridView2[0, dataGridView2.Rows.Count - 1].Value.ToString());
main.SetAttribute("GoBack", dataGridView2[1, dataGridView2.Rows.Count - 1].Value.ToString());
main.SetAttribute("StopName", dataGridView2[2, dataGridView2.Rows.Count - 1].Value.ToString());
main.SetAttribute("Time", dataGridView2[3, dataGridView2.Rows.Count - 1].Value.ToString());
main.SetAttribute("CarID", dataGridView2[4, dataGridView2.Rows.Count - 1].Value.ToString());
node.AppendChild(main);doc.Save(System.IO.Path.Combine(path, filename));
Compile & Run
自幹成功Successfully f***ed myself !
工商服務時間
中和高中 Hacking & Gaming
https://www.facebook.com/CHHG1st
SintaDesign 學生資訊社群
https://www.facebook.com/groups/sintadesign/
正太 50 無限收,意者密By 不猥瑣的正太控 Lorex
Q&A