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.0

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.0
File Size Uploaded Provenance Actions
pygeokit-0.1.0.tar.gz 5.5 MB Without provenance Details for pygeokit-0.1.0.tar.gz

Built distribution (wheel)

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

Total release size: 11.1 MB

Release files / pygeokit-0.1.0.tar.gz

Download URL pygeokit-0.1.0.tar.gz
Size 5.5 MB
Tags Source
SHA-256 checksum
How to use checksums
738acf53f78af254eecb3d8d8bed5b7c5ac94a4a0316939ae39c78ad6ee4dd52
BLAKE2b-256 checksum
How to use checksums
7fb7b7743ab89699f9bb80727fddfec3a9fa7ebb7a7d370c39ae7110b1452595
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.0-py3-none-any.whl

Download URL pygeokit-0.1.0-py3-none-any.whl
Size 5.5 MB
Tags Python 3
SHA-256 checksum
How to use checksums
24876ea078766c9614da1678206d2bbbc55db571d39427823d6951553c863bec
BLAKE2b-256 checksum
How to use checksums
a8f7ae7b625a7c53e213507d46b39d5dab01eb6211ab67bbf758635ff754c3b0
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.0

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