In this article, we’ll be working on the geocode module to perform a reverse lookup on different zipcodes. The entire code is pretty straightforward so we’ll be quickly going over the individual pieces.
How does the geocode module help?
The Python geocode module is built to process geographical data and help us pair and match different data together. With pgeocode module, we can fetch and represent the region or the area related information using zip code information.
And as such, we can use this module for our purpose.
In this article, we will be talking about the some of the functionalities of pgeocode module as mentioned below:
- Data about the country/region through postal code
- Difference between the postal codes
- Multiple regions data from postal code
Geo Code Enterprise Apps APIs
If you are looking for a ZIP Code address lookup API for business applications, I would recommend you to have a look at ZipCodeBase. They provide zip code data for over 200 countries, which is great if your application caters to users around the globe. They have various APIs and the postal code radius search API is very useful to find postal codes within a radius. Their documentation is top-notch and best part is their free plan to get us started quickly and try out the service.
1. Fetching regional data from postal codes
Let’s take a look at fetching regional data from different postal codes that are supplied as inputs. We can easily fetch the country code, state, name, etc using the geocode module.
Syntax of geocode:
The Nominatim() method allows us to query country names while the query_post_code() method works on the zip codes.
Let’s take an example of how this works in real-life codes. We’ll query the country code “US” and a random zip code and see what data the geocode module shows us:
data = pgeocode.Nominatim('US')
county_name Santa Clara
Name: 0, dtype: object
As you can see, we received a lot of data about our queried country and zip code inputs.
2. Geodistance between two zip codes
Another cool feature that the geocode module provides is the ability to find the geodistance between two zip codes. We can do the same with the help of the Geodistance() method. Let’s begin by initializing the method with the country in question and then input the two zipcodes that we’re looking to find the geodistance between.
data = pgeocode.GeoDistance('fr')
A lot of programs do require us to work with geographical locations and zipcodes and the geocode library makes it extremely easy to work with such complex data that would otherwise require a huge dataset available.
We hope you enjoyed learning about this topic. We’ve covered another module which works with geographical locations which is the geopy module.
References: Official documentation