Creates a file with point, line or polygon instance data ready for the extended NetCDF-CF timeSeries featuretype format.

Will also add attributes if provided data has them.

write_geometry(
  nc_file,
  geom_data,
  instance_dim_name = NULL,
  variables = list()
)

Arguments

nc_file

character file path to the nc file to be created.

geom_data

sf data.frame with POINT, LINESTRING, MULTILINESTRING, POLYGON, or MULTIPOLYGON geometries. Note that three dimensional geometries are not supported.

instance_dim_name

character Not required if adding geometry to a NetCDF-CF Discrete Sampling Geometries timeSeries file. For a new file, will use package default -- "instance" -- if not supplied.

variables

character If a an existing netCDF files is provided, this list of variables that should be related to the geometries.

Examples


hucPolygons <- sf::read_sf(system.file('extdata','example_huc_eta.json', package = 'ncdfgeom'))

hucPolygons_nc <- ncdfgeom::write_geometry(nc_file=tempfile(), 
                                           geom_data = hucPolygons)
try({
  ncdump <- system(paste("ncdump -h", hucPolygons_nc), intern = TRUE)
  cat(ncdump ,sep = "\n")
}, silent = TRUE)
#> netcdf file3e9c4c1bbc5 {
#> dimensions:
#> 	instance = 2 ;
#> 	char = 38 ;
#> 	node = 2628 ;
#> variables:
#> 	char TNMID(instance, char) ;
#> 		TNMID:units = "unknown" ;
#> 		TNMID:grid_mapping = "grid_mapping" ;
#> 		TNMID:geometry = "geometry_container" ;
#> 	char METASOURCE(instance, char) ;
#> 		METASOURCE:units = "unknown" ;
#> 		METASOURCE:grid_mapping = "grid_mapping" ;
#> 		METASOURCE:geometry = "geometry_container" ;
#> 	char SOURCEDATA(instance, char) ;
#> 		SOURCEDATA:units = "unknown" ;
#> 		SOURCEDATA:grid_mapping = "grid_mapping" ;
#> 		SOURCEDATA:geometry = "geometry_container" ;
#> 	char SOURCEORIG(instance, char) ;
#> 		SOURCEORIG:units = "unknown" ;
#> 		SOURCEORIG:grid_mapping = "grid_mapping" ;
#> 		SOURCEORIG:geometry = "geometry_container" ;
#> 	char SOURCEFEAT(instance, char) ;
#> 		SOURCEFEAT:units = "unknown" ;
#> 		SOURCEFEAT:grid_mapping = "grid_mapping" ;
#> 		SOURCEFEAT:geometry = "geometry_container" ;
#> 	char LOADDATE(instance, char) ;
#> 		LOADDATE:units = "unknown" ;
#> 		LOADDATE:grid_mapping = "grid_mapping" ;
#> 		LOADDATE:geometry = "geometry_container" ;
#> 	char GNIS_ID(instance, char) ;
#> 		GNIS_ID:units = "unknown" ;
#> 		GNIS_ID:grid_mapping = "grid_mapping" ;
#> 		GNIS_ID:geometry = "geometry_container" ;
#> 	double AREAACRES(instance) ;
#> 		AREAACRES:units = "unknown" ;
#> 		AREAACRES:missing_value = -9999.999 ;
#> 		AREAACRES:grid_mapping = "grid_mapping" ;
#> 		AREAACRES:geometry = "geometry_container" ;
#> 	double AREASQKM(instance) ;
#> 		AREASQKM:units = "unknown" ;
#> 		AREASQKM:missing_value = -9999.999 ;
#> 		AREASQKM:grid_mapping = "grid_mapping" ;
#> 		AREASQKM:geometry = "geometry_container" ;
#> 	char STATES(instance, char) ;
#> 		STATES:units = "unknown" ;
#> 		STATES:grid_mapping = "grid_mapping" ;
#> 		STATES:geometry = "geometry_container" ;
#> 	char HUC12(instance, char) ;
#> 		HUC12:units = "unknown" ;
#> 		HUC12:grid_mapping = "grid_mapping" ;
#> 		HUC12:geometry = "geometry_container" ;
#> 	char NAME(instance, char) ;
#> 		NAME:units = "unknown" ;
#> 		NAME:grid_mapping = "grid_mapping" ;
#> 		NAME:geometry = "geometry_container" ;
#> 	char HUTYPE(instance, char) ;
#> 		HUTYPE:units = "unknown" ;
#> 		HUTYPE:grid_mapping = "grid_mapping" ;
#> 		HUTYPE:geometry = "geometry_container" ;
#> 	char HUMOD(instance, char) ;
#> 		HUMOD:units = "unknown" ;
#> 		HUMOD:grid_mapping = "grid_mapping" ;
#> 		HUMOD:geometry = "geometry_container" ;
#> 	char TOHUC(instance, char) ;
#> 		TOHUC:units = "unknown" ;
#> 		TOHUC:grid_mapping = "grid_mapping" ;
#> 		TOHUC:geometry = "geometry_container" ;
#> 	double NONCONTRIB(instance) ;
#> 		NONCONTRIB:units = "unknown" ;
#> 		NONCONTRIB:missing_value = -9999.999 ;
#> 		NONCONTRIB:grid_mapping = "grid_mapping" ;
#> 		NONCONTRIB:geometry = "geometry_container" ;
#> 	double NONCONTR_1(instance) ;
#> 		NONCONTR_1:units = "unknown" ;
#> 		NONCONTR_1:missing_value = -9999.999 ;
#> 		NONCONTR_1:grid_mapping = "grid_mapping" ;
#> 		NONCONTR_1:geometry = "geometry_container" ;
#> 	double SHAPE_Leng(instance) ;
#> 		SHAPE_Leng:units = "unknown" ;
#> 		SHAPE_Leng:missing_value = -9999.999 ;
#> 		SHAPE_Leng:grid_mapping = "grid_mapping" ;
#> 		SHAPE_Leng:geometry = "geometry_container" ;
#> 	double SHAPE_Area(instance) ;
#> 		SHAPE_Area:units = "unknown" ;
#> 		SHAPE_Area:missing_value = -9999.999 ;
#> 		SHAPE_Area:grid_mapping = "grid_mapping" ;
#> 		SHAPE_Area:geometry = "geometry_container" ;
#> 	double x_nodes(node) ;
#> 		x_nodes:units = "degrees_east" ;
#> 		x_nodes:axis = "X" ;
#> 	double y_nodes(node) ;
#> 		y_nodes:units = "degrees_north" ;
#> 		y_nodes:axis = "Y" ;
#> 	int geometry_container ;
#> 		geometry_container:node_coordinates = "x_nodes y_nodes" ;
#> 		geometry_container:geometry_type = "polygon" ;
#> 		geometry_container:node_count = "node_count" ;
#> 		geometry_container:grid_mapping = "grid_mapping" ;
#> 	int node_count(instance) ;
#> 		node_count:long_name = "count of coordinates in each instance geometry" ;
#> 	int grid_mapping ;
#> 		grid_mapping:grid_mapping_name = "latitude_longitude" ;
#> 		grid_mapping:semi_major_axis = 6378137. ;
#> 		grid_mapping:inverse_flattening = 298.257223563 ;
#> 		grid_mapping:longitude_of_prime_meridian = 0. ;
#> 
#> // global attributes:
#> 		:Conventions = "CF-1.8" ;
#> }