Skip to content

Commit 1f59985

Browse files
author
Markus Kainu
committed
vignettes updated
1 parent 76e62c6 commit 1f59985

File tree

2 files changed

+48
-33
lines changed

2 files changed

+48
-33
lines changed

vignettes/geofi_nls_ogc.Rmd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
---
2-
title: "Accessing Spatial Data from NLS OGC apis with geofi"
2+
title: "Accessing Data from NLS OGC api"
33
author: "Markus Kainu"
44
date: "`r Sys.Date()`"
55
output: rmarkdown::html_vignette
66
vignette: >
7-
%\VignetteIndexEntry{Accessing Spatial Data from NLS OGC apis with geofi}
7+
%\VignetteIndexEntry{Accessing Data from NLS OGC api}
88
%\VignetteEngine{knitr::rmarkdown}
99
%\VignetteEncoding{UTF-8}
1010
---

vignettes/geofi_statfi_ogc.Rmd

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
---
2-
title: "Accessing Spatial and Population Data from Statistics Finland OGC api"
2+
title: "Accessing Data from Statistics Finland OGC api"
33
author: "Markus Kainu"
44
date: "`r Sys.Date()`"
55
output: rmarkdown::html_vignette
66
vignette: >
7-
%\VignetteIndexEntry{Accessing Spatial and Population Data from Statistics Finland OGC api}
7+
%\VignetteIndexEntry{Accessing Data from Statistics Finland OGC api}
88
%\VignetteEngine{knitr::rmarkdown}
99
%\VignetteEncoding{UTF-8}
1010
---
@@ -31,16 +31,15 @@ The `geofi` package provides tools to access spatial data from **Statistics Finl
3131
* Fetch population data linked to administrative units.
3232
* Access population data for statistical grid cells.
3333

34-
Unlike some other spatial data APIs, *no API key is required* to access Statistics Finland's OGC API, making it straightforward to get started. The package handles pagination, spatial filtering, and coordinate reference system (CRS) transformations, delivering data as `sf` objects compatible with the `sf` package for spatial analysis and visualization.
34+
The package handles pagination, spatial filtering, and coordinate reference system (CRS) transformations, delivering data as `sf` objects compatible with the `sf` package for spatial analysis and visualization.
3535

3636
## Package Overview
3737

3838
The `geofi` package includes the following key functions for accessing Statistics Finland data:
3939

40-
* `ogc_get_statfi_area()`: Retrieves administrative area polygons (e.g., municipalities, wellbeing areas) for specified years, scales, and tessellation types.
40+
* `ogc_get_statfi_area()`: Retrieves administrative area polygons (e.g., municipalities, wellbeing areas) for specified years and scales.
4141
* `ogc_get_statfi_area_pop()`: Fetches administrative area polygons with associated population data, pivoted into a wide format.
4242
* `ogc_get_statfi_statistical_grid()`: Retrieves population data for statistical grid cells at different resolutions (1km or 5km).
43-
* `fetch_ogc_api_statfi()`: An internal function that handles low-level API requests and pagination (not typically called directly by users).
4443

4544
All functions return spatial data as `sf` objects, making it easy to integrate with spatial analysis workflows in R.
4645

@@ -60,38 +59,37 @@ The `ogc_get_statfi_area()` function retrieves polygons for Finnish administrati
6059
Fetch all municipalities for 2022 at the 1:4,500,000 scale:
6160

6261
```{r}
63-
munis <- ogc_get_statfi_area(year = 2022, scale = 4500, tessellation = "kunta")
64-
print(munis)
62+
muni <- ogc_get_statfi_area(year = 2022, scale = 4500, tessellation = "kunta")
6563
```
6664

6765
Visualize the municipalities using `ggplot2`:
6866

6967
```{r}
70-
ggplot(munis) +
68+
ggplot(muni) +
7169
geom_sf() +
7270
theme_minimal() +
7371
labs(title = "Finnish Municipalities (2022)")
7472
```
7573

7674
### Example: Spatial Filtering with a Bounding Box
7775

78-
To retrieve municipalities within a specific area (e.g., southern Finland), use the bbox parameter. Coordinates should match the specified crs.
76+
To retrieve municipalities within a specific area (e.g., southern Finland), use the bbox parameter.
7977

8078
```{r}
81-
bbox <- "200000,6600000,500000,6900000" # In EPSG:3067
82-
munis_south <- ogc_get_statfi_area(
79+
bbox_finland_south <- "18.797607,59.573288,30.476074,61.695082"
80+
muni_south <- ogc_get_statfi_area(
8381
year = 2022,
8482
scale = 4500,
8583
tessellation = "kunta",
86-
bbox = bbox,
84+
bbox = bbox_finland_south,
8785
crs = 3067
8886
)
8987
```
9088

9189
Visualize the filtered results:
9290

