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,
  2. read the polygon data from the NetCDF file,
  3. 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.

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:

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.

(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:

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.

polygons_sf <- ncdfgeom::read_geometry(example_file)

plot(sf::st_geometry(polygons_sf))
sf::write_sf(polygons_sf, "polygons.gpkg")
temp <- file.remove(example_file, "polygons.gpkg")

Geometry Types

```r



USGS-R/netcdf.dsg documentation built on Feb. 9, 2024, 6:21 p.m.