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
$ 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