In this tutorial, we’ll use the geocode module to perform a reverse lookup on several zipcodes.
geocode Module:
The Python geocode module is designed to handle geographical data and help us in pairing and matching different types of data together.
Using the pgeocode module, we could retrieve and represent the region or area-related information based on zip code information.
As a result, we can use this module for our needs.
Some of the pgeocode module’s functions are listed below:
- Data about the country/region derived from the postal code
- The distinction between postal codes
- Data from many postal codes are used to generate data for multiple regions.
Geo Code Enterprise Application APIs
I would recommend ZipCodeBase if you are seeking for a ZIP Code address lookup API for business apps. They give zip code data for over 200 nations, which is useful if your service serves consumers from all over the world. They feature a variety of APIs, including the postal code radius search API, which is highly handy for finding postal codes within a radius. Their documentation is excellent, and the greatest thing is that they offer a free plan that allows us to get started quickly and test the service.
Obtaining regional data from postal codes
Now we see how to get regional data from various postal codes that are offered as inputs. Using the geocode module, we can simply obtain the country code, state, name, and so on.
Syntax:
pgeocode.Nominatim(country name) query_postal_code(postalcode)
The Nominatim() method allows us to query country names while the query_post_code() method works on the zip codes.
To begin, Install the pgeocode module as shown below:
pip install pgeocode
Output:
Collecting pgeocode Downloading pgeocode-0.3.0-py3-none-any.whl (8.5 kB) Requirement already satisfied: requests in /usr/local/lib/python3.7/dist- packages (from pgeocode) (2.23.0) Requirement already satisfied: numpy in /usr/local/lib/python3.7/dist-packages (from pgeocode) (1.19.5) Requirement already satisfied: pandas in /usr/local/lib/python3.7/dist-packages (from pgeocode) (1.1.5) Requirement already satisfied: pytz>=2017.2 in /usr/local/ lib/python3.7/dist-packages (from pandas->pgeocode) (2018.9) Requirement already satisfied: python-dateutil>=2.7.3 in /usr/local/lib/python3.7/ dist-packages (from pandas->pgeocode) (2.8.2) Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.7/dist-packages (from python-dateutil>=2.7 .3->pandas->pgeocode) (1.15.0) Requirement already satisfied: certifi>=2017.4 .17 in /usr/local/lib/python3.7/dist-packages (from requests->pgeocode) (2021.10.8) Requirement already satisfied: chardet<4,>=3.0.2 in /usr/ local/lib/python3.7/dist-packages (from requests->pgeocode) (3.0.4) Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.7 /dist-packages (from requests->pgeocode) (2.10) Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.7/dist- packages (from requests->pgeocode) (1.24.3) Installing collected packages: pgeocode Successfully installed pgeocode-0.3.0
For example:
Approach:
- Import the pgeocode module using the import keyword
- Pass some random country name to the Nominatim() function to query country given.
- Store it in a variable.
- Here ‘fr’ means France – to check for other country codes see the Readme list of pgeocode module.
- Pass some random postal code to the query_postal_code() function to get the information (like country code, state, name) about the region given.
- Store it in another variable.
- Print the above result.
- The Exit of the Program.
Below is the implementation:
# Import the pgeocode module using the import keyword import pgeocode # Pass some random country name to the Nominatim() function to query country given # Store it in a variable # Here 'fr' means France - to check for other country code see the Readme list of # pgeocode module cntry_data = pgeocode.Nominatim('fr') # Pass some random postal code to the query_postal_code() function to # get the information (like country code, state, name) about the region given. # Store it in another variable rslt_info = cntry_data.query_postal_code("75013") # Print the above result. print(rslt_info)
Output:
postal_code 75013 country_code FR place_name Paris 13, Paris state_name ÃŽle-de-France state_code 11 county_name Paris county_code 75 community_name Paris community_code 751 latitude 48.8322 longitude 2.35245 accuracy 5 Name: 0, dtype: object
NOTE:
To check for other country codes see the Readme list of pgeocode module. For Example US - "95014", BR for Brazil etc.
To Get Geodistance between two zip codes
Another useful feature of the geocode module is the ability to calculate the geodistance between two zip codes. We can achieve the same result by using the Geodistance() function.
Approach:
- Import the pgeocode module using the import keyword.
- Pass some random country name as an argument to the GeoDistance() function and store it in a variable.
- Pass two random postal codes as the arguments to the query_postal_code() function to get the geograpical distance between the given two zipcodes.
- Store it in another variable.
- Print the above result.
- The Exit of the Program.
Below is the implementation:
# Import the pgeocode module using the import keyword import pgeocode # Pass some random country name as an argument to the GeoDistance() function and # Store it in a variable rslt_data = pgeocode.GeoDistance('fr') # Pass two random postal codes as the arguments to the query_postal_code() function # to get the geograpical distance between the given two zipcodes. # Store it in another variable fnl_info = rslt_data.query_postal_code("75013", "69006") # Print the above result. print("The geograpical distance between given two postal codes = ") print(fnl_info)
Output:
The geograpical distance between given two postal codes = 389.2183901641009