-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
52 lines (50 loc) · 1.42 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
require 'open-uri'
require 'csv'
require 'json'
require 'nkf'
def csv_to_geojson(texts)
marker_colors = {
'死亡事故' => '#FD7400',
'軽傷事故' => '#BEDB39',
'重傷事故' => '#004358'
}
map = {}
map['type'] = 'FeatureCollection'
map['features'] = []
texts.each.with_index(1) do |text, text_index|
CSV.new(NKF.nkf('-S --cp932 -w -Lu', text), headers:true).each.with_index do |row, row_index|
next if row['名称'] !~ /全事故/
next if row['当事者種別'] !~ /自転車/
feature = {
id: text_index * row_index,
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [row['地点経度'].to_f, row['地点緯度'].to_f]
},
properties: row.to_h.merge({
'marker-symbol': 'marker',
'marker-color': marker_colors[row['事故内容']]
})
}
map['features'] << feature
end
end
return map.to_json
end
namespace :data do
task :generate do
texts = [24, 25, 26].map{ |year|
'http://db.pref.tottori.jp/opendataResearch.nsf/' \
'e92cd7f95a94c9c249257c92000236b8/' \
'5c2a1f59ec27a81a49257d630016cf34/$FILE/' \
'%E4%BA%A4%E9%80%9A%E4%BA%8B%E6%95%85-(%E5%B9%B3%E6%88%90' \
"#{year}" \
'%E5%B9%B4).csv'
}.map{ |url|
open(url){ |io|io.read }
}
x = csv_to_geojson(texts)
open('public/data.geojson', 'w'){|io| io.write(x)}
end
end