Top Related Projects
Hexagonal hierarchical geospatial indexing system
The JTS Topology Suite is a Java library for creating and manipulating vector geometry.
Slice GeoJSON into vector tiles on the fly in the browser
Quick Overview
S2Geometry is a powerful library for manipulating geometric shapes on a sphere. It's designed to handle geographic data efficiently, providing a set of tools for spatial indexing, point-in-polygon tests, and other geometric operations on the surface of a sphere. The library is particularly useful for geospatial applications and is used in various Google products.
Pros
- High-performance geometric operations on spherical surfaces
- Supports multiple programming languages (C++, Java, Python, Go)
- Provides efficient spatial indexing for large datasets
- Offers precise and consistent results across different platforms
Cons
- Steep learning curve due to its unique approach to spherical geometry
- Limited documentation and examples for some advanced features
- May be overkill for simple geospatial tasks
- Requires understanding of spherical geometry concepts
Code Examples
- Creating an S2Point from latitude and longitude:
#include "s2/s2latlng.h"
S2LatLng latlng(S1Angle::Degrees(40.7128), S1Angle::Degrees(-74.0060));
S2Point point = latlng.ToPoint();
- Checking if a point is contained in a polygon:
#include "s2/s2polygon.h"
std::unique_ptr<S2Polygon> polygon = // ... create polygon
S2Point point = // ... create point
bool contains = polygon->Contains(point);
- Computing the area of a polygon:
#include "s2/s2polygon.h"
std::unique_ptr<S2Polygon> polygon = // ... create polygon
double area_in_steradians = polygon->GetArea();
double area_in_km2 = area_in_steradians * 6371.0 * 6371.0; // Earth's radius ≈ 6371 km
Getting Started
To use S2Geometry in your C++ project:
-
Clone the repository:
git clone https://github.com/google/s2geometry.git
-
Build the library:
cd s2geometry mkdir build && cd build cmake .. make
-
Include the necessary headers in your C++ file:
#include "s2/s2point.h" #include "s2/s2latlng.h" #include "s2/s2polygon.h"
-
Link against the built library when compiling your project.
Competitor Comparisons
Hexagonal hierarchical geospatial indexing system
Pros of H3
- Hierarchical hexagonal grid system, offering more uniform cell sizes
- Supports multiple resolutions with easy parent/child relationships
- Efficient for certain types of spatial analysis and aggregation
Cons of H3
- Less flexible than S2 for covering arbitrary shapes
- Limited support for non-spherical geometries
- Smaller community and ecosystem compared to S2
Code Comparison
H3:
H3Index h3_index = geoToH3(&geo, resolution);
H3Cell* neighbors = kRing(h3_index, 1);
S2:
S2CellId cellId = S2CellId::FromLatLng(S2LatLng::FromDegrees(lat, lng));
vector<S2CellId> neighbors = cellId.GetEdgeNeighbors();
Key Differences
- H3 uses hexagonal cells, while S2 uses quadrilateral cells
- S2 provides more advanced geometric operations and projections
- H3 offers simpler hierarchical relationships between resolutions
- S2 has broader language support and a larger user base
- H3 may be more suitable for specific use cases like ride-sharing or delivery services
Both libraries have their strengths and are well-suited for different spatial indexing and analysis tasks. The choice between them depends on the specific requirements of your project and the type of spatial operations you need to perform.
The JTS Topology Suite is a Java library for creating and manipulating vector geometry.
Pros of JTS
- More comprehensive set of geometric operations and algorithms
- Better support for OGC-compliant spatial operations
- Wider language support through ports (e.g., GEOS for C++)
Cons of JTS
- Less efficient for global-scale operations
- Lacks specialized features for spherical geometry
- May have slower performance for certain operations compared to S2
Code Comparison
S2Geometry:
S2LatLng latlng(S1Angle::Degrees(latitude), S1Angle::Degrees(longitude));
S2Point point = latlng.ToPoint();
S2Cap cap = S2Cap::FromCenterAngle(point, S1Angle::Radians(radius / 6371000.0));
JTS:
GeometryFactory factory = new GeometryFactory();
Coordinate coord = new Coordinate(longitude, latitude);
Point point = factory.createPoint(coord);
Geometry circle = point.buffer(radius / 111319.9);
Both libraries provide methods for creating points and circles, but S2Geometry uses spherical geometry concepts (S2LatLng, S2Cap), while JTS uses planar geometry (Coordinate, buffer). S2Geometry's approach is more suitable for global-scale operations, while JTS offers a more traditional GIS-style API.
Slice GeoJSON into vector tiles on the fly in the browser
Pros of geojson-vt
- Specialized for handling GeoJSON data and vector tiles
- Lightweight and focused on client-side processing
- Easier to integrate with web-based mapping applications
Cons of geojson-vt
- Limited to GeoJSON format, less versatile for other geometric operations
- May not be as performant for large-scale, complex geometric calculations
- Lacks advanced spatial indexing features found in s2geometry
Code Comparison
s2geometry:
S2LatLng latlng(S1Angle::Degrees(latitude), S1Angle::Degrees(longitude));
S2CellId cellId = S2CellId::FromLatLng(latlng);
S2Cell cell(cellId);
geojson-vt:
var geojsonvt = require('geojson-vt');
var tileIndex = geojsonvt(geojsonData);
var tile = tileIndex.getTile(z, x, y);
Summary
s2geometry is a comprehensive library for spherical geometry operations, offering advanced spatial indexing and geometric calculations. It's well-suited for complex geometric tasks and large-scale applications.
geojson-vt is a specialized tool for handling GeoJSON data and creating vector tiles. It's lightweight and ideal for web-based mapping applications, but may not be as versatile or performant for complex geometric operations compared to s2geometry.
Choose s2geometry for advanced geometric calculations and spatial indexing, or geojson-vt for simpler, web-focused GeoJSON processing and vector tile creation.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
S2 Geometry Library
Overview
This is a package 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. This makes it especially suitable for working with geographic data.
If you want to learn more about the library, start by reading the overview and quick start document, then read the introduction to the basic types.
S2 documentation can be found on s2geometry.io.
API/ABI Stability
Note that all releases are version 0.x, so there are no API or ABI stability guarantees. Starting with 1.0 we will adhere to SemVer and follow the Google OSS breaking change policy
The Python API is particularly unstable, and it is planned that the SWIGged API will be replaced by a pybind11 version with more Pythonic names and more complete functionality.
Requirements for End Users using Bazel
Build
This bazel build requires 7.0.0 â bzlmod default. Builds were tested using C++20 as set in .bazelrc. This setup relies on abseil-cpp, boringssl, and googletest from the bazel central repository as set in MODULE.bazel.
To build and test using bazel, from within s2geometry/src, run:
bazel test "//:*"
To build the libary without testing, from within s2geometry/src, run:
bazel build //:s2
Status
All tests enumerated in the BUILD.bazel file pass on x86 machines. On Apple M1, s2loop_measures_test fails due to a 6% excess accumlated error. This issue may require revision of boringssl or exactfloat.
Requirements for End Users using CMake
- We aim to support all platforms supported by the Google foundational C++ support policy
- CMake >= 3.5
- A C++ compiler with C++17 support, such as g++ >= 7.5 or clang >= 7.0.0
- Abseil >= LTS
20250512
(standard library extensions) - OpenSSL (for its bignum library)
- googletest testing framework >= 1.10 (to build tests and example programs, optional)
On Ubuntu, all of these other than abseil can be installed via apt-get:
sudo apt-get install cmake googletest libssl-dev
abseil-cpp may need to be installed from source if an LTS release is not
packaged for the platform. See the use of -DCMAKE_PREFIX_PATH
in the
build instructions below.
On macOS, use MacPorts or Homebrew. For MacPorts:
sudo port install cmake abseil gtest openssl
then use
cmake -DGOOGLETEST_ROOT=/opt/local/src -DCMAKE_PREFIX_PATH=/opt/local ..
in the build instructions below.
Build and Install
You may either download the source as a ZIP archive, or clone the git repository.
Via ZIP archive
Download ZIP file
cd [parent of directory where you want to put S2]
unzip [path to ZIP file]/s2geometry-master.zip
cd s2geometry-master
Via git clone
cd [parent of directory where you want to put S2]
git clone https://github.com/google/s2geometry.git
cd s2geometry
Building
First, install Abseil.
It must be configured with -DCMAKE_POSITION_INDEPENDENT_CODE=ON
.
s2geometry must be configured to use the same C++ version that
abseil uses. The easiest way to achieve this is to pass
-DCMAKE_CXX_STANDARD=17
to cmake
when compiling both abseil and
s2geometry.
From the appropriate directory depending on how you got the source:
mkdir build
cd build
# You can omit -DGOOGLETEST_ROOT to skip tests; see above for macOS.
# Use the same CMAKE_CXX_STANDARD value that was used with absl.
cmake -DGOOGLETEST_ROOT=/usr/src/googletest -DCMAKE_PREFIX_PATH=/path/to/absl/install -DCMAKE_CXX_STANDARD=17 ..
make -j $(nproc)
make test ARGS="-j$(nproc)" # If GOOGLETEST_ROOT specified above.
sudo make install
On macOS, sysctl -n hw.logicalcpu
is the equivalent of nproc
.
Disable building of shared libraries with -DBUILD_SHARED_LIBS=OFF
.
Enable the python interface with -DWITH_PYTHON=ON
.
If OpenSSL is installed in a non-standard location set OPENSSL_ROOT_DIR
before running configure, for example on macOS:
OPENSSL_ROOT_DIR=/opt/homebrew/Cellar/openssl@3/3.1.0 cmake -DCMAKE_PREFIX_PATH=/opt/homebrew -DCMAKE_CXX_STANDARD=17
Installing
From build
subdirectory:
make install
Prefix it with sudo
if needed:
sudo make install
NOTE: There is not uninstall
target but install_manifest.txt
may be helpful.
All files will be installed at location specified in CMAKE_INSTALL_PREFIX
variable.
Several suffix variables used for some file groups:
Variable | Default | Description |
---|---|---|
CMAKE_INSTALL_INCLUDEDIR | include | For header files |
CMAKE_INSTALL_BINDIR | bin | For executables and *.dll files on DLL -based platforms |
CMAKE_INSTALL_LIBDIR | lib | For library files (*.so , *.a , *.lib etc) |
If needed set this variables on command line as cmake
arguments with -D
prefix or edit from build
subdirectory:
make edit_cache
For more info read: The CMake Cache.
Python
If you want the Python interface, you need to run cmake using
-DWITH_PYTHON=ON
. You will also need to install the following dependencies:
- SWIG 4 (for Python support, optional)
- python3-dev (for Python support, optional)
which can be installed via
sudo apt-get install swig python3-dev
or on macOS:
sudo port install swig
Version 4.0 is required, but it should be easy to make it work 3.0 or probably even 2.0.
Python 3 is required.
Creating wheels
First, make a virtual environment and install build
into it:
python3 -m venv venv
source venv/bin/activate
pip install build
Then build the wheel:
python -m build
The resulting wheel will be in the dist
directory.
If OpenSSL is in a non-standard location make sure to set
OPENSSL_ROOT_DIR
; see above for more information.
Other S2 implementations
Disclaimer
This is not an official Google product.
Top Related Projects
Hexagonal hierarchical geospatial indexing system
The JTS Topology Suite is a Java library for creating and manipulating vector geometry.
Slice GeoJSON into vector tiles on the fly in the browser
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot