GrassRaster Plugin¶
The module supplies support for reading and writing GRASS rasters. The metadata contain both the color table and the categories (if available) of the raster map. The original color table of the GRASS raster is read into the GeoTools coverage categories and can as such be visualized if using a default styler.
Maven:
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-grassraster</artifactId>
<version>${geotools.version}</version>
</dependency>
Data structure of a raster map¶
Raster data in GRASS are strictly bound to their workspace. Since a workspace is something more on user level, I will from now on use grass database for the folder structure and workspace for what the user will interact with at application level.
A GRASS raster map consists of several files in several subdirectories
in a mapset
, organized as follows:
cellhd/
: map header including projection code, coordinates representing the spatial extent of the raster map, number of rows and columns, resolution, and information about map compression;cell/
,fcell/
orgrid3/
: generic matrix of values in a compressed, portable format which depends on the raster data type (integer, floating point or 3D grid);hist/
: history file which contains metadata such as the data source, the command that was used to generate the raster map, or other information provided by the user;cats/
: optional category file which contains text or numeric labels assigned to the raster map categories;colr/
: optional color table;cell_misc/
: optional timestamp, range of values, quantization rules (for floating point maps) and null (no-data) files;WIND
: contains the active processing region and the resolutionPROJ_INFO
: contains the information about the projectionPROJ_UNITS
: contains the information about projection units usedvector/
: contain the vector data since GRASS 6
Usage¶
/*
* read a grassraster given the bounds and the number of rows and cols.
*/
// prepare the parameters
GeneralParameterValue[] readParams = new GeneralParameterValue[1];
Parameter<GridGeometry2D> readGG = new Parameter<>(AbstractGridFormat.READ_GRIDGEOMETRY2D);
GridEnvelope2D gridEnvelope = new GridEnvelope2D(0, 0, cols, rows);
ReferencedEnvelope env = new ReferencedEnvelope(w, e, s, n, crs);
readGG.setValue(new GridGeometry2D(gridEnvelope, env));
readParams[0] = readGG;
// do the reading
AbstractGridFormat format = new GrassCoverageFormatFactory().createFormat();
GridCoverageReader reader = format.getReader(file);
GridCoverage2D gc = ((GridCoverage2D) reader.read(readParams));