{"id":26149,"date":"2021-12-14T08:46:13","date_gmt":"2021-12-14T03:16:13","guid":{"rendered":"https:\/\/python-programs.com\/?p=26149"},"modified":"2021-12-14T08:46:13","modified_gmt":"2021-12-14T03:16:13","slug":"python-geocode-module-for-reverse-zipcode-lookup","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-geocode-module-for-reverse-zipcode-lookup\/","title":{"rendered":"Python geocode Module for Reverse Zipcode lookup"},"content":{"rendered":"
In this tutorial, we’ll use the geocode module to perform a reverse lookup on several zipcodes.<\/p>\n
geocode Module:<\/strong><\/p>\n The Python geocode module is designed to handle geographical data and help us in pairing and matching different types of data together.<\/p>\n Using the pgeocode module, we could retrieve and represent the region or area-related information based on zip code information.<\/p>\n As a result, we can use this module for our needs.<\/p>\n Some of the pgeocode module’s functions are listed below:<\/p>\n Geo Code Enterprise Application APIs<\/strong><\/p>\n 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.<\/p>\n 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.<\/p>\n Syntax:<\/strong><\/p>\n The Nominatim() method allows us to query country names while the query_post_code() method works on the zip codes.<\/p>\n To begin, Install the pgeocode<\/strong> module as shown below:<\/p>\n Output:<\/strong><\/p>\n For example:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n NOTE:<\/strong><\/p>\n 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.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 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 …<\/p>\n\n
Obtaining regional data from postal codes<\/strong><\/h4>\n
pgeocode.Nominatim(country name)\r\nquery_postal_code(postalcode)<\/pre>\n
pip\u00a0install\u00a0pgeocode<\/pre>\n<\/div>\n
Collecting pgeocode Downloading pgeocode-0.3.0-py3-none-any.whl (8.5 kB)\r\n Requirement already satisfied: requests in \/usr\/local\/lib\/python3.7\/dist-\r\npackages (from pgeocode) (2.23.0) Requirement already satisfied: numpy in \r\n\/usr\/local\/lib\/python3.7\/dist-packages (from pgeocode) (1.19.5) Requirement \r\nalready satisfied: pandas in \/usr\/local\/lib\/python3.7\/dist-packages (from \r\npgeocode) (1.1.5) Requirement already satisfied: pytz>=2017.2 in \/usr\/local\/\r\nlib\/python3.7\/dist-packages (from pandas->pgeocode) (2018.9) Requirement \r\nalready satisfied: python-dateutil>=2.7.3 in \/usr\/local\/lib\/python3.7\/\r\ndist-packages (from pandas->pgeocode) (2.8.2) Requirement already satisfied: \r\nsix>=1.5 in \/usr\/local\/lib\/python3.7\/dist-packages (from python-dateutil>=2.7\r\n.3->pandas->pgeocode) (1.15.0) Requirement already satisfied: certifi>=2017.4\r\n.17 in \/usr\/local\/lib\/python3.7\/dist-packages (from requests->pgeocode)\r\n (2021.10.8) Requirement already satisfied: chardet<4,>=3.0.2 in \/usr\/\r\nlocal\/lib\/python3.7\/dist-packages (from requests->pgeocode) (3.0.4) \r\nRequirement already satisfied: idna<3,>=2.5 in \/usr\/local\/lib\/python3.7\r\n\/dist-packages (from requests->pgeocode) (2.10) Requirement already satisfied:\r\nurllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in \/usr\/local\/lib\/python3.7\/dist-\r\npackages (from requests->pgeocode) (1.24.3) Installing collected packages:\r\npgeocode Successfully installed pgeocode-0.3.0<\/pre>\n
\n
# Import the pgeocode module using the import keyword\r\nimport pgeocode\r\n# Pass some random country name to the Nominatim() function to query country given\r\n# Store it in a variable\r\n# Here 'fr' means France - to check for other country code see the Readme list of \r\n# pgeocode module\r\ncntry_data = pgeocode.Nominatim('fr')\r\n# Pass some random postal code to the query_postal_code() function to\r\n# get the information (like country code, state, name) about the region given.\r\n# Store it in another variable\r\nrslt_info = cntry_data.query_postal_code(\"75013\")\r\n# Print the above result.\r\nprint(rslt_info)<\/pre>\n
postal_code 75013 \r\ncountry_code FR \r\nplace_name Paris 13, Paris \r\nstate_name \u00cele-de-France \r\nstate_code 11 \r\ncounty_name Paris \r\ncounty_code 75 \r\ncommunity_name Paris \r\ncommunity_code 751 \r\nlatitude 48.8322 \r\nlongitude 2.35245 \r\naccuracy 5 \r\nName: 0, dtype: object<\/pre>\n
To check for other country codes see the Readme list of pgeocode module.\r\nFor Example US - \"95014\", BR for Brazil etc.<\/pre>\n
To Get Geodistance between two zip codes<\/h4>\n
\n
# Import the pgeocode module using the import keyword\r\nimport pgeocode\r\n# Pass some random country name as an argument to the GeoDistance() function and\r\n# Store it in a variable\r\nrslt_data = pgeocode.GeoDistance('fr')\r\n# Pass two random postal codes as the arguments to the query_postal_code() function\r\n# to get the geograpical distance between the given two zipcodes.\r\n# Store it in another variable\r\nfnl_info = rslt_data.query_postal_code(\"75013\", \"69006\")\r\n# Print the above result.\r\nprint(\"The geograpical distance between given two postal codes = \")\r\nprint(fnl_info)\r\n<\/pre>\n
The geograpical distance between given two postal codes = \r\n389.2183901641009<\/pre>\n","protected":false},"excerpt":{"rendered":"