Save the result of a geoquery in a geopackage. The result can be a layer in the form of a flat table or a list consisting of a layer and a description table of the variables.
Examples
gms <- geomultistar(ms = ms_mrs, geodimension = "where") |>
define_geoattribute(
attribute = "city",
from_layer = usa_cities,
by = c("city" = "city", "state" = "state")
) |>
define_geoattribute(
attribute = "state",
from_layer = usa_states,
by = c("state" = "state")
) |>
define_geoattribute(attribute = "region",
from_attribute = "state") |>
define_geoattribute(attribute = "all_where",
from_layer = usa_nation)
gdq <- dimensional_query(gms) |>
select_dimension(name = "where",
attributes = c("state", "city")) |>
select_dimension(name = "when",
attributes = c("when_happened_year", "when_happened_week")) |>
select_fact(
name = "mrs_age",
measures = c("n_deaths")
) |>
select_fact(name = "mrs_cause",
measures = c("pneumonia_and_influenza_deaths", "other_deaths")) |>
filter_dimension(name = "when", when_happened_week <= "03") |>
filter_dimension(name = "where", state == "MA")
sf <- gdq |>
run_geoquery(wider = TRUE)
save_as_geopackage(sf, "city", filepath = tempdir())
#> Deleting source `/tmp/RtmpeEsTdx/city.gpkg' failed
#> Writing layer `city' to data source `/tmp/RtmpeEsTdx/city.gpkg' using driver `GPKG'
#> Writing 9 features with 19 fields and geometry type Point.
#> $sf
#> Simple feature collection with 9 features and 19 fields
#> Geometry type: POINT
#> Dimension: XY
#> Bounding box: xmin: -72.58981 ymin: 41.63622 xmax: -70.9342 ymax: 42.63342
#> Geodetic CRS: NAD83
#> # A tibble: 9 × 20
#> fid when_happened_year state city n_deaths_01 n_deaths_02 n_deaths_03
#> <int> <chr> <chr> <chr> <int> <int> <int>
#> 1 1 1962 MA Boston 262 270 237
#> 2 2 1962 MA Cambridge 38 27 36
#> 3 3 1962 MA Fall River 31 38 35
#> 4 4 1962 MA Lowell 35 37 28
#> 5 5 1962 MA Lynn 31 28 26
#> 6 6 1962 MA New Bedford 33 25 18
#> 7 7 1962 MA Somerville 19 21 20
#> 8 8 1962 MA Springfield 50 35 36
#> 9 9 1962 MA Worcester 62 71 43
#> # ℹ 13 more variables: nrow_agg_01 <int>, nrow_agg_02 <int>, nrow_agg_03 <int>,
#> # mrs_cause_pneumonia_and_influenza_deaths_01 <int>,
#> # mrs_cause_pneumonia_and_influenza_deaths_02 <int>,
#> # mrs_cause_pneumonia_and_influenza_deaths_03 <int>,
#> # mrs_cause_other_deaths_01 <int>, mrs_cause_other_deaths_02 <int>,
#> # mrs_cause_other_deaths_03 <int>, mrs_cause_nrow_agg_01 <int>,
#> # mrs_cause_nrow_agg_02 <int>, mrs_cause_nrow_agg_03 <int>, …
#>
#> $variables
#> # A tibble: 15 × 3
#> id_variable measure when_happened_week
#> <chr> <chr> <chr>
#> 1 n_deaths_01 n_deaths 01
#> 2 n_deaths_02 n_deaths 02
#> 3 n_deaths_03 n_deaths 03
#> 4 nrow_agg_01 nrow_agg 01
#> 5 nrow_agg_02 nrow_agg 02
#> 6 nrow_agg_03 nrow_agg 03
#> 7 mrs_cause_pneumonia_and_influenza_deaths_01 mrs_cause_pne… 01
#> 8 mrs_cause_pneumonia_and_influenza_deaths_02 mrs_cause_pne… 02
#> 9 mrs_cause_pneumonia_and_influenza_deaths_03 mrs_cause_pne… 03
#> 10 mrs_cause_other_deaths_01 mrs_cause_oth… 01
#> 11 mrs_cause_other_deaths_02 mrs_cause_oth… 02
#> 12 mrs_cause_other_deaths_03 mrs_cause_oth… 03
#> 13 mrs_cause_nrow_agg_01 mrs_cause_nro… 01
#> 14 mrs_cause_nrow_agg_02 mrs_cause_nro… 02
#> 15 mrs_cause_nrow_agg_03 mrs_cause_nro… 03
#>