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

Loïc Valéry |v@|ery @end|ng |rom out|ook@|r
Fri Sep 27 10:10:17 CEST 2024


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>


More information about the R-sig-Geo mailing list