2. 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
3. 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
5. 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
6. Available contents at geopy.geocoders
īļBaidu
īļBing
īļNominatim
īļArcGIS
īļWhat3Words
īļGoogleV3
īļEtc.
6
7. Nominatim
īļIs a search engine
īļAssociated with OpenStreetMap (OSM)
īļProvision for:
īļForward search (coordinate from address)
īļReverse search (look for data from coordinates)
7
8. 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
9. 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
10. 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
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() 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
14. īļ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
15. 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
16. 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
17. 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
18. 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
19. 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
21. 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")
22. 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
23. 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
24. 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
25. 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