1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
---
title: "Reading and Writing NetCDF-CF Geometry"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Reading and Writing NetCDF-CF Geometry}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.width=6,
fig.height=4
)
options(scipen = 9999)
```
## Introduction
This example shows how to
1. add polygon data to a NetCDF-CF Discrete Sampling Geometry timeSeries featureType file,
1. read the polygon data from the NetCDF file,
1. and write it to a standard GIS format.
Below the example, all the geometry types that `ncdfgeom` handles are shown in detail.
## Load Spatial Data
Tables of point, line, or polygon features with associated timeseries are the target for this functionality. Here, we use some sample data from the `ncdfgeom` package.
```{r libs}
example_file <- tempfile()
file.copy(from = system.file('extdata/example_huc_eta.nc', package = 'ncdfgeom'),
to = example_file,
overwrite = TRUE) -> quiet
polygons <- sf::read_sf(system.file('extdata/example_huc_eta.json', package = 'ncdfgeom'))
polygons <- dplyr::select(polygons, LOADDATE, AREASQKM, HUC12, NAME)
plot(sf::st_geometry(polygons))
```
Now we have the polygons as shown above and a NetCDF file with a header that looks like:
```{r dump_polygons, echo=FALSE, cache=TRUE, eval = FALSE}
try({ncdump <- system(paste("ncdump -h", example_file), intern = TRUE)
cat(ncdump ,sep = "\n")}, silent = TRUE)
```
Now we can use the `write_geometry` function to add the polygon data to the NetCDF file.
```{r demo}
(vars <- ncmeta::nc_vars(example_file))
ncdfgeom::write_geometry(nc_file=example_file,
geom_data = polygons,
instance_dim_name = "station",
variables = vars$name) -> example_file
```
Now the NetCDF file looks like:
```{r dump_polygons_ts, echo=FALSE}
try({ncdump <- system(paste("ncdump -h", example_file), intern = TRUE)
cat(ncdump ,sep = "\n")}, silent = TRUE)
```
Read the polygon data from the file and write it out to a geopackage.
```{r read, warning=F}
polygons_sf <- ncdfgeom::read_geometry(example_file)
plot(sf::st_geometry(polygons_sf))
sf::write_sf(polygons_sf, "polygons.gpkg")
```
```{r cleanup, echo=F}
temp <- file.remove(example_file, "polygons.gpkg")
```
## Geometry Types
```{r examples, child="../tests/testthat/data/geom_examples.md"}
|