ImportCities.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Place;
  5. use DB;
  6. use Illuminate\Support\Str;
  7. class ImportCities extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'import:cities {chunk=1000}';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = 'Import Cities to database';
  21. /**
  22. * Checksum of city dataset.
  23. *
  24. */
  25. const CHECKSUM = 'e203c0247538788b2a91166c7cf4b95f58291d998f514e9306d315aa72b09e48bfd3ddf310bf737afc4eefadca9083b8ff796c67796c6bd8e882a3d268bd16af';
  26. /**
  27. * List of shortened countries.
  28. *
  29. * @var array
  30. */
  31. protected $countries = [
  32. 'AE' => 'UAE',
  33. 'BA' => 'Bosnia-Herzegovina',
  34. 'BO' => 'Bolivia',
  35. 'CD' => 'Democratic Republic of Congo',
  36. 'CG' => 'Republic of Congo',
  37. 'FM' => 'Micronesia',
  38. 'GB' => 'United Kingdom',
  39. 'IR' => 'Iran',
  40. 'KP' => 'DRPK',
  41. 'KR' => 'South Korea',
  42. 'LA' => 'Laos',
  43. 'MD' => 'Moldova',
  44. 'PS' => 'Palestine',
  45. 'RU' => 'Russia',
  46. 'SH' => 'Saint Helena',
  47. 'SY' => 'Syria',
  48. 'TW' => 'Taiwan',
  49. 'TZ' => 'Tanzania',
  50. 'US' => 'USA',
  51. 'VE' => 'Venezuela',
  52. 'XK' => 'Kosovo'
  53. ];
  54. /**
  55. * Create a new command instance.
  56. *
  57. * @return void
  58. */
  59. public function __construct()
  60. {
  61. parent::__construct();
  62. }
  63. /**
  64. * Execute the console command.
  65. *
  66. * @return mixed
  67. */
  68. public function handle()
  69. {
  70. $old_memory_limit = ini_get('memory_limit');
  71. ini_set('memory_limit', '256M');
  72. $path = storage_path('app/cities.json');
  73. if(hash_file('sha512', $path) !== self::CHECKSUM) {
  74. $this->error('Invalid or corrupt storage/app/cities.json data.');
  75. $this->line('');
  76. $this->info('Run the following command to fix:');
  77. $this->info('git checkout storage/app/cities.json');
  78. return;
  79. }
  80. if (!is_file($path)) {
  81. $this->error('Missing storage/app/cities.json file!');
  82. return;
  83. }
  84. if (Place::count() > 0) {
  85. DB::table('places')->truncate();
  86. }
  87. $this->info('Importing city data into database ...');
  88. $cities = json_decode(file_get_contents($path));
  89. $cityCount = count($cities);
  90. $this->line('');
  91. $this->info("Found {$cityCount} cities to insert ...");
  92. $this->line('');
  93. $bar = $this->output->createProgressBar($cityCount);
  94. $bar->start();
  95. $buffer = [];
  96. $count = 0;
  97. foreach ($cities as $city) {
  98. $buffer[] = [
  99. "name" => $city->name,
  100. "slug" => Str::slug($city->name),
  101. "country" => $this->codeToCountry($city->country),
  102. "lat" => $city->lat,
  103. "long" => $city->lng
  104. ];
  105. $count++;
  106. if ($count % $this->argument('chunk') == 0) {
  107. $this->insertBuffer($buffer);
  108. $bar->advance(count($buffer));
  109. $buffer = [];
  110. }
  111. }
  112. $this->insertBuffer($buffer);
  113. $bar->advance(count($buffer));
  114. $bar->finish();
  115. $this->line('');
  116. $this->line('');
  117. $this->info('Successfully imported ' . $cityCount . ' entries!');
  118. $this->line('');
  119. ini_set('memory_limit', $old_memory_limit);
  120. return;
  121. }
  122. private function insertBuffer($buffer)
  123. {
  124. DB::table('places')->insert($buffer);
  125. }
  126. private function codeToCountry($code)
  127. {
  128. $countries = $this->countries;
  129. if(isset($countries[$code])) {
  130. return $countries[$code];
  131. }
  132. $country = (new \League\ISO3166\ISO3166)->alpha2($code);
  133. $this->countries[$code] = $country['name'];
  134. return $this->countries[$code];
  135. }
  136. }