1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Turahe\Master\Commands; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Exception\ServerException; |
6
|
|
|
use Illuminate\Console\Command; |
7
|
|
|
use Turahe\Master\Models\City; |
8
|
|
|
use Turahe\Master\Models\District; |
9
|
|
|
use Turahe\Master\Models\Province; |
10
|
|
|
use Turahe\Master\Models\Village; |
11
|
|
|
|
12
|
|
|
class SyncCoordinateCommand extends Command |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* The name and signature of the console command. |
16
|
|
|
* |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $signature = 'turahe:master:sync-coordinate'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The console command description. |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $description = 'Synchronize latitude longitude data in database directly using Google\'s geocoding service'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Create a new command instance. |
30
|
|
|
* |
31
|
|
|
* @return void |
32
|
|
|
*/ |
33
|
|
|
public function __construct() |
34
|
|
|
{ |
35
|
|
|
parent::__construct(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Execute the console command. |
40
|
|
|
* |
41
|
|
|
* @return mixed |
42
|
|
|
*/ |
43
|
|
|
public function handle() |
44
|
|
|
{ |
45
|
|
|
$count = Province::count(); |
46
|
|
|
$this->info("Processing {$count} Province"); |
47
|
|
|
$bar = $this->output->createProgressBar($count); |
|
|
|
|
48
|
|
|
$bar->start(); |
49
|
|
|
Province::cursor()->each(function ($item) use ($bar) { |
50
|
|
|
$meta = $item->meta; |
51
|
|
|
$geocoding = \Geocoder::getCoordinatesForAddress($item->address); |
|
|
|
|
52
|
|
|
$meta['lat'] = $geocoding['lat'] ?? null; |
53
|
|
|
$meta['long'] = $geocoding['lng'] ?? null; |
54
|
|
|
$item->meta = $meta; |
55
|
|
|
$item->save(); |
56
|
|
|
$bar->advance(); |
57
|
|
|
}); |
58
|
|
|
|
59
|
|
|
$this->line(''); |
60
|
|
|
|
61
|
|
|
$count = City::count(); |
62
|
|
|
$this->info("Processing {$count} City"); |
63
|
|
|
$bar = $this->output->createProgressBar($count); |
|
|
|
|
64
|
|
|
$bar->start(); |
65
|
|
|
City::with('provinsi')->cursor()->each(function ($item) use ($bar) { |
66
|
|
|
$meta = $item->meta; |
67
|
|
|
$geocoding = \Geocoder::getCoordinatesForAddress($item->address); |
68
|
|
|
$meta['lat'] = $geocoding['lat'] ?? null; |
69
|
|
|
$meta['long'] = $geocoding['lng'] ?? null; |
70
|
|
|
$item->meta = $meta; |
71
|
|
|
$item->save(); |
72
|
|
|
$bar->advance(); |
73
|
|
|
}); |
74
|
|
|
|
75
|
|
|
$this->line(''); |
76
|
|
|
|
77
|
|
|
$count = District::count(); |
78
|
|
|
$this->info("Processing {$count} District"); |
79
|
|
|
$bar = $this->output->createProgressBar($count); |
|
|
|
|
80
|
|
|
$bar->start(); |
81
|
|
|
District::with('kabupaten.provinsi')->cursor()->each(function ($item) use ($bar) { |
82
|
|
|
$meta = $item->meta; |
83
|
|
|
$geocoding = \Geocoder::getCoordinatesForAddress($item->address); |
84
|
|
|
$meta['lat'] = $geocoding['lat'] ?? null; |
85
|
|
|
$meta['long'] = $geocoding['lng'] ?? null; |
86
|
|
|
$item->meta = $meta; |
87
|
|
|
$item->save(); |
88
|
|
|
$bar->advance(); |
89
|
|
|
}); |
90
|
|
|
|
91
|
|
|
$this->line(''); |
92
|
|
|
|
93
|
|
|
$count = Village::whereNull('meta')->count(); |
94
|
|
|
$this->info("Processing {$count} Village"); |
95
|
|
|
$bar = $this->output->createProgressBar($count); |
|
|
|
|
96
|
|
|
$bar->start(); |
97
|
|
|
Village::whereNull('meta')->cursor()->each(function ($item) use ($bar) { |
98
|
|
|
$meta = $item->meta; |
99
|
|
|
if (!$meta) { |
100
|
|
|
try { |
101
|
|
|
$geocoding = \Geocoder::getCoordinatesForAddress($item->address); |
102
|
|
|
$meta['lat'] = $geocoding['lat'] ?? null; |
103
|
|
|
$meta['long'] = $geocoding['lng'] ?? null; |
104
|
|
|
$item->meta = $meta; |
105
|
|
|
$item->save(); |
106
|
|
|
} catch (ServerException $e) { |
107
|
|
|
$this->error($e->getMessage()); |
108
|
|
|
sleep(1); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
$bar->advance(); |
112
|
|
|
}); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|