S2 is a library for spherical geometry that aims to have the same robustness, flexibility, and performance as the best planar geometry libraries.
This is a library for manipulating geometric shapes. Unlike many geometry libraries, S2 is primarily designed to work with spherical geometry, i.e., shapes drawn on a sphere rather than on a planar 2D map. (In fact, the name S2 is derived from the mathematical notation for the unit sphere SΒ².) This makes it especially suitable for working with geographic data.
More details about S2 in general are available on the S2 Geometry Website s2geometry.io.
The library provides the following:
-
Representations of angles, intervals, latitude-longitude points, unit vectors, and so on, and various operations on these types.
-
Geometric shapes over the unit sphere, such as spherical caps ("discs"), latitude-longitude rectangles, polylines, and polygons. These are collectively known as "regions".
-
A hierarchical decomposition of the sphere into regions called "cells". The hierarchy starts with the six faces of a projected cube and recursively subdivides them in a quadtree-like fashion.
-
Robust constructive operations (e.g., union) and boolean predicates (e.g., containment) for arbitrary collections of points, polylines, and polygons.
-
Fast in-memory indexing of collections of points, polylines, and polygons.
-
Algorithms for measuring distances and finding nearby objects.
-
Robust algorithms for snapping and simplifying geometry (with accuracy and topology guarantees).
-
A collection of efficient yet exact mathematical predicates for testing relationships among geometric objects.
-
Support for spatial indexing, including the ability to approximate regions as collections of discrete "S2 cells". This feature makes it easy to build large distributed spatial indexes.
On the other hand, the following are outside the scope of S2:
-
Planar geometry.
-
Conversions to/from common GIS formats.
What do we mean by "robust"?
In the S2 library, the core operations are designed to be 100% robust. This means that each operation makes strict mathematical guarantees about its output, and is implemented in such a way that it meets those guarantees for all possible valid inputs. For example, if you compute the intersection of two polygons, not only is the output guaranteed to be topologically correct (up to the creation of degeneracies), but it is also guaranteed that the boundary of the output stays within a user-specified tolerance of true, mathematically exact result.
Robustness is very important when building higher-level algorithms, since unexpected results from low-level operations can be very difficult to handle. S2 achieves this goal using a combination of techniques from computational geometry, including conservative error bounds, exact geometric predicates, and snap rounding.
The implementation attempts to be precise both in terms of mathematical definitions (e.g. whether regions include their boundaries, and how degeneracies are handled) and numerical accuracy (e.g. minimizing cancellation error).
Note that the intent of this library is to represent geometry as a mathematical abstraction. For example, although the unit sphere is obviously a useful approximation for the Earth's surface, functions that are specifically related to geography are not part of the core library (e.g. easting/northing conversions, ellipsoid approximations, geodetic vs. geocentric coordinates, etc).
See http://godoc.org/github.com/golang/geo for specific package documentation.
For an analogous library in C++, see https://github.com/google/s2geometry, in Java, see https://github.com/google/s2-geometry-library-java, and Python, see https://github.com/google/s2geometry/tree/master/src/python
This library is principally a port of the C++ S2 library, adapting to Go idioms where it makes sense. We detail the progress of this port below relative to that C++ library.
Legend:
- β - Feature Complete
- π‘ - Mostly Complete
- β - Not available
βΒΉ - One-dimensional Cartesian coordinates
C++ Type | Go |
---|---|
R1Interval | β |
βΒ² - Two-dimensional Cartesian coordinates
C++ Type | Go |
---|---|
R2Point | β |
R2Rect | β |
βΒ³ - Three-dimensional Cartesian coordinates
C++ Type | Go |
---|---|
R3Vector | β |
R3ExactVector | β |
Matrix3x3 | β |
SΒΉ - Circular Geometry
C++ Type | Go |
---|---|
S1Angle | β |
S1ChordAngle | β |
S1Interval | β |
SΒ² - Spherical Geometry
C++ Type | Go |
---|---|
S2Cap | β |
S2Cell | β |
S2CellId | β |
S2CellIdVector | β |
S2CellIndex | π‘ |
S2CellUnion | β |
S2Coords | β |
S2DensityTree | β |
S2DistanceTarget | β |
S2EdgeVector | β |
S2LatLng | β |
S2LatLngRect | β |
S2LaxLoop | π‘ |
S2LaxPolygon | π‘ |
S2LaxPolyline | π‘ |
S2Loop | β |
S2PaddedCell | β |
S2Point | β |
S2PointIndex | β |
S2PointSpan | β |
S2PointRegion | β |
S2PointVector | β |
S2Polygon | π‘ |
S2Polyline | β |
S2R2Rect | β |
S2Region | β |
S2RegionCoverer | β |
S2RegionIntersection | β |
S2RegionUnion | β |
S2Shape | β |
S2ShapeIndex | β |
S2ShapeIndexRegion | β |
EncodedLaxPolygon | β |
EncodedLaxPolyline | β |
EncodedShapeIndex | β |
EncodedStringVector | β |
EncodedUintVector | β |
IdSetLexicon | β |
ValueSetLexicon | β |
SequenceLexicon | β |
LaxClosedPolyline | β |
VertexIDLaxLoop | β |
C++ Type | Go |
---|---|
S2ChainInterpolation | β |
S2ClosestCell | β |
S2FurthestCell | β |
S2ClosestEdge | β |
S2FurthestEdge | β |
S2ClosestPoint | β |
S2FurthestPoint | β |
S2ContainsPoint | β |
S2ContainsVertex | β |
S2ConvexHull | β |
S2CrossingEdge | β |
S2HausdorffDistance | β |
S2ShapeNesting | β |
C++ Type | Go |
---|---|
S2BooleanOperation | β |
S2BufferOperation | β |
S2Builder | β |
S2BuilderClosedSetNormalizer | β |
S2BuilderFindPolygonDegeneracies | β |
S2BuilderGraph | β |
S2BuilderLayers | β |
S2BuilderSnapFunctions | β |
S2BuilderTesting | β |
S2Builderutil* | β |
S2Coder | β |
S2EdgeClipping | β |
S2EdgeCrosser | β |
S2EdgeCrossings | β |
S2EdgeDistances | β |
S2EdgeTessellator | β |
S2LoopMeasures | β |
S2Measures | β |
S2MemoryTracker | β |
S2Metrics | β |
S2PointUtil | π‘ |
S2PolygonBuilder | β |
S2PolylineAlignment | β |
S2PolylineMeasures | β |
S2PolylineSimplifier | β |
S2Predicates | β |
S2Projections | β |
S2rectBounder | β |
S2RegionTermIndexer | β |
S2ShapeIndexMeasures | β |
S2ShapeIndexUtil* | π‘ |
S2ShapeMeasures | β |
S2ShapeUtil* | π‘ |
S2Stats | β |
S2Testing | β |
S2TextFormat | β |
S2WedgeRelations | β |
S2WindingOperation | β |
Encoding and decoding of S2 types is fully implemented and interoperable with C++ and Java.