-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_municipios.rb
121 lines (107 loc) · 3.25 KB
/
parse_municipios.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
require 'json'
require 'open-uri'
require 'debugger'
def parse_comunidad(comunidad_uri)
m = "#{comunidad_uri}".match(/http:\/\/opendata.aragon.es\/recurso\/territorio\/ComunidadAutonoma\/(.*)/)
if m.nil?
return nil
else
return m[1].gsub("_", " ")
end
end
def parse_comarca(comarca_uri)
m = "#{comarca_uri}".match(/http:\/\/opendata.aragon.es\/recurso\/territorio\/Comarca\/(.*)/)
if m.nil?
return nil
else
return m[1].gsub("_", " ")
end
end
def parse_establecimiento(establecimiento_uri)
m = "#{establecimiento_uri}".match(/http:\/\/opendata.aragon.es\/recurso\/economia\/turismo\/(.*)/)
if m.nil?
return nil
else
return m[1].gsub("_", " ")
end
end
def parse_municipio(municipio, id)
{
"id" => id,
"name" => municipio["label"],
"area" => municipio["areaTotal"],
"country" => "Spain",
"major" => municipio["alcalde"],
"men" => municipio["hombres"],
"women" => municipio["mujeres"],
"ine" => municipio["codigoINE"],
"comunidad" => parse_comunidad(municipio["comunidadAutonoma"]),
"comarca" => parse_comarca(municipio["comarca"]),
"stats" => parse_stats(municipio["datoEstadistico"], id)
}
end
def parse_stats(json_stats, municipio_id)
stats = {}
stats["vidrio"] = []
stats["establecimientos"] = []
stats["poblacion"] = []
json_stats.each do |s|
if s["kilosVidrio"]
stats["vidrio"] << {
'municipio_id' => municipio_id,
'stat_date' => s["fecha"],
'kg' => s["kilosVidrio"],
'containers_count' => s["contenedoresVidrio"]
}
elsif s["establecimientos"]
if s["establecimientos"].to_i != 0
stats["establecimientos"] << {
'municipio_id' => municipio_id,
'stat_date' => s["fecha"],
'type_establecimiento' => parse_establecimiento(s["tipoEstablecimiento"]),
'count' => s["establecimientos"], 'seats' => s["plazas"]
}
end
elsif s["poblacion"]
stats["poblacion"] << {
'municipio_id' => municipio_id,
'stat_date' => s["fecha"],
'poblacion' => s["poblacion"]
}
end
end
return stats
end
require 'json'
j = JSON.parse(File.read('Municipio.json'))
municipios = []
stats = {}
stats["vidrio"] = []
stats["establecimientos"] = []
stats["poblacion"] = []
j["result"]["items"].each_with_index do |m, i|
municipio = parse_municipio(m, i)
%w{ vidrio establecimientos poblacion}.each do |o|
stats[o].concat(municipio["stats"][o])
end
municipio.delete("stats")
municipios << municipio
end
require 'csv'
CSV.open("municipios.csv", "wb") do |csv|
csv << municipios.first.keys
municipios.each do |elem|
csv << elem.values
end
end
%w{ vidrio establecimientos poblacion}.each do |o|
CSV.open("municipios_stats_#{o}.csv", "wb") do |csv|
csv << stats[o].first.keys
stats[o].each do |elem|
csv << elem.values
end
end
end
File.open('municipios_clean.json', 'w') do |f|
f.write(municipios.to_json)
end