Skip to content

Commit

Permalink
r.series and r.patch: Fix indentation (OSGeo#1772)
Browse files Browse the repository at this point in the history
Reformat r.series and r.patch with current formatting rules.
  • Loading branch information
aaronsms authored and ninsbl committed Feb 17, 2023
1 parent 56b2861 commit 2b46852
Show file tree
Hide file tree
Showing 5 changed files with 391 additions and 387 deletions.
64 changes: 31 additions & 33 deletions raster/r.patch/do_patch.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,47 +15,45 @@ int is_zero_value(void *rast, RASTER_MAP_TYPE data_type)
/* insert 0 check here */

return Rast_is_null_value(rast, data_type) ||
Rast_get_d_value(rast, data_type) != 0.0 ? 0 : 1;
Rast_get_d_value(rast, data_type) != 0.0
? 0
: 1;
}


int do_patch(void *result, void *patch,
struct Cell_stats *statf, int ncols,
int do_patch(void *result, void *patch, struct Cell_stats *statf, int ncols,
RASTER_MAP_TYPE out_type, size_t out_cell_size, int use_zero)
{
int more;

more = 0;
while (ncols-- > 0) {
if (use_zero) { /* use 0 for transparency instead of NULL */
if (is_zero_value(result, out_type) ||
Rast_is_null_value(result, out_type)) {
/* Don't patch hole with a null, just mark as more */
if (Rast_is_null_value(patch, out_type))
more = 1;
else {
/* Mark that there is more to be done if we patch with 0 */
if (is_zero_value(patch, out_type))
more = 1;
Rast_raster_cpy(result, patch, 1, out_type);
if (out_type == CELL_TYPE)
Rast_update_cell_stats((CELL *) result, 1, statf);
}
} /* ZERO support */
}

else { /* use NULL for transparency instead of 0 */

if (Rast_is_null_value(result, out_type)) {
if (Rast_is_null_value(patch, out_type))
more = 1;
else {
Rast_raster_cpy(result, patch, 1, out_type);
if (out_type == CELL_TYPE)
Rast_update_cell_stats((CELL *) result, 1, statf);
}
} /* NULL support */
}
if (use_zero) { /* use 0 for transparency instead of NULL */
if (is_zero_value(result, out_type) ||
Rast_is_null_value(result, out_type)) {
/* Don't patch hole with a null, just mark as more */
if (Rast_is_null_value(patch, out_type))
more = 1;
else {
/* Mark that there is more to be done if we patch with 0 */
if (is_zero_value(patch, out_type))
more = 1;
Rast_raster_cpy(result, patch, 1, out_type);
if (out_type == CELL_TYPE)
Rast_update_cell_stats((CELL *) result, 1, statf);
}
} /* ZERO support */
} else { /* use NULL for transparency instead of 0 */

if (Rast_is_null_value(result, out_type)) {
if (Rast_is_null_value(patch, out_type))
more = 1;
else {
Rast_raster_cpy(result, patch, 1, out_type);
if (out_type == CELL_TYPE)
Rast_update_cell_stats((CELL *) result, 1, statf);
}
} /* NULL support */
}
result = G_incr_void_ptr(result, out_cell_size);
patch = G_incr_void_ptr(patch, out_cell_size);
}
Expand Down
4 changes: 2 additions & 2 deletions raster/r.patch/local_proto.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
int do_patch(void *result, void *, struct Cell_stats *, int, RASTER_MAP_TYPE,
size_t, int);
/* support.c */
int support(char **, struct Cell_stats *, int, struct Categories *,
int *, struct Colors *, int *, RASTER_MAP_TYPE);
int support(char **, struct Cell_stats *, int, struct Categories *, int *,
struct Colors *, int *, RASTER_MAP_TYPE);
113 changes: 57 additions & 56 deletions raster/r.patch/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* Jachym Cepicky <jachym les-ejk.cz>,
* Jan-Oliver Wagner <jan intevation.de>,
* Huidae Cho <grass4u gmail.com>
* PURPOSE:
* PURPOSE:
* COPYRIGHT: (C) 1999-2014 by the GRASS Development Team
*
* This program is free software under the GNU General Public
Expand All @@ -19,9 +19,11 @@
*****************************************************************************/
#include <stdlib.h>
#include <unistd.h>

#include <grass/gis.h>
#include <grass/raster.h>
#include <grass/glocale.h>
#include <grass/raster.h>

#include "local_proto.h"

int main(int argc, char *argv[])
Expand Down Expand Up @@ -63,9 +65,9 @@ int main(int argc, char *argv[])
G_add_keyword(_("aggregation"));
G_add_keyword(_("series"));
module->description =
_("Creates a composite raster map layer by using "
"known category values from one (or more) map layer(s) "
"to fill in areas of \"no data\" in another map layer.");
_("Creates a composite raster map layer by using "
"known category values from one (or more) map layer(s) "
"to fill in areas of \"no data\" in another map layer.");

/* Define the different options */

Expand All @@ -79,16 +81,14 @@ int main(int argc, char *argv[])

zeroflag = G_define_flag();
zeroflag->key = 'z';
zeroflag->description =
_("Use zero (0) for transparency instead of NULL");
zeroflag->description = _("Use zero (0) for transparency instead of NULL");

nosupportflag = G_define_flag();
nosupportflag->key = 's';
nosupportflag->description =
_("Do not create color and category files");
nosupportflag->description = _("Do not create color and category files");

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

use_zero = (zeroflag->answer);
no_support = (nosupportflag->answer);
Expand All @@ -100,29 +100,29 @@ int main(int argc, char *argv[])
for (ptr = names, nfiles = 0; *ptr != NULL; ptr++, nfiles++) ;

if (nfiles < 2)
G_fatal_error(_("The minimum number of input raster maps is two"));
G_fatal_error(_("The minimum number of input raster maps is two"));

infd = G_malloc(nfiles * sizeof(int));
statf = G_malloc(nfiles * sizeof(struct Cell_stats));
cellhd = G_malloc(nfiles * sizeof(struct Cell_head));

for (i = 0; i < nfiles; i++) {
const char *name = names[i];
int fd;
const char *name = names[i];
int fd;

fd = Rast_open_old(name, "");
fd = Rast_open_old(name, "");

infd[i] = fd;
infd[i] = fd;

map_type = Rast_get_map_type(fd);
if (map_type == FCELL_TYPE && out_type == CELL_TYPE)
out_type = FCELL_TYPE;
else if (map_type == DCELL_TYPE)
out_type = DCELL_TYPE;
map_type = Rast_get_map_type(fd);
if (map_type == FCELL_TYPE && out_type == CELL_TYPE)
out_type = FCELL_TYPE;
else if (map_type == DCELL_TYPE)
out_type = DCELL_TYPE;

Rast_init_cell_stats(&statf[i]);
Rast_init_cell_stats(&statf[i]);

Rast_get_cellhd(name, "", &cellhd[i]);
Rast_get_cellhd(name, "", &cellhd[i]);
}

out_cell_size = Rast_cell_size(out_type);
Expand All @@ -139,55 +139,56 @@ int main(int argc, char *argv[])

G_verbose_message(_("Percent complete..."));
for (row = 0; row < nrows; row++) {
double north_edge, south_edge;

G_percent(row, nrows, 2);
Rast_get_row(infd[0], presult, row, out_type);

north_edge = Rast_row_to_northing(row, &window);
south_edge = north_edge - window.ns_res;

if (out_type == CELL_TYPE)
Rast_update_cell_stats((CELL *) presult, ncols, &statf[0]);
for (i = 1; i < nfiles; i++) {
/* check if raster i overlaps with the current row */
if (south_edge >= cellhd[i].north ||
north_edge <= cellhd[i].south ||
window.west >= cellhd[i].east ||
window.east <= cellhd[i].west)
continue;

Rast_get_row(infd[i], patch, row, out_type);
if (!do_patch
(presult, patch, &statf[i], ncols, out_type, out_cell_size,
use_zero))
break;
}
Rast_put_row(outfd, presult, out_type);
double north_edge, south_edge;

G_percent(row, nrows, 2);
Rast_get_row(infd[0], presult, row, out_type);

north_edge = Rast_row_to_northing(row, &window);
south_edge = north_edge - window.ns_res;

if (out_type == CELL_TYPE)
Rast_update_cell_stats((CELL *) presult, ncols, &statf[0]);
for (i = 1; i < nfiles; i++) {
/* check if raster i overlaps with the current row */
if (south_edge >= cellhd[i].north ||
north_edge <= cellhd[i].south ||
window.west >= cellhd[i].east ||
window.east <= cellhd[i].west)
continue;

Rast_get_row(infd[i], patch, row, out_type);
if (!do_patch(presult, patch, &statf[i], ncols, out_type,
out_cell_size, use_zero))
break;
}
Rast_put_row(outfd, presult, out_type);
}
G_percent(row, nrows, 2);

G_free(patch);
G_free(presult);
for (i = 0; i < nfiles; i++)
Rast_close(infd[i]);
Rast_close(infd[i]);

if(!no_support) {
/*
if (!no_support) {
/*
* build the new cats and colors. do this before closing the new
* file, in case the new file is one of the patching files as well.
*/
G_verbose_message(_("Creating support files for raster map <%s>..."), new_name);
support(names, statf, nfiles, &cats, &cats_ok, &colr, &colr_ok, out_type);
G_verbose_message(_("Creating support files for raster map <%s>..."),
new_name);
support(names, statf, nfiles, &cats, &cats_ok, &colr, &colr_ok,
out_type);
}

/* now close (and create) the result */
Rast_close(outfd);
if(!no_support) {
if (!no_support) {
if (cats_ok)
Rast_write_cats(new_name, &cats);
if (colr_ok)
Rast_write_colors(new_name, G_mapset(), &colr);
Rast_write_cats(new_name, &cats);
if (colr_ok)
Rast_write_colors(new_name, G_mapset(), &colr);
}

Rast_short_history(new_name, "raster", &history);
Expand Down
74 changes: 36 additions & 38 deletions raster/r.patch/support.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@
* contributed new categories to the final result
*/

int support(char **names,
struct Cell_stats *statf, int nfiles,
struct Categories *cats,
int *cats_ok,
struct Colors *colr, int *colr_ok, RASTER_MAP_TYPE out_type)
int support(char **names, struct Cell_stats *statf, int nfiles,
struct Categories *cats, int *cats_ok, struct Colors *colr,
int *colr_ok, RASTER_MAP_TYPE out_type)
{
int i;
struct Categories pcats;
Expand All @@ -28,47 +26,47 @@ int support(char **names,
*cats_ok = 1;
*colr_ok = 1;
if (Rast_read_cats(names[0], "", cats) < 0)
*cats_ok = 0;
*cats_ok = 0;
G_suppress_warnings(1);
if (Rast_read_colors(names[0], "", colr) < 0)
*colr_ok = 0;
*colr_ok = 0;
G_suppress_warnings(0);

if (*cats_ok == 0 && *colr_ok == 0)
return 0;
return 0;

for (i = 1; i < nfiles; i++) {
do_cats = *cats_ok && (Rast_read_cats(names[i], "", &pcats) >= 0);
G_suppress_warnings(1);
do_colr = *colr_ok && (Rast_read_colors(names[i], "", &pcolr) >= 0);
G_suppress_warnings(0);
if (!do_cats && !do_colr)
continue;
if (out_type == CELL_TYPE) {
Rast_rewind_cell_stats(statf + i);
while (Rast_next_cell_stat(&n, &count, statf + i))
if (n && !Rast_find_cell_stat(n, &count, statf)) {
if (do_cats) {
Rast_update_cell_stats(&n, 1, statf);
Rast_set_c_cat(&n, &n, Rast_get_c_cat((CELL *) &n, &pcats), cats);
}
if (do_colr) {
Rast_get_c_color(&n, &red, &grn, &blu, &pcolr);
Rast_set_c_color(n, red, grn, blu, colr);
}
}
}
else {
/* the color would be the color of the first map,
* possibly not covering the range of the other maps */
*colr_ok = 0;
}
do_cats = *cats_ok && (Rast_read_cats(names[i], "", &pcats) >= 0);
G_suppress_warnings(1);
do_colr = *colr_ok && (Rast_read_colors(names[i], "", &pcolr) >= 0);
G_suppress_warnings(0);
if (!do_cats && !do_colr)
continue;
if (out_type == CELL_TYPE) {
Rast_rewind_cell_stats(statf + i);
while (Rast_next_cell_stat(&n, &count, statf + i))
if (n && !Rast_find_cell_stat(n, &count, statf)) {
if (do_cats) {
Rast_update_cell_stats(&n, 1, statf);
Rast_set_c_cat(
&n, &n, Rast_get_c_cat((CELL *) &n, &pcats), cats);
}
if (do_colr) {
Rast_get_c_color(&n, &red, &grn, &blu, &pcolr);
Rast_set_c_color(n, red, grn, blu, colr);
}
}
} else {
/* the color would be the color of the first map,
* possibly not covering the range of the other maps */
*colr_ok = 0;
}

if (do_cats)
Rast_free_cats(&pcats);
if (do_colr)
/* otherwise this memory is used in colr pointer */
Rast_free_colors(&pcolr);
if (do_cats)
Rast_free_cats(&pcats);
if (do_colr)
/* otherwise this memory is used in colr pointer */
Rast_free_colors(&pcolr);
}
return 1;
}
Loading

0 comments on commit 2b46852

Please sign in to comment.