Package: pgeocode
Postal code geocoding and distance calculationspgeocode is a Python library for high performance off-line querying of GPS coordinates, region name and municipality name
from postal codes. Distances between postal codes as well as general
distance queries are also supported.
The usedGeoNamesdatabase includes postal codes for 83 countries.Currently, only queries within the same country are supported.For additional documentation seepgeocode.readthedocs.io.Installationpgeocode requires Python 3.8+ as well asnumpyandpandaspackages. It can be installed with,pip install pgeocodeorconda install -c conda-forge pgeocodeQuickstartPostal code queries>>>importpgeocode>>>nomi=pgeocode.Nominatim('fr')>>>nomi.query_postal_code("75013")postal_code75013country_codeFRplace_nameParis13state_nameÎle-de-Francestate_code11county_namePariscounty_code75community_namePariscommunity_code751latitude48.8322longitude2.3561accuracy5>>>nomi.query_postal_code(["75013","69006"])postal_codeplace_namestate_namelatitudelongitude075013Paris13Île-de-France48.83222.3561169006Lyon06Auvergne-Rhône-Alpes45.76794.8506Place name queries>>>importpgeocode>>>nomi=pgeocode.Nominatim('fr')>>>nomi.query_location("Antibes",top_k=3)country_codepostal_codeplace_namestate_namestate_code...community_namecommunity_codelatitudelongitudeaccuracy49553FR06160AntibesProvence-Alpes-Côted'Azur        93.0  ...         Grasse            061  43.5858    7.1083         549787FR06600AntibesProvence-Alpes-Côted'Azur        93.0  ...         Grasse            061  43.5858    7.1083         549788FR06601CEDEXAntibesProvence-Alpes-Côted'Azur        93.0  ...         Grasse            061  43.5858    7.1083         5>>>nomi.query_location("Straassborg",top_k=3,fuzzy_threshold=80)country_codepostal_codeplace_namestate_namestate_code...community_namecommunity_codelatitudelongitudeaccuracy25461FR67000StrasbourgGrandEst44.0...Strasbourg67848.58397.7455525462FR67001CEDEXStrasbourgGrandEst44.0...Strasbourg67848.58397.7455525463FR67002CEDEXStrasbourgGrandEst44.0...Strasbourg67848.58397.74555Distance calculations>>>dist=pgeocode.GeoDistance('fr')>>>dist.query_postal_code("75013","69006")389.156>>>dist.query_postal_code(["75013","75014","75015"],["69006","69005","69004"])array([389.15648697,390.12577967,390.49857655])Geocoding formatThe result of a geo-localistion query is apandas.DataFramewith the following columns,country_code: iso country code, 2 characterspostal_code: postal codeplace_name: place name (e.g. town, city etc)state_name: 1. order subdivision (state)state_code: 1. order subdivision (state)county_name: 2. order subdivision (county/province)county_code: 2. order subdivision (county/province)community_name: 3. order subdivision (community)community_code: 3. order subdivision (community)latitude: estimated latitude (wgs84)longitude: estimated longitude (wgs84)accuracy: accuracy of lat/lng from 1=estimated to 6=centroidConfiguration and defaultsStorage directoryDefaults to~/.cache/pgeocode, it is the directory where data is downloaded
for later consumption. It can be changed using the environment variablePGEOCODE_DATA_DIR, i.e.exportPGEOCODE_DATA_DIR=/tmp/pgeocode_data.Data sourcesData sources are provided as a list in thepgeocode.DOWNLOAD_URLvariable.
The default value is,DOWNLOAD_URL=["https://download.geonames.org/export/zip/{country}.zip","https://symerio.github.io/postal-codes-data/data/geonames/{country}.txt",]Data sources are tried from first to last until one works. Here the second link is a mirror
of the first.It is also possible to extend this variable with third party data sources, as
long as they follow the same format. See for instancepostal-codes-datarepository for examples of data files.LicenseThe pgeocode package is distributed under the 3-clause BSD license.Supported countriesThe list of countries available in the GeoNames database, with the corresponding country codes, are given below,Andorra (AD), Argentina (AR), American Samoa (AS), Austria (AT), Australia (AU), Åland Islands (AX), Azerbaijan (AZ), Bangladesh (BD), Belgium (BE), Bulgaria (BG), Bermuda (BM), Brazil (BR), Belarus (BY), Canada (CA), Switzerland (CH), Chile (CL), Colombia (CO), Costa Rica (CR), Cyprus (CY), Czechia (CZ), Germany (DE), Denmark (DK), Dominican Republic (DO), Algeria (DZ), Estonia (EE), Spain (ES), Finland (FI), Federated States of Micronesia (FM), Faroe Islands (FO), France (FR), United Kingdom of Great Britain and Northern Ireland (GB), French Guiana (GF), Guernsey (GG), Greenland (GL), Guadeloupe (GP), Guatemala (GT), Guam (GU), Croatia (HR), Haiti (HT), Hungary (HU), Ireland (IE), Isle of Man (IM), India (IN), Iceland (IS), Italy (IT), Jersey (JE), Japan (JP), Republic of Korea (KR), Liechtenstein (LI), Sri Lanka (LK), Lithuania (LT), Luxembourg (LU), Latvia (LV), Monaco (MC), Republic of Moldova (MD), Marshall Islands (MH), The former Yugoslav Republic of Macedonia (MK), Northern Mariana Islands (MP), Martinique (MQ), Malta (MT), Malawi (MW), Mexico (MX), Malaysia (MY), New Caledonia (NC), Netherlands (NL), Norway (NO), New Zealand (NZ), Peru (PE), Philippines (PH), Pakistan (PK), Poland (PL), Saint Pierre and Miquelon (PM), Puerto Rico (PR), Portugal (PT), Palau (PW), Réunion (RE), Romania (RO), Serbia (RS), Russian Federation (RU), Sweden (SE), Singapore (SG), Slovenia (SI), Svalbard and Jan Mayen Islands (SJ), Slovakia (SK), San Marino (SM), Thailand (TH), Turkey (TR), Ukraine (UA), United States of America (US), Uruguay (UY), Holy See (VA), United States Virgin Islands (VI), Wallis and Futuna Islands (WF), Mayotte (YT), South Africa (ZA)SeeGeoNames databasefor more information.