|
| 1 | +/**************************************************************************** |
| 2 | + * |
| 3 | + * MODULE: r.mask.status |
| 4 | + * AUTHORS: Vaclav Petras |
| 5 | + * PURPOSE: Report status of raster mask |
| 6 | + * COPYRIGHT: (C) 2022 by Vaclav Petras and the GRASS Development Team |
| 7 | + * |
| 8 | + * This program is free software under the GNU General Public |
| 9 | + * License (>=v2). Read the file COPYING that comes with GRASS |
| 10 | + * for details. |
| 11 | + * |
| 12 | + *****************************************************************************/ |
| 13 | + |
| 14 | +#include <stdio.h> |
| 15 | +#include <stdlib.h> |
| 16 | +#include <string.h> |
| 17 | +#include <unistd.h> |
| 18 | +#include <grass/gis.h> |
| 19 | +#include <grass/raster.h> |
| 20 | +#include <grass/glocale.h> |
| 21 | + |
| 22 | +struct Parameters |
| 23 | +{ |
| 24 | + struct Option *format; |
| 25 | + struct Flag *like_test; |
| 26 | +}; |
| 27 | + |
| 28 | +void parse_parameters(struct Parameters *params, int argc, char **argv) |
| 29 | +{ |
| 30 | + struct GModule *module; |
| 31 | + |
| 32 | + module = G_define_module(); |
| 33 | + G_add_keyword(_("raster")); |
| 34 | + G_add_keyword(_("reclassification")); |
| 35 | + module->label = _("Reclassify raster map based on category values."); |
| 36 | + module->description = |
| 37 | + _("Creates a new raster map whose category values are based " |
| 38 | + "upon a reclassification of the categories in an existing " |
| 39 | + "raster map."); |
| 40 | + |
| 41 | + params->format = G_define_option(); |
| 42 | + params->format->key = "format"; |
| 43 | + params->format->type = TYPE_STRING; |
| 44 | + params->format->required = NO; |
| 45 | + params->format->answer = "yaml"; |
| 46 | + params->format->options = "yaml,json,bash"; |
| 47 | + params->format->description = _("Format for reporting"); |
| 48 | + |
| 49 | + params->like_test = G_define_flag(); |
| 50 | + params->like_test->key = 't'; |
| 51 | + params->like_test->lable = |
| 52 | + _("Return code 0 when mask present, 1 otherwise"); |
| 53 | + params->like_test->description = |
| 54 | + _("Behave like the test utility, 0 for true, 1 for false, no output"); |
| 55 | + //flags.like_test->guisection = _(""); |
| 56 | + // suppress_required |
| 57 | + |
| 58 | + if (G_parser(argc, argv)) |
| 59 | + exit(EXIT_FAILURE); |
| 60 | +} |
| 61 | + |
| 62 | +char *min_json_escape(const char *str) |
| 63 | +{ |
| 64 | + char *tmp1 = G_str_replace(str, "\\", "\\\\"); |
| 65 | + char *tmp2 = G_str_replace(tmp1, "\"", "\\\""); |
| 66 | + |
| 67 | + G_free(tmp1); |
| 68 | + return tmp2; |
| 69 | +} |
| 70 | + |
| 71 | +void json_print_name_mapset(const char *name, const char *mapset) |
| 72 | +{ |
| 73 | + // Being paranoid about what is in the name. |
| 74 | + char *escaped_name = min_json_escape(name); |
| 75 | + char *escaped_mapset = min_json_escape(mapset); |
| 76 | + |
| 77 | + printf("\"%s@%s\"", name, mapset); |
| 78 | + G_free(escaped_name); |
| 79 | + G_free(escaped_mapset); |
| 80 | +} |
| 81 | + |
| 82 | +int report_status(struct Parameters *params) |
| 83 | +{ |
| 84 | + |
| 85 | + char name[GNAME_MAX]; |
| 86 | + char mapset[GMAPSET_MAX]; |
| 87 | + |
| 88 | + bool is_mask_reclass; |
| 89 | + bool present = |
| 90 | + Rast_mask_status(name, mapset, reclass_name, reclass_mapset, |
| 91 | + &is_mask_reclass); |
| 92 | + bool present = Rast_mask_present(name, mapset); |
| 93 | + |
| 94 | + //printf("%s", Rast_mask_info()); |
| 95 | + |
| 96 | + if (params->like_test->answer) { |
| 97 | + if (present) |
| 98 | + return 0; |
| 99 | + else |
| 100 | + return 1; |
| 101 | + } |
| 102 | + else if (strcmp(params->format->answer, "json") == 0) { |
| 103 | + printf("{\"present\":"); |
| 104 | + if (present) |
| 105 | + printf("true"); |
| 106 | + else |
| 107 | + printf("false"); |
| 108 | + printf(",\n\"full_name\":"); |
| 109 | + if (present) |
| 110 | + json_print_name_mapset("MASK", G_mapset()); // Too much mask details here, move this to the library. |
| 111 | + else |
| 112 | + printf("null"); |
| 113 | + printf(",\n\"is_reclass_of\": "); |
| 114 | + if (is_mask_reclass) |
| 115 | + json_print_name_mapset(name, mapset); |
| 116 | + else |
| 117 | + printf("null"); |
| 118 | + printf("}\n"); |
| 119 | + } |
| 120 | + else if (strcmp(params->format->answer, "bash") == 0) { |
| 121 | + printf("present="); |
| 122 | + if (present) |
| 123 | + printf("1"); // Good choice here or not? |
| 124 | + else |
| 125 | + printf("0"); |
| 126 | + printf("\nfull_name="); |
| 127 | + if (present) { |
| 128 | + json_print_name_mapset(name, mapset); |
| 129 | + } |
| 130 | + printf("\n"); |
| 131 | + } |
| 132 | + else { |
| 133 | + printf("present: "); |
| 134 | + if (present) |
| 135 | + printf("true"); |
| 136 | + else |
| 137 | + printf("false"); |
| 138 | + printf("\nfull_name: "); |
| 139 | + if (present) |
| 140 | + printf("|-\n MASK@%s", G_mapset()); // MASK or MASK@current_mapset |
| 141 | + else |
| 142 | + printf("null"); |
| 143 | + printf("\nis_reclass_of: "); |
| 144 | + // Using block scalar with |- to avoid need for escaping. |
| 145 | + if (is_mask_reclass) |
| 146 | + printf("|-\n %s@%s", name, mapset); |
| 147 | + else |
| 148 | + printf("null"); |
| 149 | + // true if MASK in current mapset and is reclass, false otherwise, |
| 150 | + // then also outputting mask cats is needed to inform user about the portion of the map |
| 151 | + // printf("\nmask_reclass: "); |
| 152 | + // if (is_mask_reclass) |
| 153 | + // printf("true"); |
| 154 | + // else |
| 155 | + // printf("false"); |
| 156 | + printf("\n"); |
| 157 | + } |
| 158 | + return EXIT_SUCCESS; |
| 159 | +} |
| 160 | + |
| 161 | +int main(int argc, char **argv) |
| 162 | +{ |
| 163 | + struct Parameters params; |
| 164 | + |
| 165 | + G_gisinit(argv[0]); |
| 166 | + parse_parameters(¶ms, argc, argv); |
| 167 | + return report_status(¶ms); |
| 168 | +} |
0 commit comments