0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

GeoJSONのオープンデータをKibanaのマップに表示する

Posted at

はじめに

GeoJSONは、JSONで地理的データを記述するためのフォーマットです。
GeoJSON形式で様々なデータが公開されています。
今回は公開されている鎌倉市のAED設置場所のGeoJSONファイルを読み込んで、KibanaのMap上に表示させてみたいと思います。

環境

使用した環境は以下のとおり。

  • CentOS 7.5
  • Elasticsearch 7.8.0
  • Kibana 7.8.0

ElasticStack 7.8 環境構築」でインストールした環境を利用しています。

鎌倉市のAED配置場所のデータは以下からダウンロードしました。

鎌倉市 オープンデータ、鎌倉市・鎌倉シチズンネット(KCN)、クリエイティブ・コモンズ・ライセンス 表示 4.0(http://creativecommons.org/licenses/by/4.0/)

上のサイトから、以下の「Geojson(.geojson)」をダウンロードします。

image.png

マップを作成する

まず、Kibanaのトップページから[Maps]を選択します。

image.png

[Create map]を選択します。

image.png

地図が表示されるので、ここで[Add layer]を選択します。

image.png

追加するデータとして[Upload GeoJSON]を選択します。

image.png

あらかじめダウンロードしていた、鎌倉のAEDデータを[Import file]をドロップし、「index name」を入力して「Import file」を選択します。

image.png

すると、GeoJSONファイルが読み込まれ、インデックスが作成された結果が表示されます。
エラーとならなければそのまま「Add layer」でマップ上にレイヤーを追加できます。

image.png

レイヤーの名前、ツールチップに表示するフィールドを入力し、「Save & close」を選択して完了です。

image.png

レイヤーの追加が終わると、以下のようにAEDの設置場所がポイントで表示されています。

image.png

ポイントを選択するとツールチップが表示されるのですが、文字化けしています。。。
確認したところGeoJSONファイルはUTF-8で文字コードでしたので問題ないはずです。
調べてみても原因分からず断念。

image.png

一応、「Save」しておきます。

image.png

GeoJSONではなくCSVファイルから読み込んでやり直し

結局、調べても文字化けが直らなかったので、CSVファイルをダウンロードしてやり直しました。

まず、インデックスを作成します。

curl -H "Content-Type: application/json" -XPUT 'http://localhost:9200/map-test-index?pretty' -d '
{
    "mappings" : {
        "properties" : {
            "Category" : { "type" : "text" },
            "Hiragana" : { "type" : "text" },
            "latitude" : { "type" : "text" },
            "longitude" : { "type" : "text" },
            "name" : { "type" : "text" },
            "section" : { "type" : "text" },
            "住所" : { "type" : "text" },
            "備考" : { "type" : "text" },
            "設置数" : { "type" : "text" },
            "location" : { "type" : "geo_point" }
        }
    }
}'

次にCSVファイルを読み込んでElasticsearchへJSONをPOSTするプログラムをPythonで作成し、ドキュメントを読み込みました。

import json
import csv
import urllib.request

if __name__ == "__main__":
    url = "http://192.168.10.126:9200/map-test-index/_doc/" 
    method = "POST"
    headers = {"Content-Type" : "application/json"}

    # CSV ファイルの読み込み
    with open('_61_Aed.csv', 'r') as f:
        for line in csv.DictReader(f):
            line['location'] = line['latitude'] + "," + line['longitude']
            line_json = json.dumps(line, ensure_ascii=True).encode("utf-8")
            print(line_json)

            request = urllib.request.Request(url, data=line_json, method=method, headers=headers)
            with urllib.request.urlopen(request) as response:
                response_body = response.read().decode("utf-8")
                print(response_body)

KibanaでIndex Patternを登録し、マップ上に表示した例が以下のようになります。
今度は文字化けなく表示できました。

image.png

参考

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?