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 model of the city (ring / sector / multi-core) get_city_model, load_urban_areas
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.

Urban models

get_city_model(lat, lon) matches each point to the nearest activity center of a GHSL urban area (9,293 areas, centers detected from night lights and POI by Pang et al., arXiv:2503.06445) and returns soft scores p_ring, p_sector, p_multi_core (sum to 1), model and models (several types joined with +), plus measured values: number of centers, spread, elongation, distances to the main / nearest center. The scores are a heuristic: more centers -> multi_core, a stretched footprint -> sector. Large agglomerations have many centers and score as multi_core.

Data and license

Code: MIT. City and country data: GeoNames, licensed under CC BY 4.0. Urban centers: Pang, Zhang, Liu, Dong, arXiv:2503.06445, https://github.com/urbansci/city-centers.

Metadata

Release files for pygeokit 0.1.3

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.3
File Size Uploaded Provenance Actions
pygeokit-0.1.3.tar.gz 6.0 MB Without provenance Details for pygeokit-0.1.3.tar.gz

Built distribution (wheel)

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

Total release size: 12.1 MB

Release files / pygeokit-0.1.3.tar.gz

Download URL pygeokit-0.1.3.tar.gz
Size 6.0 MB
Tags Source
SHA-256 checksum
How to use checksums
1a1d742d3f0faa078beaa0a855a9d77dd7c79ad7c42fc95e0d3b36f6e0622bfe
BLAKE2b-256 checksum
How to use checksums
0e87e1f39d215cac744db52058b4c09c202e3df8dd387054005db4301ee43958
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.3-py3-none-any.whl

Download URL pygeokit-0.1.3-py3-none-any.whl
Size 6.0 MB
Tags Python 3
SHA-256 checksum
How to use checksums
cf4df083000c9611162d051e2366ee77b1b9a4a617a8326857a90fed5b4bdc6c
BLAKE2b-256 checksum
How to use checksums
239f8f7a5080db511ad797049d329304b57f40631f523ff72224f0c2c2604627
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.3

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