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