What is "sf_use_s2() is TRUE" output when loading R "sf" package?

,

When loading the “sf” package in R, it indicates that “sf_use_s2() is TRUE” in the output. What does this mean?

> library(sf)
Linking to GEOS 3.10.2, GDAL 3.4.3, PROJ 8.2.1; sf_use_s2() is TRUE

The “s2” is an enhancement of the “sf” package that allows one to complete spatial operations with data that is in geographic coordinate system (the coordinates are in degrees). Before, one would need to transform the data into a projection before doing any spatial operations.

The “s2” was developed by Google and it uses spherical datum. By default the “s2” is enabled and “sf” will default to using it whenever a spatial operation is done. One can turn it off and have “sf” use the traditional “GEOS” library, but the data will need to be transformed into a projection. To turn “s2” off, run the following command:

sf::sf_use_s2(FALSE)

I have found some drawbacks in having “s2” enabled:

  1. The “s2” library has more constrains on what it considers a valid geometry, compared to the traditional “GEOS” library. So additional data conditioning may be required.

  2. I have found using “s2” can result in 25 to 50% longer computation time compared to using the “GEOS” library. S2 requires the use of trig function to do the calculations, which I understand is computation slower. Additional GEOS tends to build spatial indexing on the fly which tends to improve performance. So if you are trying to squeeze out performance, try disabling “s2” but choose an appropriate projection for the data to accommodate distortion.

More information about s2 can be found here:

Hi, I think the sf package in R uses the S2 geometry library for spherical geometry operations by default. This is because S2 provides a number of advantages over traditional planar geometry algorithms, including improved accuracy, speed, and scalability. S2 is particularly well-suited for working with large geospatial datasets.