Generazione file in formato umap con raggruppamento per carburante

This commit is contained in:
Fabio Lovato 2022-07-31 21:58:47 +02:00
parent 7cacc28c2c
commit e146666f17
2 changed files with 183 additions and 34 deletions

View File

@ -1,7 +1,7 @@
<?php
include 'vendor/autoload.php';
/*
$client = new \GuzzleHttp\Client();
$response = $client->request('GET', 'http://www.mise.gov.it/images/exportCSV/anagrafica_impianti_attivi.csv');
@ -9,11 +9,9 @@ file_put_contents('anagrafica_impianti_attivi.csv', $response->getBody() );
$response = $client->request('GET', 'http://www.mise.gov.it/images/exportCSV/prezzo_alle_8.csv');
file_put_contents('prezzo_alle_8.csv', $response->getBody());
// Header file geojson finale
$geojson = [
"type" => "FeatureCollection",
];
*/
// Lettura template file umap
$umap = json_decode( file_get_contents('template.umap'), 1 );
// Lettura 2 CSV
$csvDistributori = fopen('anagrafica_impianti_attivi.csv', 'r');
@ -22,6 +20,71 @@ $csvPrezzi = fopen('prezzo_alle_8.csv', 'r');
// Cache prezzi per distributore
$prezzi = [];
$prezzi_per_tipo = [];
/**
* Raggruppamento varie tipologie di benzina per evitare di creare troppi
* livelli e di facilitare anche la ricerca all'utente
*/
$raggruppamenti_iniziali = [
'BENZINA' => 'BENZINA',
'GPL' => 'GPL',
'BLUE DIESEL' => 'DIESEL',
'HI-Q DIESEL' => 'DIESEL',
'HIQ PERFORM+' => 'DIESEL',
'V-POWER' => 'BENZINA',
'GASOLIO' => 'GASOLIO',
'GASOLIO PREMIUM' => 'GASOLIO',
'SUPREME DIESEL' => 'DIESEL',
'METANO' => 'METANO',
'BENZINA SPECIALE' => 'BENZINA',
'BLUE SUPER' => 'BENZINA',
'L-GNC' => 'L-GNC',
'GNL' => 'GAS',
'GASOLIO ARTICO' => 'GASOLIO',
'BENZINA WR 100' => 'BENZINA',
'GASOLIO SPECIALE' => 'GASOLIO',
'EXCELLIUM DIESEL' => 'DIESEL',
'BENZINA PLUS 98' => 'BENZINA',
'DIESEL SHELL V POWER' => 'DIESEL',
'E-DIESEL' => 'DIESEL',
'R100' => 'BENZINA',
'GASOLIO ORO DIESEL' => 'GASOLIO',
'DIESELMAX' => 'DIESEL',
'GASOLIO ALPINO' => 'GASOLIO',
'BLU DIESEL ALPINO' => 'DIESEL',
'GASOLIO GELO' => 'GASOLIO',
'GASOLIO ECOPLUS' => 'GASOLIO',
'S-DIESEL' => 'DIESEL',
'V-POWER DIESEL' => 'DIESEL',
'DIESEL E+10' => 'DIESEL',
'GP DIESEL' => 'DIESEL',
'F101' => 'BENZINA',
'BENZINA 100 OTTANI' => 'BENZINA',
'GASOLIO ENERGY D' => 'GASOLIO',
'BENZINA ENERGY 98 OTTANI' => 'BENZINA',
'BENZINA SHELL V POWER' => 'BENZINA',
'SSP98' => 'BENZINA'
];
$raggruppamenti = [];
// Aggiunta (self) e (servito) ai tipi
foreach ($raggruppamenti_iniziali as $tipo => $raggruppamento) {
$raggruppamenti[$tipo.' (self)'] = $raggruppamento;
$raggruppamenti[$tipo.' (servito)'] = $raggruppamento;
}
$colori_per_tipo = [
'BENZINA' => '#119429',
'GPL' => '#d9c800',
'DIESEL' => '#193cbf',
'GASOLIO' => '#0636a5',
'METANO' => '#067057',
'GAS' => '#193e92',
'L-GNC' => '#193e92',
];
// Salto le 2 inutili prime righe
fgetcsv($csvPrezzi);
@ -48,8 +111,8 @@ fclose($csvPrezzi);
// Salto le 2 inutili prime righe
fgetcsv($csvDistributori);
fgetcsv($csvDistributori);
// Costruzione dati geojson
// Costruzione dati umap
while(($line = fgetcsv($csvDistributori, 0, ';')) !== FALSE){
$idimpianto = $line[0];
$name = $line[2];
@ -65,43 +128,83 @@ while(($line = fgetcsv($csvDistributori, 0, ';')) !== FALSE){
if (isset($prezzi[$idimpianto])) {
foreach ($prezzi[$idimpianto] as $idx => $prezzo) {
$description[] = '**'.$prezzo['tipo'].($prezzo['isSelf'] ? ' (self)' : '').':**
'.number_format($prezzo['prezzo'], 3, ',', '.').'
';
$tipo = $prezzo['tipo'].($prezzo['isSelf'] ? ' (self)' : ' (servito)');
$tipo_raggruppato = $raggruppamenti[ $tipo ];
// Tengo lo stesso per il diverso tipo di carburante
$ultimo_aggiornamento = $prezzo['ultimo_aggiornamento'];
$prezzi_per_tipo[$tipo_raggruppato][$idimpianto][$tipo] = [
'idimpianto' => $idimpianto,
'ultimo_aggiornamento' => $ultimo_aggiornamento,
'nome' => $name,
'prezzo' => (float)$prezzo['prezzo'],
'lat' => $lat,
'lon' => $lon
];
}
}
$description[] = 'Ultimo aggiornamento: '.$ultimo_aggiornamento;
$geojson['features'][] = [
"type" => "Feature",
"properties" => [
"idImpianto" => $idimpianto,
"description" => implode("\n", $description),
"name" => $name
],
"geometry" => [
"type" => "Point",
"coordinates" => [
$lon,
$lat
]
]
];
}
}
// Chiusura CSV originale
fclose($csvDistributori);
$json_response = json_encode($geojson, JSON_PRETTY_PRINT);
$layers = [];
// Salvataggio file geojson
// Generazione file per layer
foreach ($prezzi_per_tipo as $tipo_raggruppato => $idimpianti) {
$markers = [];
foreach ($idimpianti as $idimpianto => $tipi) {
$prezzi = [];
$descriptions = [];
foreach ($tipi as $tipo => $impianto) {
$prezzi[] = $impianto['prezzo'];
$descriptions[] = $tipo.': '.$impianto['prezzo']."\n";
}
$markers[] = [
"type" => "Feature",
"properties" => [
"idImpianto" => $idimpianto,
'description' => "**".$impianto['nome']."**\n".implode("\n", $descriptions)."\n*Ultimo agg.: ".$ultimo_aggiornamento.'*',
'name' => min($prezzi).' - '.max($prezzi).' €'
],
"geometry" => [
"type" => "Point",
"coordinates" => [
$impianto['lon'],
$impianto['lat']
]
]
];
}
$layers[] = [
'type' => 'FeatureCollection',
'features' => [
$markers
],
'_umap_options' => [
'displayOnLoad' => false,
'browsable' => true,
'name' => $tipo_raggruppato,
'color' => $colori_per_tipo[$tipo_raggruppato],
'type' => 'Cluster',
'cluster' => [
'radius' => 80
]
]
];
}
$umap['layers'] = $layers;
$json_response = json_encode($umap, JSON_PRETTY_PRINT);
// Salvataggio file umap
if (isset($_GET['response'])) {
echo $json_response;
} else {
file_put_contents( 'data.geojson', $json_response );
file_put_contents( 'data.umap', $json_response );
}

