Skip to content

Commit f3360fc

Browse files
authored
Merge pull request #5 from b-cubed-eu/add-manual-resource
Add manual resource
2 parents 2390c89 + 94e27bd commit f3360fc

File tree

5 files changed

+143
-41
lines changed

5 files changed

+143
-41
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*.xls*
1818
*_files
1919
*.gz
20+
*.geojson
2021
docs
2122
libs
2223
output

data/b3data_package/datapackage.json

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,15 @@
1111
"encoding": "utf-8",
1212
"title": "Occurrence cube for birds in Belgium (MGRS 10 km)",
1313
"description": "Occurrence cube for birds in Belgium between 2000 en 2024. The taxonomical resolution is 'species' and the temporal resolution is 'year' Spatial aggregation is done using the MGRS grid at 10 km scale. Only grid cells that fall within the 10 km MGRS reference grid for mainland Belgium (see b3data: `mgrs10_refgrid_belgium.geojson`) are included.",
14-
"sources": ["GBIF Occurrence Download", "https://doi.org/10.15468/dl.y3wpwk"],
15-
"licenses": [" CC BY-NC 4.0", "https://creativecommons.org/licenses/by-nc/4.0/", "Creative Commons Attribution-NonCommercial 4.0 International"],
14+
"sources": {
15+
"title": "GBIF Occurrence Download",
16+
"path": "https://doi.org/10.15468/dl.y3wpwk"
17+
},
18+
"licenses": {
19+
"name": "CC BY-NC 4.0",
20+
"path": "https://creativecommons.org/licenses/by-nc/4.0/",
21+
"title": "Creative Commons Attribution-NonCommercial 4.0 International"
22+
},
1623
"schema": {
1724
"fields": [
1825
{
@@ -49,27 +56,47 @@
4956
}
5057
]
5158
}
59+
},
60+
{
61+
"name": "mgrs10_refgrid_belgium",
62+
"path": "mgrs10_refgrid_belgium.geojson",
63+
"profile": "spatial-data-resource",
64+
"format": "geojson",
65+
"title": "MGRS 10 Km reference grid Belgium",
66+
"description": "MGRS 10 Km reference grid for the mainland of Belgium.",
67+
"licenses": {
68+
"name": "CC0 1.0",
69+
"path": "https://creativecommons.org/publicdomain/zero/1.0/",
70+
"title": "Creative Commons Zero v1.0 Universal"
71+
}
5272
}
5373
],
5474
"title": "b3data: Data resources for the b3verse",
5575
"description": "This data package contains data resources to be used across the b3verse (https://docs.b-cubed.eu/guides/b3verse/). This includes example datasets (occurrence cubes) as well as spatial resources like reference grids or raster data.",
56-
"keywords1": "data cubes",
57-
"licenses.name": " CC BY-NC 4.0",
76+
"keywords": ["data cubes", "b3verse", "frictionless", "biodiversity"],
77+
"licenses": {
78+
"name": "CC BY-NC 4.0",
79+
"path": "https://creativecommons.org/licenses/by-nc/4.0/",
80+
"title": "Creative Commons Attribution-NonCommercial 4.0 International"
81+
},
5882
"version": "0.1.0",
59-
"sources.title": "b3data-scripts",
60-
"contributors.title": "Ward Langeraert",
61-
"contributors.path": "https://orcid.org/0000-0002-5900-8109",
62-
"contributors.email": "[email protected]",
63-
"contributors.role": "author",
64-
"contributors.organization": "Research Institute for Nature and Forest (INBO)",
65-
"contributors.title.1": "Toon Van Daele",
66-
"contributors.path.1": "https://orcid.org/0000-0002-1362-853X",
67-
"contributors.role.1": "contributor",
68-
"contributors.organization.1": "Research Institute for Nature and Forest (INBO)",
69-
"sources.path": "https://github.com/b-cubed-eu/b3data-scripts",
70-
"licenses.path": "https://creativecommons.org/licenses/by-nc/4.0/",
71-
"licenses.title": "Creative Commons Attribution-NonCommercial 4.0 International",
72-
"keywords2": "b3verse",
73-
"keywords3": "frictionless",
74-
"keywords4": "biodiversity"
83+
"sources": {
84+
"title": "b3data-scripts",
85+
"path": "https://github.com/b-cubed-eu/b3data-scripts"
86+
},
87+
"contributors": [
88+
{
89+
"title": "Ward Langeraert",
90+
"path": "https://orcid.org/0000-0002-5900-8109",
91+
"email": "[email protected]",
92+
"role": "author",
93+
"organization": "Research Institute for Nature and Forest (INBO)"
94+
},
95+
{
96+
"title": "Toon Van Daele",
97+
"path": "https://orcid.org/0000-0002-1362-853X",
98+
"role": "contributor",
99+
"organization": "Research Institute for Nature and Forest (INBO)"
100+
}
101+
]
75102
}

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: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,15 @@ 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)
2831
29-
out_path <- here::here("data", "processed")
30-
dir.create(out_path, showWarnings = FALSE, recursive = TRUE)
32+
package_path <- here::here("data", "b3data_package")
33+
dir.create(package_path, showWarnings = FALSE, recursive = TRUE)
3134
```
3235

3336
# Goal
@@ -64,6 +67,8 @@ Each dataset includes the following metadata (see: [Frictionless resource spec](
6467
# Datasets
6568
## MGRS 10 Km reference grid Belgium
6669

70+
Load the data.
71+
6772
```{r}
6873
# Read reference grid
6974
utm10_bel <- st_read(file.path(data_path, "utm10_bel.shp"))
@@ -72,9 +77,53 @@ utm10_bel <- st_read(file.path(data_path, "utm10_bel.shp"))
7277
ggplot() + geom_sf(data = utm10_bel)
7378
```
7479

80+
Clean data and transform cell codes to MGRS.
81+
82+
```{r}
83+
mgrs10_refgrid_belgium <- utm10_bel %>%
84+
select(utm_tag = TAG, geometry) %>%
85+
mutate(mgrscode = ifelse(grepl("^[A-G]", utm_tag), paste0("31U", utm_tag),
86+
paste0("32U", utm_tag))) %>%
87+
select(mgrscode, geometry)
88+
```
89+
90+
Write to data package and add metadata.
91+
92+
```{r}
93+
st_write(mgrs10_refgrid_belgium,
94+
file.path(package_path, "mgrs10_refgrid_belgium.geojson"),
95+
delete_dsn = TRUE)
96+
```
97+
98+
```{r}
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 = list(
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)
125+
```
75126

76127
## EEA 100 km reference grid Europe
77128

78129
> Coming soon
79-
80-
# Write package

source/create_b3data_package.Rmd

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ ggplot() + geom_sf(data = utm10_bel)
152152
```{r}
153153
# Only select grid cells from reference grid
154154
bird_cube_belgium_mgrs10 <- bird_cube_belgium_mgrs10_full %>%
155-
filter(substr(mgrscode, 4, nchar(mgrscode)) %in% utm10_bel$TAG)
155+
filter(substring(mgrscode, 4) %in% utm10_bel$TAG)
156156
```
157157

158158
We create the package and add the dataset.
@@ -171,12 +171,12 @@ b3data_package <- create_package() %>%
171171
"Only grid cells that fall within the 10 km MGRS reference grid for",
172172
"mainland Belgium (see b3data: `mgrs10_refgrid_belgium.geojson`) are",
173173
"included."),
174-
sources = c(
174+
sources = list(
175175
title = "GBIF Occurrence Download",
176176
path = "https://doi.org/10.15468/dl.y3wpwk"
177177
),
178-
licenses = c(
179-
name = " CC BY-NC 4.0",
178+
licenses = list(
179+
name = "CC BY-NC 4.0",
180180
path = "https://creativecommons.org/licenses/by-nc/4.0/",
181181
title = "Creative Commons Attribution-NonCommercial 4.0 International"
182182
)
@@ -210,45 +210,47 @@ b3data_package <- append(
210210
"resources like reference grids or raster data."
211211
)),
212212
after = 4)
213-
b3data_package <- append(b3data_package,
214-
c(keywords = c("data cubes", "b3verse", "frictionless",
215-
"biodiversity")),
216-
after = 5)
217213
b3data_package <- append(
218214
b3data_package,
219-
c(licenses = c(
220-
name = " CC BY-NC 4.0",
215+
c(keywords = list(list(
216+
"data cubes", "b3verse", "frictionless", "biodiversity"
217+
))),
218+
after = 5)
219+
b3data_package <- append(
220+
b3data_package,
221+
c(licenses = list(list(
222+
name = "CC BY-NC 4.0",
221223
path = "https://creativecommons.org/licenses/by-nc/4.0/",
222224
title = "Creative Commons Attribution-NonCommercial 4.0 International"
223-
)),
225+
))),
224226
after = 6)
225227
b3data_package <- append(b3data_package,
226228
c(version = "0.1.0"),
227229
after = 7)
228230
b3data_package <- append(
229231
b3data_package,
230-
c(sources = c(
232+
c(sources = list(list(
231233
title = "b3data-scripts",
232234
path = "https://github.com/b-cubed-eu/b3data-scripts"
233-
)),
235+
))),
234236
after = 8)
235237
b3data_package <- append(
236238
b3data_package,
237-
c(contributors = c(
238-
c(
239+
c(contributors = list(list(
240+
list(
239241
title = "Ward Langeraert",
240242
path = "https://orcid.org/0000-0002-5900-8109",
241243
email = "[email protected]",
242244
role = "author",
243245
organization = "Research Institute for Nature and Forest (INBO)"
244246
),
245-
c(
247+
list(
246248
title = "Toon Van Daele",
247249
path = "https://orcid.org/0000-0002-1362-853X",
248250
role = "contributor",
249251
organization = "Research Institute for Nature and Forest (INBO)"
250252
)
251-
)),
253+
))),
252254
after = 9)
253255
254256
# Warning: append() drops the custom datapackage class.

0 commit comments

Comments
 (0)