/**
* 座標系関数
*/
require_once DEF_PATH_COMMON . "class/clsDb_MySQL.php";
require_once DEF_PATH_COMMON . "class/clsWhereMake.php";
require_once DEF_PATH_COMMON . "function/funcRobot.php";
require_once DEF_PATH_COMMON . "function/funcModRewrite.php";
require_once DEF_PATH_COMMON . "function/funcLog.php";
require_once dirname(__FILE__) . "/funcTemplate.php";
/**
* 郵便番号→座標
*
* @param object $objDB データベース接続オブジェクト
* @param array $hashKeyId 検索に使用するキーの配列
* @param array $hashZahyou 座標情報を含む連想配列
* @return array 最寄りの駅情報を含む連想配列
*/
function Postno2Zahyou($objDB, $hashKeyId, $strPostNo)
{
if (strpos(gethostname(), ".sakura.ne.jp") !== false) {
$blnPloxy = true;
} else {
$blnPloxy = false;
}
$hashVal = [];
for ($ii = 0; $ii < 3; $ii++) {
$hashSet = [];
$hashSet["postal"] = $strPostNo;
// if($ii>1) $blnPloxy = false; // 最後のリトライはプロキシ無しで取りに行く
$strClassName = "webservice_GeoApiCoordinates";
$objWS = new webservice_GeoApiCoordinates("1", "UTF-8", $blnPloxy);
$objWS->SetPara($hashSet);
if ($objWS->ApiUrlCall()) {
$hashResult = $objWS->Result();
$aryhashList = $objWS->Parse();
if (isset($aryhashList[0]["x"]) && isset($aryhashList[0]["y"])) {
$hashVal["Longitude"] = $aryhashList[0]["x"];
$hashVal["Latitude"] = $aryhashList[0]["y"];
$hashVal["Bikou"] = Get_SQLValueString($strClassName);
EasyUpdate($objDB, "com05chouiki", $hashVal, sprintf("WHERE KenId=%s AND ChikuId=%s AND ChouikiId=%s", $hashKeyId[1], $hashKeyId[2], $hashKeyId[3]));
return $aryhashList[0]; // 正常
} else {
$str = sprintf("%s パースERROR:%s\n", $strClassName, $hashSet["postal"]);
DbLogWrite($objDB, DEF_LOG_KIND_Error, $str);
if (strlen($hashResult["error"]) > 0) {
// 〒→座標でエラーコードがある場合は、座標取得ができない住所なので-1を格納しておく(あとで手作業で対応)
$hashVal["Longitude"] = -1;
$hashVal["Latitude"] = -1;
$hashVal["Bikou"] = Get_SQLValueString($strClassName);
EasyUpdate($objDB, "com05chouiki", $hashVal, sprintf("WHERE KenId=%s AND ChikuId=%s AND ChouikiId=%s", $hashKeyId[1], $hashKeyId[2], $hashKeyId[3]));
$str = sprintf("%s -1 Update:%s\n", $strClassName, $hashSet["postal"]);
DbLogWrite($objDB, DEF_LOG_KIND_Warning, $str);
break;
}
}
} else {
if ($objWS->posXml() > 0) {
$str = sprintf("%s APIコールERROR:MSG=%s, %s\n", $strClassName, json_encode($objWS->Result()), $hashSet["postal"]);
DbLogWrite($objDB, DEF_LOG_KIND_Error, $str);
} else {
$str = sprintf("%s PROXY ERROR:%s\n", $strClassName, $hashSet["postal"]);
DbLogWrite($objDB, DEF_LOG_KIND_Debug, $str);
}
}
sleep(1);
}
return null;
}
/**
* 座標→最寄り駅
*
* @param object $objDB データベース接続オブジェクト
* @param array $hashKeyId 検索に使用するキーの配列
* @param array $hashZahyou 座標情報を含む連想配列
* @return array 最寄りの駅情報を含む連想配列
*/
function Zahyou2Staion($objDB, $hashKeyId, $strPostNo, $hashZahyou)
{
if (!isset($hashZahyou["x"]) || !isset($hashZahyou["y"]) || $hashZahyou["x"] < 100 || $hashZahyou["y"] < 20) {
return null; // 最寄り駅を取得できる前提条件として、経度緯度が必要
}
if (strpos(gethostname(), ".sakura.ne.jp") !== false) {
$blnPloxy = true;
} else {
$blnPloxy = false;
}
$aryHash = [];
$hashSet = [];
$hashSet["x"] = $hashZahyou["x"];
$hashSet["y"] = $hashZahyou["y"];
$intRegNo = 0;
// 排他制御 start
$objExclusive = new clsExclusiveFlock();
$objExclusive->Start();
// DBからSELECT
$aryStation = EasySelect(
$objDB,
"ybt07station",
"RegNo",
["StationName", "StationKana", "LineName", "City", "Direction", "Distance", "WalkTime"],
sprintf("WHERE KenId=%s AND ChikuId=%s AND ChouikiId=%s ORDER BY Distance", $hashKeyId[1], $hashKeyId[2], $hashKeyId[3])
);
if (!$aryStation) {
// DBにない→apiから取得
for ($ii = 0; $ii < 3; $ii++) {
// if($ii>1) $blnPloxy = false; // 最後のリトライはプロキシ無しで取りに行く
$strClassName = "webservice_GeoApiStation2";
$objWS = new webservice_GeoApiStation2("1", "UTF-8", $blnPloxy);
$objWS->SetPara($hashSet);
if ($objWS->ApiUrlCall()) {
$aryhashList = $objWS->Parse();
if (is_array($aryhashList) && count($aryhashList) > 0) {
foreach ($aryhashList as $hash) {
$intRegNo++;
// SimpleApiStationsの型に変換する
$hash = clsGeocode::convOldType($hash, $hashZahyou);
// 表示用 SELECTされた場合と同じもの
$hashVal = [];
$hashVal["StationName"] = $hash["name"];
$hashVal["StationKana"] = $hash["furigana"];
$hashVal["LineName"] = $hash["line"];
$hashVal["City"] = $hash["city"];
$hashVal["Direction"] = $hash["direction"];
$hashVal["Distance"] = $hash["distance"];
$hashVal["WalkTime"] = $hash["traveltime"];
$aryStation[$intRegNo] = $hashVal;
// INSERT用
$hashVal = [];
$hashVal["KenId"] = $hashKeyId[1];
$hashVal["ChikuId"] = $hashKeyId[2];
$hashVal["ChouikiId"] = $hashKeyId[3];
$hashVal["RegNo"] = $intRegNo;
$hashVal["StationName"] = Get_SQLValueString($hash["name"]);
$hashVal["StationKana"] = Get_SQLValueString($hash["furigana"]);
$hashVal["LineName"] = Get_SQLValueString($hash["line"]);
$hashVal["City"] = Get_SQLValueString($hash["city"]);
$hashVal["KenName"] = Get_SQLValueString($hash["prefecture"]);
$hashVal["Direction"] = Get_SQLValueString($hash["direction"]);
$hashVal["Distance"] = Get_SQLValueString($hash["distance"], "int");
$hashVal["WalkTime"] = Get_SQLValueString($hash["traveltime"]);
$hashVal["Bikou"] = Get_SQLValueString($strClassName);
$hashVal["UpdateTime"] = Get_SQLValueString(date("Y-m-d H:i:s"), "date");
$hashVal["CreateTime"] = Get_SQLValueString(date("Y-m-d H:i:s"), "date");
$aryHash[] = $hashVal;
}
EasyMultiInsert($objDB, "ybt07station", $aryHash);
break;
} else {
$str = sprintf(
"%s 最寄り駅取得できず:Id = %d, %d, %d X:%f, Y:%f 〒:%s\n",
$strClassName,
$hashKeyId[1],
$hashKeyId[2],
$hashKeyId[3],
$hashZahyou["x"],
$hashZahyou["y"],
$strPostNo
);
DbLogWrite($objDB, DEF_LOG_KIND_Debug, $str);
if ($objWS->posXml() > 0) {
$hashResult = $objWS->Result();
if (isset($hashResult["error"]) && strlen($hashResult["error"]) > 0) {
$str = sprintf(
"%s APIコールERROR:MSG=%s, Id=%d, %d, %d X:%f, Y:%f 〒:%s\n",
$strClassName,
json_encode($objWS->Result()),
$hashKeyId[1],
$hashKeyId[2],
$hashKeyId[3],
$hashZahyou["x"],
$hashZahyou["y"],
$strPostNo
);
DbLogWrite($objDB, DEF_LOG_KIND_Error, $str);
} else {
break; // xmlも取れてる&エラーもない なら最寄り駅無しでOK リトライしない
}
} else {
$str = sprintf(
"%s PROXY ERROR:Id=%d, %d, %d X:%f, Y:%f 〒:%s\n",
$strClassName,
$hashKeyId[1],
$hashKeyId[2],
$hashKeyId[3],
$hashZahyou["x"],
$hashZahyou["y"],
$strPostNo
);
DbLogWrite($objDB, DEF_LOG_KIND_Error, $str);
}
}
} else {
if ($objWS->posXml() > 0) {
$str = sprintf(
"%s APIコールERROR:MSG=%s, Id=%d, %d, %d X:%f, Y:%f 〒:%s\n",
$strClassName,
json_encode($objWS->Result()),
$hashKeyId[1],
$hashKeyId[2],
$hashKeyId[3],
$hashZahyou["x"],
$hashZahyou["y"],
$strPostNo
);
DbLogWrite($objDB, DEF_LOG_KIND_Error, $str);
} else {
$str = sprintf("%s PROXY ERROR:Id=%d, %d, %d X:%f, Y:%f 〒:%s\n", $strClassName, $hashKeyId[1], $hashKeyId[2], $hashKeyId[3], $hashSet["x"], $hashSet["y"], $strPostNo);
DbLogWrite($objDB, DEF_LOG_KIND_Error, $str);
}
}
sleep(2);
}
// リトライしても取得できなかった → RegNo=-1の空データをINSERTしておく
if (!$aryStation) {
// 表示用 SELECTされた場合と同じもの
$hashVal = [];
$aryStation[-1] = $hashVal;
// INSERT用
$hashVal = [];
$hashVal["KenId"] = $hashKeyId[1];
$hashVal["ChikuId"] = $hashKeyId[2];
$hashVal["ChouikiId"] = $hashKeyId[3];
$hashVal["RegNo"] = -1;
$hashVal["Bikou"] = Get_SQLValueString("最寄り駅なし");
$hashVal["UpdateTime"] = Get_SQLValueString(date("Y-m-d H:i:s"), "date");
$hashVal["CreateTime"] = Get_SQLValueString(date("Y-m-d H:i:s"), "date");
EasyInsert($objDB, "ybt07station", $hashVal);
$str = sprintf(
"%s 「最寄り駅なし」で確定:Id = %d, %d, %d X:%f, Y:%f 〒:%s\n",
$strClassName,
$hashKeyId[1],
$hashKeyId[2],
$hashKeyId[3],
$hashZahyou["x"],
$hashZahyou["y"],
$strPostNo
);
DbLogWrite($objDB, DEF_LOG_KIND_Info, $str);
}
}
// 排他制御終了
$objExclusive->End();
return $aryStation;
}
/**
* 座標→郵便局
*
* @param object $objDB データベース接続オブジェクト
* @param array $hashKeyId 検索に使用するキーの配列
* @param array $hashZahyou 座標情報を含む連想配列
* @return array 最寄りの郵便局情報を含む連想配列
*/
function Zahyou2postoffice($objDB, $hashKeyId, $hashZahyou)
{
$postoffice_list = [];
if (!isset($hashZahyou["x"]) || !isset($hashZahyou["y"]) || $hashZahyou["x"] < 100 || $hashZahyou["y"] < 20) {
return []; // 前提条件として、経度緯度が必要
}
// DBからSELECT
$post_id_list = EasySelect(
$objDB,
"ybt08postoffice_relation",
"RegNo",
["Id"],
sprintf("WHERE KenId=%s AND ChikuId=%s AND ChouikiId=%s ORDER BY RegNo", $hashKeyId[1], $hashKeyId[2], $hashKeyId[3])
);
// 既に結果が入っていたらDBのデータを採用
if ($post_id_list) {
// array_filter()でkey(RegNo)が0以下を抽出し、その結果が空の場合はtrue(0以下が存在しない)
$plus_only_flg = empty(array_filter(array_keys($post_id_list), function ($key) {
return $key < 0;
}));
if ($plus_only_flg) {
$postoffice_list = EasySelect(
$objDB,
"geo01postoffice",
"Id",
["Name", "Yomi", "Longitude", "Latitude", "Address", "Tel"],
sprintf("WHERE Id IN (%s)", implode(",", array_column($post_id_list, "Id")))
);
} else {
$postoffice_list = []; // DBに有効な行がないが正常
}
return $postoffice_list;
}
// DBにないのでAPIで郵便局情報を取得
$blnPloxy = false;
$hashSet = [];
$hashSet["lon"] = $hashZahyou["x"];
$hashSet["lat"] = $hashZahyou["y"];
$hashSet['dist'] = 3;
$hashSet['sort'] = "dist";
$hashSet['results'] = 100;
$intRegNo = 0;
// 排他制御 start
$objExclusive = new clsExclusiveFlock();
$objExclusive->Start();
// DBにない→apiから取得
for ($ii = 0; $ii < 3; $ii++) {
//if($ii>1) $blnPloxy = false; // 最後のリトライはプロキシ無しで取りに行く
$strClassName = "webservice_YahoolocalSearch";
$objWS = new webservice_YahoolocalSearch(DEF_YahooApiClientId, "0411001", "1", "UTF-8", $blnPloxy);
$objWS->SetPara($hashSet);
if ($objWS->ApiUrlCall()) {
$xml = $objWS->Parse();
if (isset($xml->Feature) && is_iterable($xml->Feature)) {
$ins08 = $ins09 = [];
foreach ($xml->Feature as $key => $feature) {
if (empty($feature->Id) || empty($feature->Name)) {
continue; // 不正データなのでskip
}
// 緯度・経度を少数6桁で丸める
//list($lon, $lat) = array_map(function ($val) {return number_format(trim($val), 6, '.', '');}, explode(',', $feature->Geometry->Coordinates));
$lon = $lat = 0;
if (isset($feature->Geometry->Coordinates)) {
$parts = explode(',', $feature->Geometry->Coordinates);
if (count($parts) === 2) {
// 数字かどうかチェックしてなければ0を入れる
$lon = is_numeric(trim($parts[0])) ? number_format(trim($parts[0]), 6, '.', '') : '0';
$lat = is_numeric(trim($parts[1])) ? number_format(trim($parts[1]), 6, '.', '') : '0';
}
}
// INSERT用 08
$intRegNo++;
$ins08[] = [
"KenId" => $hashKeyId[1],
"ChikuId" => $hashKeyId[2],
"ChouikiId" => $hashKeyId[3],
"RegNo" => $intRegNo,
"Id" => (int)$feature->Id
];
// 既に登録済みか
$row = Easy1Record($objDB, "geo01postoffice", null, sprintf("WHERE Id=%d", (int)$feature->Id));
if (!$row) {
// INSERT用 09
$hash = [
"Id" => (int)$feature->Id,
"Gid" => Get_SQLValueString($feature->Gid ?? ""),
"Name" => Get_SQLValueString($feature->Name ?? ""),
"Yomi" => Get_SQLValueString($feature->Property->Yomi ?? ""),
"Longitude" => $lon,
"Latitude" => $lat,
"Location" => sprintf("POINT(%s, %s)", $lon, $lat),
"Address" => Get_SQLValueString($feature->Property->Address ?? ""),
"Tel" => Get_SQLValueString($feature->Property->Tel1 ?? "")
];
$ins09[] = $hash;
$postoffice_list[$intRegNo] = $hash; // return用
} else {
$postoffice_list[$intRegNo] = $row;
}
}
if ($ins08) EasyMultiInsert($objDB, "ybt08postoffice_relation", $ins08);
if ($ins09) EasyMultiInsert($objDB, "geo01postoffice", $ins09);
break;
} else {
$str = sprintf(
"%s 郵便局取得できず:Id = %d, %d, %d X:%f, Y:%f\n",
$strClassName,
$hashKeyId[1],
$hashKeyId[2],
$hashKeyId[3],
$hashZahyou["x"],
$hashZahyou["y"]
);
DbLogWrite($objDB, DEF_LOG_KIND_Debug, $str);
if ($objWS->posXml() > 0) {
$hashResult = $objWS->Result();
if (isset($hashResult["Error"]) && strlen($hashResult["Error"]) > 0) {
$str = sprintf(
"%s APIコールERROR:MSG=%s, Id=%d, %d, %d X:%f, Y:%f\n",
$strClassName,
json_encode($objWS->Result()),
$hashKeyId[1],
$hashKeyId[2],
$hashKeyId[3],
$hashZahyou["x"],
$hashZahyou["y"]
);
DbLogWrite($objDB, DEF_LOG_KIND_Error, $str);
} else {
// xmlも取れてる&エラーもない なら郵便局無しでOK リトライしない
break;
}
} else {
$str = sprintf(
"%s PROXY ERROR:Id=%d, %d, %d X:%f, Y:%f\n",
$strClassName,
$hashKeyId[1],
$hashKeyId[2],
$hashKeyId[3],
$hashZahyou["x"],
$hashZahyou["y"]
);
DbLogWrite($objDB, DEF_LOG_KIND_Error, $str);
}
}
} else {
if ($objWS->posXml() > 0) {
$str = sprintf(
"%s APIコールERROR:MSG=%s, Id=%d, %d, %d X:%f, Y:%f\n",
$strClassName,
json_encode($objWS->Result()),
$hashKeyId[1],
$hashKeyId[2],
$hashKeyId[3],
$hashZahyou["x"],
$hashZahyou["y"]
);
DbLogWrite($objDB, DEF_LOG_KIND_Error, $str);
} else {
$str = sprintf(
"%s PROXY ERROR:Id=%d, %d, %d X:%f, Y:%f\n",
$strClassName,
$hashKeyId[1],
$hashKeyId[2],
$hashKeyId[3],
$hashZahyou["x"],
$hashZahyou["y"]
);
DbLogWrite($objDB, DEF_LOG_KIND_Error, $str);
}
}
sleep(2);
}
// リトライしても取得できなかった → RegNo=-1の空データをINSERTしておく
if (empty($postoffice_list)) {
// INSERT用
$ins08nothing = [
"KenId" => $hashKeyId[1],
"ChikuId" => $hashKeyId[2],
"ChouikiId" => $hashKeyId[3],
"RegNo" => -1
];
EasyInsert($objDB, "ybt08postoffice_relation", $ins08nothing);
$str = sprintf(
"%s 「郵便局なし」で確定:Id = %d, %d, %d X:%f, Y:%f\n",
$strClassName,
$hashKeyId[1],
$hashKeyId[2],
$hashKeyId[3],
$hashZahyou["x"],
$hashZahyou["y"]
);
DbLogWrite($objDB, DEF_LOG_KIND_Info, $str);
}
// 排他制御終了
$objExclusive->End();
return $postoffice_list;
}
宮城県栗原市金成稲荷前 〒989-5127 〒郵便番号検索
宮城県 栗原市 金成稲荷前
みやぎけん くりはらし かんなりいなりまえ
旧郵便番号(5桁):〒989-51
地方公共団体コード:04213
金成稲荷前の座標
東経 :141.078166度北緯 :38.823688度
金成稲荷前の最寄り駅
Fatal error : Uncaught Error: Call to undefined function Zahyou2Staion() in /var/www/html/yuubinbangou.net/www/function/funcDisp.php:719
Stack trace:
#0 /var/www/html/yuubinbangou.net/www/function/funcDisp.php(244): Disp_PostNoPage()
#1 /var/www/html/yuubinbangou.net/www/function/funcDisp.php(124): Disp_Main()
#2 /var/www/html/yuubinbangou.net/www/rewrite.php(134): DispInformation()
#3 {main}
thrown in /var/www/html/yuubinbangou.net/www/function/funcDisp.php on line 719