Error saving a tess object as shapefile in R with sf::st_write

I'm using the spatstat package to create a hexagonal tessellation over a specified area and count points within each hexagon. After that, I want to save the tessellation as a shapefile for use in GIS.

How can I convert the tess object to sf or another compatible format so I can save it as a shapefile? Any guidance would be much appreciated!

Below is the code for a specific dataset which unfortunately isn't publicly available, but the plot is shown and it is the object QC_empresastur_hex that should be converted to sf format for subsequent export as shapefile.

library(spatstat)
hexagonos <- hextess (win_natales, 1000)
QC_ empresastur_hex <- quadratcount(empresastur_ppp, tess = hexagonos)
plot (empresastur_ppp, main = "Conteo de empresas por hexágonos", cols = "orange")
plot(QC_empresastur_hex, add = TRUE, cex = 1)

enter image description here

Upvotes: 1

Views: 54

Answers (1)

Ege Rubak
Ege Rubak

Reputation: 4507

Here is an example using a built-in dataset:

library(sf)
library(spatstat)
hex <- hextess(Window(chorley), s = 4)
counts <- quadratcount(chorley, tess = hex)
plot(counts)

hex_sf_list <- lapply(tiles(hex), st_as_sf)
hex_sf <- Reduce(rbind, hex_sf_list)
counts_sf <- cbind(counts = as.integer(counts), hex_sf)
plot(counts_sf)

Upvotes: 0

Related Questions