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.

信州割の観光クーポン対象店舗をPDFから抽出してGoogle Mapに表示する実験

0
Posted at

信州割(長野県の県民割)でもらえる観光クーポンがどこで使えるのか分かりにくいんじゃい!
ということでGoogle My Mapsに表示させてみました。

結果

やったこと

  1. クーポン対象観光施設の一覧PDFからpdfplumberを使用してテーブルデータを抽出
  2. Google Maps APIのFind Placeで施設名の緯度経度を取得
  3. KMLに出力
  4. Google My Mapsにインポート

リポジトリ: https://github.com/diontools/ShinshuCouponMap

PDFからテーブルデータ抽出

意外とこれは簡単でした。

import pdfplumber

with pdfplumber.open('./list-k-adv.pdf') as pdf:
  data: list[list[str | None]] = []
  for page in pdf.pages:
    table = page.extract_table()
    data.extend(table[2:])

テーブル内のハイパーリンクは抽出されないので、何とかする必要があります。
今回はテーブル内の"HP"のみにリンクがあるので、順番に割り当てました。

links = [x['uri'] for x in pdf.hyperlinks]
linkIndex = 0
for values in data:
  if values[7] == 'HP':
    values[7] = links[linkIndex]
    linkIndex += 1

施設名から緯度経度の取得

最初はGoogle Maps APIのGeocodingを試しましたが、曖昧なデータだとあまり精度が良くないようなので、Find Placeを使用しました。
市町村別に同名の施設(チェーン店など)があるため、市町村名+施設名で検索しました。

import googlemaps

gmaps = googlemaps.Client(key='APIKEY')
result = gmaps.find_place(
  text,
  input_type='textquery',
  fields=['business_status','formatted_address','geometry','icon','name','photos','place_id','plus_code','types'],
  language='ja',
  location_bias=f'rectangle:35.1598723715222,138.82338748509508|37.16621915151721,137.27180990577534'
)

おわり

体感8,9割ぐらい上手くいきましたが、元データ(施設名)がGoogle Mapの表記と違ったり、似たような名称だったりしてミスマッチが起きています。
検索結果がない・住所が長野県ではない・市町村名が一致しない場合のみ手動で修正をしましたが、これを修正していくのはちょっと辛いと思いました。(小並感)
あとFind Placeは1000リクエストで$17するので結構お高い。7000リクエストぐらいしたので1.5万円分。

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?