s2sphere Cell ID到Corner Vertices

时间:2018-02-27 16:03:27

标签: python geolocation

我已经以487a73cc7c的格式获得了来自OSMCoverer的S2 Level 17 Cell ID列表,我需要得到每个的角点。我使用Python s2sphere库在Reddit here上找到了一个用户的脚本,我相信我可以使用它,但它没有输出正确的co-ords,它们应该在52.80xxxx左右, - 2.xxxxx范围。

我认为这与第5行的轮班操作有关,但我不太了解它。

import s2sphere
from s2sphere import CellId, LatLng, Cell

def get_corners(s2CellId_str, level):
    c1 = Cell(CellId(int(s2CellId_str,16)<<(60 - 2*level)))

    print(c1)

    c0 = LatLng.from_point(c1.get_center())  # center lat/lon of s2 cell
    v0 = LatLng.from_point(c1.get_vertex(0)) # lat/lon of upper/left corner
    v1 = LatLng.from_point(c1.get_vertex(1)) # lat/lon of lower/left corner
    v2 = LatLng.from_point(c1.get_vertex(2)) # lat/lon of lower/right corner
    v3 = LatLng.from_point(c1.get_vertex(3)) # lat/lon of upper/right corner
    print('    // s2 level ' + str(level) + ' cell id = ' + s2CellId_str)
    print('Center  = ' + str(c0))
    print('Vertex0 = ' + str(v0))
    print('Vertex1 = ' + str(v1))
    print('Vertex2 = ' + str(v2))
    print('Vertex3 = ' + str(v3))

get_corners("487a73cc7c", 17)

提前致谢。

1 个答案:

答案 0 :(得分:0)

感谢Reddit上一位乐于帮助的用户,他指出我所认为的不是CellID,而是被称为令牌,这已经解决了。

CellID具有from_token功能,可以这样使用。

import s2sphere
from s2sphere import CellId, LatLng, Cell

def get_corners(s2CellId_str, level):
    c1 = Cell(CellId.from_token(s2CellId_str))

    print(c1)

    c0 = LatLng.from_point(c1.get_center())  # center lat/lon of s2 cell
    v0 = LatLng.from_point(c1.get_vertex(0)) # lat/lon of upper/left corner
    v1 = LatLng.from_point(c1.get_vertex(1)) # lat/lon of lower/left corner
    v2 = LatLng.from_point(c1.get_vertex(2)) # lat/lon of lower/right corner
    v3 = LatLng.from_point(c1.get_vertex(3)) # lat/lon of upper/right corner
    print('    // s2 level ' + str(level) + ' cell id = ' + s2CellId_str)
    print('Center  = ' + str(c0))
    print('Vertex0 = ' + str(v0))
    print('Vertex1 = ' + str(v1))
    print('Vertex2 = ' + str(v2))
    print('Vertex3 = ' + str(v3))

get_corners("487a73cc7c", 17)

文档:http://s2sphere.readthedocs.io/en/latest/api.html#s2sphere.CellId.from_token

作为一个稍微不同的解决方案,如果你有lat / long值,你可以这样做:

import s2sphere
lat = 52.809766
lng = -2.088996
cell_id = s2sphere.CellId.from_lat_lng(s2sphere.LatLng.from_degrees(lat,lng)).parent(12)
print (s2sphere.LatLng.from_point(s2sphere.Cell(cell_id).get_center()))
print (s2sphere.LatLng.from_point(s2sphere.Cell(cell_id).get_vertex(0)))
print (s2sphere.LatLng.from_point(s2sphere.Cell(cell_id).get_vertex(1)))
print (s2sphere.LatLng.from_point(s2sphere.Cell(cell_id).get_vertex(2)))
print (s2sphere.LatLng.from_point(s2sphere.Cell(cell_id).get_vertex(3)))