GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-847944e18e
maskfn.c
Go to the documentation of this file.
1 /***************************************************************************
2  * MODULE: this structs/functions are used by r3.mask and r3.null
3  *
4  * AUTHOR(S): Roman Waupotitsch, Michael Shapiro, Helena Mitasova,
5  * Bill Brown, Lubos Mitas, Jaro Hofierka
6  *
7  * COPYRIGHT: (C) 2005 by the GRASS Development Team
8  *
9  * This program is free software under the GNU General Public
10  * License (>=v2). Read the file COPYING that comes with GRASS
11  * for details.
12  *
13  *****************************************************************************/
14 /*Helperfunctions */
15 
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <grass/gis.h>
20 #include <grass/raster3d.h>
21 #include <grass/glocale.h>
22 
23 /*local prototypes */
24 static void add_d_mask_rule(d_Mask *d_mask, double a, double b, int inf);
25 static void parse_d_mask_rule(char *vallist, d_Mask *d_mask, char *where);
26 static void init_d_mask_rules(d_Mask *d_mask);
27 
28 void init_d_mask_rules(d_Mask *d_mask)
29 {
30  d_mask->list = NULL;
31 }
32 
33 void add_d_mask_rule(d_Mask *d_mask, double a, double b, int inf)
34 {
35  d_Interval *I;
36 
37  I = (d_Interval *)G_malloc(sizeof(d_Interval));
38  I->low = a <= b ? a : b;
39  I->high = a >= b ? a : b;
40  I->inf = inf;
41  I->next = d_mask->list;
42  d_mask->list = I;
43 }
44 
46 {
47  d_Interval *I;
48 
49  if (mask->list == NULL)
50  return 0;
51  for (I = mask->list; I; I = I->next) {
53  return 1;
54  }
55  return 0;
56 }
57 
59 {
60  if (I->inf < 0)
61  return x <= I->low;
62 
63  if (I->inf > 0)
64  return x >= I->high;
65 
66  return x >= I->low && x <= I->high;
67 }
68 
69 void parse_d_mask_rule(char *vallist, d_Mask *d_mask, char *where)
70 {
71  double a, b;
72  char junk[128];
73 
74  /* #-# */
75  if (sscanf(vallist, "%lf-%lf", &a, &b) == 2) {
76  G_message(_("Adding rule: %lf - %lf"), a, b);
77  add_d_mask_rule(d_mask, a, b, 0);
78  }
79  /* inf-# */
80  else if (sscanf(vallist, "%[^ -\t]-%lf", junk, &a) == 2)
81  add_d_mask_rule(d_mask, a, a, -1);
82 
83  /* #-inf */
84  else if (sscanf(vallist, "%lf-%[^ \t]", &a, junk) == 2)
85  add_d_mask_rule(d_mask, a, a, 1);
86 
87  /* # */
88  else if (sscanf(vallist, "%lf", &a) == 1)
89  add_d_mask_rule(d_mask, a, a, 0);
90 
91  else {
92  if (where)
93  G_message("%s: ", where);
94  G_warning(_("%s: illegal value spec"), vallist);
95  G_usage();
96  exit(EXIT_FAILURE);
97  }
98 }
99 
100 void Rast3d_parse_vallist(char **vallist, d_Mask **d_mask)
101 {
102  char buf[1024];
103  char x[2];
104  FILE *fd;
105 
106  *d_mask = (d_Mask *)G_malloc(sizeof(d_Mask));
107 
108  init_d_mask_rules(*d_mask);
109  if (vallist == NULL)
110  return;
111 
112  for (; *vallist; vallist++) {
113  if (*vallist[0] == '/') {
114  fd = fopen(*vallist, "r");
115  if (fd == NULL) {
116  perror(*vallist);
117  G_usage();
118  exit(EXIT_FAILURE);
119  }
120  while (fgets(buf, sizeof buf, fd)) {
121  if (sscanf(buf, "%1s", x) != 1 || *x == '#')
122  continue;
123  parse_d_mask_rule(buf, *d_mask, *vallist);
124  }
125  fclose(fd);
126  }
127  else
128  parse_d_mask_rule(*vallist, *d_mask, (char *)NULL);
129  }
130 }
#define NULL
Definition: ccmath.h:32
void G_warning(const char *,...) __attribute__((format(printf
#define G_malloc(n)
Definition: defs/gis.h:94
void G_usage(void)
Command line help/usage message.
Definition: parser_help.c:48
void G_message(const char *,...) __attribute__((format(printf
double DCELL
Definition: gis.h:628
#define _(str)
Definition: glocale.h:10
void Rast3d_parse_vallist(char **vallist, d_Mask **d_mask)
Definition: maskfn.c:100
int Rast3d_mask_d_select(DCELL *x, d_Mask *mask)
Definition: maskfn.c:45
DCELL Rast3d_mask_match_d_interval(DCELL x, d_Interval *I)
Definition: maskfn.c:58
double b
Definition: r_raster.c:39
double low
Definition: raster3d.h:243
struct _d_interval * next
Definition: raster3d.h:245
double high
Definition: raster3d.h:243
d_Interval * list
Definition: raster3d.h:249
#define x