Database with real data - initial version

master
Goncalo Bras 2017-07-28 20:42:30 +01:00
parent 789dc61be0
commit f643f2d716
4 changed files with 168 additions and 28 deletions

10
laravel/app/FuelPrice.php Normal file
View File

@ -0,0 +1,10 @@
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class FuelPrice extends Model
{
protected $table = 'fuel_price';
}

10
laravel/app/Location.php Normal file
View File

@ -0,0 +1,10 @@
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Location extends Model
{
protected $table = "location";
}

View File

@ -15,18 +15,12 @@ class CreateFuelPriceTable extends Migration
{
Schema::create('fuel_price', function (Blueprint $table) {
$table->increments('id');
$table->decimal('diesel', 4, 3);
$table->decimal('diesel_colored', 4, 3);
$table->decimal('diesel_special', 4, 3);
$table->decimal('diesel_simple', 4, 3);
$table->decimal('petrol_95_simple', 4, 3);
$table->decimal('petrol_95', 4, 3);
$table->decimal('petrol_98_simple', 4, 3);
$table->decimal('petrol_98', 4, 3);
$table->decimal('petrol_special_95', 4, 3);
$table->decimal('petrol_special_98', 4, 3);
$table->decimal('petrol_simple_95', 4, 3);
$table->decimal('gas_natural_compressed_kg', 4, 3);
$table->decimal('gas_natural_compressed_m3', 4, 3);
$table->decimal('gas_natural_liquefied', 4, 3);
$table->decimal('diesel_simple', 4, 3);
$table->decimal('diesel', 4, 3);
$table->decimal('gpl', 4, 3);
});
}

View File

