LoginSignup
3
2

More than 3 years have passed since last update.

イントラネットで地図サービスを使いたい5‐ジオコーダー編2‐

Last updated at Posted at 2019-04-20

前の記事

リバースジオコーダー

 前回、サラッと紹介したが、地理院地図には緯度経度から住所を割り出す機能が下の画像赤枠部など幾つか使われている。
sketch-1555769799192.png

 東大ジオコーダが使われている。
 データを送った場合、LonLatToAddress.jsonというファイルが返ってくる。
 中身については、下記の通り
http://mreversegeocoder.gsi.go.jp/reverse-geocoder/LonLatToAddress?lon=139.229393&lat=35.521259

LonLatToAddress.json
{
  "results":{
    "muniCd":"14402",
    "lv01Nm":"宮ヶ瀬"
  }
}

 で、これに関してはLonLaToAddress.jsonを返せば下の赤枠部分が表示されると思っていたが、実際は上の赤枠部分に使用されていた。
 前回使用したDmGeocoderにリバースジオコーダ機能も搭載されているので、それをもとに作ったリバースジオコーを紹介する。
 DmGeocoderに緯度経度を送ったとしてもmuniCdを返さないので、muniCd.jsに記載されているmuniCdを元にcsvで予めmuniCdのリストを製作しておく。

muniCd.csv
 1101,北海道札幌市中央区

 作成したリストの右側の住所とDmGeocoderに緯度経度を送って得たデータとを照らし合わせ、一致した物のmuniCdと住所をjsonで返している。

LonLatToAddress.php
<?php
ini_set('display_errors', 'ON');
error_reporting(E_ALL);
mb_internal_encoding('UTF-8');

$LIB_DIR = realpath(dirname(__FILE__).'/../src/').'/';
require_once $LIB_DIR.'Dm/Geocoder.php';
require_once $LIB_DIR.'Dm/Geocoder/Address.php';
require_once $LIB_DIR.'Dm/Geocoder/Prefecture.php';
require_once $LIB_DIR.'Dm/Geocoder/Query.php';
require_once $LIB_DIR.'Dm/Geocoder/GISCSV.php';
require_once $LIB_DIR.'Dm/Geocoder/GISCSV/Finder.php';
require_once $LIB_DIR.'Dm/Geocoder/GISCSV/Reader.php';

$lat = $_POST['lat'];
$lon = $_POST['lon'];
$Srh = Dm_Geocoder::reverseGeocode($lat,$lon,1);
$a = $Srh[0]->prefectureName;
$b = $Srh[0]->municipalityName;
$c = $Srh[0]->localName;
$Srh = $a.$b.$c;

$csv = file_get_contents(dirname(__File__).'/data/MuniCd.csv');
$csv = explode("\n",$csv);
foreach($csv as &$value){
    $value = mb_substr($value,0,-1,"UTF-8");
    $value = explode(',',$value);
    if(mb_strpos($Srh,$value[1])!==false){
        $hako = array(
            "results" => $point = array(
                "muniCd" => $value[0],
                "lv01Nm" => str_replace ($value[1],"",$Srh),
        ),
    );
    break;
    }
}  

function raw_json_encode($input, $flags = 0) {
    $fails = implode('|', array_filter(array(
        '\\\\',
        $flags & JSON_HEX_TAG ? 'u003[CE]' : '',
        $flags & JSON_HEX_AMP ? 'u0026' : '',
        $flags & JSON_HEX_APOS ? 'u0027' : '',
        $flags & JSON_HEX_QUOT ? 'u0022' : '',
    )));
    $pattern = "/\\\\(?:(?:$fails)(*SKIP)(*FAIL)|u([0-9a-fA-F]{4}))/";
    $callback = function ($m) {
        return html_entity_decode("&#x$m[1];", ENT_QUOTES, 'UTF-8');
    };
    return preg_replace_callback($pattern, $callback, json_encode($input, $flags));
}

header('Content-type: application/json; charset=utf-8');
echo raw_json_encode($hako);
?>

 地理院地図側は、gsimaps.jsに記載がある以下のように記載されているのでURLを変更する。

gsimaps.js
CONFIG.SERVERAPI.GETADDR = "https://mreversegeocoder.gsi.go.jp/reverse-geocoder/LonLatToAddress";

また、ajaxでtypeが指定されていないのでPOSTを指定する。

gsimaps.js
$.ajax({
            type: "POST",
            url: CONFIG.SERVERAPI.GETADDR,
            dataType: "json",
            data: {
              lon: record.geometry.coordinates[0],
              lat: record.geometry.coordinates[1]
            },

 繰り返すが、住所検索結果部分はこれで反映される。

まとめ

 できることは、限られるが、地理院地図で使いたい機能は概ね網羅できたので、以上でおわりにしたい。
 拙いプログラムと文書にここまでお付き合いいただいた方ありがとうございました。
 今後、変更するようなことがあれば、また、記事についても更新していく……かもしれない。

参考にさせていただいたサイト

国土地理院情報普及課公式GitHub
国土地理院
国土地理院 -地理院タイル仕様-
国土地理院 -地理院タイル一覧-
国土交通省 -国土数値情報-
国土交通省 -旧国土数値情報-
東京大学ジオコーダー
Github demouth -DmGeocoder-
Github Hidenori FUJIMURA -地理院地図のジオコーダーの現状-
y2blog -WEBマップシステム Misc.-
Qiita json_encode における JSON_UNESCAPED_UNICODE に PHP5.3 で対応する
Wikipedia -geojson-
webGISを自作してみたい - いつかのぎゃふんのために

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2