Skip to main content

BGrid Python Library

A Python implementation of the BGrid hierarchical coordinate system that divides Earth into 2048 parcels per level, with support for BIP39 mnemonic words in multiple languages.

🌟 Features

  • ✅ Convert decimal degrees (DD) coordinates to BGrid format
  • ✅ Convert BGrid coordinates back to DD with bounds
  • ✅ Support for 1-4 hierarchical levels
  • ✅ BIP39 word encoding/decoding in 9 languages
  • ✅ Calculate cell areas at each level
  • ✅ Zero external dependencies (uses only Python standard library)
  • ✅ Type hints and dataclasses for better IDE support
  • ✅ Command-line interface included
  • ✅ Comprehensive unit tests

📦 Installation

From Source

git clone https://github.com/bgrid-maps/bgrid-python
cd bgrid-python
pip install -e .

Using pip (when published)

pip install bgrid

🚀 Quick Start

Basic Usage

from bgrid import BGrid, Language, dd_to_bgrid, bgrid_to_dd

# Quick conversion: Coordinates to BGrid
bgrid_str = dd_to_bgrid(40.7128, -74.0060, level=3)
print(bgrid_str)  # "1234,567,89"

# Quick conversion: BGrid to coordinates
lat, lon = bgrid_to_dd("1234,567,89")
print(f"({lat}, {lon})")  # Center coordinates

# Using BIP39 words
bgrid_words = dd_to_bgrid(40.7128, -74.0060, level=2, use_words=True)
print(bgrid_words)  # "abandon,ability"

Advanced Usage

# Initialize BGrid with multiple languages
bgrid = BGrid(load_languages=[Language.ENGLISH, Language.SPANISH])

# Convert coordinates to BGrid coordinate object
coord = bgrid.dd_to_bgrid(40.7128, -74.0060, level=3)
print(f"Center: {coord.center_lat}, {coord.center_lon}")
print(f"Bounds: {coord.bounds}")
print(f"Area: ~{bgrid.get_area_km2(coord.level):.2f} km²")

# Convert to words in different languages
words_en = coord.to_words(Language.ENGLISH, bgrid)
words_es = coord.to_words(Language.SPANISH, bgrid)

🖥️ Command Line Interface

The library includes a CLI tool for quick conversions:

# Encode coordinates to BGrid
python bgrid_cli.py encode 40.7128 -74.0060 --level 3

# Decode BGrid to coordinates
python bgrid_cli.py decode "1234,567,89"

📖 API Reference

Core Classes

BGrid

Main class for BGrid operations.

Constructor:

BGrid(load_languages=None, bip39_dir=None)

Key Methods:

BGridCoordinate

Represents a BGrid coordinate with properties and methods.

Properties:

  • indices - List of BGrid indices
  • level - Hierarchical level
  • center_lat, center_lon - Center coordinates
  • bounds - Cell boundaries

Methods:

Convenience Functions

🌍 Supported Languages

The library supports BIP39 word lists in 9 languages:

  • 🇺🇸 English (Language.ENGLISH)
  • 🇪🇸 Spanish (Language.SPANISH)
  • 🇫🇷 French (Language.FRENCH)
  • 🇧🇷 Portuguese (Language.PORTUGUESE)
  • 🇨🇳 Chinese Simplified (Language.CHINESE)
  • 🇮🇹 Italian (Language.ITALIAN)
  • 🇯🇵 Japanese (Language.JAPANESE)
  • 🇰🇷 Korean (Language.KOREAN)
  • 🇨🇿 Czech (Language.CZECH)

📊 Hierarchical Levels

The BGrid system uses up to 4 hierarchical levels:

Level Grid Size Total Cells Approx. Area per Cell
1 64×32 2,048 ~249,000 km²
2 32×64 2,048² ~122 km²
3 64×32 2,048³ ~0.06 km²
4 32×64 2,048⁴ ~0.00003 km²

🧪 Testing

Run the test suite:

python -m pytest test_bgrid.py -v

Or run tests directly:

python test_bgrid.py

🔐 PyPI Credentials

This project expects credentials in ~/.pypirc. Example:

[distutils]
index-servers =
  pypi
  testpypi

[pypi]
username = __token__
password = pypi-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

[testpypi]
repository = https://test.pypi.org/legacy/
username = __token__
password = pypi-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

📁 Project Structure

bgrid-python/
├── bgrid.py          # Main library code
├── bgrid_cli.py      # Command-line interface
├── examples.py       # Usage examples
├── test_bgrid.py     # Unit tests
├── setup.py          # Package setup
├── requirements.txt  # Dependencies
├── README.md         # This file
├── LICENSE          # MIT license
└── .gitignore       # Git ignore rules

📄 Examples

For comprehensive usage examples, see examples.py:

python examples.py

📝 License

This project is licensed under the MIT License - see the LICENSE file for details.

Release files for bgrid 1.0.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 bgrid 1.0.0
File Size Uploaded
bgrid-1.0.0.tar.gz 4.8 kB Details

Built distribution (wheel)

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

Total release size: 9.4 kB

Release files / bgrid-1.0.0.tar.gz

Download URL bgrid-1.0.0.tar.gz
Size 4.8 kB
Tags Source
SHA-256 checksum
How to use checksums
d60204893d06e733bfc7e1409d40a7cdcdcb9ea40d62eb038673ca4a230d262a
BLAKE2b-256 checksum
How to use checksums
d5680ab1ed26a302c9b86a68b7f3b880add56901741db3adea9c0f273b994f93
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.7

Release files / bgrid-1.0.0-py3-none-any.whl

Download URL bgrid-1.0.0-py3-none-any.whl
Size 4.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f9a040692ee0327e4cd86c08a1fd6766f2be764883c4c7226ce7aaf820052aa1
BLAKE2b-256 checksum
How to use checksums
463f98606a0633bc2bba649bdd212f308f4e10dc010282f329d06ebb10c1ac78
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.7

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

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