s2 icon indicating copy to clipboard operation
s2 copied to clipboard

Converting s2_geography using supported methods is not guaranteed to get back the original data

Open tzakharko opened this issue 7 months ago • 1 comments

I am trying to work around the lack of serialization support (#259) by converting s2 geography data into other formats (such as using simple features or wkb), but the data can get corrupted under certain circumstances. That is, doing s2_as_geography(st_as_sfc(g)) or s2_as_geography(s2_as_binary(g)) is not guaranteed to recover the original data.

Consider the following example from real-world code:

g <- geodesic_grid$geography
all(s2_is_valid(g))
#> [1] TRUE

g1 <- as_s2_geography(s2_as_binary(g), check = FALSE)
all(s2_is_valid(g1))
#> [1] FALSE

g2 <- as_s2_geography(sf::st_as_sfc(g), check = FALSE)
all(s2_is_valid(g2))
#> [1] FALSE

Unfortunately, I cannot provide an MRE because I do not know how to accurately persist s2 geography data. In the cases I am dealing with, it seems to be a precision issue when converting the data between various formats. The affected geographies have some points that are very close by, and it appears that these are detected as duplicates when the data is converted back to s2. While I can fix this by running s2_simplify(), it would be good to have a way to persist the data as is.

tzakharko avatar Jul 12 '24 06:07 tzakharko