Skip to content

Commit 42242ac

Browse files
Add data retrieval functions and make pacakge pass checks
1 parent 635900b commit 42242ac

21 files changed

+390
-146
lines changed

DESCRIPTION

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
Type: Package
22
Package: geodk
33
Title: Access Danish Geospatial Data
4-
Version: 0.0.0.9000
5-
Date: 2024-11-25
4+
Version: 0.0.0.9001
5+
Date: 2025-03-10
66
Authors@R: c(
77
person("Aleksander", "Bang-Larsen", , "contact@aleksanderbl.dk",
88
role = c("aut", "cre", "cph"),
@@ -42,8 +42,6 @@ Remotes:
4242
Config/testthat/edition: 3
4343
Config/testthat/parallel: true
4444
Encoding: UTF-8
45-
LazyData: true
46-
LazyDataCompression: xz
4745
Roxygen: list(markdown = TRUE)
4846
RoxygenNote: 7.3.2
4947
X-schema.org-isPartOf: http://ropengov.org/

NAMESPACE

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,17 @@
22

33
S3method(geodk_enrich,default)
44
S3method(geodk_enrich,dkstat_Denmark_municipality_07)
5+
export(constituencies)
56
export(geodk_enrich)
7+
export(get_geo)
68
export(get_levels)
9+
export(municipalities)
10+
export(parishes)
711
export(plot_denmark)
812
export(plot_municipalities)
913
export(plot_regions)
14+
export(prefill_cache)
15+
export(regions)
1016
importFrom(ggplot2,aes)
1117
importFrom(ggplot2,geom_sf)
1218
importFrom(ggplot2,ggplot)

NEWS.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# geodk 0.0.0.9001
2+
3+
* Cached datasets have been removed.
4+
* The function `prefill_chache()` have been added.
5+
* Added data retrieval functions.
6+
* Implemented first class for dkstat integration. Datasets with class `dkstat_Denmark_municipality_07` should now work with `geodk_enrich()`.

R/data.R

Lines changed: 0 additions & 40 deletions
This file was deleted.

R/geodk_enrich.R

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#'
1010
#'
1111
#' @param x Dataframe. Preferably from `{dkstat}`
12+
#' @importFrom rlang .data
1213
#'
1314
#' @returns Returns a dataframe of the dimensions `cols +1 by rows`.
1415
#' @export
@@ -19,11 +20,17 @@ geodk_enrich <- function(x) {
1920

2021
#' @export
2122
geodk_enrich.default <- function(x) {
22-
cli::cli_abort("Only output from dkstat is supported at the moment")
23+
cli::cli_abort(
24+
c(
25+
"Only output from dkstat is supported at the moment.",
26+
"If you have provided output from dkstat and it still does not work,",
27+
"please submit an issue on {.url {'https://github.com/rOpenGov/geodk/issues'}}"
28+
)
29+
)
2330
}
2431

2532
#' @export
26-
geodk_enrich.dkstat_kom_omraade <- function(x) {
33+
geodk_enrich.dkstat_Denmark_municipality_07 <- function(x) {
2734

2835
# List groupings
2936
hele_landet <- "000 Hele landet"
@@ -38,32 +45,32 @@ geodk_enrich.dkstat_kom_omraade <- function(x) {
3845
cli::cli_inform(c("i" = "Getting municipality geometries."))
3946
# Get sf geometries for individual municipalities
4047
individuals_sf <- dawaR::get_map_data("kommuner", verbose = FALSE) |>
41-
dplyr::select(navn)
48+
dplyr::select(.data$navn)
4249

4350
cli::cli_inform(c("i" = "Assigning geometries to individual municipalities."))
4451
# Split individual observations and merge with geometries
4552
individuals <- x |>
46-
dplyr::filter(!KOMGRP %in% c(hele_landet, groups)) |>
47-
dplyr::mutate(navn = substr(KOMGRP, 5, nchar(KOMGRP))) |>
53+
dplyr::filter(!.data$KOMGRP %in% c(hele_landet, groups)) |>
54+
dplyr::mutate(navn = substr(.data$KOMGRP, 5, nchar(.data$KOMGRP))) |>
4855
dplyr::left_join(individuals_sf, by = dplyr::join_by(navn)) |>
49-
dplyr::select(-navn) |>
56+
dplyr::select(-.data$navn) |>
5057
sf::st_as_sf(crs = sf::st_crs(individuals_sf))
5158

5259
cli::cli_inform(c("i" = "Drawing geometries for municipality groupings."))
5360
# Group municipality geometries to their groupings and return sf geometries
5461
groupings_sf <- individuals |>
5562
dplyr::left_join(komgrp, by = c("KOMGRP" = "name")) |>
5663
stats::na.omit() |>
57-
dplyr::select(level, geometry) |>
64+
dplyr::select(.data$level, .data$geometry) |>
5865
unique() |>
5966
sf::st_as_sf(crs = sf::st_crs(individuals_sf)) |> #sf::st_union()
60-
dplyr::group_by(level) |>
67+
dplyr::group_by(.data$level) |>
6168
dplyr::summarise() # This uses the summarise method for sf objects.
6269

6370
cli::cli_inform(c("i" = "Assigning geometries to municipality groupings."))
6471
# Filter out groupings and merge with their geometries
6572
groupings <- x |>
66-
dplyr::filter(KOMGRP %in% groups) |>
73+
dplyr::filter(.data$KOMGRP %in% groups) |>
6774
dplyr::left_join(groupings_sf, by = c("KOMGRP" = "level")) |>
6875
sf::st_as_sf(crs = sf::st_crs(groupings_sf))
6976

@@ -75,15 +82,15 @@ geodk_enrich.dkstat_kom_omraade <- function(x) {
7582
sf::st_union(),
7683
nrow(
7784
x |>
78-
dplyr::filter(KOMGRP %in% hele_landet)
85+
dplyr::filter(.data$KOMGRP %in% hele_landet)
7986
)
8087
)
8188
)
8289

8390
cli::cli_inform(c("i" = "Assigning geometry to entire Denmark."))
8491
# Merge data with geometries
8592
denmark <- x |>
86-
dplyr::filter(KOMGRP %in% hele_landet) |>
93+
dplyr::filter(.data$KOMGRP %in% hele_landet) |>
8794
sf::st_sf(geometry = dk_sf)
8895

8996
cli::cli_inform(c("i" = "Merging data."))
@@ -94,12 +101,13 @@ geodk_enrich.dkstat_kom_omraade <- function(x) {
94101
individuals
95102
)
96103

97-
cli::cli_inform(c("i" = "Assigning the proper class."))
104+
# cli::cli_inform(c("i" = "Assigning the proper class."))
98105
# Return data.frame - Essentially strips all other classes
99106
# structure(
100107
# out,
101108
# class = "data.frame"
102109
# )
103110
class(out) <- "data.frame"
104-
}
105111

112+
out
113+
}

R/prefill_cache.R

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#' Prefill data retrieval cache
2+
#'
3+
#' @param specific_section Enter a seciton name (or vector of section names) you
4+
#' would like to have pre-filled. Options can be found in
5+
#' `dawaR::avaialable_sections()`
6+
#' @description This function prefills the cache of the underlying API-function.
7+
#' By default the sections shown in the example below is prefilled. This is
8+
#' meant to replace the cached dataset that once was in the package. It may be
9+
#' useful to prefill the cache before using `geodk_enrich`.
10+
#'
11+
#' @returns Returns nothing. Is called for the side effect of prefilling the
12+
#' cache.
13+
#' @export
14+
#'
15+
#' @examples
16+
#' sections <- c(
17+
#' "regioner",
18+
#' "kommuner",
19+
#' "postnumre",
20+
#' "sogne",
21+
#' "regioner",
22+
#' "opstillingskredse",
23+
#' "afstemningsomraader"
24+
#' )
25+
prefill_cache <- function(specific_section = NULL) {
26+
sections <- c(
27+
"regioner",
28+
"kommuner",
29+
"postnumre",
30+
"sogne",
31+
"regioner",
32+
"opstillingskredse",
33+
"afstemningsomraader",
34+
specific_section
35+
)
36+
37+
cli::cli_inform("This will prefill the cache for {sections}")
38+
39+
for (section in sections) {
40+
dawaR::get_map_data(section, cache = TRUE) |>
41+
invisible()
42+
}
43+
}

R/retrieval.R

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#' Get regions
2+
#'
3+
#' @returns Returns a `{sf}` object for the danish regions
4+
#' @importFrom rlang .data
5+
#' @export
6+
#' @family data retrieval
7+
regions <- function() {
8+
dawaR::get_map_data("regioner") |>
9+
dplyr::rename(code = .data$kode,
10+
name = .data$navn,
11+
visualcenter_x = .data$visueltcenter_x,
12+
visualcenter_y = .data$visueltcenter_y) |>
13+
dplyr::select(
14+
.data$code,
15+
.data$name,
16+
.data$visualcenter_x,
17+
.data$visualcenter_y
18+
)
19+
}
20+
21+
#' Get municipalities
22+
#'
23+
#' @returns Returns a `{sf}` object for the danish municipalities
24+
#' @importFrom rlang .data
25+
#' @export
26+
#' @family data retrieval
27+
municipalities <- function() {
28+
dawaR::get_map_data("kommuner") |>
29+
dplyr::rename(code = .data$kode,
30+
name = .data$navn,
31+
region_code = .data$regionskode,
32+
region_name = .data$regionsnavn,
33+
visualcenter_x = .data$visueltcenter_x,
34+
visualcenter_y = .data$visueltcenter_y) |>
35+
dplyr::select(
36+
.data$code,
37+
.data$name,
38+
.data$region_code,
39+
.data$region_name,
40+
.data$visualcenter_x,
41+
.data$visualcenter_y
42+
)
43+
}
44+
45+
#' Get parishes
46+
#'
47+
#' @returns Returns a `{sf}` object for the danish parishes
48+
#' @importFrom rlang .data
49+
#' @export
50+
#' @family data retrieval
51+
parishes <- function() {
52+
dawaR::get_map_data("sogne") |>
53+
dplyr::rename(code = .data$kode,
54+
name = .data$navn,
55+
visualcenter_x = .data$visueltcenter_x,
56+
visualcenter_y = .data$visueltcenter_y) |>
57+
dplyr::select(.data$code,
58+
.data$name,
59+
.data$visualcenter_x,
60+
.data$visualcenter_y)
61+
}
62+
63+
#' Get constituencies
64+
#'
65+
#' @returns Returns a `{sf}` object for the danish constituencies
66+
#' @importFrom rlang .data
67+
#' @export
68+
#' @family data retrieval
69+
constituencies <- function() {
70+
dawaR::get_map_data("afstemningsomraader") |>
71+
dplyr::rename(code = .data$nummer,
72+
name = .data$navn,
73+
placename = .data$afstemningsstedsnavn,
74+
address = .data$afstemningsstedadressebetegnelse,
75+
address_access_x = .data$afstemningssted_adgangspunkt_x,
76+
address_access_y = .data$afstemningssted_adgangspunkt_y,
77+
municipality = .data$kommunenavn,
78+
region = .data$regionsnavn,
79+
visualcenter_x = .data$visueltcenter_x,
80+
visualcenter_y = .data$visueltcenter_y) |>
81+
dplyr::select(
82+
.data$code,
83+
.data$name,
84+
.data$placename,
85+
.data$address,
86+
.data$address_access_x,
87+
.data$address_access_y,
88+
.data$municipality,
89+
.data$region,
90+
.data$visualcenter_x,
91+
.data$visualcenter_y
92+
)
93+
}
94+
95+
#' Get geogrpahies
96+
#'
97+
#' @returns Returns a `{sf}` object for the specified geography
98+
#'
99+
#' @param section The geographic level wanted.
100+
#'
101+
#' @export
102+
#' @family data retrieval
103+
get_geo <- function(section = "postnumre") {
104+
dawaR::get_map_data(section)
105+
}
106+

README.Rmd

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,29 @@ devtools::install_github("rOpenGov/geodk")
6767

6868
## Functions
6969

70-
`{geodk}` provides to main sets of functions:
70+
`{geodk}` provides three main sets of functions:
7171

7272
- Data retrieval
7373
- Plotting
74+
- Enrichment of statistical data from `{dkstat}`
75+
76+
### Data retrieval
77+
78+
The package contains a few functions for retrieving data.
79+
80+
```{r eval=FALSE}
81+
regions()
82+
municipalities()
83+
parishes()
84+
constituencies()
85+
```
86+
87+
The geographic levels not included in their own functions can be accessed through `get_geo()`.
88+
89+
```{r eval=FALSE}
90+
get_geo()
91+
prefill_cache()
92+
```
7493

7594
### Plotting
7695

@@ -93,3 +112,13 @@ municipality <- plot_municipalities(municipality = c("Aarhus", "Favrskov", "Vejl
93112
library(patchwork)
94113
region + municipality
95114
```
115+
116+
### Enrichment of statistical data from `{dkstat}`
117+
118+
The function `geodk_enrich()` enables you to add appropriate geographic information to statistical descriptions from the `{dkstat}` package.
119+
120+
```{r eval=FALSE}
121+
dkstat::dst_get_all_data("laby01") |>
122+
geodk::geodk_enrich()
123+
```
124+

0 commit comments

Comments
 (0)