-
Notifications
You must be signed in to change notification settings - Fork 117
/
analyser_merge_fuel_IT.py
189 lines (168 loc) · 9.38 KB
/
analyser_merge_fuel_IT.py
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
178
179
180
181
182
183
184
185
186
187
188
189
#!/usr/bin/env python
#-*- coding: utf-8 -*-
###########################################################################
## ##
## Copyrights Francesco Ansanelli 2020 ##
## ##
## 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/>. ##
## ##
###########################################################################
from modules.OsmoseTranslation import T_
from .Analyser_Merge import Analyser_Merge_Point, Source, CSV, Load_XY, Conflate, Select, Mapping
from modules import downloader
from modules import italian_strings
import csv
import datetime
OCTANE_95 = 1 << 0# fuel:octane_95=yes
OCTANE_98 = 1 << 1# fuel:octane_98=yes
OCTANE_100 = 1 << 2# fuel:octane_100=yes
DIESEL = 1 << 3# fuel:diesel=yes
DIESEL_CL2 = 1 << 4# fuel:diesel:class2=yes
GTL_DIESEL = 1 << 5# fuel:GTL_diesel=yes
HGV_DIESEL = 1 << 6# fuel:HGV_diesel=yes
LNG = 1 << 7# fuel:lng=yes
LPG = 1 << 8# fuel:lpg=yes
CNG = 1 << 9# fuel:cng=yes
class Analyser_Merge_Fuel_IT(Analyser_Merge_Point):
def __init__(self, config, logger = None):
Analyser_Merge_Point.__init__(self, config, logger)
self.def_class_missing_official(item = 8200, id = 11, level = 3, tags = ['merge', 'highway', 'fix:imagery', 'fix:survey'],
title = T_('Gas station not integrated'))
self.def_class_missing_osm(item = 7250, id = 12, level = 3, tags = ['merge', 'highway', 'fix:chair'],
title = T_('Gas station without tag `ref:mise` or invalid'))
self.def_class_possible_merge(item = 8201, id = 13, level = 3, tags = ['merge', 'highway', 'fix:chair', 'fix:survey'],
title = T_('Gas station integration suggestion'))
self.def_class_update_official(item = 8202, id = 14, level = 3, tags = ['merge', 'highway', 'fix:chair', 'fix:survey'],
title = T_('Gas station update'))
self.init(
'https://www.mise.gov.it/index.php/it/open-data/elenco-dataset/2032336-carburanti-prezzi-praticati-e-anagrafica-degli-impianti',
'MISE - Ministero Sviluppo Economico',
CSV(Source_Fuel(Source(attribution = 'MISE - Ministero Sviluppo Economico', fileUrl = 'https://www.mise.gov.it/images/exportCSV/anagrafica_impianti_attivi.csv',
filter = lambda t: t.replace(''', '\'')),
fileUrl = 'https://www.mise.gov.it/images/exportCSV/prezzo_alle_8.csv')),
Load_XY('Longitudine', 'Latitudine',
where = lambda row: row['Longitudine'] != 'NULL' and row['Latitudine'] != 'NULL'),
Conflate(
select = Select(
types = ['nodes', 'ways'],
tags = {'amenity': 'fuel'}),
osmRef = 'ref:mise',
conflationDistance = 50,
mapping = Mapping(
static1 = {'amenity': 'fuel'},
static2 = {'source': self.source},
mapping1 = {
'ref:mise': 'idImpianto',
'brand': lambda res: res['Bandiera'] if res['Bandiera'] != 'Pompe Bianche' else Mapping.delete_tag,
'fuel:octane_95': lambda res: 'yes' if (int(res['Carburanti']) & OCTANE_95) != 0 else Mapping.delete_tag,
'fuel:octane_98': lambda res: 'yes' if (int(res['Carburanti']) & OCTANE_98) != 0 else Mapping.delete_tag,
'fuel:octane_100': lambda res: 'yes' if (int(res['Carburanti']) & OCTANE_100) != 0 else Mapping.delete_tag,
'fuel:diesel': lambda res: 'yes' if (int(res['Carburanti']) & DIESEL) != 0 else Mapping.delete_tag,
'fuel:diesel:class2': lambda res: 'yes' if (int(res['Carburanti']) & DIESEL_CL2) != 0 else Mapping.delete_tag,
'fuel:GTL_diesel': lambda res: 'yes' if (int(res['Carburanti']) & GTL_DIESEL) != 0 else Mapping.delete_tag,
'fuel:HGV_diesel': lambda res: 'yes' if (int(res['Carburanti']) & HGV_DIESEL) != 0 else Mapping.delete_tag,
'fuel:lng': lambda res: 'yes' if (int(res['Carburanti']) & LNG) != 0 else Mapping.delete_tag,
'fuel:lpg': lambda res: 'yes' if (int(res['Carburanti']) & LPG) != 0 else Mapping.delete_tag,
'fuel:cng': lambda res: 'yes' if (int(res['Carburanti']) & CNG) != 0 else Mapping.delete_tag,
},
mapping2 = {
'operator': lambda res: italian_strings.normalize_common(res['Gestore']).replace(' % ', ' - ') if res['Gestore'] else None,
},
text = lambda tags, fields: {'en': '{0}, {1}'.format(fields['Indirizzo'], fields['Comune'])} )))
class Source_Fuel(Source):
def __init__(self, source, fileUrl):
self.source = source
self.fileUrl = fileUrl
def __getattr__(self, name):
return getattr(self.source, name)
def open(self):
return open(downloader.update_cache('join://' + self.source.fileUrl, 15, fetch=self.fetch))
def fetch(self, url, tmp_file, date_string=None):
f = downloader.urlopen(self.fileUrl, 15)
# 0 1 2 3 4
#idImpianto;descCarburante;prezzo;isSelf;dtComu
csvreader = csv.reader(f, delimiter=';')
next(csvreader) # Skip date
next(csvreader) # Skip header
impianti = {}
for row in csvreader:
impianto = impianti.get(row[0], 0)
carburante = self.FUEL_TYPE_MAP.get(row[1].upper())
if not carburante or (impianto & carburante) != 0:
continue
dt_price = Mapping.date_format(row[4], '%d/%m/%Y %H:%M:%S')
if not dt_price or self.diff_days(dt_price) > 30:
continue
impianto |= carburante
impianti[row[0]] = impianto
csvfile = open(tmp_file, 'w', encoding='utf-8')
writer = csv.writer(csvfile)
f = self.source.open()
csvreader = csv.reader(f, delimiter=';', quotechar = '~')
next(csvreader) # Skip date
header = next(csvreader)
writer.writerow(header + ['Carburanti'])
for row in csvreader:
if len(row) != 10:
continue
impianto = impianti.get(row[0])
if impianto:
writer.writerow(row + [ impianto ])
csvfile.seek(0)
return csvfile
FUEL_TYPE_MAP = {
'BENZINA': OCTANE_95,
'BENZINA 100 OTTANI': OCTANE_100,
'BENZINA ENERGY 98 OTTANI': OCTANE_98,
'BENZINA PLUS 98': OCTANE_98,
'BENZINA SHELL V POWER': OCTANE_100,
'BENZINA SPECIALE': OCTANE_100,
'BENZINA WR 100': OCTANE_100,
'BLU DIESEL ALPINO': DIESEL_CL2,
'BLUE DIESEL': GTL_DIESEL,
'BLUE SUPER': OCTANE_100,
'DIESEL E+10': GTL_DIESEL,# repsol
'DIESELMAX': GTL_DIESEL,
'DIESEL SHELL V POWER': GTL_DIESEL,
'E-DIESEL': HGV_DIESEL,# esso
'EXCELLIUM DIESEL': GTL_DIESEL,
'F101': OCTANE_100,
'GASOLIO': DIESEL,
'GASOLIO ALPINO': DIESEL_CL2,
'GASOLIO ARTICO': DIESEL_CL2,
'GASOLIO ECOPLUS': DIESEL,
'GASOLIO ENERGY D': HGV_DIESEL,
'GASOLIO GELO': DIESEL_CL2,
'GASOLIO ORO DIESEL': GTL_DIESEL,
'GASOLIO PREMIUM': GTL_DIESEL,
'GASOLIO SPECIALE': GTL_DIESEL,
'GNL': LNG,
'GP DIESEL': GTL_DIESEL,
'GPL': LPG,
'HI-Q DIESEL': GTL_DIESEL,
'HIQ PERFORM+': OCTANE_100,
'L-GNC': LNG,
'MAGIC DIESEL': HGV_DIESEL,
'METANO': CNG,
'R100': OCTANE_100,# repsol
'S-DIESEL': GTL_DIESEL,# ?
'SSP98': OCTANE_98,# ?
'SUPREME DIESEL': GTL_DIESEL,# esso
'V-POWER': OCTANE_100,
'V-POWER DIESEL': GTL_DIESEL,
}
def diff_days(self, date_string):
d1 = datetime.datetime.strptime(date_string, '%Y-%m-%d').date()
d2 = datetime.date.today()
return abs((d2 - d1).days)