Skip to content

Instantly share code, notes, and snippets.

@redfoxkamakura
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save redfoxkamakura/9799191 to your computer and use it in GitHub Desktop.
Save redfoxkamakura/9799191 to your computer and use it in GitHub Desktop.

#総務省のデータを使ってElasticSearchで住所取得

国土交通省だと市町村区までですが、 総務省のデータを使うと何丁目まで取得できるようです。
実際に試してみたのでそのメモです。

※参考にしたページ
総務省のデータを使って、緯度経度から市区町村の何丁目までを取り出す
総務省のデータを Elasticsearch にぶち込んで、緯度経度から市区町村の何丁目までを取り出す

##ESが動くvagrant boxの作成

GitHubにES+Vagrantの環境が上がっていたのでそれをForkしました。
そのまま起動しようとしてもうまくいかなかったので改修しました。以下にあります。
https://github.com/redfoxkamakura/vagrant-elasticsearch-box/tree/work/

##総務省のデータをESにINSERT こちらを参考にデータをダウンロード、INSERT用のスクリプト作成しました。
変更した箇所は以下です。

  • PUTを使っていますが、POSTでAutoInclimentを使えるようなのでPOSTを使いました。
  • Shapefile→GeoJSONをオンラインツール使って変換していますが、 ogr2ogrというクライアントで動くツールがあります。
    こっちを使ったほうがスクリプト書けば一括で変換できるので楽です。
    $brew install gdal //macならこれでインストールできます。

INSERT用のスクリプトは、総務省からダウンロードして来たZIPを一箇所のディレクトに入れて以下を実行するとshディレクトリ内に出力されます。

find . -maxdepth 1 -mindepth 1 -type f -name "*.zip" -exec unzip {} -d {}.dir \;
find . -maxdepth 2 -mindepth 2 -type f -name "*.shp" -exec ogr2ogr -f GeoJSON -t_srs crs:84 {}.json {} \;
mkdir sh
find . -maxdepth 2 -mindepth 1 -type f -name "*.json" -exec sed -e '1,5d' -e "s/^}//g" -e "s/^]//g" -e "s/^{/curl -XPOST 'http:\/\/localhost:9200\/towns\/town\/' -d {'/g" -e "s/,$//g" -e "s/}$/}'/g" {} >> sh/script.sh \;
chmod +x sh/script.sh

INSERTは結構時間がかかりました。 東京のデータが12,000件程だったのですがそれでも6時間くらいかかりました。。
関係のないパラメーターを省けばもう少し短縮できると思います。AutoInclimentが遅い原因かもしれません。
HOST:CPU:Core i5 2.7GHz 4Core,メモリ:8GB
VM:CPU:Core i5 2.7GHz 2Core,メモリ1GB

全国のデータをINSERTするのは時間がかかるのでやっていません。
また東京のみでも6時間かかるので、東京のデータを入れてあるvagrant boxをdropboxに入れておきました。
https://dl.dropboxusercontent.com/u/13788898/vagrant-elasticsearch-geocode.box
vagrantのbox.urlに記述しているので下記の動作確認をやれば自動的にダウンロードされます。
(800MBくらいあるので削除しちゃうかもしれません。)
追記:
削除しました。
動作確認をしたい場合は、https://github.com/redfoxkamakura/vagrant-elasticsearch-box/tree/work/こちらのboxを使ってデータを自分でINSERTしてください。

##動作確認

環境から用意してあるので特に難しい準備はいらない。
東京以外のデータをテストしたい場合はデータダウンロードからする必要があります。

git clone https://github.com/redfoxkamakura/vagrant-elasticsearch-box.git
cd vagrant-elasticsearch-box
git checkout -b work origin/work
git submodule init
git submodule update 
vagrant up

これで東京の住所データが入ったESが起動しているので、
試しに西新宿にある野村不動産ビルの緯度/経度(35.694477/139.693276)で検索してみる。
HOSTマシンで以下を実行

curl -XPOST 'http://localhost:9200/towns/town/_search' -d '{
    "query": {
        "filtered" : {
            "query" : {
                "match_all" : {}
            },
            "filter" : {
                 "geo_shape": {
                    "town.geometry": {
                        "shape": {
                            "type" : "envelope",
                            "coordinates" : [[139.693276, 35.694477], [139.693276, 35.694477]]
                        }
                    }
                }
            }
        }
    }
}'

結果

 { 
    "type": 
        "Feature", 
            "properties": {
                 "KEN_NAME": "東京都", 
                 "GST_NAME": "新宿区", 
                 "MOJI": "西新宿8丁目",
                 ・・・省略・・・
            }, 
            "geometry": { 
                "type": "Polygon",
                    "coordinates": 
                    [[  [ 139.696054051679539, 35.697581957123624 ],
                        [ 139.696149460849341, 35.697502984887052 ],
                        [ 139.695667380367553, 35.696707328823507 ],
                        ・・・省略・・・
                      ]
                    ]
            }
}

こんな感じで"東京都新宿区西新宿8丁目"にあることがわかります。

KEN_NAME 都道府県名
GST_NAME 郡市・特別区・政令指定都市名
CSS_NAME 区町村名
MOJI 町丁・字等名称

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment