API

class s2sphere.Angle(radians=0)[source]

A one-dimensional angle (as opposed to a two-dimensional solid angle).

It has methods for converting angles to or from radians and degrees.

Parameters:radians (float) – angle in radians

see S1Angle

classmethod from_degrees(degrees)[source]

class generator

Parameters:degrees (float) – degrees
classmethod from_radians(radians)[source]
radians
degrees
class s2sphere.AreaMetric(deriv)[source]

Area metric. A 2D specialization of s2sphere.Metric (check for API).

Preconfigured instances of this class are s2sphere.AVG_AREA, s2sphere.MIN_AREA, s2sphere.MAX_AREA.

See C++ docs at S2::AreaMetric.

s2sphere.MIN_AREA = <s2sphere.sphere.AreaMetric object>

Minimum cell area for quadratic projections.

s2sphere.AVG_AREA = <s2sphere.sphere.AreaMetric object>

Average cell area for all projections.

s2sphere.MAX_AREA = <s2sphere.sphere.AreaMetric object>

Maximum cell area for quadratic projections.

class s2sphere.Cap(axis=Point: (1, 0, 0), height=-1)[source]

A spherical cap, which is a portion of a sphere cut off by a plane.

see S2Cap

ROUND_UP = 1.0000000000000002
classmethod from_axis_height(axis, height)[source]
classmethod from_axis_angle(axis, angle)[source]
classmethod get_height_for_angle(radians)[source]
classmethod from_axis_area(axis, area)[source]
classmethod empty()[source]
classmethod full()[source]
height()[source]
axis()[source]
area()[source]

2 * pi * height

angle()[source]
is_valid()[source]
is_empty()[source]
is_full()[source]
get_cap_bound()[source]
add_point(point)[source]
complement()[source]
contains(other)[source]
interior_contains(other)[source]
intersects(*args)[source]
may_intersect(other)[source]
interior_intersects(other)[source]
get_rect_bound()[source]
approx_equals(other, max_error=1e-14)[source]
expanded(distance)[source]
class s2sphere.Cell(cell_id=None)[source]

Cell

see S2Cell

classmethod from_lat_lng(lat_lng)[source]
classmethod from_point(point)[source]
classmethod from_face_pos_level(face, pos, level)[source]
id()[source]
face()[source]
level()[source]
orientation()[source]
is_leaf()[source]
get_edge(k)[source]

the k-th edge

Return the inward-facing normal of the great circle passing through the edge from vertex k to vertex k+1 (mod 4). The normals returned by GetEdgeRaw are not necessarily unit length.

get_edge_raw(k)[source]
get_vertex(k)[source]

Return the k-th vertex of the cell (k = 0,1,2,3).

Vertices are returned in CCW order. The points returned by GetVertexRaw are not necessarily unit length.

get_vertex_raw(k)[source]
exact_area()[source]

cell area in steradians accurate to 6 digits but slow to compute

Return the area of this cell as accurately as possible. This method is more expensive but it is accurate to 6 digits of precision even for leaf cells (whose area is approximately 1e-18).

average_area()[source]

cell area in steradians

approx_area()[source]

approximate cell area in steradians accurate to within 3%

For cells at level 5 or higher (cells with edge length 350km or smaller), it is accurate to within 0.1%.

subdivide()[source]
get_center()[source]
get_center_raw()[source]
contains(other)[source]
may_intersect(cell)[source]
get_latitude(i, j)[source]
get_longitude(i, j)[source]
get_cap_bound()[source]
get_rect_bound()[source]
class s2sphere.CellId(id_=0)[source]

S2 cell id

The 64-bit ID has:

  • 3 bits to encode the face
  • 0-60 bits to encode the position
  • a 1

The final 1 is the least significant bit (lsb) in the underlying integer representation and is returned with s2sphere.CellId.lsb().

see S2CellId

