GRASS 8 Programmer's Manual 8.6.0dev(2026)-1878fdfec5
Loading...
Searching...
No Matches
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 * SPDX-FileCopyrightText: 2005 GRASS Development Team
8 * SPDX-License-Identifier: GPL-2.0-or-later
9 *
10 *****************************************************************************/
11/*Helperfunctions */
12
13#include <stdio.h>
14#include <stdlib.h>
15#include <string.h>
16#include <grass/gis.h>
17#include <grass/raster3d.h>
18#include <grass/glocale.h>
19
20/*local prototypes */
21static void add_d_mask_rule(d_Mask *d_mask, double a, double b, int inf);
22static void parse_d_mask_rule(char *vallist, d_Mask *d_mask, char *where);
23static void init_d_mask_rules(d_Mask *d_mask);
24
25void init_d_mask_rules(d_Mask *d_mask)
26{
27 d_mask->list = NULL;
28}
29
30void add_d_mask_rule(d_Mask *d_mask, double a, double b, int inf)
31{
33
34 I = (d_Interval *)G_malloc(sizeof(d_Interval));
35 I->low = a <= b ? a : b;
36 I->high = a >= b ? a : b;
37 I->inf = inf;
38 I->next = d_mask->list;
39 d_mask->list = I;
40}
41
43{
45
46 if (mask->list == NULL)
47 return 0;
48 for (I = mask->list; I; I = I->next) {
50 return 1;
51 }
52 return 0;
53}
54
56{
57 if (I->inf < 0)
58 return x <= I->low;
59
60 if (I->inf > 0)
61 return x >= I->high;
62
63 return x >= I->low && x <= I->high;
64}
65
66void parse_d_mask_rule(char *vallist, d_Mask *d_mask, char *where)
67{
68 double a, b;
69 char junk[128];
70
71 /* #-# */
72 if (sscanf(vallist, "%lf-%lf", &a, &b) == 2) {
73 G_message(_("Adding rule: %lf - %lf"), a, b);
74 add_d_mask_rule(d_mask, a, b, 0);
75 }
76 /* inf-# */
77 else if (sscanf(vallist, "%[^ -\t]-%lf", junk, &a) == 2)
78 add_d_mask_rule(d_mask, a, a, -1);
79
80 /* #-inf */
81 else if (sscanf(vallist, "%lf-%[^ \t]", &a, junk) == 2)
82 add_d_mask_rule(d_mask, a, a, 1);
83
84 /* # */
85 else if (sscanf(vallist, "%lf", &a) == 1)
86 add_d_mask_rule(d_mask, a, a, 0);
87
88 else {
89 if (where)
90 G_message("%s: ", where);
91 G_warning(_("%s: illegal value spec"), vallist);
92 G_usage();
94 }
95}
96
98{
99 char buf[1024];
100 char x[2];
101 FILE *fd;
102
103 *d_mask = (d_Mask *)G_malloc(sizeof(d_Mask));
104
105 init_d_mask_rules(*d_mask);
106 if (vallist == NULL)
107 return;
108
109 for (; *vallist; vallist++) {
110 if (*vallist[0] == '/') {
111 fd = fopen(*vallist, "r");
112 if (fd == NULL) {
113 perror(*vallist);
114 G_usage();
116 }
117 while (fgets(buf, sizeof buf, fd)) {
118 if (sscanf(buf, "%1s", x) != 1 || *x == '#')
119 continue;
120 parse_d_mask_rule(buf, *d_mask, *vallist);
121 }
122 fclose(fd);
123 }
124 else
125 parse_d_mask_rule(*vallist, *d_mask, (char *)NULL);
126 }
127}
#define NULL
Definition ccmath.h:32
void G_warning(const char *,...) __attribute__((format(printf
#define G_malloc(n)
Definition defs/gis.h:136
void G_usage(void)
Command line help/usage message.
Definition parser_help.c:46
void G_message(const char *,...) __attribute__((format(printf
double DCELL
Definition gis.h:632
#define _(str)
Definition glocale.h:10
void Rast3d_parse_vallist(char **vallist, d_Mask **d_mask)
Definition maskfn.c:97
int Rast3d_mask_d_select(DCELL *x, d_Mask *mask)
Definition maskfn.c:42
DCELL Rast3d_mask_match_d_interval(DCELL x, d_Interval *I)
Definition maskfn.c:55
double b
Definition r_raster.c:37
#define x