GeoPy Module in
Python
MEMBERS:
ASHMITA DHAKAL (07)
ROHIT GAUTAM (09)
PRASHANT THAPALIYA (29)
AAKASH THAPA (31)
1
Introduction to ‘geopy’
A module is a file containing Python definitions and statements.
geopy - used for geocoding web services.
A Python 2 and 3 client.
Makes easier to geolocate:
Addresses
Cities
Countries
Landmarks
along with co-ordinates and vice versa.
2
geopy and geocoders
geopy provides geolocation across the globe
Uses third-party (i.e. geocoders)
 Uses Google Maps, Bing Maps or Nominatim as geolocation sevices.
Such services has its own class in geopy.geocoders
Each geocoder accepts the required settings or credentials to interact with
services
3
Installation of geopy
install geopy in command prompt by :
pip install geopy
4
Importing geopy module
In order to use geopy module, the module should be imported.
The module can be imported as:
from geopy import *
from geopy.geocoders import *
NOTE: executing geopy module for geolocation requires fast internet connection.
5
Available contents at geopy.geocoders
Baidu
Bing
Nominatim
ArcGIS
What3Words
GoogleV3
Etc.
6
Nominatim
Is a search engine
Associated with OpenStreetMap (OSM)
Provision for:
Forward search (coordinate from address)
Reverse search (look for data from coordinates)
7
Nominatim
Nomination is used to mention the application name which is used in program
to geolocate the inputs.
Nominatim is a search engine for OpenStreetMap data. This is the debugging
interface.
>>> from geopy.geocoders import Nominatim
>>> geolocator = Nominatim(user_agent=“OSM")
8
geopy module methods
Methods are the member of classes.
Use to perform specific task.
 An object method can only have access to the data known by that object.
Various methods are explained in further slides.
9
geocode() method
geocode() method is used to geolocate a query of given input address.
The output of the given program is:
(27.6877024, 85.3345680822887)
from geopy import Nominatim
geolocator = Nominatim(user_agent="OSM")
location = geolocator.geocode("Liverpool International College")
print((location.latitude, location.longitude))
10
geocode() method
geocode(query, exactly_one=True, timeout=None)
Parameters:
◦ query(String)= the address or query you wish to geocode.
◦ exactly_one(bool)= Return one result or a list of results, if available.
◦ Timeout(int)– Time, in seconds, to wait for the geocoding service to respond
11
reverse() method
Reverse(query, exactly_one=True,timeout=None,distance=None,wkid=4326)
Parameters:
◦ query(geopy.point.Point, list or tupel of (latitude,longitude))=The coordinates for which you
wish to obtain the closest human-readable addresses.
◦ exactly_one(bool)= Return one result,or a list?
◦ timeout (int) – Time, in seconds, to wait for the geocoding service to respond
◦ distance (int) – Distance from the query location, in meters, within which to search
◦ wkid (string) – WKID to use for both input and output coordinates.
12
reverse() method
reverse() method is used to find the corresponding to a set of coordinates.
The output of the given program is:
Kathmandu University, KU road, Kuttal, 28 Kilo, Dhulikhel, काभ्रेपलाञ्चोक,
बागमती अञ्चल, मध्यमाञ्चल विकास क्षेत्र, 09771, नेपाल
from geopy import *
geolocator = Nominatim(user_agent=“OSM")
location = geolocator.reverse((27.61866315,85.538226277388))
print(location.address)
13
geocode and reverse return three types of values:
None: when no value is found
Location as geopy.location.Location object when argument is true
A list of geopy.location.Location object when exactly one is false
When response is not received in allotted timeout, exception is received
(geopy.exc.GeocoderTimedOut)
14
Unit Conversion
There is provision of unit conversion on geopy in Geopy.units
Different units can be used for angle and distance conversion such as:
◦ Angle: Degrees, minutes, seconds and radians
◦ Distance: Mile, kilometer, feet, meter, nautical
15
Functions available for angle unit conversion are:
1. arcminutes(degrees=0, radians=0, arcseconds=0)
converts one of the input parameter into minutes
2. arcseconds(degrees=0, radians=0, arcminutes=0)
converts one of the input parameter into seconds
3. degrees(radians=0, arcminutes=0, arcseconds=0)
converts one of the input parameter into degrees.
4. radians(degrees=0, arcminutes=0, arcseconds=0)
converts one of the input parameter into radians.
There can be one or more arguments in each functions.
16
from geopy.units import *
print(degrees(radians= 3.14))
Output: 179.9087476710785
Functions available for distance conversion are:
1. feet( kilometres=0, meters=0, miles=0, nautical=0)
converts one of the input parameters into feet
2. kilometres(meters=0, miles=0, feet=0, nautical=0)
converts one of the input parameters into kilometres
3. meters( kilometres=0, miles=0, feet=0, nautical=0)
converts one of the input parameters into meters
4. miles( kilometres=0, meters=0, feet=0, nautical=0)
converts one of the input parameters into miles
5. nautical( kilometres=0, meters=0, miles=0, feet=0)
converts one of the input parameters into nautical
17
from geopy.units import *
print(feet(kilometers=90))
Output: 295275.59055118106
Distance Computation
calculates distance between two points by:
Geodesic distance
shortest distance on the surface of an ellipsoidal model of the earth
Great circle distance
uses a spherical model of the earth, using the mean earth radius as defined by the
International Union of Geodesy and Geophysics
18
geodesic() method
geodesic() method is used to calculate the distance between two co-ordinates
with a default of the geodesic distance available as the function
geopy.distance.distance.
from geopy import *
from geopy import Nominatim
from geopy.distance import *
geolocator = Nominatim(user_agent="OSM")
location1 = geolocator.geocode("Kathmandu")
location2 = geolocator.geocode("Pokhara")
ktm = (location1.latitude,location1.longitude)
pkh= (location2.latitude,location2.longitude)
d=str(geodesic(ktm,pkh).kilometers)
print("Distance between Kathmandu and Pokhara is :"+d+"km"
19
geodesic() method
The output of the given program is:
Distance between Kathmandu and Pokhara is :142.0431085720416km
20
great_circle()
Calculates distance from the method of great circle.
The output is: Distance between Kathmandu and Pokhara is
:141.8939645114832km
21
from geopy import *
from geopy import Nominatim
from geopy.distance import great_circle
geolocator = Nominatim(user_agent="OSM")
location1 = geolocator.geocode("Kathmandu")
location2 = geolocator.geocode("Pokhara")
ktm = (location1.latitude,location1.longitude)
pkh= (location2.latitude,location2.longitude)
d=str(great_circle(ktm,pkh).kilometers)
print("Distance between Kathmandu and Pokhara is :"+d+"km")
Different ellipsoids are available for calculating distance between two
location but the default ellipsoid is WGS-84
The sets of ellipsoids available is:
model major (km) minor (km) flattening
ELLIPSOIDS = {'WGS-84': (6378.137, 6356.7523142, 1 /298.257223563),
'GRS-80': (6378.137, 6356.7523141, 1 / 298.257222101),
'Airy (1830)': (6377.563396, 6356.256909, 1 / 299.3249646),
'Intl 1924': (6378.388, 6356.911946, 1 / 297.0),
'Clarke (1880)': (6378.249145, 6356.51486955, 1 / 293.465),
'GRS-67': (6378.1600, 6356.774719, 1 / 298.25),
}
22
We can also can change the ellipsoid model used by the geodesic formulas like:
Here, ellipsoid GRS-80 is used.
23
from geopy import *
from geopy.distance import geodesic
print(distance.geodesic(27,35,ellipsoid='GRS-80').miles)
Output:551.1330125824877
from geopy import *
from geopy.distance import geodesic
print(geodesic(27,35 ,ellipsoid=(6377.0, 6356.0, 1 / 297.05)).miles)
Output:551.0256807403239
EXCEPTIONS
geopy.exc.geopyError: all exceptions are inherited from GeopyError
geopy.exc.GeocoderNotFound exception: when the string passed in geocode()
is not recognized.
geopy.exc.GeocoderQueryError: when geopy detected input that cause
request to fail or if the geocoding service responded that request was bad.
geopy.exc.GeocoderTimedOut: when no service has been received in the
given timeout argument
geopy.exc.GeopyError: when no geocoder could be found.
Etc.
24
Uses of the module
Can be used during natural disaster assessment to locate the affected areas.
Can be used in extracting the filtered raw information from web services.
Can be used with various module.
for example: geopy with pandas is used to create a database in rows and columns.
25
References
URL: https://geopy.readthedocs.io/en/stable/
o Accessed date: 18th December,2018
URL:https://pypi.org/project/geopy/
o Accessed date: 18th December, 2018
URL:https://www.youtube.com/watch?v=5z4v4qX2_7c
o Accessed date: 19th December,2018
26
27

Geopy module in python

  • 1.
    GeoPy Module in Python MEMBERS: ASHMITADHAKAL (07) ROHIT GAUTAM (09) PRASHANT THAPALIYA (29) AAKASH THAPA (31) 1
  • 2.
    Introduction to ‘geopy’ Amodule is a file containing Python definitions and statements. geopy - used for geocoding web services. A Python 2 and 3 client. Makes easier to geolocate: Addresses Cities Countries Landmarks along with co-ordinates and vice versa. 2
  • 3.
    geopy and geocoders geopyprovides geolocation across the globe Uses third-party (i.e. geocoders)  Uses Google Maps, Bing Maps or Nominatim as geolocation sevices. Such services has its own class in geopy.geocoders Each geocoder accepts the required settings or credentials to interact with services 3
  • 4.
    Installation of geopy installgeopy in command prompt by : pip install geopy 4
  • 5.
    Importing geopy module Inorder to use geopy module, the module should be imported. The module can be imported as: from geopy import * from geopy.geocoders import * NOTE: executing geopy module for geolocation requires fast internet connection. 5
  • 6.
    Available contents atgeopy.geocoders Baidu Bing Nominatim ArcGIS What3Words GoogleV3 Etc. 6
  • 7.
    Nominatim Is a searchengine Associated with OpenStreetMap (OSM) Provision for: Forward search (coordinate from address) Reverse search (look for data from coordinates) 7
  • 8.
    Nominatim Nomination is usedto mention the application name which is used in program to geolocate the inputs. Nominatim is a search engine for OpenStreetMap data. This is the debugging interface. >>> from geopy.geocoders import Nominatim >>> geolocator = Nominatim(user_agent=“OSM") 8
  • 9.
    geopy module methods Methodsare the member of classes. Use to perform specific task.  An object method can only have access to the data known by that object. Various methods are explained in further slides. 9
  • 10.
    geocode() method geocode() methodis used to geolocate a query of given input address. The output of the given program is: (27.6877024, 85.3345680822887) from geopy import Nominatim geolocator = Nominatim(user_agent="OSM") location = geolocator.geocode("Liverpool International College") print((location.latitude, location.longitude)) 10
  • 11.
    geocode() method geocode(query, exactly_one=True,timeout=None) Parameters: ◦ query(String)= the address or query you wish to geocode. ◦ exactly_one(bool)= Return one result or a list of results, if available. ◦ Timeout(int)– Time, in seconds, to wait for the geocoding service to respond 11
  • 12.
    reverse() method Reverse(query, exactly_one=True,timeout=None,distance=None,wkid=4326) Parameters: ◦query(geopy.point.Point, list or tupel of (latitude,longitude))=The coordinates for which you wish to obtain the closest human-readable addresses. ◦ exactly_one(bool)= Return one result,or a list? ◦ timeout (int) – Time, in seconds, to wait for the geocoding service to respond ◦ distance (int) – Distance from the query location, in meters, within which to search ◦ wkid (string) – WKID to use for both input and output coordinates. 12
  • 13.
    reverse() method reverse() methodis used to find the corresponding to a set of coordinates. The output of the given program is: Kathmandu University, KU road, Kuttal, 28 Kilo, Dhulikhel, काभ्रेपलाञ्चोक, बागमती अञ्चल, मध्यमाञ्चल विकास क्षेत्र, 09771, नेपाल from geopy import * geolocator = Nominatim(user_agent=“OSM") location = geolocator.reverse((27.61866315,85.538226277388)) print(location.address) 13
  • 14.
    geocode and reversereturn three types of values: None: when no value is found Location as geopy.location.Location object when argument is true A list of geopy.location.Location object when exactly one is false When response is not received in allotted timeout, exception is received (geopy.exc.GeocoderTimedOut) 14
  • 15.
    Unit Conversion There isprovision of unit conversion on geopy in Geopy.units Different units can be used for angle and distance conversion such as: ◦ Angle: Degrees, minutes, seconds and radians ◦ Distance: Mile, kilometer, feet, meter, nautical 15
  • 16.
    Functions available forangle unit conversion are: 1. arcminutes(degrees=0, radians=0, arcseconds=0) converts one of the input parameter into minutes 2. arcseconds(degrees=0, radians=0, arcminutes=0) converts one of the input parameter into seconds 3. degrees(radians=0, arcminutes=0, arcseconds=0) converts one of the input parameter into degrees. 4. radians(degrees=0, arcminutes=0, arcseconds=0) converts one of the input parameter into radians. There can be one or more arguments in each functions. 16 from geopy.units import * print(degrees(radians= 3.14)) Output: 179.9087476710785
  • 17.
    Functions available fordistance conversion are: 1. feet( kilometres=0, meters=0, miles=0, nautical=0) converts one of the input parameters into feet 2. kilometres(meters=0, miles=0, feet=0, nautical=0) converts one of the input parameters into kilometres 3. meters( kilometres=0, miles=0, feet=0, nautical=0) converts one of the input parameters into meters 4. miles( kilometres=0, meters=0, feet=0, nautical=0) converts one of the input parameters into miles 5. nautical( kilometres=0, meters=0, miles=0, feet=0) converts one of the input parameters into nautical 17 from geopy.units import * print(feet(kilometers=90)) Output: 295275.59055118106
  • 18.
    Distance Computation calculates distancebetween two points by: Geodesic distance shortest distance on the surface of an ellipsoidal model of the earth Great circle distance uses a spherical model of the earth, using the mean earth radius as defined by the International Union of Geodesy and Geophysics 18
  • 19.
    geodesic() method geodesic() methodis used to calculate the distance between two co-ordinates with a default of the geodesic distance available as the function geopy.distance.distance. from geopy import * from geopy import Nominatim from geopy.distance import * geolocator = Nominatim(user_agent="OSM") location1 = geolocator.geocode("Kathmandu") location2 = geolocator.geocode("Pokhara") ktm = (location1.latitude,location1.longitude) pkh= (location2.latitude,location2.longitude) d=str(geodesic(ktm,pkh).kilometers) print("Distance between Kathmandu and Pokhara is :"+d+"km" 19
  • 20.
    geodesic() method The outputof the given program is: Distance between Kathmandu and Pokhara is :142.0431085720416km 20
  • 21.
    great_circle() Calculates distance fromthe method of great circle. The output is: Distance between Kathmandu and Pokhara is :141.8939645114832km 21 from geopy import * from geopy import Nominatim from geopy.distance import great_circle geolocator = Nominatim(user_agent="OSM") location1 = geolocator.geocode("Kathmandu") location2 = geolocator.geocode("Pokhara") ktm = (location1.latitude,location1.longitude) pkh= (location2.latitude,location2.longitude) d=str(great_circle(ktm,pkh).kilometers) print("Distance between Kathmandu and Pokhara is :"+d+"km")
  • 22.
    Different ellipsoids areavailable for calculating distance between two location but the default ellipsoid is WGS-84 The sets of ellipsoids available is: model major (km) minor (km) flattening ELLIPSOIDS = {'WGS-84': (6378.137, 6356.7523142, 1 /298.257223563), 'GRS-80': (6378.137, 6356.7523141, 1 / 298.257222101), 'Airy (1830)': (6377.563396, 6356.256909, 1 / 299.3249646), 'Intl 1924': (6378.388, 6356.911946, 1 / 297.0), 'Clarke (1880)': (6378.249145, 6356.51486955, 1 / 293.465), 'GRS-67': (6378.1600, 6356.774719, 1 / 298.25), } 22
  • 23.
    We can alsocan change the ellipsoid model used by the geodesic formulas like: Here, ellipsoid GRS-80 is used. 23 from geopy import * from geopy.distance import geodesic print(distance.geodesic(27,35,ellipsoid='GRS-80').miles) Output:551.1330125824877 from geopy import * from geopy.distance import geodesic print(geodesic(27,35 ,ellipsoid=(6377.0, 6356.0, 1 / 297.05)).miles) Output:551.0256807403239
  • 24.
    EXCEPTIONS geopy.exc.geopyError: all exceptionsare inherited from GeopyError geopy.exc.GeocoderNotFound exception: when the string passed in geocode() is not recognized. geopy.exc.GeocoderQueryError: when geopy detected input that cause request to fail or if the geocoding service responded that request was bad. geopy.exc.GeocoderTimedOut: when no service has been received in the given timeout argument geopy.exc.GeopyError: when no geocoder could be found. Etc. 24
  • 25.
    Uses of themodule Can be used during natural disaster assessment to locate the affected areas. Can be used in extracting the filtered raw information from web services. Can be used with various module. for example: geopy with pandas is used to create a database in rows and columns. 25
  • 26.
    References URL: https://geopy.readthedocs.io/en/stable/ o Accesseddate: 18th December,2018 URL:https://pypi.org/project/geopy/ o Accessed date: 18th December, 2018 URL:https://www.youtube.com/watch?v=5z4v4qX2_7c o Accessed date: 19th December,2018 26
  • 27.

Editor's Notes

  • #19 (2a + b)/3 = 6371.0087714150598 kilometers approx 6371.009 km (for WGS-84)