2
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 5 years have passed since last update.

【Python】Reverse Geocodeを使った逆ジオコーディング

Posted at

はじめに

Reverse Geocodeは緯度/経度座標を取得し、国と都市を返します。
このモジュールには、既知のジオコーディングされた場所のセットがあり、k-dツリーを使用して、最も近い場所を効率的に見つけるそうです。

環境

  • Windows 10
  • Python 3.7.3
  • reverse-geocode=1.4

インストール

$ pip install reverse-geocode

使い方

>>> import reverse_geocode
>>> coordinates = (35.69, 139.77), (40.71, -74.01)
>>> reverse_geocode.search(coordinates)
[{'country_code': 'JP', 'city': 'Tokyo', 'country': 'Japan'}, {'country_code': 'US', 'city': 'Financial District', 'country': 'United States'}]

しかし!
Windowsだと色々エラーが出ました。

OverflowError

>>> import reverse_geocode
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\grin\Anaconda3\envs\practice\lib\site-packages\reverse_geocode\__init__.py", line 4, in <module>
    csv.field_size_limit(sys.maxsize)
OverflowError: Python int too large to convert to C long

解決法

パッケージのソースを直接いじるのはとても嫌なのですが、仕方がないので該当部分を修正します。

修正後
csv.field_size_limit(2**20)

UnicodeDecodeError

>>> import reverse_geocode
>>> coordinates = (35.69, 139.77), (40.71, -74.01)
>>> reverse_geocode.search(coordinates)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\grin\Anaconda3\envs\practice\lib\site-packages\reverse_geocode\__init__.py", line 118, in search
    gd = GeocodeData()
  File "C:\Users\grin\Anaconda3\envs\practice\lib\site-packages\reverse_geocode\__init__.py", line 22, in getinstance
    instances[cls] = cls()
  File "C:\Users\grin\Anaconda3\envs\practice\lib\site-packages\reverse_geocode\__init__.py", line 31, in __init__
    coordinates, self.locations = self.extract(rel_path(geocode_filename))
  File "C:\Users\grin\Anaconda3\envs\practice\lib\site-packages\reverse_geocode\__init__.py", line 97, in extract
    for latitude, longitude, country_code, city in rows:
UnicodeDecodeError: 'cp932' codec can't decode byte 0x98 in position 429: illegal multibyte sequence

解決法

ファイルのオープンをしているところに、エンコーディングの指定を付ける。

修正後
rows = csv.reader(open(local_filename, encoding='utf_8'))

これでなんとか動きました。

参考URL

2
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
2
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?