Geocoding in python

I tried the Python library for the first time in a while, so as a reminder. I used geopy and geocoder. Since I used Google Colab, the version of python is 3. https://colab.research.google.com/drive/1LlQ2131p4JV3IY9xOyZcGp_Wg3fRk2p3

Installation package

$ pip install geopy geocoder

https://github.com/geopy/geopy https://github.com/DenisCarriere/geocoder

code geocoder

import geocoder

my_location = geocoder.ip('me')
my_location.geojson

Since I ran it in Colab, org is Google. I was wondering if the address in the west would come out, but it seems that it is automatically connected to a physically close server. By the way, the place where I ran the code is downtown NY.

{'features': [{'geometry': {'coordinates': [-74.006, 40.7143],
    'type': 'Point'},
   'properties': {'address': 'New York City, New York, US',
    'city': 'New York City',
    'country': 'US',
    'hostname': '169.205.229.35.bc.googleusercontent.com',
    'ip': '35.229.205.169',
    'lat': 40.7143,
    'lng': -74.006,
    'ok': True,
    'org': 'AS15169 Google LLC',
    'postal': '10004',
    'raw': {'city': 'New York City',
     'country': 'US',
     'hostname': '169.205.229.35.bc.googleusercontent.com',
     'ip': '35.229.205.169',
     'loc': '40.7143,-74.0060',
     'org': 'AS15169 Google LLC',
     'postal': '10004',
     'readme': 'https://ipinfo.io/missingauth',
     'region': 'New York',
     'timezone': 'America/New_York'},
    'state': 'New York',
    'status': 'OK'},
   'type': 'Feature'}],
 'type': 'FeatureCollection'}
#longitude latitude
my_location.latlng

#Street address
my_location.address

Since the address does not appear up to the street address, the granularity of the corresponding data is the city size. Gonyo Gonyo seems to be easy to do.

[40.7143, -74.006]
'New York City, New York, US'

code geopy

from geopy.geocoders import Nominatim
#Create locator
geolocator = Nominatim(user_agent="user", timeout=10)
location = geolocator.geocode('New York City, New York, US')
location.latitude, location.longitude
location = geolocator.reverse("40.7127281 -74.0060152")
location.address

I used the address displayed by geocoder for comparison. It seems that geopy comes out in more detail, as far as you can see on Google Maps, the accuracy of the address is quite good. https://www.google.com/maps/place/40%C2%B042'45.8%22N+74%C2%B000'21.7%22W/@40.7127281,-74.0066803,18z/data=!3m1!4b1!4m5!3m4!1s0x0:0x0!8m2!3d40.7127281!4d-74.0060152

(40.7127281, -74.0060152)
'New York City Hall, 260, Broadway, Civic Center, Manhattan Community Board 1, Manhattan, New York County, New York, 10000, United States of America'

Finally, I checked the distance between Tokyo Tower and Times Square.

from geopy import distance

loc_tokyo = geolocator.geocode("Tokyo tower, Tokyo, Japan")
loc_times_square = geolocator.geocode("Times Square, Manhattan, NY")
src = (loc_tokyo.latitude, loc_tokyo.longitude)
dist = (loc_times_square.latitude, loc_times_square.longitude)
distance.distance(src, dist).km
distance.distance(src, dist).miles

It was easy to put out. However, if you look at the address of loc_tokyo, The address is "Tokyo Tower, Tokyo Tower Street, Azabu, Minami Aoyama 6, Minato Ward, Tokyo, 105-0011, Japan". Perhaps if you set the locations of Tokyo and NY properly with lat & lon, the accuracy will increase.

10870.216621346974 # km
6754.439461884453 # mile

Recommended Posts

Geocoding in python
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
SendKeys in Python
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
Sorted list in Python
Daily AtCoder # 36 in Python
Clustering text in Python
Daily AtCoder # 2 in Python
Implement Enigma in python
Daily AtCoder # 32 in Python
Daily AtCoder # 6 in Python
Daily AtCoder # 18 in Python
Edit fonts in Python
Singleton pattern in Python
File operations in Python
Read DXF in python
Daily AtCoder # 53 in Python
Key input in Python
Use config.ini in Python
Daily AtCoder # 33 in Python
Solve ABC168D in Python
Logistic distribution in Python