Skip to content

Commit

Permalink
[news] r.out.gdal: add offset/scale options (#1992)
Browse files Browse the repository at this point in the history
* Apply suggestions from code review

Co-authored-by: Markus Metz <33666869+metzm@users.noreply.github.com>
  • Loading branch information
landam and metzm committed Nov 30, 2021
1 parent 230e5f1 commit eca1d2f
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 4 deletions.
13 changes: 12 additions & 1 deletion raster/r.out.gdal/export_band.c
Expand Up @@ -215,7 +215,8 @@ int export_band(GDALDatasetH hMEMDS, int band,
const char *name, const char *mapset,
struct Cell_head *cellhead, RASTER_MAP_TYPE maptype,
double nodataval, int suppress_main_colortable,
int no_metadata, int writenodata)
int no_metadata, int writenodata,
double offsetval, double scaleval)
{
struct Colors sGrassColors;
GDALColorTableH hCT;
Expand Down Expand Up @@ -355,6 +356,16 @@ int export_band(GDALDatasetH hMEMDS, int band,
GDALSetMetadataItem(hBand, key, value, NULL);
}
}

/* apply offset factor if not 0 (zero) */
if (offsetval) {
GDALSetRasterOffset(hBand, offsetval);
}

/* apply scale factor if not 1 */
if (scaleval != 1) {
GDALSetRasterScale(hBand, scaleval);
}
}

/* Create GRASS raster buffer */
Expand Down
2 changes: 1 addition & 1 deletion raster/r.out.gdal/local_proto.h
Expand Up @@ -60,7 +60,7 @@
/* export_band.c */
int export_band(GDALDatasetH, int, const char *,
const char *, struct Cell_head *, RASTER_MAP_TYPE,
double, int, int, int);
double, int, int, int, double, double);
int exact_checks(GDALDataType, const char *, const char *,
struct Cell_head *, RASTER_MAP_TYPE, double,
const char *, int);
Expand Down
35 changes: 33 additions & 2 deletions raster/r.out.gdal/main.c
Expand Up @@ -118,7 +118,7 @@ int main(int argc, char *argv[])
struct GModule *module;
struct Flag *flag_l, *flag_c, *flag_m, *flag_f, *flag_t;
struct Option *input, *format, *type, *output, *createopt, *metaopt,
*nodataopt, *overviewopt;
*nodataopt, *overviewopt, *offsetopt, *scaleopt;

struct Cell_head cellhead;
struct Ref ref;
Expand Down Expand Up @@ -251,6 +251,24 @@ int main(int argc, char *argv[])
overviewopt->required = NO;
overviewopt->guisection = _("Creation");

offsetopt = G_define_option();
offsetopt->key = "offset";
offsetopt->type = TYPE_DOUBLE;
offsetopt->description =
_("Assign a specified offset value to output bands");
offsetopt->multiple = NO;
offsetopt->required = NO;
offsetopt->guisection = _("Creation");

scaleopt = G_define_option();
scaleopt->key = "scale";
scaleopt->type = TYPE_DOUBLE;
scaleopt->description =
_("Assign a specified scale value to output bands");
scaleopt->multiple = NO;
scaleopt->required = NO;
scaleopt->guisection = _("Creation");

if (G_parser(argc, argv))
exit(EXIT_FAILURE);

Expand Down Expand Up @@ -586,6 +604,18 @@ int main(int argc, char *argv[])
set_default_nodata_value(datatype, export_min, export_max);
}

/* Offset value */
double offsetval = 0; /* not defined */
if (offsetopt->answer != NULL) {
offsetval = atof(offsetopt->answer);
}

/* Scale value */
double scaleval = 1; /* not defined */
if (scaleopt->answer != NULL) {
scaleval = atof(scaleopt->answer);
}

/* exact range and nodata checks for each band */
G_message(_("Checking GDAL data type and nodata value..."));
for (band = 0; band < ref.nfiles; band++) {
Expand Down Expand Up @@ -707,7 +737,8 @@ int main(int argc, char *argv[])
retval = export_band
(hCurrDS, band + 1, ref.file[band].name,
ref.file[band].mapset, &cellhead, maptype, nodataval,
flag_c->answer, flag_m->answer, (nodataopt->answer != NULL));
flag_c->answer, flag_m->answer, (nodataopt->answer != NULL),
offsetval, scaleval);

/* read/write error */
if (retval == -1) {
Expand Down
32 changes: 32 additions & 0 deletions raster/r.out.gdal/r.out.gdal.html
Expand Up @@ -180,6 +180,10 @@ <h3>Improving GeoTIFF compatibility</h3>
<em>gdaladdo</em> to build overviews.
-->

<h3>Offset/scale parameters</h3>

Offset is only relevant if not zero. Scale is only relevant if not 1.

<h2>EXAMPLES</h2>

<h3>Export the integer raster basin_50K map to GeoTIFF format</h3>
Expand Down Expand Up @@ -323,6 +327,34 @@ <h3>Export the floating point raster elevation map to ERDAS/IMG format</h3>
r.out.gdal input=elevation output=elelevation.img format=HFA type=Float32
</pre></div>

<h3>Export raster map with offset/scale assigned</h3>

An offset value can be assigned to output raster data by <b>offset</b>
parameter. Similarly a scale value can be assigned by <b>scale</b>
parameter.

<div class="code"><pre>
# produce raster map
g.region n=100 s=0 e=100 w=0 res=1
r.random.cells output=random distance=1.0
r.info -r random

# export raster data with offset/scale assigned
r.out.gdal input=random output=random.tif offset=100 scale=0.01
</pre></div>

Check result by <em>gdalinfo</em> command line tool:

<div class="code"><pre>
gdalinfo random.tif
...
Band 1 Block=100x40 Type=UInt16, ColorInterp=Palette
Description = random
Computed Min/Max=1.000,3661.000
NoData Value=65535
Offset: 100, Scale:0.01
...
</pre></div>

<h2>GDAL RELATED ERROR MESSAGES</h2>

Expand Down

0 comments on commit eca1d2f

Please sign in to comment.