LINEAR_PROJECTION = 0
TAN_PROJECTION = 1
QUADRATIC_PROJECTION = 2
PROJECTION = 2
FACE_BITS = 3
NUM_FACES = 6
MAX_LEVEL = 30
POS_BITS = 61
MAX_SIZE = 1073741824
WRAP_OFFSET = 13835058055282163712L
classmethod from_lat_lng(ll)[source]
classmethod from_point(p)[source]
classmethod from_face_pos_level(face, pos, level)[source]
classmethod from_face_ij(face, i, j)[source]
classmethod from_face_ij_wrap(face, i, j)[source]
classmethod from_face_ij_same(face, i, j, same_face)[source]
classmethod st_to_ij(s)[source]
classmethod lsb_for_level(level)[source]
parent(*args)[source]
child(pos)[source]
contains(other)[source]
intersects(other)[source]
is_face()[source]
id()[source]
is_valid()[source]
lsb()[source]
face()[source]
pos()[source]
is_leaf()[source]
level()[source]
child_begin(*args)[source]
child_end(*args)[source]
prev()[source]
next()[source]
children(*args)[source]
range_min()[source]
range_max()[source]
classmethod begin(level)[source]
classmethod end(level)[source]
classmethod walk(level)[source]

Walk along a Hilbert curve at the given level.

This function does not exist in the SWIG bindings of the original C++ library. It provides a more Pythonic way to iterate over cells.

Returns:Iterator over instances of CellId s.
classmethod walk_fast(level)[source]

Walk along a Hilbert curve at the given level.

This function does not exist in the SWIG bindings of the original C++ library. It provides a more Pythonic way to iterate over cells.

Use with caution: this repeatedly mutates a single instance with a changing id. If you save the object, it will change out from underneath you.

Returns:Iterator over ids in the same instance of CellId.
classmethod none()[source]
prev_wrap()[source]
next_wrap()[source]
advance_wrap(steps)[source]
advance(steps)[source]
to_lat_lng()[source]
to_point_raw()[source]
to_point()[source]
get_center_si_ti()[source]
get_center_uv()[source]

center of the cell in (u, v) coordinates

Return type:pair
to_face_ij_orientation()[source]
get_edge_neighbors()[source]
get_vertex_neighbors(level)[source]

Return the neighbors of closest vertex to this cell.

Normally there are four neighbors, but the closest vertex may only have three neighbors if it is one of the 8 cube vertices.

get_all_neighbors(nbr_level)[source]
get_size_ij(*args)[source]
to_token()[source]

A unique string token for this cell id.

This is a hex encoded version of the cell id with the right zeros stripped of.

classmethod from_token(token)[source]

Creates a CellId from a hex encoded cell id string, called a token.

Parameters:token (str) – A hex representation of the cell id. If the input is shorter than 16 characters, zeros are appended on the right.
classmethod st_to_uv(s)[source]
classmethod uv_to_st(u)[source]
classmethod max_edge()[source]
classmethod max_angle_span()[source]
classmethod max_diag()[source]
classmethod min_width()[source]
class s2sphere.CellUnion(cell_ids=None, raw=True)[source]

Cell Union

see S2CellUnion

classmethod get_union(x, y)[source]
classmethod get_intersection(*args)[source]
expand(*args)[source]
classmethod get_difference(x, y)[source]
num_cells()[source]
cell_id(i)[source]
cell_ids()[source]
normalize()[source]
denormalize(min_level, level_mod)[source]
contains(*args)[source]
intersects(*args)[source]
get_rect_bound()[source]

rectangular bound

class s2sphere.Interval(lo, hi)[source]

Interval interface

lo()[source]
hi()[source]
bound(i)[source]
bounds()[source]
classmethod empty()[source]
class s2sphere.LatLng(lat, lng)[source]

A point on a sphere in latitute-longitude coordinates.

see S2LatLng

classmethod from_degrees(lat, lng)[source]
classmethod from_radians(lat, lng)[source]
classmethod from_point(point)[source]
classmethod from_angles(lat, lng)[source]
classmethod default()[source]
classmethod invalid()[source]
static latitude(point)[source]
static longitude(point)[source]
lat()[source]
lng()[source]
is_valid()[source]
to_point()[source]
normalized()[source]
approx_equals(other, max_error=1e-15)[source]
get_distance(other)[source]
class s2sphere.LatLngRect(*args)[source]

A rectangle in latitude-longitude space.

see S2LatLngRect

