geocompx / geocompr

Geocomputation with R: an open source book
https://r.geocompx.org/
Other
1.59k stars 584 forks source link

Using write_sf() to add a new layer (8.4.1) #1065

Closed tibbles-and-tribbles closed 9 months ago

tibbles-and-tribbles commented 9 months ago

In subsection 8.4.1, you state that setting append = TRUE will create a new layer, but I'm not able to get that result.

write_sf(world, dsn = "world_many_layers.gpkg", append = TRUE)

st_layers("world_many_layers.gpkg")

Output:

Driver: GPKG 
Available layers:
         layer_name geometry_type features fields crs_name
1 world_many_layers Multi Polygon      177     10   WGS 84

Even if I use the world.gpkg file instead, I still don't get a multilayer file.

write_sf(world, dsn = "world.gpkg")

write_sf(world, dsn = "world.gpkg", append = TRUE)

st_layers("world.gpkg")

Output:

Driver: GPKG 
Available layers:
  layer_name geometry_type features fields crs_name
1      world Multi Polygon      354     10   WGS 84
Nowosad commented 9 months ago

Thanks @tibbles-and-tribbles! I looked at the write_sf help file and it states "append logical; should we append to an existing layer, or replace it? if TRUE append". Thus, it seems to me like a bug in sf. @Robinlovelace -- can you confirm it? @tibbles-and-tribbles can you open a new issue on the sf issue tracker?

library(sf)
library(spData)
write_sf(world, dsn = "world_many_layers.gpkg", append = TRUE)
write_sf(world, dsn = "world_many_layers.gpkg", append = TRUE)
st_layers("world_many_layers.gpkg")
#> Driver: GPKG 
#> Available layers:
#>          layer_name geometry_type features fields crs_name
#> 1 world_many_layers Multi Polygon      354     10   WGS 84
tibbles-and-tribbles commented 9 months ago

I thought the documentation matched my results (or maybe I'm just interpreting it incorrectly). The new data is being appended to an existing layer when I set append = TRUE, for example, the existing layer in world.gpkg doubled in terms of features. But the docs for append don't say that a new layer would be created.

Nowosad commented 9 months ago

@tibbles-and-tribbles -- you are totally right! Sorry for adding more confusion

Nowosad commented 9 months ago

@tibbles-and-tribbles What do you think about the following fix?

Instead of overwriting the file, we could add a new layer to the file by specifying the `layer` argument.
This is supported by several spatial formats, including GeoPackage.

```{r 07-read-write-plot-31, results='hide'}
write_sf(obj = world, dsn = "world_many_layers.gpkg", layer = "second_layer")
tibbles-and-tribbles commented 9 months ago

@tibbles-and-tribbles -- you are totally right! Sorry for adding more confusion

No worries. I'm just happy that I can contribute!

tibbles-and-tribbles commented 9 months ago

@tibbles-and-tribbles What do you think about the following fix?

Instead of overwriting the file, we could add a new layer to the file by specifying the `layer` argument.
This is supported by several spatial formats, including GeoPackage.

```{r 07-read-write-plot-31, results='hide'}
write_sf(obj = world, dsn = "world_many_layers.gpkg", layer = "second_layer")

Sure, that makes more sense, but I haven't played around with the layer argument yet.

Nowosad commented 9 months ago

See:

library(sf)
#> Linking to GEOS 3.12.1, GDAL 3.7.3, PROJ 9.2.1; sf_use_s2() is TRUE
library(spData)
write_sf(world, dsn = "world_many_layers.gpkg", append = TRUE)
write_sf(world, dsn = "world_many_layers.gpkg", layer = "second_layer")
st_layers("world_many_layers.gpkg")
#> Driver: GPKG 
#> Available layers:
#>          layer_name geometry_type features fields crs_name
#> 1 world_many_layers Multi Polygon      177     10   WGS 84
#> 2      second_layer Multi Polygon      177     10   WGS 84