9391
```{r}
94-
ggplot(munis_south) +
92+
ggplot(muni_south) +
9593
geom_sf() +
9694
theme_minimal() +
9795
labs(title = "Municipalities in Southern Finland (2022)")
@@ -120,30 +118,37 @@ The `ogc_get_statfi_area_pop()` function fetches administrative area polygons wi
120118

121119
### Example: Fetching Population Data
122120

123-
Retrieve population data for 2021:
121+
Retrieve population data for 2021.
124122

125123
```{r}
126124
pop_data <- ogc_get_statfi_area_pop(year = 2021, crs = 3067)
127-
print(pop_data)
128125
```
129126

130-
Visualize population density (assuming a variable like `population_total` exists):
127+
By default, function returns the at all regional breakdown available, and it is users task to filter out the regional breakdown of interest. At the moment, that can be down using regular expressions on the prefix of variable `areaStatisticalUnit_inspireId_localId`. Following prefixes are available `"avi", "ely", "kunta", "maakunta", "seutukunta", "suuralue"`
128+
129+
130+
Visualize the share of female population at municpality (`kunta`) level.
131131

132132
```{r}
133-
ggplot(pop_data) +
134-
geom_sf(aes(fill = population_total)) +
135-
scale_fill_viridis_c(option = "plasma") +
136-
theme_minimal() +
137-
labs(title = "Population by Administrative Area (2021)", fill = "Population")
133+
ggplot(data=pop_data |>
134+
filter(grepl("^kunta", areaStatisticalUnit_inspireId_localId))) +
135+
geom_sf(aes(fill = female_percentage)) +
136+
scale_fill_viridis_c(option = "plasma") +
137+
theme_minimal() +
138+
labs(title = "Population by Administrative Area (2021)", fill = "share of females (%)")
138139
```
139140

140141
## Example: Population Data with Bounding Box
141142

142143
Fetch population data within a bounding box:
143144

144145
```{r}
145-
bbox <- "200000,6600000,500000,6900000"
146-
pop_south <- ogc_get_statfi_area_pop(year = 2021, bbox = bbox, crs = 3067)
146+
pop_south <- ogc_get_statfi_area_pop(year = 2021, bbox = bbox_finland_south, crs = 4326)
147+
ggplot(data=pop_south |> filter(grepl("^kunta", areaStatisticalUnit_inspireId_localId))) +
148+
geom_sf(aes(fill = female_percentage)) +
149+
scale_fill_viridis_c(option = "plasma") +
150+
theme_minimal() +
151+
labs(title = "Population by Administrative Area (2021)", fill = "share of females (%)")
147152
```
148153

149154
## Step 3: Retrieving Population Data by Statistical Grid
@@ -160,16 +165,15 @@ The `ogc_get_statfi_statistical_grid()` function retrieves population data for s
160165
Retrieve population data for a 5km grid in 2021:
161166

162167
```{r}
163-
grid_data <- ogc_get_statfi_statistical_grid(year = 2021, resolution = 5000)
164-
print(grid_data)
168+
grid_data <- ogc_get_statfi_statistical_grid(year = 2021, resolution = 5000, bbox = bbox_finland_south)
165169
```
166170

167171
Visualize the grid data:
168172

169173
```{r}
170174
ggplot(grid_data) +
171-
geom_sf(aes(fill = population_total), color = NA) +
172-
scale_fill_viridis_c(option = "magma") +
175+
geom_sf(aes(fill = total_count), color = NA) +
176+
scale_fill_viridis_c(option = "magma", trans='sqrt') +
173177
theme_minimal() +
174178
labs(title = "Population by 5km Grid Cells (2021)", fill = "Population")
175179
```
@@ -179,14 +183,25 @@ ggplot(grid_data) +
179183
Fetch 1km grid data within a bounding box:
180184

181185
```{r}
182-
bbox <- "200000,6600000,500000,6900000"
183-
grid_south <- ogc_get_statfi_statistical_grid(
186+
bbox_capital_region <- "24.441147,60.102168,25.285034,60.369071"
187+
grid_capital <- ogc_get_statfi_statistical_grid(
184188
year = 2021,
185189
resolution = 1000,
186-
bbox = bbox
190+
bbox = bbox_capital_region
187191
)
188192
```
189193

194+
195+
Visualize the grid data:
196+
197+
```{r}
198+
ggplot(grid_capital) +
199+
geom_sf(aes(fill = total_count), color = NA) +
200+
scale_fill_viridis_c(option = "magma", trans='sqrt') +
201+
theme_minimal() +
202+
labs(title = "Population by 1km Grid Cells (2021)", fill = "Population")
203+
```
204+
190205
## Advanced Features
191206

192207
### Pagination
@@ -212,7 +227,7 @@ Note that `ogc_get_statfi_statistical_grid()` is fixed to EPSG:3067, as per the
212227

213228
### Bounding Box Filtering
214229

215-
The `bbox` parameter allows spatial filtering to focus on specific regions. Ensure coordinates match the specified `crs` (e.g., EPSG:3067 for grid data). Example format: "`200000`,`6600000`,`500000`,`6900000`".
230+
The `bbox` parameter allows spatial filtering to focus on specific regions. Bounding box coordinates in EPSG:4326 will work with both crs EPSG:4326 and EPSG:3067. Bounding box in EPSG:3067 requires crs to be also set to EPSG:3067 with the function argument. Example format: `"18.797607,59.573288,30.476074,61.695082"`.
216231

217232
## Best Practices
218233

0 commit comments

Comments
 (0)