[R-sig-Geo] Polygons changed when the geom column is converted to text and then back to sfc

Michael Sumner md@umner @end|ng |rom gm@||@com
Fri Sep 27 12:08:07 CEST 2024


try 'digits = 8' or higher in st_as_text e.g.

test_chr <- test |>
  dplyr::mutate(geom = sf::st_as_text(geom, EWKT = TRUE, digits = 8)) |>
  as.data.frame()

(or set with options("digits" = <8 or higher>))

Default value doesn't seem to be documented in sf, but the source shows it
calls out to lwgeom::st_astext which defaults to whatever options("digits")
value is.

HTH





On Fri, Sep 27, 2024 at 6:23 PM Loïc Valéry <lvalery using outlook.fr> wrote:

> Dear list members,
>
> I hope this message finds you well.
> I submit to you a problem with which I have been struggling for several
> days without finding the beginning of a solution.
>
> I need (i) to convert the geom column of a sf object into text and
> transform the sf object into a dataframe,
>            (ii) then convert this dataframe back into an sf object.
> My problem is that this only operation changes the shape of the original
> polygons.
> I guess the problem lies with the layer itself, but so far I haven't been
> able to find the cause (the crs seems O.K., the geometry seems valid).
>
> Please find attached a test layer containing only three contiguous
> polygons and, below, the code needed to reproduce the problem :
>
>
> test <- sf::st_read("Please add your path/polygons.gpkg")
>
> # Converting the sf object into a dataframe with geom as text
> test_chr <- test |>
>   dplyr::mutate(geom = sf::st_as_text(geom, EWKT = TRUE)) |>
>   as.data.frame()
>
> # Converting back to a sf object
> test_new <- test_chr |>
>   dplyr::mutate(geom = sf::st_as_sfc(geom)) |>
>   sf::st_sf()
>
> # Comparing the original ('test') and the new ('test_new') sf objects
> plot(test$geom, lwd = 1, border = "blue")
> plot(test_new$geom, lwd = 2, lty = 2, border = "red", add = TRUE)
>
>
> Thank you in advance for your help.
> Cheers,
> Loïc
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo using r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>


-- 
Michael Sumner
Research Software Engineer
Australian Antarctic Division
Hobart, Australia
e-mail: mdsumner using gmail.com

	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list