Skip to main content

pygeokit

Offline geo features from latitude / longitude for tabular ML: country, nearest and main city, capitals, city types and sizes, distances, density of neighbours, ring / sector / multi-core city structure, grids, and a few urban-economics formulas (Reilly, Huff, Zipf). The city data is bundled, so nothing is downloaded at run time.

pip install pygeokit

Depends only on numpy, pandas, scipy, scikit-learn. h3 is optional (hex_cell).

Quick start

import pygeokit as gk

gk.help()                                   # cheat sheet (in Russian)

X, cat = gk.make_X(df, "lat", "lon")        # ready matrix + categorical columns
model = CatBoostRegressor(cat_features=cat).fit(X, y)

feats = gk.get_features(df, lat_col="lat", lon_col="lon")   # reference features only

gk.get_country(55.75, 37.62)                # 'RU'
gk.get_capital("RU")                        # {'name': 'Moscow', 'lat': ..., ...}
gk.get_main_city(lat, lon)                  # biggest city within 60 km
gk.get_city_type(lat, lon)                  # capital / region_center / ... + size class
gk.haversine(lat1, lon1, lat2, lon2)        # km

What is inside

Group Functions
Ready matrix for CatBoost make_X, cat_columns
Everything at once get_features
Cities and countries get_country, get_country_info, get_capital, get_nearest_city, get_main_city, get_city_type, dist_to_capital, load_cities, load_countries
Distances haversine, manhattan, bearing, to_km, travel_minutes
Your own points dist_to_nearest, count_within
Density / neighbours neighbor_features, oof_neighbor_features (leak-free target stats)
City structure find_centers, center_features, multi_center_features, city_shape (ring / sector / multi-core heuristic)
Grids and validation grid_cell, hex_cell, spatial_groups, group_stats
Urban economics city_gravity, huff_probabilities, reilly_breakpoint, zipf_expected

city_type comes from the GeoNames feature code: PPLC capital, PPLA region center, PPLA2 district center, PPLA3/4/5 local center, PPLX city section, everything else settlement. size_class: tiny < 10k, small < 50k, medium < 100k, big < 250k, large < 1M, million.

get_country returns the country of the nearest city, so points right on a border or far out at sea can get the neighbouring country.

Leak-free neighbour target features

tr_f = gk.oof_neighbor_features(train.lat, train.lon, y)                 # train rows
te_f = gk.neighbor_features(test.lat, test.lon, train.lat, train.lon, values=y)

Building from source

The data files are not stored in the repository. Build them once:

python tools/build_data.py      # downloads GeoNames dumps (~12 MB)
python -m pytest tests          # tests use a tiny fake data set
python -m build

Set PYGEOKIT_DATA_DIR to use your own data directory.

Data and license

Code: MIT. City and country data: GeoNames, licensed under CC BY 4.0.

Metadata

Release files for pygeokit 0.1.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 pygeokit 0.1.1
File Size Uploaded Provenance Actions
pygeokit-0.1.1.tar.gz 5.5 MB Without provenance Details for pygeokit-0.1.1.tar.gz

Built distribution (wheel)

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

Total release size: 11.1 MB

Release files / pygeokit-0.1.1.tar.gz

Download URL pygeokit-0.1.1.tar.gz
Size 5.5 MB
Tags Source
SHA-256 checksum
How to use checksums
3a4ef80ea3c92ddb4119353793c622acc60e05171f20ecaac1a7615cec317650
BLAKE2b-256 checksum
How to use checksums
1f634619206499fd53fce0cb0a21442893ac00b7766d19c3c2a3cf332d37b30f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.7

Release files / pygeokit-0.1.1-py3-none-any.whl

Download URL pygeokit-0.1.1-py3-none-any.whl
Size 5.5 MB
Tags Python 3
SHA-256 checksum
How to use checksums
8cf58bdbf47aa43fd434a4e92cdc4d5a9c5ff643bc14de2c398fbdf6c247d651
BLAKE2b-256 checksum
How to use checksums
59cd30021d7f5ca8ecbb66b098b992cd3fc5d462c4a00d4e2f02d6af9e594a43
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.7

Security for pygeokit 0.1.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