Dear Michael,

Thank you very much for your prompt and efficient reply : it solved the problem 
(the default value of the number of digits was set to 7).

To tell you the truth, I don't think I'd have found it on my own !
Cheers,
Loïc

________________________________________
De : Michael Sumner <[email protected]>
Envoyé : vendredi 27 septembre 2024 12:08
À : Loïc Valéry <[email protected]>
Cc : R-sig-geo Mailing List <[email protected]>
Objet : Re: [R-sig-Geo] Polygons changed when the geom column is converted to 
text and then back to sfc
 
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 <[email protected]> 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
[email protected]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo


--
Michael Sumner
Research Software Engineer
Australian Antarctic Division
Hobart, Australia
e-mail: [email protected]
_______________________________________________
R-sig-Geo mailing list
[email protected]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo

Reply via email to