lat()[source]
lng()[source]
lat_lo()[source]
lat_hi()[source]
lng_lo()[source]
lng_hi()[source]
lo()[source]
hi()[source]
classmethod from_center_size(center, size)[source]
classmethod from_point(p)[source]
classmethod from_point_pair(a, b)[source]
classmethod full_lat()[source]
classmethod full_lng()[source]
classmethod full()[source]
is_full()[source]
is_valid()[source]
classmethod empty()[source]
get_center()[source]
get_size()[source]
get_vertex(k)[source]
area()[source]

Area in steradians.

is_empty()[source]
is_point()[source]
convolve_with_cap(angle)[source]
contains(other)[source]
interior_contains(other)[source]
may_intersect(cell)[source]
intersects(*args)[source]
classmethod intersects_lng_edge(a, b, lat, lng)[source]
classmethod intersects_lat_edge(a, b, lat, lng)[source]
interior_intersects(*args)[source]
union(other)[source]
intersection(other)[source]
expanded(margin)[source]
approx_equals(other, max_error=1e-15)[source]
get_cap_bound()[source]
class s2sphere.LengthMetric(deriv)[source]

Length metric. A 1D specialization of s2sphere.Metric.

Preconfigured instances of this class are s2sphere.AVG_ANGLE_SPAN, s2sphere.MIN_ANGLE_SPAN, s2sphere.MAX_ANGLE_SPAN, s2sphere.AVG_EDGE, s2sphere.MIN_EDGE, s2sphere.MAX_EDGE, s2sphere.AVG_DIAG, s2sphere.MIN_DIAG, s2sphere.MAX_DIAG, s2sphere.AVG_WIDTH, s2sphere.MIN_WIDTH and s2sphere.MAX_WIDTH.

see S2::LengthMetric

class s2sphere.LineInterval(lo=1, hi=0)[source]

Line Interval in R1

see R1Interval

classmethod from_point_pair(a, b)[source]
contains(other)[source]
interior_contains(other)[source]
intersects(other)[source]
interior_intersects(other)[source]
union(other)[source]
intersection(other)[source]
expanded(radius)[source]
get_center()[source]
get_length()[source]
is_empty()[source]
approx_equals(other, max_error=1e-15)[source]
class s2sphere.Metric(deriv, dim)[source]

Metric

The classes s2sphere.LengthMetric and s2sphere.AreaMetric are specializations of this class.

see S2::Metric

deriv()[source]
get_value(level)[source]

The value of this metric at a given level.

Returns:Depending on whether this is used in one or two dimensions, this is an angle in radians or a solid angle in steradians.
get_closest_level(value)[source]

Closest cell level according to the given value.

Return the level at which the metric has approximately the given value. For example, s2sphere.AVG_EDGE.get_closest_level(0.1) returns the level at which the average cell edge length is approximately 0.1. The return value is always a valid level.

Parameters:value – Depending on whether this is used in one or two dimensions, this is an angle in radians or a solid angle in steradians.
get_min_level(value)[source]

Minimum cell level for given value.

Return the minimum level such that the metric is at most the given value, or s2sphere.CellId.MAX_LEVEL if there is no such level. For example, s2sphere.MAX_DIAG.get_min_level(0.1) returns the minimum level such that all cell diagonal lengths are 0.1 or smaller. The return value is always a valid level.

Parameters:value – Depending on whether this is used in one or two dimensions, this is an angle in radians or a solid angle in steradians.
get_max_level(value)[source]

Maximum cell level for given value.

Return the maximum level such that the metric is at least the given value, or zero if there is no such level. For example, s2sphere.MIN_WIDTH.get_max_level(0.1) returns the maximum level such that all cells have a minimum width of 0.1 or larger. The return value is always a valid level.

Parameters:value – Depending on whether this is used in one or two dimensions, this is an angle in radians or a solid angle in steradians.
class s2sphere.Point(x, y, z)[source]

A point in 3d Euclidean space.

“Normalized” points are points on the unit sphere.

see S2Point

abs()[source]
largest_abs_component()[source]
angle(other)[source]
cross_prod(other)[source]
dot_prod(other)[source]
norm2()[source]
norm()[source]
normalize()[source]
class s2sphere.RegionCoverer[source]

Region Coverer

see S2RegionCoverer

class Candidate[source]
num_children
min_level
max_level
level_mod
max_cells
get_covering(region)[source]
get_interior_covering(region)[source]
classmethod flood_fill(region, start)[source]
classmethod get_simple_covering(region, start, level)[source]
class s2sphere.SphereInterval(lo=3.141592653589793, hi=-3.141592653589793, args_checked=False)[source]

