Skip to main content

Minimal Python S2 cell ID, S2 token and lat/lon conversion library

Project description

Minimal Python S2 cell ID, S2 token and lat/lon conversion library.

CI Status License PyPI Version

Overview

This library does conversion between S2 cell ID, S2 token and latitude/longitude and was written as a method to understand the way the S2 cell system works; hopefully this is useful to others as a single-file reference on the process. All steps in the conversion are well commented and written to be understandable and functional rather than necessarily fast.

The library is checked against a test suite generated from the reference C++ implementation to ensure conformity with the standard.

Should you need more complete S2 functionality or a fast C-based implementation, please consider using the Python extension included in the s2geometry repository or the pure-Python s2sphere implementation.

Issues and PRs are very welcome to improve the descriptions or correct any misunderstandings of the S2 cell system. Please note that this library strives to be an easy to read reference rather than aiming for peak performance (it is in Python after all), so PRs which reduce readability of the implementation (such as for Python specific speed optimisations) are generally discouraged. However, if you have optimisations that are applicable to S2 implementations across many langauges and can be described easily, please do consider making a PR.

Installation

This package can be installed from PyPI with pip or any other Python package manager:

pip install s2cell

Usage

The library is designed to be minimal and purely functional. Conversion from lat/lon (in degrees) to a cell ID or token can be done with the following two functions:

s2cell.lat_lon_to_cell_id(-10.490091, 105.641318)  # -> 3383782026967071427
s2cell.lat_lon_to_token(-10.490091, 105.641318)    # -> '2ef59bd352b93ac3'

By default, these conversions will give you a level 30 leaf-cell as output. If you require a lower precision level, you can specify this:

s2cell.lat_lon_to_cell_id(-10.490091, 105.641318, 10)  # -> 3383781119341101056
s2cell.lat_lon_to_token(-10.490091, 105.641318, 0)     # -> '3'

Conversion from a cell ID or token to lat/lon (in degrees) can be done with the following two functions:

s2cell.cell_id_to_lat_lon(3383781119341101056)  # -> (-10.452552407574101, 105.6412526632361)
s2cell.token_to_lat_lon('3')                    # -> (0.0, 90.0)

The lat/lon returned will be the centre of the cell at the level available in the provided cell ID or token.

There are also a few other useful functions for inspecting or converting a cell ID/token:

# Conversion
s2cell.cell_id_to_token(3383781119341101056)  # -> '2ef59b'
s2cell.token_to_cell_id('3')                  # -> 3458764513820540928

# Level extraction
s2cell.cell_id_to_level(3383781119341101056)  # -> 10
s2cell.token_to_level('3')                    # -> 0

License

This project is released under the same license as the reference C++ S2 Geometry implementation, namely the Apache 2.0 License.

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

s2cell-1.1.0.tar.gz (13.8 kB view hashes)

Uploaded Source

Built Distribution

s2cell-1.1.0-py3-none-any.whl (15.6 kB view hashes)

Uploaded Python 3

Supported by

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