Sometimes we want to fetch addresses using the ZIP Code. There are few Python modules that allow us to find the address using zip code. In this article, we will be focusing on Python pgeocode module, in detail.
So, let us get started!
Table of Contents
First, what is served by Python pgeocode module?
Python programming provides us with various modules to work with the data and also fetch different details in terms of statistics, predictions, etc.
Apart from this, Python also provides us with functionalities to get information about the location in terms of GPS co-ordinates, region data, etc using postal codes.
Yes, we have Python pgeocode module to help us in geographical data as well. With pgeocode module, we can fetch and represent the region or the area related information using zip code information.
Thus, all the entire country/region related information can be obtained through postal codes itself.
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
Let us now have a look at each of them in detail in the below section.
ZIP Code APIs for Enterprise Applications
If you are building an enterprise application, it’s always better to go with a professional service. I have used ZipCodeBase, which provides postal code data from all around the world. If your application caters to worldwide users, it’s much better than competitors because they have data for all the countries. The best part is their free plan to get us started quickly and try out the service. Their documentation is top-notch and one of the best features I liked is the postal code radius search API.
1. Fetching data about the region through postal code
Using pgeocode, we can easily fetch data with regards to different regions and countries through the postal codes.
For the same, we need to pass the postal codes to the function and it returns the data about the country ode, state name, etc.
pgeocode.Nominatim(country) query_postal_code(postal code)
pgeocode.Nominatim() function helps us query the geographical data by feeding a country name or a postal code. Further, the
query_postal_code() function helps us query information about the area through the postal code of a particular country.
In the below piece of code, we have imported the library into the Python environment. Further, we have set the country to ‘US’ through pgeocode.Nomination() function. After which, we have passed the postal code of ‘US’ to the query_postal_code() function to get the relevant data regarding the country and addresses.
import pgeocode data = pgeocode.Nominatim('US') print(data.query_postal_code("95014"))
As a result, we get the information with regards to the area represented by the postal code.
postal_code 95014 country_code US place_name Cupertino state_name California state_code CA county_name Santa Clara county_code 85 community_name NaN community_code NaN latitude 37.318 longitude -122.045 accuracy 4 Name: 0, dtype: object
2. Difference between two postal codes
The pgeocode library also provides us with GeoDistance that helps us with the calculation of distances between the postal codes with query_postal_code() function.
For the same, we initialize the GeoDistance class using
GeoDistance() function. Further to which, we point the query_postal_code() function to the initialized class. We also pass the two postal codes to the function to get the distance between them in kms.
import pgeocode data = pgeocode.GeoDistance('fr') print(data.query_postal_code("75013", "69006"))
3. Data of various regions within the same country code
The pgeocode module also provides the feature to fetch the data of multiple regions within the same country code at a single go. For the same, it makes use of query_postal_code() under the pgeocode.Nominatim class.
Initially, we set the country code using Nominatim() function. Ahead of it, we point the object of it to query_postal_code() function passing multiple postal codes as parameter to it.
In the below example, we have passed the postal codes of the country code ‘fr’ to get the region related details.
import pgeocode data = pgeocode.Nominatim('fr') print(data.query_postal_code(["75013", "69006"]))
By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question.
For more such posts related to Python programming and modules, Stay tuned with us.
Till then, Happy Learning!! 🙂