| 1 |
|
|---|
| 2 | /****************************************************************************
|
|---|
| 3 | *
|
|---|
| 4 | * MODULE: r.cats
|
|---|
| 5 | *
|
|---|
| 6 | * AUTHOR(S): Michael Shapiro - CERL
|
|---|
| 7 | * Hamish Bowman, Dunedin, New Zealand (label creation opts)
|
|---|
| 8 | *
|
|---|
| 9 | * PURPOSE: Prints category values and labels associated with
|
|---|
| 10 | * user-specified raster map layers.
|
|---|
| 11 | *
|
|---|
| 12 | * COPYRIGHT: (C) 2006 by the GRASS Development Team
|
|---|
| 13 | *
|
|---|
| 14 | * This program is free software under the GNU General Public
|
|---|
| 15 | * License (>=v2). Read the file COPYING that comes with GRASS
|
|---|
| 16 | * for details.
|
|---|
| 17 | *
|
|---|
| 18 | ***************************************************************************/
|
|---|
| 19 |
|
|---|
| 20 | #include <stdlib.h>
|
|---|
| 21 | #include <stdio.h>
|
|---|
| 22 | #include <string.h>
|
|---|
| 23 | #include <grass/gis.h>
|
|---|
| 24 | #include <grass/raster.h>
|
|---|
| 25 | #include <grass/glocale.h>
|
|---|
| 26 | #include "local_proto.h"
|
|---|
| 27 |
|
|---|
| 28 | static struct Categories cats;
|
|---|
| 29 | static char *fs;
|
|---|
| 30 |
|
|---|
| 31 | int main(int argc, char *argv[])
|
|---|
| 32 | {
|
|---|
| 33 | const char *name;
|
|---|
| 34 | const char *mapset;
|
|---|
| 35 |
|
|---|
| 36 | long x, y;
|
|---|
| 37 | double dx;
|
|---|
| 38 | RASTER_MAP_TYPE map_type;
|
|---|
| 39 | int i;
|
|---|
| 40 | int from_stdin = FALSE;
|
|---|
| 41 | struct GModule *module;
|
|---|
| 42 |
|
|---|
| 43 | struct
|
|---|
| 44 | {
|
|---|
| 45 | struct Option *map, *fs, *cats, *vals, *raster, *file, *fmt_str,
|
|---|
| 46 | *fmt_coeff;
|
|---|
| 47 | } parm;
|
|---|
| 48 |
|
|---|
| 49 | G_gisinit(argv[0]);
|
|---|
| 50 |
|
|---|
| 51 | module = G_define_module();
|
|---|
| 52 | G_add_keyword(_("raster"));
|
|---|
| 53 | G_add_keyword(_("category"));
|
|---|
| 54 | module->description =
|
|---|
| 55 | _("Manages category values and labels associated "
|
|---|
| 56 | "with user-specified raster map layers.");
|
|---|
| 57 |
|
|---|
| 58 | parm.map = G_define_standard_option(G_OPT_R_MAP);
|
|---|
| 59 |
|
|---|
| 60 | parm.cats = G_define_standard_option(G_OPT_V_CATS);
|
|---|
| 61 | parm.cats->multiple = YES;
|
|---|
| 62 | parm.cats->guisection = _("Selection");
|
|---|
| 63 |
|
|---|
| 64 | parm.vals = G_define_option();
|
|---|
| 65 | parm.vals->key = "values";
|
|---|
| 66 | parm.vals->type = TYPE_DOUBLE;
|
|---|
| 67 | parm.vals->multiple = YES;
|
|---|
| 68 | parm.vals->required = NO;
|
|---|
| 69 | parm.vals->label = _("Comma separated value list");
|
|---|
| 70 | parm.vals->description = _("Example: 1.4,3.8,13");
|
|---|
| 71 | parm.vals->guisection = _("Selection");
|
|---|
| 72 |
|
|---|
| 73 | parm.fs = G_define_standard_option(G_OPT_F_SEP);
|
|---|
| 74 | parm.fs->answer = "tab";
|
|---|
| 75 |
|
|---|
| 76 | parm.raster = G_define_standard_option(G_OPT_R_INPUT);
|
|---|
| 77 | parm.raster->key = "raster";
|
|---|
| 78 | parm.raster->required = NO;
|
|---|
| 79 | parm.raster->description =
|
|---|
| 80 | _("Raster map from which to copy category table");
|
|---|
| 81 | parm.raster->guisection = _("Define");
|
|---|
| 82 |
|
|---|
| 83 | parm.file = G_define_standard_option(G_OPT_F_INPUT);
|
|---|
| 84 | parm.file->key = "rules";
|
|---|
| 85 | parm.file->required = NO;
|
|---|
| 86 | parm.file->description =
|
|---|
| 87 | _("File containing category label rules (or \"-\" to read from stdin)");
|
|---|
| 88 | parm.file->guisection = _("Define");
|
|---|
| 89 |
|
|---|
| 90 | parm.fmt_str = G_define_option();
|
|---|
| 91 | parm.fmt_str->key = "format";
|
|---|
| 92 | parm.fmt_str->type = TYPE_STRING;
|
|---|
| 93 | parm.fmt_str->required = NO;
|
|---|
| 94 | parm.fmt_str->label =
|
|---|
| 95 | _("Default label or format string for dynamic labeling");
|
|---|
| 96 | parm.fmt_str->description =
|
|---|
| 97 | _("Used when no explicit label exists for the category");
|
|---|
| 98 |
|
|---|
| 99 | parm.fmt_coeff = G_define_option();
|
|---|
| 100 | parm.fmt_coeff->key = "coefficients";
|
|---|
| 101 | parm.fmt_coeff->type = TYPE_DOUBLE;
|
|---|
| 102 | parm.fmt_coeff->required = NO;
|
|---|
| 103 | parm.fmt_coeff->key_desc = "mult1,offset1,mult2,offset2";
|
|---|
| 104 | /* parm.fmt_coeff->answer = "0.0,0.0,0.0,0.0"; */
|
|---|
| 105 | parm.fmt_coeff->label = _("Dynamic label coefficients");
|
|---|
| 106 | parm.fmt_coeff->description =
|
|---|
| 107 | _("Two pairs of category multiplier and offsets, for $1 and $2");
|
|---|
| 108 |
|
|---|
| 109 | if (G_parser(argc, argv))
|
|---|
| 110 | exit(EXIT_FAILURE);
|
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 | name = parm.map->answer;
|
|---|
| 114 |
|
|---|
| 115 | fs = G_option_to_separator(parm.fs);
|
|---|
| 116 |
|
|---|
| 117 | mapset = G_find_raster2(name, "");
|
|---|
| 118 | if (mapset == NULL)
|
|---|
| 119 | G_fatal_error(_("Raster map <%s> not found"), name);
|
|---|
| 120 |
|
|---|
| 121 | map_type = Rast_map_type(name, mapset);
|
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 | /* create category labels */
|
|---|
| 125 | if (parm.raster->answer || parm.file->answer ||
|
|---|
| 126 | parm.fmt_str->answer || parm.fmt_coeff->answer) {
|
|---|
| 127 |
|
|---|
| 128 | /* restrict editing to current mapset */
|
|---|
| 129 | if (strcmp(mapset, G_mapset()) != 0)
|
|---|
| 130 | G_fatal_error(_("Raster map <%s> not found in current mapset"),
|
|---|
| 131 | name);
|
|---|
| 132 |
|
|---|
| 133 | /* use cats from another map */
|
|---|
| 134 | if (parm.raster->answer) {
|
|---|
| 135 | int fd;
|
|---|
| 136 | const char *cmapset;
|
|---|
| 137 |
|
|---|
| 138 | cmapset = G_find_raster2(parm.raster->answer, "");
|
|---|
| 139 | if (cmapset == NULL)
|
|---|
| 140 | G_fatal_error(_("Raster map <%s> not found"),
|
|---|
| 141 | parm.raster->answer);
|
|---|
| 142 |
|
|---|
| 143 | fd = Rast_open_old(name, mapset);
|
|---|
| 144 |
|
|---|
| 145 | Rast_init_cats("", &cats);
|
|---|
| 146 |
|
|---|
| 147 | if (0 > Rast_read_cats(parm.raster->answer, cmapset, &cats))
|
|---|
| 148 | G_fatal_error(_("Unable to read category file of raster map <%s@%s>"),
|
|---|
| 149 | parm.raster->answer, cmapset);
|
|---|
| 150 |
|
|---|
| 151 | Rast_write_cats(name, &cats);
|
|---|
| 152 | G_message(_("Category table for <%s> set from <%s>"),
|
|---|
| 153 | name, parm.raster->answer);
|
|---|
| 154 |
|
|---|
| 155 | Rast_close(fd);
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | /* load cats from rules file */
|
|---|
| 159 | if (parm.file->answer) {
|
|---|
| 160 | FILE *fp;
|
|---|
| 161 | char **tokens;
|
|---|
| 162 | int ntokens;
|
|---|
| 163 | char *e1;
|
|---|
| 164 | char *e2;
|
|---|
| 165 |
|
|---|
| 166 | if (strcmp("-", parm.file->answer) == 0) {
|
|---|
| 167 | from_stdin = TRUE;
|
|---|
| 168 | fp = stdin;
|
|---|
| 169 | }
|
|---|
| 170 | else {
|
|---|
| 171 | fp = fopen(parm.file->answer, "r");
|
|---|
| 172 | if (!fp)
|
|---|
| 173 | G_fatal_error(_("Unable to open file <%s>"),
|
|---|
| 174 | parm.file->answer);
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | Rast_init_cats("", &cats);
|
|---|
| 178 |
|
|---|
| 179 | for (;;) {
|
|---|
| 180 | char buf[1024];
|
|---|
| 181 | DCELL d1, d2;
|
|---|
| 182 | int parse_error = 0;
|
|---|
| 183 |
|
|---|
| 184 | if (!G_getl2(buf, sizeof(buf), fp))
|
|---|
| 185 | break;
|
|---|
| 186 |
|
|---|
| 187 | tokens = G_tokenize(buf, fs);
|
|---|
| 188 | ntokens = G_number_of_tokens(tokens);
|
|---|
| 189 |
|
|---|
| 190 | if (ntokens == 3) {
|
|---|
| 191 | d1 = strtod(tokens[0], &e1);
|
|---|
| 192 | d2 = strtod(tokens[1], &e2);
|
|---|
| 193 | if (*e1 == 0 && *e2 == 0)
|
|---|
| 194 | Rast_set_d_cat(&d1, &d2, tokens[2], &cats);
|
|---|
| 195 | else
|
|---|
| 196 | parse_error = 1;
|
|---|
| 197 | }
|
|---|
| 198 | else if (ntokens == 2) {
|
|---|
| 199 | d1 = strtod(tokens[0], &e1);
|
|---|
| 200 | if (*e1 == 0)
|
|---|
| 201 | Rast_set_d_cat(&d1, &d1, tokens[1], &cats);
|
|---|
| 202 | else
|
|---|
| 203 | parse_error = 1;
|
|---|
| 204 | }
|
|---|
| 205 | else if (!strlen(buf))
|
|---|
| 206 | continue;
|
|---|
| 207 | else
|
|---|
| 208 | parse_error = 1;
|
|---|
| 209 |
|
|---|
| 210 | if (parse_error)
|
|---|
| 211 | G_fatal_error(_("Incorrect format of input rules. "
|
|---|
| 212 | "Check separators. Invalid line is:\n%s"), buf);
|
|---|
| 213 | }
|
|---|
| 214 | G_free_tokens(tokens);
|
|---|
| 215 | Rast_write_cats(name, &cats);
|
|---|
| 216 |
|
|---|
| 217 | if (!from_stdin)
|
|---|
| 218 | fclose(fp);
|
|---|
| 219 | }
|
|---|
| 220 |
|
|---|
| 221 | /* set dynamic cat rules for cats without explicit labels */
|
|---|
| 222 | if (parm.fmt_str->answer || parm.fmt_coeff->answer) {
|
|---|
| 223 | char *fmt_str;
|
|---|
| 224 | double m1, a1, m2, a2;
|
|---|
| 225 |
|
|---|
| 226 | /* read existing values */
|
|---|
| 227 | Rast_init_cats("", &cats);
|
|---|
| 228 |
|
|---|
| 229 | if (0 > Rast_read_cats(name, G_mapset(), &cats))
|
|---|
| 230 | G_warning(_("Unable to read category file of raster map <%s@%s>"),
|
|---|
| 231 | name, G_mapset());
|
|---|
| 232 |
|
|---|
| 233 | if (parm.fmt_str->answer) {
|
|---|
| 234 | fmt_str =
|
|---|
| 235 | G_malloc(strlen(parm.fmt_str->answer) > strlen(cats.fmt)
|
|---|
| 236 | ? strlen(parm.fmt_str->answer) +
|
|---|
| 237 | 1 : strlen(cats.fmt) + 1);
|
|---|
| 238 | strcpy(fmt_str, parm.fmt_str->answer);
|
|---|
| 239 | }
|
|---|
| 240 | else {
|
|---|
| 241 | fmt_str = G_malloc(strlen(cats.fmt) + 1);
|
|---|
| 242 | strcpy(fmt_str, cats.fmt);
|
|---|
| 243 | }
|
|---|
| 244 |
|
|---|
| 245 | m1 = cats.m1;
|
|---|
| 246 | a1 = cats.a1;
|
|---|
| 247 | m2 = cats.m2;
|
|---|
| 248 | a2 = cats.a2;
|
|---|
| 249 |
|
|---|
| 250 | if (parm.fmt_coeff->answer) {
|
|---|
| 251 | m1 = atof(parm.fmt_coeff->answers[0]);
|
|---|
| 252 | a1 = atof(parm.fmt_coeff->answers[1]);
|
|---|
| 253 | m2 = atof(parm.fmt_coeff->answers[2]);
|
|---|
| 254 | a2 = atof(parm.fmt_coeff->answers[3]);
|
|---|
| 255 | }
|
|---|
| 256 |
|
|---|
| 257 | Rast_set_cats_fmt(fmt_str, m1, a1, m2, a2, &cats);
|
|---|
| 258 |
|
|---|
| 259 | Rast_write_cats(name, &cats);
|
|---|
| 260 | }
|
|---|
| 261 |
|
|---|
| 262 | Rast_free_cats(&cats);
|
|---|
| 263 | exit(EXIT_SUCCESS);
|
|---|
| 264 | }
|
|---|
| 265 | else {
|
|---|
| 266 | if (Rast_read_cats(name, mapset, &cats) < 0)
|
|---|
| 267 | G_fatal_error(_("Unable to read category file of raster map <%s> in <%s>"),
|
|---|
| 268 | name, mapset);
|
|---|
| 269 | }
|
|---|
| 270 |
|
|---|
| 271 | /* describe the category labels */
|
|---|
| 272 | /* if no cats requested, use r.describe to get the cats */
|
|---|
| 273 | if (parm.cats->answer == NULL) {
|
|---|
| 274 | if (map_type == CELL_TYPE) {
|
|---|
| 275 | get_cats(name, mapset);
|
|---|
| 276 | while (next_cat(&x))
|
|---|
| 277 | print_label(x);
|
|---|
| 278 | exit(EXIT_SUCCESS);
|
|---|
| 279 | }
|
|---|
| 280 | }
|
|---|
| 281 | else {
|
|---|
| 282 | if (map_type != CELL_TYPE)
|
|---|
| 283 | G_warning(_("The map is floating point! Ignoring cats list, using vals list"));
|
|---|
| 284 | else { /* integer map */
|
|---|
| 285 |
|
|---|
| 286 | for (i = 0; parm.cats->answers[i]; i++)
|
|---|
| 287 | if (!scan_cats(parm.cats->answers[i], &x, &y)) {
|
|---|
| 288 | G_usage();
|
|---|
| 289 | exit(EXIT_FAILURE);
|
|---|
| 290 | }
|
|---|
| 291 | for (i = 0; parm.cats->answers[i]; i++) {
|
|---|
| 292 | scan_cats(parm.cats->answers[i], &x, &y);
|
|---|
| 293 | while (x <= y)
|
|---|
| 294 | print_label(x++);
|
|---|
| 295 | }
|
|---|
| 296 | exit(EXIT_SUCCESS);
|
|---|
| 297 | }
|
|---|
| 298 | }
|
|---|
| 299 | if (parm.vals->answer == NULL)
|
|---|
| 300 | G_fatal_error(_("vals argument is required for floating point map!"));
|
|---|
| 301 | for (i = 0; parm.vals->answers[i]; i++)
|
|---|
| 302 | if (!scan_vals(parm.vals->answers[i], &dx)) {
|
|---|
| 303 | G_usage();
|
|---|
| 304 | exit(EXIT_FAILURE);
|
|---|
| 305 | }
|
|---|
| 306 | for (i = 0; parm.vals->answers[i]; i++) {
|
|---|
| 307 | scan_vals(parm.vals->answers[i], &dx);
|
|---|
| 308 | print_d_label(dx);
|
|---|
| 309 | }
|
|---|
| 310 | exit(EXIT_SUCCESS);
|
|---|
| 311 | }
|
|---|
| 312 |
|
|---|
| 313 |
|
|---|
| 314 |
|
|---|
| 315 | int print_label(long x)
|
|---|
| 316 | {
|
|---|
| 317 | char *label;
|
|---|
| 318 |
|
|---|
| 319 | G_squeeze(label = Rast_get_c_cat((CELL *) &x, &cats));
|
|---|
| 320 | fprintf(stdout, "%ld%s%s\n", x, fs, label);
|
|---|
| 321 |
|
|---|
| 322 | return 0;
|
|---|
| 323 | }
|
|---|
| 324 |
|
|---|
| 325 | int print_d_label(double x)
|
|---|
| 326 | {
|
|---|
| 327 | char *label, tmp[40];
|
|---|
| 328 | DCELL dtmp;
|
|---|
| 329 |
|
|---|
| 330 | dtmp = x;
|
|---|
| 331 | G_squeeze(label = Rast_get_d_cat(&dtmp, &cats));
|
|---|
| 332 | sprintf(tmp, "%.10f", x);
|
|---|
| 333 | G_trim_decimal(tmp);
|
|---|
| 334 | fprintf(stdout, "%s%s%s\n", tmp, fs, label);
|
|---|
| 335 |
|
|---|
| 336 | return 0;
|
|---|
| 337 | }
|
|---|
| 338 |
|
|---|
| 339 | int scan_cats(const char *s, long *x, long *y)
|
|---|
| 340 | {
|
|---|
| 341 | char dummy[2];
|
|---|
| 342 |
|
|---|
| 343 | *dummy = 0;
|
|---|
| 344 | if (sscanf(s, "%ld-%ld%1s", x, y, dummy) == 2)
|
|---|
| 345 | return (*dummy == 0 && *x <= *y);
|
|---|
| 346 | *dummy = 0;
|
|---|
| 347 | if (sscanf(s, "%ld%1s", x, dummy) == 1 && *dummy == 0) {
|
|---|
| 348 | *y = *x;
|
|---|
| 349 | return 1;
|
|---|
| 350 | }
|
|---|
| 351 | return 0;
|
|---|
| 352 | }
|
|---|
| 353 |
|
|---|
| 354 | int scan_vals(const char *s, double *x)
|
|---|
| 355 | {
|
|---|
| 356 | char dummy[10];
|
|---|
| 357 |
|
|---|
| 358 | *dummy = 0;
|
|---|
| 359 | if (sscanf(s, "%lf%1s", x, dummy) == 1 && *dummy == 0)
|
|---|
| 360 | return 1;
|
|---|
| 361 | return 0;
|
|---|
| 362 | }
|
|---|