python - 使用 Python S2/S2sphere 库 - 在一个圆中找到特定级别的所有 s2 单元(给出纬度、经度和半径)

标签 python geolocation s2

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

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/

我正在寻找类似的东西

region_circle = S2latLangCircle(lat,lang,radius)

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

谢谢

最佳答案

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

我正在使用 python s2sphere 库。

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 单元格(矩形边界),这些单元格被从给定纬度绘制的圆覆盖,长为中心和给定半径

关于python - 使用 Python S2/S2sphere 库 - 在一个圆中找到特定级别的所有 s2 单元(给出纬度、经度和半径),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44649831/

相关文章:

python - 我该如何替换 python 中的正则表达式?

python - 如何在 Pandas 数据框中插入列名?

python - pandas DataFrame 中 bool 数组的按行求和

Android 获取位置或在禁用时提示启用位置服务

javascript - 如何在 python 中制作 HTML5 Geolocation 对象?

python - 如何在pyspark中并行写入多个parquet文件?

ios - xamarin 形成使用位置 iOS 的权限

android - S2RegionCoverer 不遵守最小/最大单元级别?

google-bigquery - BigQuery 中的 S2 地理函数?

python - 如何使用 Google S2 几何执行搜索操作