def test_geocode_error_string(self): DAMS.init_dams() address = "東京都足立区/バイク/スポーツ/仲間集め" returned = DAMS.geocode(address) expected = {"score": 0, "tail": "", "candidates": []} self.assertEqual(returned, expected)
def test_geocode_segfault_string(self): DAMS.init_dams() address = "東京都足立区/バイク/スポーツ/仲間集め" try: returned = DAMS.geocode(address) except Exception as e: self.fail("unexpectedly caught exception.")
def test_geocode_level_3(self): DAMS.init_dams() address = "港区" returned = DAMS.geocode(address) expected = { "score": 2, "candidates": [[{ "name": "愛知県", "x": 136.907, "y": 35.1809, "level": 1 }, { "name": "名古屋市", "x": 136.907, "y": 35.1809, "level": 3 }, { "name": "港区", "x": 136.885, "y": 35.1082, "level": 4 }], [{ "name": "大阪府", "x": 135.52, "y": 34.6864, "level": 1 }, { "name": "大阪市", "x": 135.501, "y": 34.6939, "level": 3 }, { "name": "港区", "x": 135.461, "y": 34.6641, "level": 4 }], [ { "name": "東京都", "x": 139.692, "y": 35.6895, "level": 1 }, { "name": "港区", "x": 139.752, "y": 35.6585, "level": 3 }, ]] } self._assert_geocoded_object(returned, expected)
def test_geocode_oaza_batch(self): DAMS.init_dams() for address in self._lst_oaza: returned = DAMS.geocode(address) n_candidates = len(returned["candidates"]) score = returned["score"] if n_candidates == 1: self.assertGreaterEqual(score, 3) else: self.assertGreaterEqual(score, 1)
def test_pretty_print(self): DAMS.init_dams() address = "東京都" returned = DAMS.geocode(address) with suppress_stdout(): pretty_print(returned) address = "東京都千代田区富士見3-1-11" returned = DAMS.geocode_simplify(address) with suppress_stdout(): pretty_print(returned) self.assertTrue(True)
def test_geocode_oaza_subtest(self): DAMS.init_dams() for address in self._lst_oaza: returned = DAMS.geocode(address) n_candidates = len(returned["candidates"]) score = returned["score"] with self.subTest(address=address, score=score, candidates=n_candidates): if n_candidates == 1: self.assertGreaterEqual(score, 3) else: self.assertGreaterEqual(score, 1)
def test_geocode_level_1(self): DAMS.init_dams() address = "東京都" returned = DAMS.geocode(address) expected = { "score": 3, "candidates": [[{ "name": "東京都", "x": 139.692, "y": 35.6895, "level": 1 }]] } self._assert_geocoded_object(returned, expected)
def test_geocode_simplify_level_5(self): DAMS.init_dams() address = "東京都千代田区富士見3-1-11" returned = DAMS.geocode_simplify(address) expected = { "score": 5, "candidates": [{ "name": "東京都千代田区富士見", "x": 139.746558, "y": 35.698418, "level": 5 }] } self._assert_geocoded_object(returned, expected, distance_eps=200)
def test_geocode_town_with_chinese_numerals(self): DAMS.init_dams() address = "福島県大沼郡金山町越川五十苅象" returned = DAMS.geocode_simplify(address) expected = { 'score': 5, 'tail': '象', 'candidates': [{ 'level': 6, 'x': 139.4859161376953, 'y': 37.43180847167969, 'name': '福島県大沼郡金山町越川五十苅' }] } self._assert_geocoded_object(returned, expected, distance_eps=200)
def geocode_with_pydams(normalized_address: str): """ PyDAMSを利用したジオコーディング """ # @see http://newspat.csis.u-tokyo.ac.jp/geocode/modules/dams/index.php?content_id=4 geocoded = DAMS.geocode_simplify(normalized_address) if not geocoded: raise GeocodeError("ジオコーディングの結果がありせんでした。(内部エラー)") lat = round(geocoded["candidates"][0]["y"], 6) # 国土地理院地図に合わせて6桁とした lng = round(geocoded["candidates"][0]["x"], 6) if lat == 0 and lng == 0: raise GeocodeError("ジオコーディング結果がlat=0, lng=0(Null島)を示しています。") score = geocoded["score"] # 1〜5 name = geocoded["candidates"][0]["name"] # ジオコーディングに寄与する住所 tail = geocoded["tail"] # ジオコーディングに寄与してない住所 return (lat, lng, (score, name, tail))
def test_init_dams(self): ret = DAMS.init_dams() self.assertTrue(ret)
import re from functools import lru_cache import pandas as pd import posuto import w3lib.html from logzero import logger from pydams import DAMS from validator_collection import checkers from .exceptions import GeocodeError, NormalizeError, ValidationWarning DAMS.init_dams() @lru_cache(maxsize=None) def cached_posuto_pref(zip_code: str): """ posutoが内部的にsqlite3を使っており、バッチ用途で使うと結構遅くなるので、簡易キャッシュさせている """ return posuto.get(zip_code).prefecture def validate(row: pd.Series): """ 入力データに対する、お気持ち程度のバリデーションチェック """ official_page = row["official_page"] if official_page and checkers.is_url(official_page) == False: raise ValidationWarning("公式URL(officical_page)が不正です") detail_page = row["detail_page"] if detail_page and checkers.is_url(detail_page) == False: raise ValidationWarning("詳細ページ(detail_page)のURLが不正です")