Skip to content

Commit a7b3510

Browse files
committed
create function to manually add resource to package
1 parent 800eec7 commit a7b3510

File tree

2 files changed

+53
-29
lines changed

2 files changed

+53
-29
lines changed

source/R/add_manual_resource.R

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
add_manual_resource <- function(package, new_resource, replace = TRUE) {
2+
if (replace) {
3+
# Filter out any resource with same name or path
4+
package$resources <- Filter(function(res) {
5+
!(res$name == new_resource$name && res$path == new_resource$path)
6+
}, package$resources)
7+
} else {
8+
# If not overwriting, check for duplicates and warn
9+
conflict <- any(vapply(package$resources, function(res) {
10+
res$name == new_resource$name && res$path == new_resource$path
11+
}, logical(1)))
12+
13+
if (conflict) {
14+
warning(paste("Resource with same name and path already exists and",
15+
"`replace = FALSE`. Skipping."))
16+
return(package)
17+
}
18+
}
19+
20+
# Append new resource
21+
package$resources <- append(package$resources, list(new_resource))
22+
return(package)
23+
}

source/add_spatial_resources.Rmd

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ library(tidyverse) # Data wrangling and visualisation
2222
library(frictionless) # Create frictionless data package
2323
library(sf) # Spatial objects
2424
25+
# Source functions
26+
source(here::here("source", "R", "add_manual_resource.R"))
27+
2528
# Data path and create directory if necessary
2629
data_path <- here::here("data", "raw")
2730
dir.create(data_path, showWarnings = FALSE, recursive = TRUE)
@@ -89,38 +92,36 @@ Write to data package and add metadata.
8992
```{r}
9093
st_write(mgrs10_refgrid_belgium,
9194
file.path(package_path, "mgrs10_refgrid_belgium.geojson"),
92-
append = FALSE)
95+
delete_dsn = TRUE)
9396
```
9497

9598
```{r}
96-
if (FALSE) {
97-
# Read package
98-
b3data_package <- read_package(file.path(package_path, "datapackage.json"))
99-
100-
# Add spatial resources manually
101-
b3data_package$resources <- append(b3data_package$resources, list(
102-
list(
103-
name = "mgrs10_refgrid_belgium",
104-
path = "mgrs10_refgrid_belgium.geojson",
105-
profile = "spatial-data-resource",
106-
format = "geojson",
107-
title = "MGRS 10 Km reference grid Belgium",
108-
description = "MGRS 10 Km reference grid for the mainland of Belgium.",
109-
licenses = c(
110-
name = "CC0 1.0",
111-
path = "https://creativecommons.org/publicdomain/zero/1.0/",
112-
title = "Creative Commons Zero v1.0 Universal"
113-
)
114-
)
115-
))
116-
117-
# Write package to directory
118-
# Write package to directory
119-
write_package(
120-
package = b3data_package,
121-
directory = package_path,
122-
compress = TRUE)
123-
}
99+
# Read package
100+
b3data_package <- read_package(file.path(package_path, "datapackage.json"))
101+
102+
# Add resource to data package
103+
mgrs10_resource <- list(
104+
name = "mgrs10_refgrid_belgium",
105+
path = "mgrs10_refgrid_belgium.geojson",
106+
profile = "spatial-data-resource",
107+
format = "geojson",
108+
title = "MGRS 10 Km reference grid Belgium",
109+
description = "MGRS 10 Km reference grid for the mainland of Belgium.",
110+
licenses = c(
111+
name = "CC0 1.0",
112+
path = "https://creativecommons.org/publicdomain/zero/1.0/",
113+
title = "Creative Commons Zero v1.0 Universal"
114+
)
115+
)
116+
117+
b3data_package <- add_manual_resource(
118+
b3data_package, mgrs10_resource, replace = TRUE)
119+
120+
# Write package to directory
121+
write_package(
122+
package = b3data_package,
123+
directory = package_path,
124+
compress = TRUE)
124125
```
125126

126127
## EEA 100 km reference grid Europe

0 commit comments

Comments
 (0)