ホーム 記事一覧 ブック DH週間トピックス 検索 このサイトについて
English
ジオコーディングのライブラリを試す

ジオコーディングのライブラリを試す

概要 ジオコーディングのライブラリを試す機会がありましたので、備忘録です。 対象 今回は、以下のような文字列を対象にしてみます。 岡山市旧御野郡金山寺村。現在の岡山市金山寺。市の中心部からは直線で北方約一〇キロを隔てた金山の中腹にある。 ツール1: Jageocoder - A Python Japanese geocoder まず以下の「Jageocoder」を試します。 https://t-sagara.github.io/jageocoder/ ソースコードの例は以下です。 import json import jageocoder jageocoder.init(url='https://jageocoder.info-proto.com/jsonrpc') results = jageocoder.search('岡山市旧御野郡金山寺村。現在の岡山市金山寺。市の中心部からは直線で北方約一〇キロを隔てた金山の中腹にある。') print(json.dumps(results, indent=2, ensure_ascii=False)) 以下の結果が得られました。 { "matched": "岡山市", "candidates": [ { "id": 197677508, "name": "岡山市", "x": 133.91957092285156, "y": 34.65510559082031, "level": 3, "priority": 1, "note": "geoshape_city_id:33100A2009/jisx0402:33100/jisx0402:33201", "fullname": [ "岡山県", "岡山市" ] } ] } 設定の問題かもしれませんが、岡山市までの情報が得られました。 なお、以下のページでウェブUI経由で試すこともできました。 https://jageocoder.info-proto.com/ ツール2: GeoNLP 以下のページでウェブUI経由で試すことができました。 https://geonlp.ex.nii.ac.jp/jageocoder/demo/ GeoNLPも内部的にはjageocoderを使用しているとのことで、同様の結果が得られました。 また、以下の「テキストジオタギング(GeoNLP)デモ」も試してみました。 https://geonlp.ex.nii.ac.jp/demo/ 入力テキストから複数の地名が抽出されました。 ツール3: Google Maps Platform API 最後に、Google Maps Platform APIを使用してみます。 https://developers.google.com/maps?hl=ja import requests import os from dotenv import load_dotenv load_dotenv(verbose=True) def geocode_address(address, api_key): url = "https://maps.googleapis.com/maps/api/geocode/json" params = { "address": address, "key": api_key, "language": "ja" # 日本語で返却されるように設定 } response = requests.get(url, params=params) if response.status_code == 200: data = response.json() if data['status'] == "OK": result = data['results'][0] location = result['geometry']['location'] print(f"Address: {result['formatted_address']}") print(f"Latitude: {location['lat']}, Longitude: {location['lng']}") else: print(f"Error: {data['status']}") else: print(f"HTTP Error: {response.status_code}") # 使用例 API_KEY = os.getenv("API_KEY") address = "岡山市旧御野郡金山寺村。現在の岡山市金山寺。市の中心部からは直線で北方約一〇キロを隔てた金山の中腹にある。" geocode_address(address, API_KEY) 結果、以下が得られました。 ...