S2
s2sphere Python Module #
Lat and Long #
Precision #
Convert a latitude and longitude from their integer forms (we prefer integers to floating points because the latter results in too large a loss of precision) to a level 30 cell ID. This conversion will result in a loss of precision by a few centimeters.
lat = 37791541
lng = -122390014
cellid = s2sphere.CellId.from_lat_lng(s2sphere.LatLng.from_degrees(lat/1e6,lng/1e6))
print(f"LatLong: {cellid.to_lat_lng()}")
LatLong: LatLng: 37.79154098519877,-122.39001395963571
The average size of a level 30 S2 cell is 0.74 cm2, level 20 S2 cell is 77.32 m2, and level 10 S2 cell is 81.07 km2.
Representation #
Cell IDs can be represented as an integer or a compressed hexadecimal value that is referred to as a token.
print(f"Cell ID:{cellid.id()} Token:{cellid.to_token()}")
Cell ID:9260949427115031679 Token:8085806ff60c7c7f
The parent() function returns the parent cell for a given cellid. The smaller the cell level the larger the area covered by the cell.
def iterate_over_cells(cell):
while cell.level() >= 1:
yield cell
cell = cell.parent()
for x in iterate_over_cells(cellid):
print(f"Level: {x.level()} Cell ID: {x.id()} Min: {x.range_min().id()} Max: {x.range_max().id()} Token: {x.to_token()}")
Level: 30 Cell ID: 9260949427115031679 Min: 9260949427115031679 Max: 9260949427115031679 Token: 8085806ff60c7c7f
Level: 29 Cell ID: 9260949427115031676 Min: 9260949427115031673 Max: 9260949427115031679 Token: 8085806ff60c7c7c
Level: 28 Cell ID: 9260949427115031664 Min: 9260949427115031649 Max: 9260949427115031679 Token: 8085806ff60c7c7
Level: 27 Cell ID: 9260949427115031616 Min: 9260949427115031553 Max: 9260949427115031679 Token: 8085806ff60c7c4
Level: 26 Cell ID: 9260949427115031808 Min: 9260949427115031553 Max: 9260949427115032063 Token: 8085806ff60c7d
Level: 25 Cell ID: 9260949427115031552 Min: 9260949427115030529 Max: 9260949427115032575 Token: 8085806ff60c7c
Level: 24 Cell ID: 9260949427115028480 Min: 9260949427115024385 Max: 9260949427115032575 Token: 8085806ff60c7
Level: 23 Cell ID: 9260949427115016192 Min: 9260949427114999809 Max: 9260949427115032575 Token: 8085806ff60c4
Level: 22 Cell ID: 9260949427115065344 Min: 9260949427114999809 Max: 9260949427115130879 Token: 8085806ff60d
Level: 21 Cell ID: 9260949427114999808 Min: 9260949427114737665 Max: 9260949427115261951 Token: 8085806ff60c
Level: 20 Cell ID: 9260949427115261952 Min: 9260949427114213377 Max: 9260949427116310527 Token: 8085806ff61
Level: 19 Cell ID: 9260949427118407680 Min: 9260949427114213377 Max: 9260949427122601983 Token: 8085806ff64
Level: 18 Cell ID: 9260949427130990592 Min: 9260949427114213377 Max: 9260949427147767807 Token: 8085806ff7
Level: 17 Cell ID: 9260949427080658944 Min: 9260949427013550081 Max: 9260949427147767807 Token: 8085806ff4
Level: 16 Cell ID: 9260949427013550080 Min: 9260949426745114625 Max: 9260949427281985535 Token: 8085806ff
Level: 15 Cell ID: 9260949426208243712 Min: 9260949425134501889 Max: 9260949427281985535 Token: 8085806fc
Level: 14 Cell ID: 9260949422987018240 Min: 9260949418692050945 Max: 9260949427281985535 Token: 8085806f
Level: 13 Cell ID: 9260949410102116352 Min: 9260949392922247169 Max: 9260949427281985535 Token: 8085806c
Level: 12 Cell ID: 9260949427281985536 Min: 9260949358562508801 Max: 9260949496001462271 Token: 8085807
Level: 11 Cell ID: 9260949221123555328 Min: 9260948946245648385 Max: 9260949496001462271 Token: 8085804
Level: 10 Cell ID: 9260950045757276160 Min: 9260948946245648385 Max: 9260951145268903935 Token: 808581
Level: 9 Cell ID: 9260953344292159488 Min: 9260948946245648385 Max: 9260957742338670591 Token: 808584
Level: 8 Cell ID: 9260966538431692800 Min: 9260948946245648385 Max: 9260984130617737215 Token: 80859
Level: 7 Cell ID: 9261019314989826048 Min: 9260948946245648385 Max: 9261089683734003711 Token: 8085c
Level: 6 Cell ID: 9260808208757293056 Min: 9260526733780582401 Max: 9261089683734003711 Token: 8085
Level: 5 Cell ID: 9260526733780582400 Min: 9259400833873739777 Max: 9261652633687425023 Token: 8084
Level: 4 Cell ID: 9263904433501110272 Min: 9259400833873739777 Max: 9268408033128480767 Token: 809
Level: 3 Cell ID: 9277415232383221760 Min: 9259400833873739777 Max: 9295429630892703743 Token: 80c
Level: 2 Cell ID: 9295429630892703744 Min: 9223372036854775809 Max: 9367487224930631679 Token: 81
Level: 1 Cell ID: 9511602413006487552 Min: 9223372036854775809 Max: 9799832789158199295 Token: 84
Use the contains() method to determine if a cell ID is part of a given cell.
cell_tokens = ["80857fc","808581","808585c","808587","808f78c","808f7d","808f7f","808f804","808f81c","808f824"]
for x in cell_tokens:
print(f"{x} {s2sphere.CellId.from_token(x).contains(cellid)}")
80857fc False
808581 True
808585c False
808587 False
808f78c False
808f7d False
808f7f False
808f804 False
808f81c False
808f824 False