-
Help
DescriptionHi there, I've had some success with storing raster files as geotiffs in targets, as per the solution here: #1238 However I cannot work out how to store a shapefile? I had assumed that I could write a format_shapefile <- tar_format(
read = function(path) terra::vect(path),
write = function(object, path) {
terra::writeVector(
x = object,
filename = path,
filetype = "ESRI Shapefile",
overwrite = TRUE
)
},
marshal = function(object) terra::wrap(object),
unmarshal = function(object) terra::unwrap(object)
) That could be used like so: tar_target(
example_shapefile,
get_example_shapefile(),
format = format_shapefile
) Where get_example_shapefile <- function() {
f <- system.file("ex/lux.shp", package="terra")
v <- vect(f)
v
} I've written a reprex here: https://github.com/njtierney/demo-geospatial-targets |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 8 replies
-
I think the main issue with a shapefile is that it is actually multiple files.
So, perhaps the simplest solution would be to use a different vector storage format that is only a single file. (I personally like .gpkg/GeoPackage for this). If each GeoPackage contains only one layer it is functionally equivalent to a shapefile without limitations of DBF etc. If you want to make shapefile work, or any target that might constitute multiple related files, can use a trick I suggested in side discussion of #809 (reply in thread) and bundle the multiple files in a ZIP file. As discussed in #809 there are analogous issues with GeoTIFF that contain categorical information, as this is stored in a .tif.aux.xml "sidecar" file by GDAL. For zipped spatial data sources you can then use GDAL virtual filesystem I updated your reprex here with an example of how this can be done. The bulk of the changes were in the format_shapefile <- tar_format(
read = function(path) terra::vect(paste0("/vsizip/", file.path(path, gsub("\\.zip", ".shp", basename(path))))),
write = function(object, path) {
terra::writeVector(
x = object,
filename = gsub("\\.zip", ".shp", basename(path)),
filetype = "ESRI Shapefile",
overwrite = TRUE
)
zf <- list.files(pattern = gsub("\\.zip", "", basename(path)))
utils::zip(path, zf)
unlink(zf)
},
marshal = function(object) terra::wrap(object),
unmarshal = function(object) terra::unwrap(object)
) When run it gives the following output running
|
Beta Was this translation helpful? Give feedback.
I think the main issue with a shapefile is that it is actually multiple files.
tar_format(..., write=...)
argument requires thatThe function need not return a value, but the file written to path must be a single file, and it cannot be a directory.
So, perhaps the simplest solution would be to use a different vector storage format that is only a single file. (I personally like .gpkg/GeoPackage for this). If each GeoPackage contains only one layer it is functionally equivalent to a shapefile without limitations of DBF etc.
If you want to make shapefile work, or any target that might constitute multiple related files, can use a trick I suggested in side discussion of #809 (reply in thread) …