๐Ÿ“š S2 geometry - Awesome Go Library for Geographic

Go Gopher mascot for S2 geometry

S2 geometry library in Go.

๐Ÿท๏ธ Geographic
๐Ÿ“‚ Geographic tools and servers
โญ 1,680 stars
View on GitHub ๐Ÿ”—

Detailed Description of S2 geometry

Overview

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.

Scope

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.

Robustness

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

Status of the Go Library

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++ TypeGo
R1Intervalโœ…

โ„ยฒ - Two-dimensional Cartesian coordinates

C++ TypeGo
R2Pointโœ…
R2Rectโœ…

โ„ยณ - Three-dimensional Cartesian coordinates

C++ TypeGo
R3Vectorโœ…
R3ExactVectorโœ…
Matrix3x3โœ…

Sยน - Circular Geometry

C++ TypeGo
S1Angleโœ…
S1ChordAngleโœ…
S1Intervalโœ…

Sยฒ - Spherical Geometry

Basic Types

C++ TypeGo
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โŒ

Query Types

C++ TypeGo
S2ChainInterpolationโŒ
S2ClosestCellโŒ
S2FurthestCellโŒ
S2ClosestEdgeโœ…
S2FurthestEdgeโœ…
S2ClosestPointโŒ
S2FurthestPointโŒ
S2ContainsPointโœ…
S2ContainsVertexโœ…
S2ConvexHullโœ…
S2CrossingEdgeโœ…
S2HausdorffDistanceโŒ
S2ShapeNestingโŒ

Supporting Types

C++ TypeGo
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โŒ

Encode/Decode

Encoding and decoding of S2 types is fully implemented and interoperable with C++ and Java.