2016年4月28日 星期四

Raspberry Pi: Geolocation

since: 2016/04/28
update: 2016/04/28
reference:
1. geocoder : Python Package Index
2. geopy: Python Package Index
3. python-geoip — python-geoip

A. Get current IP location
    1. install geocoder (for python2 only)
        $ pip --upgrade geocoder

    2. get latitude(緯度) and longitude(經度) from current IP
pi@raspberrypi:~ $ python
Python 2.7.9 (default, Mar  8 2015, 00:52:26)
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import geocoder
>>> g = geocoder.ip('me')
>>> g.latlng
[23.5, 121.0]
>>> exit()


-----------------------------------------------------------------------------------------------

B. Measuring Distance
    1. install geopy (for both python2 and python3)
       $ sudo pip install geopy
       $ sudo pip install python-geoip-python3

    2. Measuring Distance (Using great-circle distance)
pi@raspberrypi:~ $ python3
Python 3.4.2 (default, Oct 19 2014, 13:31:11)
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from geopy.distance import great_circle
>>> newport_ri = (41.49008, -71.312796)
>>> cleveland_oh = (41.499498, -81.695391)
>>> print(great_circle(newport_ri, cleveland_oh).meters)
864456.7616296598
>>> exit()


-----------------------------------------------------------------------------------------------

C. Looks up the IP information (test for python2 only)
    1. install geoip
       $ sudo pip install python-geoip
       $ sudo pip install python-geoip-python3
       $ sudo pip install python-geoip-geolite2

    2. Looks up the IP information (real IP)
pi@raspberrypi:~ $ python
Python 2.7.9 (default, Mar  8 2015, 00:52:26)
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from geoip import geolite2
>>> match = geolite2.lookup('17.0.0.1')
>>> match.location
(37.323, -122.0322)
>>> exit()