Geopy Python Module
Geopy Python Module
ON
GEOPY PYTHON MODULE
PRESENT BY:
Sayam Baidar(02)
Sijan Bhandari(04)
Rabina Twayana(29)
1
CONTENTS
Python Module
Introduction to GeoPy
Installation of GeoPy
Geocoding Vs Reverse Geocoding
Commonly Used method in GeoPy
Uses of GeoPy in Distance Calculation
Accessing data in GeoPy
Exception in GeoPy
2
Python Module
A module is a file consisting of Python code.
A module can define functions, classes and variables.
A module can also include runnable code.
A module is a file containing Python definitions and statements
3
Introduction to GeoPy
Geopy is a python module that includes geocoders class for calling several geocoding
web services such as:
• Open StreetMap Nominatim
• ESRI ArcGIS
• Google Geocoding API(V3)
• Bing Maps API
• Yandex
• OpenMapQuest
• NaviData
• GeoNames
• DeocodeFare etc
4
Contd….
Makes easy to locate :
• Co-ordinate
• Address
• Geographic location(Mountains, rivers etc)
• Landmarks
• Cities
• Countries across the globe using geocoders and other data sources
5
Installation using pip
pip is install by default if we are using python 2>=2.7.9 or python 3>=3.4
If not, then install pip or install latest python for windows which support pip from
https://www.python.org/
6
To check installed pip version
7
geopy installation
In command prompt run:
pip install geopy
8
Geocoder
A class for handling Geocoding and Reverse Geocoding
Geocoders define at least a geocode() method and a reverse() method.
Each geolocation service we might use, such as Google Maps, Bing Maps, or
Nominatim etc, has its own class in geopy.geocoders
Geocoder accepts any settings needed to interact with its service, e.g., an API key,
during its initialization.
An API key is a unique identifier used to authenticate a user, or developer to an API
which allow communication with services.
from geopy.geocoders import Nominatim
geolocator=Nominatim(user_agent=“specify_your_app_name_here”)
9
Geocoding Vs Reverse Geocoding
Geocoding(Forward Geocoding) Reverse Geocoding
The process of converting addresses (like a The process of converting geographic
street address) into geographic coordinates coordinates into a human-readable address.
(like latitude and longitude),
Used to place markers on a map, or position Used to find the address corresponding to a set
the map. of coordinates:
10
Commonly used methods in geopy
geocode()
Syntax: geocode(query)
where, query(str) is address we wish to geocode
reverse()
Syntax: reverse(query)
where query is coordinates(lat,long) for which we wish to obtain human-readable
addresses.
Both method returns geopy.location.Location object
11
geocode() method
Resolves given address into pair of coordinates
Output:
Kathmandu University, KU road, Kuttal, Srikhandapur, Dhulikhel, काभ्रेपलाञ्चोक, Bagmati
Pradesh, 09771, Nepal
(27.61866315, 85.53822627738802)
12
How to get an API key for OpenMapQuest?
Signed-up first from https://developer.mapquest.com
Click on "Create a New Key"
Filled in "App Name"
Filled in "Callback URL“
Click on “Create app”
Click on uparrow of app name
Get API key for OpenMapQuest
13
Geocoding using API key
Output:
27.6474649
85.3348047874904
14
reverse() method
Resolve pair of co-ordinates to an address.
Output
Kathmandu University, KU road, Kuttal, Srikhandapur, Dhulikhel, काभ्रेपलाञ्चोक, Bagmati
Pradesh, 09771, Nepal
(27.61866315, 85.53822627738802)
15
Distance Calculation
GeoPy can calculate distance between two points.
Mainly two types of distance are calculated by GeoPy:
• Great Circle Distance
• Geodesic Distance
16
Great Circle Distance
Great circle distance is the shortest distance between two points on the surface of a
sphere
Uses spherical model of the Earth to calculate the surface distance between two points.
Syntax:
great_circle (args1, args2)
where args1=location1 co-ordinate value
args2= location2 co-ordinate value
17
Output:
388.4096417472016 km
241.3465621689344
388409.6417472016
18
Geodesic Distance
A geodesic is the shortest path between two points on a curved surface, analogous to a
straight line on a plane surface
Uses ellipsoidal model of the Earth to calculate shortest distance between two point on
the surface of Earth.
More accurate than great_circle distance
default is WGS-84 ellipsoid which is most globally accurate
Includes few other models in the distance.ELLIPSOIDS dictionary:
• GRS-80
• GRS-67
• Airy (1830)
• Intl 1924
• Clarke (1880)
19
Syntax
geodesic(args1, args2)
Output
386.2061376343612 km
386.20613763557986 km
20
Data
Class geopy.location.Location
Access the properties
• address: return type- Unicode
Location as a formatted string returned by the geocoder, depending on the service
• latitude: return type- float
Location’s latitude
• longitude: return type –float
Location’s longitude
• altitude :return type- float
Location’s altitude.
• raw: return type-dict
Location’s raw, unparsed geocoder response.
21
22
Contd..
Class geopy.point.Point
A geodetic point with latitude, longitude, and altitude.
Latitude and longitude are floating point values in degrees, minutes, seconds. Altitude
is a floating point value in kilometers.
Points can be created in a number of ways…..
23
Output
41 30m 0s S, 81 0m 0s W
41 30m 0s S, 81 0m 0s W
41 30m 0s S, 81 0m 0s W, 2.5km
23 26m 22s N, 23 27m 30s E, 33.796224km
3 26m 22s N, 23 27m 30s E
24
Exception class Hierarchy in GeoPy
BaseException
Exception
GeopyError
25
Exception
In Python, all exceptions must be instances of a class that derives from BaseException,
then Exception and the GeopyError
Geopy-specific exceptions are all inherited from GeopyError.
Three Exception class are inherited from GeopyError Class. They are
• ConfigurationError
When instantiating a geocoder, the arguments given were invalid.
• GeocoderNotFound
Caller requested the geocoder matching a string, e.g., "google" > GoogleV3, but no
geocoder could be found.
26
Contd
• GeocoderServiceError
There was an exception caused when calling the remote geocoding service, and no more specific
exception could be raised by geopy.
When calling geocoders’ geocode or reverse methods, this is the most generic exception that can
be raised, and any non-geopy exception will be caught and turned into this.
27
GeocoderQueryError
GeocoderAuthenticationFailure
GeocoderInsufficientPrivileges GeocoderParseError
28
EXCEPTION DESCRIPTION
GeocoderQueryError Either geopy detected input that would cause a request to fail, or a request was made and the remote
geocoding service responded that the request was bad
GeocoderUnavailable when it was not possible to establish a connection to the remote geocoding service
GeocoderParseError Geopy could not parse the service’s response. This is probably due to a bug in geopy.
GeocoderInsufficientPrivileges The remote geocoding service refused to fulfill a request using the account credentials given.
GeocoderAuthenticationFailure The remote geocoding service rejected the API key, geocoder instantiated with.
GeocoderTimedOut The call to the geocoding service was aborted because no response has been received within the
timeout argument .Some services are just consistently slow, and a higher timeout may be needed to
use them.
29
Output:
27.61866315 85.53822627738802
OR
Error
Service timed out
30
GeocoderQueryError
31
ConfigurationError
32
REFRENCES
GeoPy’s documentation. (2017, February 2). Retrieved January 13, 2020, from GeoPy
Stable: https://geopy.readthedocs.io/en/stable/
Python, 3.8.1. (2019, December 18). Retrieved January 7, 2020, from
https://www.python.org/downloads/
Reddy., G. C. (2019, September 1). Python Modules. Retrieved January 11, 2020, from
Python Tutorials for Beginners: http://www.gcreddy.com/python/python-modules/
THANK YOU FOR YOUR
ATTENTATION!!!!!!
34
ANY QUESTIONS?
35