Interval in S1

see S1Interval

classmethod from_point_pair(a, b)[source]
classmethod positive_distance(a, b)[source]
classmethod full()[source]
is_full()[source]
is_valid()[source]
is_inverted()[source]
is_empty()[source]
get_center()[source]
get_length()[source]
complement()[source]

Return the complement of the interior of the interval.

An interval and its complement have the same boundary but do not share any interior values. The complement operator is not a bijection, since the complement of a singleton interval (containing a single value) is the same as the complement of an empty interval.

approx_equals(other, max_error=1e-15)[source]
fast_contains(other)[source]
contains(other)[source]
interior_contains(other)[source]
intersects(other)[source]
interior_intersects(other)[source]
union(other)[source]
intersection(other)[source]
expanded(radius)[source]
get_complement_center()[source]
get_directed_hausdorff_distance(other)[source]

Utilities

s2sphere.area(a, b, c)[source]

Area of the triangle (a, b, c).

see S2::Area()

s2sphere.drem(x, y)[source]

Like fmod but rounds to nearest integer instead of floor.

s2sphere.face_uv_to_xyz(face, u, v)[source]

(face, u, v) to xyz

see S2::FaceUVtoXYZ()

s2sphere.face_xyz_to_uv(face, p)[source]

(face, XYZ) to UV

see S2::FaceXYZtoUV()

s2sphere.get_norm(face)[source]
s2sphere.get_u_axis(face)[source]
s2sphere.get_u_norm(face, u)[source]

Vector normal to the positive v-axis and the plane through the origin.

The vector is normal to the positive v-axis and a plane that contains the origin and the v-axis.

The right-handed normal (not necessarily unit length) for an edge in the direction of the positive v-axis at the given u-value on the given face. (This vector is perpendicular to the plane through the sphere origin that contains the given edge.)

Return type:Point

see S2::GetUNorm()

s2sphere.get_v_axis(face)[source]
s2sphere.get_v_norm(face, v)[source]

Vector normal to the positive u-axis and the plane through the origin.

The vector is normal to the positive u-axis and a plane that contains the origin and the u-axis.

Return the right-handed normal (not necessarily unit length) for an edge in the direction of the positive u-axis at the given v-value on the given face.

see S2::GetVNorm()

s2sphere.girard_area(a, b, c)[source]

see S2::GirardArea()

s2sphere.is_unit_length(p)[source]
s2sphere.origin()[source]

A unique and empirically chosen reference point.

see S2::Origin()

s2sphere.ortho(a)[source]

see S2::Ortho()

s2sphere.robust_cross_prod(a, b)[source]

A numerically more robust cross product.

The direction of \(a \times b\) becomes unstable as \((a + b)\) or \((a - b)\) approaches zero. This leads to situations where \(a \times b\) is not very orthogonal to \(a\) and/or \(b\). We could fix this using Gram-Schmidt, but we also want \(b \times a = - a \times b\).

The easiest fix is to just compute the cross product of \((b+a)\) and \((b-a)\). Mathematically, this cross product is exactly twice the cross product of \(a\) and \(b\), but it has the numerical advantage that \((b+a)\) and \((b-a)\) are always perpendicular (since \(a\) and \(b\) are unit length). This yields a result that is nearly orthogonal to both \(a\) and \(b\) even if these two values differ only in the lowest bit of one component.

see S2::RobustCrossProd()

s2sphere.simple_ccw(a, b, c)[source]

Simple Counterclockwise test.

Return true if the points A, B, C are strictly counterclockwise. Return false if the points are clockwise or collinear (i.e. if they are all contained on some great circle).

Due to numerical errors, situations may arise that are mathematically impossible, e.g. ABC may be considered strictly CCW while BCA is not. However, the implementation guarantees the following:

If simple_ccw(a,b,c), then !simple_ccw(c,b,a) for all a,b,c.

see S2::SimpleCCW()

s2sphere.simple_crossing(a, b, c, d)[source]

see S2EdgeUtil::SimpleCrossing()

s2sphere.valid_face_xyz_to_uv(face, p)[source]
s2sphere.xyz_to_face_uv(p)[source]