Wordpressで全国駅マスタが必要になった
汎用コードはどこにも転がってなかった。ただ、駅マスタを公開しているサイトはあった。
駅データ.jpである。
https://ekidata.jp/faq.php
このサイト、faqを見れば分かるようにかなりデータの扱いが柔軟ぽい。無償であれば有償データも第3者に提供してよいとなっている。
メンテナンスは素人には出来ないであろう、、というプロの余裕を感じるし、実際その通りだと思う。なかなか至れる境地ではなく、感服しきりである。
ありがたくデータを使わせてもらい、出来れば読者諸兄にも有償データを購入していただき、メンテナンスを応援してもらいたい。
マスタコード
都道府県番号からorder順を生成するので、最低限は順番通りに並ぶはず。
県レベル→路線レベル→駅レベル
という3レベル構造です。
https://github.com/obwebsys/obwebsys/blob/master/create-ekidata202209.php
生成コード(おまけ)
https://ekidata.jp/api/
こちらからAPIデータを取得し、ローカルに実行環境を整えて、そのうえで走らせるコードです。
有償データを前提に、駅名をslug名ローマ字に変換しています。
https://github.com/kuuuuuuuuuuma/convert_kana_to_hebon
20231127
・47都道府県をループで連続取得に改編
・新幹線を含むデータ取得に修正
・ローマ字を駅名だけはDBに含まれるものを使うコードに
<?php
include ("class-convert-kana-to-hebon.php");
$ary_todofuken = array(
"01"=>"北海道","02"=>"青森県","03"=>"岩手県","04"=>"宮城県","05"=>"秋田県","06"=>"山形県","07"=>"福島県","08"=>"茨城県","09"=>"栃木県","10"=>"群馬県","11"=>"埼玉県","12"=>"千葉県","13"=>"東京都","14"=>"神奈川県","15"=>"新潟県","16"=>"富山県","17"=>"石川県","18"=>"福井県","19"=>"山梨県","20"=>"長野県","21"=>"岐阜県","22"=>"静岡県","23"=>"愛知県","24"=>"三重県","25"=>"滋賀県","26"=>"京都府","27"=>"大阪府","28"=>"兵庫県","29"=>"奈良県","30"=>"和歌山県","31"=>"鳥取県","32"=>"島根県","33"=>"岡山県","34"=>"広島県","35"=>"山口県","36"=>"徳島県","37"=>"香川県","38"=>"愛媛県","39"=>"高知県","40"=>"福岡県","41"=>"佐賀県","42"=>"長崎県","43"=>"熊本県","44"=>"大分県","45"=>"宮崎県","46"=>"鹿児島県","47"=>"沖縄県");
$ary_todofuken_k = array(
"01"=>"hokkaido","02"=>"aomori","03"=>"iwate","04"=>"miyagi","05"=>"akita","06"=>"yamagata","07"=>"fukushima","08"=>"ibaraki","09"=>"tochigi","10"=>"gumma","11"=>"saitama","12"=>"chiba","13"=>"tokyo","14"=>"kanagawa","15"=>"niigata","16"=>"toyama","17"=>"ishikawa","18"=>"fukui","19"=>"yamanashi","20"=>"nagano","21"=>"gifu","22"=>"shizuoka","23"=>"aichi","24"=>"mie","25"=>"shiga","26"=>"kyoto","27"=>"osaka","28"=>"hyogo","29"=>"nara","30"=>"wakayama","31"=>"tottori","32"=>"shimane","33"=>"okayama","34"=>"hiroshima","35"=>"yamaguchi","36"=>"tokushima","37"=>"kagawa","38"=>"ehime","39"=>"kochi","40"=>"fukuoka","41"=>"saga","42"=>"nagasaki","43"=>"kumamoto","44"=>"oita","45"=>"miyazaki","46"=>"kagoshima","47"=>"okinawa");
$out_str1 = "";
// 「路線 - 駅」形式で出力する
$lcount = 0;
$scount = 0;
echo('<pre>');
foreach ($ary_todofuken as $pacode=>$pakanji) {
// $pref_id = 2;
$pref_id = intval($pacode);
echo '$_area_parent2 = $_func_lvx_create($_area_parent1,$_post_param,"'.$ary_todofuken_k[$pacode].'","'.$ary_todofuken[$pacode].'",'.intval($pacode).',$updateym);'."<br>";
$sql = "SELECT distinct SQL_CALC_FOUND_ROWS m_line.line_cd,m_line.line_name,m_line.line_name_k,m_line.lon,m_line.lat,m_line.zoom FROM m_line inner JOIN m_station ON m_line.line_cd = m_station.line_cd WHERE m_station.pref_cd = ".$pref_id." AND m_line.e_status = 0 ORDER BY m_line.e_sort,m_line.line_cd";
$stmt = $pdo->prepare($sql);
$stmt -> execute();
$csth = $pdo->query("SELECT FOUND_ROWS()");
$resultNumRows = $csth->fetchColumn();
$counter = 0;
$line_name_k_pre1 = array();
$station_name_k_pre1 = array();
$ary_slug1 = array();
$ary_slug2 = array();
if($resultNumRows>0){
foreach($stmt as $row){
$line_cd = $row['line_cd'];
$line_name = $row['line_name'];
$line_name_k = $row['line_name_k'];
$line_name_k = new Convert_Kana_To_Hebon( $line_name_k );
$line_name_k = strtolower($line_name_k->get_hebon()[0]);
if (in_array($line_name_k,$line_name_k_pre1)) {
$line_name_k_pre1[] = $line_name_k;
$line_name_k = $line_name_k.array_count_values($line_name_k_pre1)[$line_name_k];
} else {
$line_name_k_pre1[] = $line_name_k;
}
$line_lon = $row['lon'];
$line_lat = $row['lat'];
$line_zoom = $row['zoom'];
//駅一覧
$sql2 = "SELECT SQL_CALC_FOUND_ROWS station_cd,station_g_cd,station_name,station_name_r,lon,lat";
$sql2 .= " FROM m_station";
// $sql2 .= " WHERE e_status = 0 AND station_cd > 1000000 AND line_cd = " . $line_cd . " AND pref_cd = ".$pref_id;
$sql2 .= " WHERE e_status = 0 AND line_cd = " . $line_cd . " AND pref_cd = ".$pref_id; // 新幹線も含む
$sql2 .= " ORDER BY e_sort,station_cd";
$stmt2 = $pdo->prepare($sql2);
$stmt2 -> execute();
$csth2 = $pdo->query("SELECT FOUND_ROWS()");
$resultNumRows2 = $csth2->fetchColumn();
if($resultNumRows2>0){
$ary_slug_tmp = array();
$ary_slug_tmp["parent"] = array();
$ary_slug_tmp["parent"][$line_name_k] = $line_name;
$ary_slug_tmp["child"] = array();
$out_str1 .= "<br>".$line_name.$line_name_k."<br>";
$ii=0;
foreach($stmt2 as $row2){
$station_cd = $row2['station_cd'];
$station_g_cd = $row2['station_g_cd'];
$lon = $row2['lon'];
$lat = $row2['lat'];
$station_name = $row2['station_name'];
// $station_name_k = $row2['station_name_k'];
// $station_name_k = new Convert_Kana_To_Hebon( $station_name_k );
// $station_name_k = strtolower($station_name_k->get_hebon()[0]);
$station_name_k = $row2['station_name_r']; // dbにローマ字があるのでそれを使う
$station_name_k = strtolower($station_name_k);
if (in_array($station_name_k,$station_name_k_pre1)) {
$station_name_k_pre1[] = $station_name_k;
$station_name_k = $station_name_k.array_count_values($station_name_k_pre1)[$station_name_k];
} else {
$station_name_k_pre1[] = $station_name_k;
}
$ii++;
$out_str1 .= $station_name.$station_name_k."<br>";
$ary_slug_tmp["child"][$station_name_k] = $station_name; // 出力用
}
$ary_slug1[] = $ary_slug_tmp;
$station_name_k_pre1 = array();
$counter++;
}
}
// 整形
foreach ($ary_slug1 as $key=>$val) {
foreach ($val["parent"] as $line_name_k => $line_name) {
echo '$_area_parent3 = $_func_lvx_create($_area_parent2,$_post_param,"'.$line_name_k.'","'.$line_name.'","'.sprintf("%02d",$pref_id).sprintf("%05d",$_pi++)."0".'",$updateym);'."<br>";
$lcount++;
}
foreach ($val["child"] as $station_name_k=>$station_name) {
echo '$_area_parent4 = $_func_lvx_create($_area_parent3,$_post_param,"'.$station_name_k.'","'.$station_name.'","'.sprintf("%02d",$pref_id).sprintf("%05d",$_pi++)."0".'",$updateym);'."<br>";
$scount++;
}
}
}
}
echo('</pre>');
echo "station count = ".$scount."<br>";
echo "line count = ".$lcount."<br>";
?>