示例#1
0
 function actionFix()
 {
     $count = CitymapAddress::find()->select('itemid')->where('areaid IS NULL')->count();
     $this->_report("Найдено {$count} записей с незаполненным areaid");
     //dgis
     $dgis_regions_response = json_decode(file_get_contents("http://catalog.api.2gis.ru/2.0/region/list?key=ruffzo9376&locale_filter=ru_RU&fields=items.settlements"));
     $dgis_regions = [];
     foreach ($dgis_regions_response->result->items as $region) {
         $dgis_regions[$region->name] = $region->name;
         if (!empty($region->settlements)) {
             foreach ($region->settlements as $settlement) {
                 $dgis_regions[$settlement] = $region->name;
             }
         }
     }
     //$count = 5085285;
     $size = 1000;
     $pages = ceil($count / $size);
     $geos = [];
     for ($i = 0; $i <= $pages; $i++) {
         $this->_report("Страница {$i} из {$pages}");
         $records = CitymapAddress::find()->where('areaid IS NULL')->limit($size)->offset($i * $size)->all();
         foreach ($records as $rec) {
             $short_lat = substr($rec->place_lat, 0, 2);
             $short_lng = substr($rec->place_lng, 0, 2);
             $code = $short_lng . '#' . $short_lat;
             if (empty($geos[$code])) {
                 $this->_report("Обновляем {$code}");
                 $city = "";
                 $query = "https://geocode-maps.yandex.ru/1.x/?format=json&geocode=" . $rec->place_lng . ',' . $rec->place_lat;
                 $geocode = json_decode(file_get_contents($query));
                 if (!$geocode->response->GeoObjectCollection->metaDataProperty->GeocoderResponseMetaData->found) {
                     $this->_report($code . ' не найдено ничего по запросу (0 results) $query');
                     continue;
                 }
                 //$this->_report("Ищем город по координатам");
                 $features = $geocode->response->GeoObjectCollection->featureMember;
                 foreach ($features as $feature) {
                     if ($feature->GeoObject->metaDataProperty->GeocoderMetaData->kind == 'locality') {
                         if (isset($dgis_regions[$feature->GeoObject->name]) && ($city = Place::findOne(['name' => $dgis_regions[$feature->GeoObject->name]]))) {
                             //$this->_report("Записываем в базу {$feature->GeoObject->name} areaid {$city->areaid}");
                             $updated = CitymapAddress::updateAll(['areaid' => $city->areaid], "areaid IS NULL AND place_lat LIKE '{$short_lat}%' AND place_lng LIKE '{$short_lng}%'");
                             $ct = $dgis_regions[$feature->GeoObject->name];
                             $this->_report("Записано: {$updated} город {$ct}");
                             $geos[$code] = $city->name;
                             break;
                         } else {
                             $this->_report("Locality {$feature->GeoObject->name} найден, но в базе тугис такой отсутствует {$query}");
                             break;
                         }
                     }
                 }
             }
         }
     }
 }
 /**
  * Finds the Place model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return Place the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = Place::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }