This repository has been archived by the owner on Sep 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
youbike.rb
executable file
·177 lines (154 loc) · 5.21 KB
/
youbike.rb
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env ruby
# encoding: utf-8
=begin
YouBike rental stations data to JOSM XML, JSON, CSV file format converter
Copyright (C) 2013 Huei-Horng Yo
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
=end
require 'csv'
require 'json'
require 'nokogiri'
require 'open-uri'
require 'net/http'
require 'slop'
require 'pp'
class Nodes
COLUMNS = [:iid, :sv, :sd, :vtyp, :sno, :sna, :sip, :tot, :sbi, :sarea, :mday, :lat, :lng, :ar, :sareaen, :snaen, :aren, :nbcnt, :bemp, :act]
FORMATS = ['osm', 'json', 'csv']
def initialize(mode, opts)
@options = opts
@youbike_nodes = Array.new
load_http if mode == 'get'
load_file if mode == 'convert'
end
def save
@format = FORMATS.include?(@options[:format]) ? @options[:format] : "osm"
@output = (@options[:output].nil?) ? "youbike-#{Time.now.to_i}.#{@format}" : @options[:output]
puts "Saving '#{@output}' ..."
case @format
when 'osm'
self.to_osm
when 'json'
self.to_json
when 'csv'
self.to_csv
end
end
def to_csv
CSV.open(@output, "wb", {:force_quotes => true}) do |csv|
csv << COLUMNS
@youbike_nodes.each do |node|
csv << node.values
end
end
end
def to_json
File.open(@output, "wb") do |file|
file << @youbike_nodes.to_json
end
end
def to_osm
builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
xml.osm(:version => '0.6') {
@youbike_nodes.each_with_index do |node, index|
xml.node(:id => -index - 1, :visible => 'true', :lat => node['lat'], :lon => node['lng']) {
xml.tag(:k => 'amenity', :v => 'bicycle_rental')
xml.tag(:k => 'name', :v => node['sna'])
xml.tag(:k => 'name:en', :v => node['snaen'])
xml.tag(:k => 'name:zh', :v => node['sna'])
xml.tag(:k => 'ref', :v => node['sno'])
xml.tag(:k => 'capacity', :v => node['tot'])
xml.tag(:k => 'network', :v => 'YouBike 微笑單車')
xml.tag(:k => 'network:en', :v => 'YouBike')
xml.tag(:k => 'network:zh', :v => '微笑單車')
}
end
}
end
osm_xml = File.new(@output, 'w')
osm_xml << builder.to_xml
osm_xml.close
end
private
def load_http
@youbike_data = Net::HTTP.get(URI.parse("http://opendata.dot.taipei.gov.tw/opendata/gwjs_cityhall.json"))
@youbike_nodes = JSON.parse(@youbike_data)['retVal']
end
def load_file
abort if @options[:input].nil?
abort unless File.readable?(@options[:input])
@input = @options[:input]
case File.extname(@input)
when '.osm'
Nokogiri::XML(File.open(@input).read).xpath('//node').each do |node|
# mapping fields
# :iid, :sv, :sd, :vtyp, :sno, :sna, :sip, :tot, :sbi, :sarea, :mday, :lat, :lng, :ar, :sareaen, :snaen, :aren, :nbcnt, :bemp, :act
@youbike_nodes << Hash[ COLUMNS.zip [
nil,
nil,
nil,
nil,
node.xpath("tag[@k='ref']").attr('v').to_s,
node.xpath("tag[@k='name:zh']").attr('v').to_s,
nil,
node.xpath("tag[@k='capacity']").attr('v').to_s,
nil,
nil,
nil,
node['lat'],
node['lon'],
nil,
nil,
node.xpath("tag[@k='name:en']").attr('v').to_s,
nil,
nil,
nil,
nil] ]
end
when '.json'
JSON.parse(File.open(@input).read).each do |node|
@youbike_nodes << node.inject({}){ |h, (n,v)| h[n.to_sym] = v; h }
end
when '.csv'
CSV.open(@input, options = {:headers => true}).each do |node|
@youbike_nodes << node.to_hash.inject({}){ |h, (n,v)| h[n.to_sym] = v; h }
end
end
end
end
# main
begin
options = Slop.parse(help: true) do
banner "Usage: youbike.rb command [options]"
command "get" do
banner "Usage: youbike.rb get [options]"
on :t=, :format=, "Choose the output file format: osm, json, csv"
on :o=, :output=, "Specify the output filename"
run do |opts|
nodes = Nodes.new(opts.config[:command], opts.to_hash(true))
nodes.save
end
end
command "convert" do
banner "Usage: youbike.rb convert [options]"
on :t=, :format=, "Choose the output file format: osm, json, csv"
on :i=, :input=, "Specify the local data source file"
on :o=, :output=, "Specify the output filename"
run do |opts|
nodes = Nodes.new(opts.config[:command], opts.to_hash(true))
nodes.save
end
end
end
rescue Slop::MissingArgumentError
puts "[Oops] Missing argument(s) :-("
end