Skip to main content

pgeocode

pypi condaforge rdfd GHactions

Postal code geocoding and distance calculations

pgeocode 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 used GeoNames database includes postal codes for 83 countries.

Currently, only queries within the same country are supported.

For additional documentation see pgeocode.readthedocs.io.

Installation

pgeocode requires Python 3.8+ as well as numpy and pandas packages. It can be installed with,

pip install pgeocode

or

conda install -c conda-forge pgeocode

Quickstart

Postal code queries

>>> import pgeocode

>>> nomi = pgeocode.Nominatim('fr')
>>> nomi.query_postal_code("75013")
postal_code               75013
country_code                 FR
place_name             Paris 13
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.3561
accuracy                      5

>>> nomi.query_postal_code(["75013", "69006"])
      postal_code place_name            state_name  latitude  longitude
0       75013   Paris 13         Île-de-France   48.8322     2.3561
1       69006    Lyon 06  Auvergne-Rhône-Alpes   45.7679     4.8506

Place name queries

>>> import pgeocode

>>> nomi = pgeocode.Nominatim('fr')
>>> nomi.query_location("Antibes", top_k=3)
    country_code  postal_code place_name                  state_name  state_code  ... community_name community_code latitude longitude  accuracy
49553           FR        06160    Antibes  Provence-Alpes-Côte d'Azur        93.0  ...         Grasse            061  43.5858    7.1083         5
49787           FR        06600    Antibes  Provence-Alpes-Côte d'Azur        93.0  ...         Grasse            061  43.5858    7.1083         5
49788           FR  06601 CEDEX    Antibes  Provence-Alpes-Côte d'Azur        93.0  ...         Grasse            061  43.5858    7.1083         5

>>> nomi.query_location("Straassborg", top_k=3, fuzzy_threshold=80)
    country_code  postal_code  place_name state_name  state_code  ... community_name community_code latitude longitude  accuracy
25461           FR        67000  Strasbourg  Grand Est        44.0  ...     Strasbourg            678  48.5839    7.7455         5
25462           FR  67001 CEDEX  Strasbourg  Grand Est        44.0  ...     Strasbourg            678  48.5839    7.7455         5
25463           FR  67002 CEDEX  Strasbourg  Grand Est        44.0  ...     Strasbourg            678  48.5839    7.7455         5

Distance 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 format

The result of a geo-localistion query is a pandas.DataFrame with the following columns,

  • country_code: iso country code, 2 characters

  • postal_code : postal code

  • place_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=centroid

Configuration and defaults

Storage directory

Defaults to ~/.cache/pgeocode, it is the directory where data is downloaded for later consumption. It can be changed using the environment variable PGEOCODE_DATA_DIR, i.e. export PGEOCODE_DATA_DIR=/tmp/pgeocode_data.

Data sources

Data sources are provided as a list in the pgeocode.DOWNLOAD_URL variable. 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 instance postal-codes-data repository for examples of data files.

License

The pgeocode package is distributed under the 3-clause BSD license.

Supported countries

The 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)

See GeoNames database for more information.

Metadata

Release files for pgeocode 0.4.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pgeocode 0.4.1
File Size Uploaded Provenance Actions
pgeocode-0.4.1.tar.gz 10.4 kB Without provenance Details for pgeocode-0.4.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pgeocode 0.4.1
File Interpreter ABI Platform Actions
pgeocode-0.4.1-py3-none-any.whl Python 3 none any Details for pgeocode-0.4.1-py3-none-any.whl

Total release size: 20.2 kB

Release files / pgeocode-0.4.1.tar.gz

Download URL pgeocode-0.4.1.tar.gz
Size 10.4 kB
Tags Source
SHA-256 checksum
How to use checksums
08f35dedf79957769641c7137aa9cc189e1bb63033226372dce372b14973e8b2
BLAKE2b-256 checksum
How to use checksums
d3d93d2688b81c49f4355069f6b143dde4fae638e66046e7105e8e6a5e2b5dac
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.0

Release files / pgeocode-0.4.1-py3-none-any.whl

Download URL pgeocode-0.4.1-py3-none-any.whl
Size 9.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0cc3916d75c41ffcd910ccc2252235a66c627346502cba5d2e97b6ea0aa83257
BLAKE2b-256 checksum
How to use checksums
bd2423b9c7808065230aa7f4ebdbcbd35519e6e779454bb803f0f72dfd263fcb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.0

Security for pgeocode 0.4.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page