107 lines
4.6 KiB
PHP
107 lines
4.6 KiB
PHP
|
|
<?php
|
|||
|
|
|
|||
|
|
set_time_limit(0);
|
|||
|
|
error_reporting(E_ALL & ~E_DEPRECATED & ~E_STRICT & ~E_NOTICE);
|
|||
|
|
require_once($_SERVER['DOCUMENT_ROOT']."/config.php");
|
|||
|
|
//TODO раскомментировать, когда нужно будет парсить не только регионы
|
|||
|
|
// id_rf_region = 1483 AND - кемерово
|
|||
|
|
$sql = "SELECT objects.id, objects.adres, objects.id_rf_region, objects.rayon, objects.dom, objects.korpus, objects.litera, rayon.rayon as rayon_name,
|
|||
|
|
object_location.id as object_location_id FROM `objects`
|
|||
|
|
left join `object_location` on object_location.object_id = objects.id
|
|||
|
|
left join `rayon` on rayon.id = objects.rayon
|
|||
|
|
WHERE
|
|||
|
|
objects.archive = 0 AND
|
|||
|
|
(objects.id_rf_region = 1483 || objects.id_rf_region = 1484 || objects.id_rf_region = 45 || objects.id_rf_region = 81 || objects.id_rf_region = 2 || objects.id_rf_region = 50 || objects.id_rf_region = 1) AND ISNULL(object_location.id) AND
|
|||
|
|
(ISNULL(object_location.latitude) || ISNULL(object_location.longitude))
|
|||
|
|
and objects.adres != '' and objects.rayon > 0
|
|||
|
|
ORDER BY objects.id DESC LIMIT 200";
|
|||
|
|
$rez = mysql_query($sql);
|
|||
|
|
|
|||
|
|
|
|||
|
|
$success = 0;
|
|||
|
|
$fail = 0;
|
|||
|
|
|
|||
|
|
while($obj = mysql_fetch_assoc($rez)) {
|
|||
|
|
$address = '';
|
|||
|
|
|
|||
|
|
if ($obj['id_rf_region'] == 50) {
|
|||
|
|
if(is_bool(strpos($obj['adres'], "Ленинградская область"))){
|
|||
|
|
$address .= "Ленинградская область,";
|
|||
|
|
}
|
|||
|
|
}else if($obj['id_rf_region'] == 81){
|
|||
|
|
$address .= "Санкт Петербург,";
|
|||
|
|
} else if($obj['id_rf_region'] == 77) {
|
|||
|
|
$sqlRegion = "SELECT parent.name as parent,rf_regions.name FROM rf_regions as parent
|
|||
|
|
LEFT JOIN rf_regions on rf_regions.parent = parent.id
|
|||
|
|
LEFT JOIN rf_region_rayons ON rf_region_rayons.rf_region_id = rf_regions.id
|
|||
|
|
WHERE rayon_id in (select rayon from objects where id = $obj[id])";
|
|||
|
|
$rezRegion = mysql_query($sqlRegion);
|
|||
|
|
if($rezRegion){
|
|||
|
|
$region = mysql_fetch_assoc($rezRegion);
|
|||
|
|
$address .= $region['parent'].','.$region['name'].' округ,';
|
|||
|
|
}
|
|||
|
|
}else{
|
|||
|
|
$sqlRegion = "SELECT parent.name as parent,rf_regions.name FROM rf_regions as parent
|
|||
|
|
LEFT JOIN rf_regions on rf_regions.parent = parent.id
|
|||
|
|
LEFT JOIN rf_region_rayons ON rf_region_rayons.rf_region_id = rf_regions.id
|
|||
|
|
WHERE rayon_id in (select rayon from objects where id = $obj[id])";
|
|||
|
|
$rezRegion = mysql_query($sqlRegion);
|
|||
|
|
if($rezRegion){
|
|||
|
|
$region = mysql_fetch_assoc($rezRegion);
|
|||
|
|
$address .= $region['parent'];
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
if($obj['rayon_name'] && $obj['id_rf_region'] == 81){
|
|||
|
|
if (strpos($obj['rayon'], "район") or strpos($obj['adres'], "район") or strpos($obj['adres'], "р-н")) {
|
|||
|
|
$address .= $obj['rayon_name'].',';
|
|||
|
|
} else {
|
|||
|
|
$address .= $obj['rayon_name'] . " район,";
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
$address .= ' ' . $obj['adres'];
|
|||
|
|
/*$address .= str_replace('г. Санкт-Петербург, ', '', $obj['adres']);*/
|
|||
|
|
|
|||
|
|
if ($obj['dom']) {
|
|||
|
|
$address .= ", " . $obj['dom'];
|
|||
|
|
}
|
|||
|
|
if ($obj['korpus']) {
|
|||
|
|
$address .= ", корпус " . $obj['korpus'];
|
|||
|
|
}
|
|||
|
|
if ($obj['litera']) {
|
|||
|
|
$address .= ", литера " . $obj['litera'];
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
$addressArr[] = $address;
|
|||
|
|
$response = json_decode(file_get_contents('http://search.maps.sputnik.ru/search?q='.str_replace('%2B', '+', urlencode(str_replace(' ','+',$address)))));
|
|||
|
|
|
|||
|
|
if (!empty($response))
|
|||
|
|
{
|
|||
|
|
$lat = $response->result[0]->position->lat;
|
|||
|
|
$lng = $response->result[0]->position->lon;
|
|||
|
|
|
|||
|
|
if(!empty($lat) && !empty($lng)){
|
|||
|
|
if(!empty($obj['object_location_id'])){
|
|||
|
|
$sql = "UPDATE object_location SET address = '$address' latitude = '$lat', longitude = '$lng' WHERE id=$obj[object_location_id]";
|
|||
|
|
}else{
|
|||
|
|
$sql = "INSERT INTO `object_location` (`object_id`, `latitude`, `longitude`, `address`) VALUES (".$obj['id'].", '$lat', '$lng', '$address');";
|
|||
|
|
}
|
|||
|
|
mysql_query($sql);
|
|||
|
|
$success++;
|
|||
|
|
} else {
|
|||
|
|
$lat = 'NULL';
|
|||
|
|
$lng = 'NULL';
|
|||
|
|
$sql = "INSERT INTO `object_location` (`object_id`, `latitude`, `longitude`, `address`) VALUES (".$obj['id'].", $lat, $lng, '$address');";
|
|||
|
|
mysql_query($sql);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
$fail++;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
echo 'success:'. $success . ' fail:'.$fail;
|