|
| 1 | +# pgeocode |
| 2 | + |
| 3 | +[](https://pypi.org/project/pgeocode/) |
| 4 | +[](http://pgeocode.readthedocs.io/) |
| 5 | +[](https://github.com/symerio/pgeocode/actions?query=branch%3Amaster+) |
| 6 | + |
| 7 | +Postal code geocoding and distance calculations |
| 8 | + |
| 9 | +pgeocode is a Python library for high performance off-line querying of |
| 10 | +GPS coordinates, region name and municipality name from postal codes. |
| 11 | +Distances between postal codes as well as general distance queries are |
| 12 | +also supported. The used |
| 13 | +[GeoNames](http://download.geonames.org/export/zip/) database includes |
| 14 | +postal codes for 83 countries. |
| 15 | + |
| 16 | +Currently, only queries within the same country are supported. |
| 17 | + |
| 18 | +For additional documentation see |
| 19 | +[pgeocode.readthedocs.io](https://pgeocode.readthedocs.io). |
| 20 | + |
| 21 | +# Installation |
| 22 | + |
| 23 | +pgeocode requires Python 3.10+ as well as `numpy` and `pandas` packages. |
| 24 | +It can be installed with, |
| 25 | + |
| 26 | +``` |
| 27 | +pip install pgeocode |
| 28 | +``` |
| 29 | + |
| 30 | + |
| 31 | +# Quickstart |
| 32 | + |
| 33 | +**Postal code queries** |
| 34 | + |
| 35 | +```python |
| 36 | +>>> import pgeocode |
| 37 | + |
| 38 | +>>> nomi = pgeocode.Nominatim('fr') |
| 39 | +>>> nomi.query_postal_code("75013") |
| 40 | +postal_code 75013 |
| 41 | +country_code FR |
| 42 | +place_name Paris 13 |
| 43 | +state_name Île-de-France |
| 44 | +state_code 11 |
| 45 | +county_name Paris |
| 46 | +county_code 75 |
| 47 | +community_name Paris |
| 48 | +community_code 751 |
| 49 | +latitude 48.8322 |
| 50 | +longitude 2.3561 |
| 51 | +accuracy 5 |
| 52 | + |
| 53 | +>>> nomi.query_postal_code(["75013", "69006"]) |
| 54 | + postal_code place_name state_name latitude longitude |
| 55 | +0 75013 Paris 13 Île-de-France 48.8322 2.3561 |
| 56 | +1 69006 Lyon 06 Auvergne-Rhône-Alpes 45.7679 4.8506 |
| 57 | +``` |
| 58 | + |
| 59 | +**Place name queries** |
| 60 | + |
| 61 | +```python |
| 62 | +>>> import pgeocode |
| 63 | + |
| 64 | +>>> nomi = pgeocode.Nominatim('fr') |
| 65 | +>>> nomi.query_location("Antibes", top_k=3) |
| 66 | + country_code postal_code place_name state_name state_code ... community_name community_code latitude longitude accuracy |
| 67 | +49553 FR 06160 Antibes Provence-Alpes-Côte d'Azur 93.0 ... Grasse 061 43.5858 7.1083 5 |
| 68 | +49787 FR 06600 Antibes Provence-Alpes-Côte d'Azur 93.0 ... Grasse 061 43.5858 7.1083 5 |
| 69 | +49788 FR 06601 CEDEX Antibes Provence-Alpes-Côte d'Azur 93.0 ... Grasse 061 43.5858 7.1083 5 |
| 70 | + |
| 71 | +>>> nomi.query_location("Straassborg", top_k=3, fuzzy_threshold=80) |
| 72 | + country_code postal_code place_name state_name state_code ... community_name community_code latitude longitude accuracy |
| 73 | +25461 FR 67000 Strasbourg Grand Est 44.0 ... Strasbourg 678 48.5839 7.7455 5 |
| 74 | +25462 FR 67001 CEDEX Strasbourg Grand Est 44.0 ... Strasbourg 678 48.5839 7.7455 5 |
| 75 | +25463 FR 67002 CEDEX Strasbourg Grand Est 44.0 ... Strasbourg 678 48.5839 7.7455 5 |
| 76 | +``` |
| 77 | + |
| 78 | +**Distance calculations** |
| 79 | + |
| 80 | +```python |
| 81 | +>>> dist = pgeocode.GeoDistance('fr') |
| 82 | +>>> dist.query_postal_code("75013", "69006") |
| 83 | +389.156 |
| 84 | +>>> dist.query_postal_code(["75013", "75014", "75015"], ["69006", "69005", "69004"]) |
| 85 | +array([ 389.15648697, 390.12577967, 390.49857655]) |
| 86 | +``` |
| 87 | + |
| 88 | +# Geocoding format |
| 89 | + |
| 90 | +The result of a geo-localistion query is a `pandas.DataFrame` with the |
| 91 | +following columns, |
| 92 | + |
| 93 | +- `country_code`: iso country code, 2 characters |
| 94 | +- `postal_code` : postal code |
| 95 | +- `place_name` : place name (e.g. town, city etc) |
| 96 | +- `state_name` : 1. order subdivision (state) |
| 97 | +- `state_code` : 1. order subdivision (state) |
| 98 | +- `county_name` : 2. order subdivision (county/province) |
| 99 | +- `county_code` : 2. order subdivision (county/province) |
| 100 | +- `community_name` : 3. order subdivision (community) |
| 101 | +- `community_code` : 3. order subdivision (community) |
| 102 | +- `latitude` : estimated latitude (wgs84) |
| 103 | +- `longitude` : estimated longitude (wgs84) |
| 104 | +- `accuracy` : accuracy of lat/lng from 1=estimated to 6=centroid |
| 105 | + |
| 106 | +# Configuration and defaults |
| 107 | + |
| 108 | +**Storage directory** |
| 109 | + |
| 110 | +Defaults to `~/.cache/pgeocode`, it is the directory where data is |
| 111 | +downloaded for later consumption. It can be changed using the |
| 112 | +environment variable `PGEOCODE_DATA_DIR`, i.e. |
| 113 | +`export PGEOCODE_DATA_DIR=/tmp/pgeocode_data`. |
| 114 | + |
| 115 | +**Data sources** |
| 116 | + |
| 117 | +Data sources are provided as a list in the `pgeocode.DOWNLOAD_URL` |
| 118 | +variable. The default value is, |
| 119 | + |
| 120 | +``` python |
| 121 | +DOWNLOAD_URL = [ |
| 122 | + "https://download.geonames.org/export/zip/{country}.zip", |
| 123 | + "https://symerio.github.io/postal-codes-data/data/geonames/{country}.txt", |
| 124 | +] |
| 125 | +``` |
| 126 | + |
| 127 | +Data sources are tried from first to last until one works. Here the |
| 128 | +second link is a mirror of the first. |
| 129 | + |
| 130 | +It is also possible to extend this variable with third party data |
| 131 | +sources, as long as they follow the same format. See for instance |
| 132 | +[postal-codes-data](https://github.com/symerio/postal-codes-data/tree/master/data/geonames) |
| 133 | +repository for examples of data files. |
| 134 | + |
| 135 | +# License |
| 136 | + |
| 137 | +The pgeocode package is distributed under the 3-clause BSD license. |
| 138 | + |
| 139 | +The pgeocode package is maintained by [Symerio](https://www.symerio.com). |
| 140 | + |
| 141 | +# Supported countries |
| 142 | + |
| 143 | +The list of countries available in the GeoNames database, with the |
| 144 | +corresponding country codes, are given below, |
| 145 | + |
| 146 | +Andorra (AD), Argentina (AR), American Samoa (AS), Austria (AT), |
| 147 | +Australia (AU), Åland Islands (AX), Azerbaijan (AZ), Bangladesh (BD), |
| 148 | +Belgium (BE), Bulgaria (BG), Bermuda (BM), Brazil (BR), Belarus (BY), |
| 149 | +Canada (CA), Switzerland (CH), Chile (CL), Colombia (CO), Costa Rica |
| 150 | +(CR), Cyprus (CY), Czechia (CZ), Germany (DE), Denmark (DK), Dominican |
| 151 | +Republic (DO), Algeria (DZ), Estonia (EE), Spain (ES), Finland (FI), |
| 152 | +Federated States of Micronesia (FM), Faroe Islands (FO), France (FR), |
| 153 | +United Kingdom of Great Britain and Northern Ireland (GB), French Guiana |
| 154 | +(GF), Guernsey (GG), Greenland (GL), Guadeloupe (GP), Guatemala (GT), |
| 155 | +Guam (GU), Croatia (HR), Haiti (HT), Hungary (HU), Ireland (IE), Isle of |
| 156 | +Man (IM), India (IN), Iceland (IS), Italy (IT), Jersey (JE), Japan (JP), |
| 157 | +Republic of Korea (KR), Liechtenstein (LI), Sri Lanka (LK), Lithuania |
| 158 | +(LT), Luxembourg (LU), Latvia (LV), Monaco (MC), Republic of Moldova |
| 159 | +(MD), Marshall Islands (MH), The former Yugoslav Republic of Macedonia |
| 160 | +(MK), Northern Mariana Islands (MP), Martinique (MQ), Malta (MT), Malawi |
| 161 | +(MW), Mexico (MX), Malaysia (MY), New Caledonia (NC), Netherlands (NL), |
| 162 | +Norway (NO), New Zealand (NZ), Peru (PE), Philippines (PH), Pakistan |
| 163 | +(PK), Poland (PL), Saint Pierre and Miquelon (PM), Puerto Rico (PR), |
| 164 | +Portugal (PT), Palau (PW), Réunion (RE), Romania (RO), Serbia (RS), |
| 165 | +Russian Federation (RU), Sweden (SE), Singapore (SG), Slovenia (SI), |
| 166 | +Svalbard and Jan Mayen Islands (SJ), Slovakia (SK), San Marino (SM), |
| 167 | +Thailand (TH), Turkey (TR), Ukraine (UA), United States of America (US), |
| 168 | +Uruguay (UY), Holy See (VA), United States Virgin Islands (VI), Wallis |
| 169 | +and Futuna Islands (WF), Mayotte (YT), South Africa (ZA) |
| 170 | + |
| 171 | +See [GeoNames database](http://download.geonames.org/export/zip/) for |
| 172 | +more information. |
0 commit comments