46
template.umap Normal file
View File

@ -0,0 +1,46 @@
{
"type": "umap",
"uri": "https://umap.openstreetmap.fr/it/map/prezzi-distributori-italia_792885#8/44.721/12.063",
"properties": {
"easing": true,
"measureControl": null,
"embedControl": true,
"fullscreenControl": true,
"locateControl": true,
"searchControl": true,
"datalayersControl": true,
"zoomControl": true,
"longCredit": "Open Data del MISE:\nhttps://www.mise.gov.it/index.php/it/open-data/elenco-dataset/2032336-carburanti-prezzi-praticati-e-anagrafica-degli-impianti",
"shortCredit": "MISE",
"showLabel": true,
"slideshow": [],
"captionBar": false,
"zoomTo": 14,
"popupContentTemplate": "# {name}\n{description}",
"popupShape": "Large",
"smoothFactor": "2.5",
"iconUrl": "/uploads/pictogram/fuel-24_1.png",
"iconClass": "Drop",
"color": "Blue",
"limitBounds": [],
"tilelayer": [],
"licence": "",
"description": "# ISTRUZIONI\n1. Diminuisci l'area di visualizzazione centrando la mappa nella tua posizione\n2. Clicca sull'icona simile a dei dischi sovrapposti per scegliere il tipo di carburante\n3. Trova il distributore più vantaggioso 😉\nATTENZIONE: la mappa contiene tutti i distributori d'Italia per cui alla selezione del tipo di carburante potrebbe essere un po' lento a caricare i dati.\n",
"name": "Distributori prezzi Italia",
"onLoadPanel": "caption",
"displayPopupFooter": false,
"miniMap": false,
"moreControl": true,
"scaleControl": true,
"scrollWheelZoom": true,
"zoom": 6
},
"geometry": {
"type": "Point",
"coordinates": [
12.063,
44.721
]
},
"layers": []
}