Skip to main content

Hierarchical coordinate system with BIP39 word encoding

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bgrid-1.0.0.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bgrid-1.0.0-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file bgrid-1.0.0.tar.gz.

File metadata

  • Download URL: bgrid-1.0.0.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for bgrid-1.0.0.tar.gz
Algorithm Hash digest
SHA256 d60204893d06e733bfc7e1409d40a7cdcdcb9ea40d62eb038673ca4a230d262a
MD5 51bc23e22d2264a3927d4b42644fd907
BLAKE2b-256 d5680ab1ed26a302c9b86a68b7f3b880add56901741db3adea9c0f273b994f93

See more details on using hashes here.

File details

Details for the file bgrid-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: bgrid-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for bgrid-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f9a040692ee0327e4cd86c08a1fd6766f2be764883c4c7226ce7aaf820052aa1
MD5 5e5dba5c9d785133dab953e678480ddf
BLAKE2b-256 463f98606a0633bc2bba649bdd212f308f4e10dc010282f329d06ebb10c1ac78

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page