-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRakefile
65 lines (56 loc) · 1.44 KB
/
Rakefile
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
#!/usr/bin/env rake
# encoding utf8
require 'csv'
require 'json'
require 'builder'
task :default => [:json, :kml]
def csv_to_hash
stations = {}
CSV.foreach("kvb_stops.csv", {:col_sep => ";", :quote_char => '$'}) do |row|
if stations[row[0]].nil?
stations[row[0]] = {
"kvb-id" => row[0],
"station" => row[1],
"points" => []
}
end
stations[row[0]]["points"] << {
"lat" => row[2],
"long" => row[3],
"type" => row[4]
}
end
stations
end
def blank(str)
str.nil? || str.length == 0
end
desc "Creates a JSON file"
task :json do
puts "Generating JSON"
File.write("kvb_stops.json", JSON.pretty_generate(csv_to_hash.values) + "\n")
end
desc "Create a KML file"
task :kml do
puts "Generating KML"
builder = Builder::XmlMarkup.new(indent: 2)
builder.instruct! :xml, :version=>"1.0", :encoding=>"UTF-8"
xml = builder.kml("xmlns"=>"http://www.opengis.net/kml/2.2") do |kml|
kml.Document do |doc|
doc.name "KVB train and bus stops"
csv_to_hash.values.each do |station|
station["points"].each do |point|
if (!blank(point["lat"]) && !blank(point["long"]))
doc.Placemark do |place|
place.name station["station"]
place.Point do |p|
p.coordinates "#{point["long"]},#{point["lat"]}"
end
end
end
end
end
end
end
File.write("kvb_stops.kml", xml)
end