Skip to content

Commit

Permalink
r.external speed-up (#1305)
Browse files Browse the repository at this point in the history
 * faster linking with r.external
 * super-fast mode for r.external
 * warning for fast mode
 * update r.info if range is missing
 * fix r.support to re-compute range for fp maps

Co-authored-by: Markus Neteler <neteler@gmail.com>
  • Loading branch information
metzm and neteler committed Feb 18, 2021
1 parent efeffd0 commit cf17201
Show file tree
Hide file tree
Showing 7 changed files with 231 additions and 141 deletions.
12 changes: 12 additions & 0 deletions lib/raster/range.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ int Rast_read_fp_range(const char *name, const char *mapset,
Rast_update_fp_range(dcell2, drange);
close(fd);
}
else {
/* "f_range" file does not exist */
G_warning(_("Missing fp range file for <%s> (run r.support -s)"),
G_fully_qualified_name(name, mapset));
return -1;
}

return 1;
}
Expand Down Expand Up @@ -237,6 +243,12 @@ int Rast_read_range(const char *name, const char *mapset, struct Range *range)
}
fclose(fd);
}
else {
/* "range" file does not exist */
G_warning(_("Missing range file for <%s> (run r.support -s)"),
G_fully_qualified_name(name, mapset));
return -1;
}

return 1;
}
Expand Down
47 changes: 17 additions & 30 deletions raster/r.external/link.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ void query_band(GDALRasterBandH hBand, const char *output,
break;
}

if (info->have_minmax)
GDALComputeRasterMinMax(hBand, 0, info->minmax);

Rast_init_colors(&info->colors);

if (GDALGetRasterColorTable(hBand) != NULL) {
Expand Down Expand Up @@ -184,7 +187,6 @@ void create_map(const char *input, int band, const char *output,
struct History history;
struct Categories cats;
char buf[1024];
int outfd;

Rast_put_cellhd(output, cellhd);

Expand All @@ -211,39 +213,24 @@ void create_map(const char *input, int band, const char *output,
if (title)
Rast_put_cell_title(output, title);

/* get stats for this raster band */
G_remove_misc("cell_misc", "stats", output);

outfd = Rast_open_old(output, G_mapset());
if (info->data_type == CELL_TYPE) {
int r;
struct Range range;
CELL *rbuf = Rast_allocate_buf(CELL_TYPE);
if (info->have_minmax) {
if (info->data_type == CELL_TYPE) {
struct Range range;

G_remove_misc("cell_misc", "range", output);
Rast_init_range(&range);
for (r = 0; r < cellhd->rows; r++) {
Rast_get_row(outfd, rbuf, r, CELL_TYPE);
Rast_row_update_range(rbuf, cellhd->cols, &range);
Rast_init_range(&range);
Rast_update_range((CELL)info->minmax[0], &range);
Rast_update_range((CELL)info->minmax[1], &range);
Rast_write_range(output, &range);
}
Rast_write_range(output, &range);
G_free(rbuf);
}
else {
int r;
struct FPRange fprange;
void *rbuf = Rast_allocate_buf(info->data_type);

G_remove_misc("cell_misc", "f_range", output);
Rast_init_fp_range(&fprange);
for (r = 0; r < cellhd->rows; r++) {
Rast_get_row(outfd, rbuf, r, info->data_type);
Rast_row_update_fp_range(rbuf, cellhd->cols, &fprange, info->data_type);
else {
struct FPRange fprange;

Rast_init_fp_range(&fprange);
Rast_update_fp_range(info->minmax[0], &fprange);
Rast_update_fp_range(info->minmax[1], &fprange);
Rast_write_fp_range(output, &fprange);
}
Rast_write_fp_range(output, &fprange);
G_free(rbuf);
}
Rast_unopen(outfd);

G_message(_("Link to raster map <%s> created."), output);
}
8 changes: 7 additions & 1 deletion raster/r.external/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ int main(int argc, char *argv[])
struct Option *input, *source, *output, *band, *title;
} parm;
struct {
struct Flag *o, *j, *f, *e, *h, *v, *t, *a;
struct Flag *o, *j, *f, *e, *h, *v, *t, *a, *r;
} flag;
int min_band, max_band, band;
struct band_info info;
Expand Down Expand Up @@ -135,6 +135,11 @@ int main(int argc, char *argv[])
flag.t->guisection = _("Print");
flag.t->suppress_required = YES;

flag.r = G_define_flag();
flag.r->key = 'r';
flag.r->label = _("Create fast link without data range");
flag.r->description = _("WARNING: some modules do not work correctly without known data range");

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

Expand Down Expand Up @@ -219,6 +224,7 @@ int main(int argc, char *argv[])
I_init_group_ref(&reference);
}

info.have_minmax = !flag.r->answer;
for (band = min_band; band <= max_band; band++) {
char *output2, *title2 = NULL;

Expand Down
2 changes: 2 additions & 0 deletions raster/r.external/proto.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ struct band_info
GDALDataType gdal_type;
int has_null;
double null_val;
DCELL minmax[2];
int have_minmax;
struct Colors colors;
};

Expand Down

0 comments on commit cf17201

Please sign in to comment.