使用pythons2/S2sphere库查找一个圆内某一级别的所有S2单元(给出lat、long和radius)

2024-06-16 12:39:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我应该使用什么样的s2区域,以及我应该如何使用从给定的纬度、长度和半径绘制的圆覆盖特定父级别(比如9)的所有s2单元格。下面是一个使用pythons2库获取矩形下所有单元格的示例。在

region_rect = S2LatLngRect(
              S2LatLng.FromDegrees(-51.264871, -30.241701),
              S2LatLng.FromDegrees(-51.04618, -30.000003))
coverer = S2RegionCoverer()
coverer.set_min_level(8)
coverer.set_max_level(15)
coverer.set_max_cells(500)
covering = coverer.GetCovering(region_rect)

示例源http://blog.christianperone.com/2015/08/googles-s2-geometry-on-the-sphere-cells-and-hilbert-curve/

我在找类似的东西

^{pr2}$

我为用c++Using google s2 library - find all s2 cells of a certain level within the circle, given lat/lng and radius in miles/km实现的googles2库找到了这个问题的答案,但我需要在python中使用它。在

谢谢


Tags: andtherect区域示例levelregionmax
1条回答
网友
1楼 · 发布于 2024-06-16 12:39:53

link的帮助下,我制定了python解决方案。在

我正在使用pythons2sphere库。在

earthCircumferenceMeters = 1000 * 40075.017
def earthMetersToRadians(meters):
    return (2 * math.pi) * (float(meters) / 
    const.earthCircumferenceMeters)


def getCoveringRect(lat, lng, radius, parent_level):
    radius_radians = earthMetersToRadians(radius)
    latlng = LatLng.from_degrees(float(lat), 
             float(lng)).normalized().to_point()
    region = Cap.from_axis_height(latlng, 
    (radius_radians*radius_radians)/2)
    coverer = RegionCoverer()
    coverer.min_level = int(parent_level)
    coverer.max_level = int(parent_level)
    coverer.max_cells = const.MAX_S2_CELLS
    covering = coverer.get_covering(region)
    s2_rect = []
    for cell_id in covering:
    new_cell = Cell(cell_id)
    vertices = []
    for i in range(4):
        vertex = new_cell.get_vertex(i)
        latlng = LatLng.from_point(vertex)
        vertices.append((math.degrees(latlng.lat().radians),
                         math.degrees(latlng.lng().radians)))
    s2_rect.append(vertices)
    return s2_rect

getCoveringRect方法返回给定父级的所有s2单元格(矩形边界),这些单元格由从给定纬度绘制的圆覆盖,包括圆心和给定半径

相关问题 更多 >