@ -1,6 +1,10 @@
<?php
use Illuminate\Database\Seeder;
use App\Station;
use App\Location;
use App\FuelPrice;
use App\District;
class AllTableSeeder extends Seeder
{
@ -11,25 +15,118 @@ class AllTableSeeder extends Seeder
*/
public function run()
{
$content = file_get_contents('public/files/Postos-Alves-Bandeira.geojson');
$content .= file_get_contents('public/files/Postos-BP.geojson');
$content .= file_get_contents('public/files/Postos-Cepsa.geojson');
$content .= file_get_contents('public/files/Postos-E.Leclerc.geojson');
$content .= file_get_contents('public/files/Postos-Ecobrent.geojson');
$content .= file_get_contents('public/files/Postos-Galp.geojson');
$content .= file_get_contents('public/files/Postos-Intermarche.geojson');
$content .= file_get_contents('public/files/Postos-Jumbo.geojson');
$content .= file_get_contents('public/files/Postos-Outras-Marcas.geojson');
$content .= file_get_contents('public/files/Postos-OZ-Energia.geojson');
$content .= file_get_contents('public/files/Postos-Pingo-Doce.geojson');
$content .= file_get_contents('public/files/Postos-Prio.geojson');
$content .= file_get_contents('public/files/Postos-Rede-Energia.geojson');
$content .= file_get_contents('public/files/Postos-Repsol.geojson');
$content .= file_get_contents('public/files/Postos-Total.geojson');
$array = json_decode($content, true);
for($i = 0; $i < 15; $i++){
echo $array;
$all = array();
array_push($all, file_get_contents('public/files/Postos-Alves-Bandeira.geojson'));
array_push($all, file_get_contents('public/files/Postos-BP.geojson'));
array_push($all, file_get_contents('public/files/Postos-Cepsa.geojson'));
array_push($all, file_get_contents('public/files/Postos-E.Leclerc.geojson'));
array_push($all, file_get_contents('public/files/Postos-Ecobrent.geojson'));
array_push($all, file_get_contents('public/files/Postos-Galp.geojson'));
array_push($all, file_get_contents('public/files/Postos-Intermarche.geojson'));
array_push($all, file_get_contents('public/files/Postos-Jumbo.geojson'));
array_push($all, file_get_contents('public/files/Postos-Outras-Marcas.geojson'));
array_push($all, file_get_contents('public/files/Postos-OZ-Energia.geojson'));
array_push($all, file_get_contents('public/files/Postos-Pingo-Doce.geojson'));
array_push($all, file_get_contents('public/files/Postos-Prio.geojson'));
array_push($all, file_get_contents('public/files/Postos-Rede-Energia.geojson'));
array_push($all, file_get_contents('public/files/Postos-Repsol.geojson'));
array_push($all, file_get_contents('public/files/Postos-Total.geojson'));
$counter = 0;
foreach ($all as $station) {
$object = json_decode($station);
foreach($object->features as $feature){
$counter++;
$nameBrand = $feature->properties->Name;
$description = $feature->properties->description;
$descriptionString = html_entity_decode($description, ENT_QUOTES);
$descriptionString = preg_replace("/(<div[^>]*>)(.*?)(<\/div>)/i", '$2', $descriptionString);
// Convert HTML entities to characters
// Remove characters other than the specified list.
/*FUEL TYPES*/
preg_match_all('/(title=")(.*?)(")/', $descriptionString, $fuel);
//print_r($fuel[2]);
/*PRICES*/
$fuelPriceArray = array();//main array
$fuelKeys = array();//fuel => price array
/*foreach($fuel[2] as $key => $value){
echo $value;
}
array_push($fuelPriceArray, $fuelKeys);
*/
preg_match_all('/\d.\d\d\d+/', $descriptionString, $priceMatches);
$fuelPriceArray = $this->array_combine2($fuel[2], $priceMatches[0]);
$fuelPrices = array("petrol_95_simple" => 0, "petrol_95" => 0, "petrol_98_simple" => 0, "petrol_98" => 0,"diesel_simple" => 0, "diesel" => 0, "gpl" => 0);
foreach($fuelPriceArray as $key => $value){
//echo "$key - $value\n";
if($key == "Gasolina 95 Simples")
$fuelPrices["petrol_95_simple"] = $value;
if($key == "Gasolina 95 +")
$fuelPrices["petrol_95"] = $value;
if($key == "Gasolina 98 Simples")
$fuelPrices["petrol_98_simple"] = $value;
if($key == "Gasolina 98 +")
$fuelPrices["petrol_98"] = $value;
if($key == "Gasóleo Simples")
$fuelPrices["diesel_simple"] = $value;
if($key == "Gasóleo +")
$fuelPrices["diesel"] = $value;
if($key == "GPL Auto")
$fuelPrices["gpl"] = $value;
}
$priceId = FuelPrice::insertGetId($fuelPrices);
/*
foreach($priceMatches[0] as $key => $value){
echo $value;
}*/
/*Coordinates and Insert Coordinates*/
$latitude = $feature->geometry->coordinates[1];
$longitude = $feature->geometry->coordinates[0];
$locationId = Location::insertGetId(['latitude' => $latitude, 'longitude' => $longitude]);
//insert district
$districtId = $this->associateDistrict($locationId);
//Name and Brand
preg_match_all('/()(.*?)( - )/', $nameBrand, $brand);
preg_match_all('/( - )(.*?)($)/', $nameBrand, $name);
//Insert Station
if(isset($name[2][0]) && isset($brand[2][0]))
Station::insert(['name' => $name[2][0], 'brand' => $brand[2][0], 'location' => $locationId, 'district' => $districtId,'fuel_price' => $priceId, 'services' => 0, 'last_update' => new DateTime("now"),'schedule' => 0]);
}
}
//echo $obj->features;
/*for($i = 0; $i < 15; $i++){
}*/
//for ($i = 0; $i < DB::table('location')->count(); $i++) {
@ -54,4 +151,33 @@ class AllTableSeeder extends Seeder
//}
}
private function array_combine2($arr1, $arr2) {
$count = min(count($arr1), count($arr2));
return array_combine(array_slice($arr1, 0, $count), array_slice($arr2, 0, $count));
}
public function associateDistrict($id)
{
$location = Location::where('id', $id)->first();
$link = "http://maps.google.com/maps/api/geocode/json?address=$location->latitude,$location->longitude";
$data = file_get_contents($link);
$json = json_decode($data, true);
if(isset($json['results'][0]['address_components'][1]['long_name'])){
$districtString = $json['results'][0]['address_components'][1]['long_name'];
$districtName = trim(str_replace('district', '', $districtString)); //Distrito em texto
if($districtName=='Lisbon'){
$district = District::where('name', 'like', "Lisboa")->first();
}else{
$district = District::where('name', 'like', "%$districtName%")->first();
}
if(isset($district)){
return $district->id;
}else{
$districtId = District::insertGetId(['name'=>$districtName]);
return $districtId;
}
}return 1;
}
}