[PYTHON] Verify coordinates on Google Map with Geocoder

On a certain website, I felt that the Google Maps flag of the facility was off, so Compare the latitude and longitude calculated from the address and facility name with the latitude and longitude specified on Google Maps.

Check the latitude and longitude of Tokyo Tower on the Geocoding site

If you search for Tokyo Tower on the Geocoding site, you can find the address and coordinates.

〒105-0011 4-2-8 Shibakoen, Minato-ku, Tokyo Tokyo Tower Coordinates: 35.658581, 139.745433

Get latitude and longitude from address with Geocoder

First, install geocoder.

pip install geocoder

Python code is very easy.

geocode.py


import geocoder
n = "Tokyo Tower"
a = "4-2-8 Shibakoen, Minato-ku, Tokyo"
p = [35.658581, 139.745433]

g = geocoder.google(a)
print(a, p,"<-->", g.latlng)

g = geocoder.google(n)
print(n, p,"<-->", g.latlng)

Calculate the distance between points

With this alone, I'm not sure how much it is off, so [Python code that calculates the distance from the latitude and longitude of two points] Borrow (http://blogs.yahoo.co.jp/qga03052/33991636.html) and calculate the distance.

geocode.py


import geocoder
import math

#Calculation of distance between two points(Python)
# http://blogs.yahoo.co.jp/qga03052/33991636.html
def deg2rad(deg):
    return( deg * (2 * math.pi) / 360 )

def Hubeny(lat1, lon1, lat2, lon2) :
    a =6378137.000
    b =6356752.314140
    e =math.sqrt( (a**2 - b**2) / a**2 )
    e2 =e**2
    mnum =a * (1 - e2)

    my =deg2rad((lat1+lat2) /2.0)
    dy =deg2rad( lat1-lat2)
    dx =deg2rad( lon1-lon2)
    sin =math.sin(my)
    w =math.sqrt(1.0-e2 * sin *sin)
    m =mnum /(w *w *w)
    n =a/w
    dym =dy*m
    dxncos=dx*n*math.cos(my)
    return( math.sqrt( dym**2 + dxncos**2) )

n = "Tokyo Tower"
a = "4-2-8 Shibakoen, Minato-ku, Tokyo"
p = [35.658581, 139.745433]

g = geocoder.google(a)
dist = Hubeny(p[0], p[1], g.latlng[0],g.latlng[1])
print(a, p,"<-->", g.latlng,dist)

g = geocoder.google(n)
dist = Hubeny(p[0], p[1], g.latlng[0],g.latlng[1])
print(n, p,"<-->", g.latlng,dist)

As a result, there was a deviation of several meters when I thought that they would match perfectly.

4-2-8 Shibakoen, Minato-ku, Tokyo[35.658581, 139.745433] <--> [35.6585817, 139.7454636] 2.771940296311124
Tokyo Tower[35.658581, 139.745433] <--> [35.6585696, 139.745484] 4.78817294783698

This degree of deviation is sufficient, but since there was a facility that was off by several hundred meters on Google Map, It is a good idea to web scrape the relevant part with BeautifulSoap etc. and verify it.

> var myLatLng = new google.maps.LatLng(37.695042, 140.127955);

Because it is often described in

r = re.compile(r'LatLng\((.*),(.*)\)')
m = re.search(r, str)
   if m:
      p = [float(m.group(1)),float(m.group(2))]```

It seems that you can find it with the regular expression.

Recommended Posts

Verify coordinates on Google Map with Geocoder
Display multiple markers on Google Map
Play with Turtle on Google Colab
Introducing Google Map API with rails
Map rent information on a map with python
Machine learning with Pytorch on Google Colab
Folium: Visualize data on a map with Python
Save Twitter's tweets with Geo in CSV and plot them on Google Map.
Visualize grib2 on a map with python (matplotlib)
Implement Sign In With Google on the backend side
PIL with Python on Windows 8 (for Google App Engine)
[Ruby on Rails] Multiple pins, balloons, and links on Google map
Display the address entered using Rails gem'geocoder' on Google Map
Deploy your Go app on Google App Engine with GitHub Actions
Deep Learning with Shogi AI on Mac and Google Colab