示例#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;
                         }
                     }
                 }
             }
         }
     }
 }
示例#2
0
 private function stealBranchAddress($branch, $record_id)
 {
     if (empty($branch->point)) {
         return;
     }
     $record = new CitymapAddress(['itemid' => $record_id, 'place_lat' => $branch->point->lat, 'place_lng' => $branch->point->lon, 'status' => 0, 'place' => '', 'place_address' => '']);
     if (isset($branch->adm_div[0])) {
         $record->place .= trim(preg_replace('/г\\./', '', $branch->adm_div[0]->name));
     } else {
         $record->place .= $this->current_region->name;
     }
     $record->place_address = $record->place;
     // это судя по всему регион или что то вроде этого
     $record->place .= ', улица ' . $branch->address_name;
     if (!$record->save()) {
         $this->_saveErrors($record->errors);
     }
 }