File: /var/www/elite/coordParser/nextGisParse.php
<?php
use coordParser\CommimAddress;
use coordParser\CommimAddressModel;
use coordParser\NextGisParser;
ini_set('max_execution_time', '-1');
ini_set('display_errors', '1');
require __DIR__ . '/../vendor/autoload.php';
try {
$commimAddress = new CommimAddress();
} catch (Throwable $exception) {
print_r($exception->getMessage() . PHP_EOL);
die;
}
$filter = [
'type' => CommimAddressModel::TYPE_PLOT,
'minArea' => 500,
];
$addresses = $commimAddress->findWithOutPolygon($filter);
if (empty($addresses)) {
print_r('Для всех адресов получены координаты' . PHP_EOL);
return;
}
print_r('Найдено адресов без координат: ' . count($addresses) . PHP_EOL);
$nextGisParser = new NextGisParser();
$success = 0;
foreach ($addresses as $address) {
if ($nextGisParser->getLimitRemain() <= 0) {
print_r('На текущий час всё' . PHP_EOL);
break;
}
print_r('Проверяем ' . $address->obj_kd_nmb . PHP_EOL);
$result = $nextGisParser->getById($address->obj_kd_nmb);
if (empty($result->features)) {
print_r('Ничего не найдено' . PHP_EOL);
$commimAddress->updateNextGisCoordinates($address->obj_id, '[]', 0, 0);
$nextGisParser->adviceError();
continue;
}
$features = $result->features;
if (empty($features[0]->geometry->coordinates)) {
print_r('Координаты не найдены' . PHP_EOL);
$commimAddress->updateNextGisCoordinates($address->obj_id, '[]', 0, 0);
$nextGisParser->adviceError();
continue;
}
$coordinates = $features[0]->geometry->coordinates;
$center = [0, 0, 0];
foreach ($coordinates as &$polygons) {
foreach ($polygons as &$polygon) {
foreach ($polygon as &$coordinate) {
$center[0] += $coordinate[0];
$center[1] += $coordinate[1];
$center[2]++;
[$coordinate[0], $coordinate[1]] = [$coordinate[1], $coordinate[0]];
}
}
}
$center[0] = $center[0] / $center[2];
$center[1] = $center[1] / $center[2];
unset($center[2]);
$success++;
$commimAddress->updateNextGisCoordinates($address->obj_id, json_encode($coordinates), $center[0], $center[1]);
}
print_r('Успешно обновлено: ' . $success . PHP_EOL);