Skip to content

Polygons changed when the geom column is converted to text and then back to sfc

3 messages · Loïc Valéry, Michael Sumner

#
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

-------------- next part --------------
A non-text attachment was scrubbed...
Name: polygons.zip
Type: application/x-zip-compressed
Size: 5544 bytes
Desc: polygons.zip
URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20240927/47dd18ad/attachment.bin>
#
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 at outlook.fr> wrote:

            

  
    
#
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 <mdsumner at gmail.com>
Envoy? :?vendredi 27 septembre 2024 12:08
? :?Lo?c Val?ry <lvalery at outlook.fr>
Cc?:?R-sig-geo Mailing List <R-sig-Geo at r-project.org>
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 <lvalery at 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 at 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 at gmail.com