Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Python S2/S2sphere library - find all s2 cells of a particular level with in a circle(lat, long and radius is given)

What S2Region and how should I use to get all s2 cells at certain parent level(let say 9) covered by the circle drawn from given lat, long and radius. Below is an example which use python s2 library for getting all cells under a rectangle.

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)

source of example http://blog.christianperone.com/2015/08/googles-s2-geometry-on-the-sphere-cells-and-hilbert-curve/

I am looking for something like

region_circle = S2latLangCircle(lat,lang,radius)

I find answer of this question for google s2 library implemented in c++ Using google s2 library - find all s2 cells of a certain level within the circle, given lat/lng and radius in miles/km but I need this in python.

Thanks

like image 620
Gaurav Jain Avatar asked Jun 20 '17 09:06

Gaurav Jain


People also ask

What is S2 level?

The S2 Cell is a cell on a spherical surface and its edges are geodesics. S2 Cell has 31 levels of hierarchy with area coverage ranging from 85,011,012.19km² at the highest level of 0 to 0.44cm² at the lowest level of 30. S2 Cell preserves the cell center well during level increase from 0 to 30.

How do S2 cells work?

S2 cells is a way to split a sphere into squarish sections of approximately the same size. It was invented by Google to make some algorithms in Google Maps run faster. S2 cells come in different sizes called "levels". S2 cell of level N contains four S2 cells of level N+1.

What is S2 cell?

S2 cells are a mathematical mechanism that helps computers translate Earth's spherical 3D shape into 2D geometry. You can think about them as tiny units of geography that computers understand and developers love to use.


1 Answers

With the help of link, I worked out for python solution.

I am using python s2sphere library.

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 method returns all s2 cells(Rectangle boundary) at given parent level which is covered by circle drawn from given lat, long as center and given radius

like image 89
Gaurav Jain Avatar answered Sep 28 '22 00:09

Gaurav Jain