GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing thepythoncode.com — How to Get Geolocation in Python - The Python Code Unlock the secrets of your code with our AI-powered Code Explainer. Take a look! Geocoding is the process of transforming a description of a location (such as a physical address or the name of a place) into a pair of latitude and longitude on the Earth's surface for that place. It also refers to converting geographic coordinates to a description of a location (such as an address); this is often called reverse geocoding. In this tutorial, we will learn how to do both with the help of the GeoPy library in Python. However, if you want to geolocate IP addresses, then this tutorial is for you. GeoPy is a Python client that provides several popular geocoding web services; it makes it easy for Python developers to locate the coordinates of an address, a city, or a country, and vice-versa. To get started, let's install it:
GeoPy provides many geocoding service wrappers, such as OpenStreetMap Nominatim, Google Geocoding API V3, Bing Maps, and more. In this tutorial, we gonna stick with OpenStreetMap Nominatim. Here is what we'll be covering:
Getting Latitude and Longitude from an Address (Geocoding)In this section, we will use OpenStreetMap Nominatim API to get latitude and longitude from a physical address, city, or any location name. Let's import the library first:
Notice we chose Nominatim geocoder, now creating a new instance of it:
Now, let's try to get geographic data from an address:
Output:
Awesome, we have latitude at As you can see, the Nominatim API does not require a full address (that consists of street, house number, and city), you can also pass business addresses and points of your interests, it supports that! However, if you call this function repeatedly (such as iterating over a list of addresses), you will encounter a timed-out error, and that's because if you read the Nominatim Usage Policy, it requires you to use a maximum of 1 request per second, and that's absolutely acceptable since it's a free service. As a result, the below function respects that requirement and sleeps for one second before making a request:
So whenever a timed-out error is raised, we catch that and call the function recursively, and this function will sleep for another second and hopefully, retrieve the result:
Output:
Getting Address from Latitude and Longitude (Reverse Geocoding)Now, to retrieve an address, city, country, and various other information only from latitude and longitude, we simply use the The following function reverses the coordinates while respecting Nominatim usage policy:
So this function expects latitude and longitude as parameters and returns the raw geographic data, here is an example usage:
Output:
So this will return all address data, including state, town, postcode, districts, and more. If you wish to return this information in a specific language, you can set ConclusionAs always, we only saw simple examples of what GeoPy can do; I highly suggest you read the documentation if you're interested in more advanced utilities. Learn also: How to Get Domain Name Information in Python. Happy Coding ♥ Want to code smarter? Our Python Code Assistant is waiting to help you. Try it now!
Sharing is caring!
Read alsoHow to Use Google Drive API in PythonLearn how you can use Google Drive API to list files, search for specific files or file types, download and upload files from/to Google Drive in Python. How to Use Github API in PythonUsing Github Application Programming Interface v3 to search for repositories, users, making a commit, deleting a file, and more in Python using requests and PyGithub libraries. How to Use Google Custom Search Engine API in PythonLearning how to create your own Google Custom Search Engine and use its Application Programming Interface (API) in Python. CommentsGot a coding query or need some guidance before you comment? Check out this Python Code Assistant for expert advice and handy tips. It's like having a coding tutor right in your fingertips! |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |