2
**********************************************************************
4
* MODULE: r3.support (GRASS core)
6
* AUTHOR(S): Soeren Gebbert vTI/AK based on r.support
7
* Original r.support by Michael Shapiro - CERL
9
* PURPOSE: Build support files for raster map
10
* - Edit header, units
11
* - Update status (range)
13
* COPYRIGHT: (C) 2000-2007 by the GRASS Development Team
15
* This program is free software under the GNU General
16
* Public License (>=v2). Read the file COPYING that comes
17
* with GRASS for details.
19
**********************************************************************/
24
#include <grass/gis.h>
25
#include <grass/raster.h>
26
#include <grass/raster3d.h>
27
#include <grass/glocale.h>
28
#include "local_proto.h"
30
#define MAX_TITLE_LEN 1022
32
int main(int argc, char *argv[])
35
struct GModule *module;
36
struct Option *raster, *title_opt, *history_opt;
37
struct Option *datasrc1_opt, *datasrc2_opt, *datadesc_opt;
38
struct Option *map_opt, *units_opt, *vunits_opt;
39
struct Option *load_opt, *save_opt;
40
struct Flag *stats_flag;
42
char title[MAX_TITLE_LEN + 1];
45
/* Initialize GIS engine */
48
module = G_define_module();
49
G_add_keyword(_("raster3d"));
50
G_add_keyword(_("metadata"));
51
G_add_keyword(_("voxel"));
52
module->description = _("Allows creation and/or modification of "
53
"3D raster map layer support files.");
55
raster = G_define_standard_option(G_OPT_R3_MAP);
57
title_opt = G_define_option();
58
title_opt->key = "title";
59
title_opt->key_desc = "phrase";
60
title_opt->type = TYPE_STRING;
61
title_opt->required = NO;
62
title_opt->description = _("Text to use for map title");
64
history_opt = G_define_option();
65
history_opt->key = "history";
66
history_opt->key_desc = "phrase";
67
history_opt->type = TYPE_STRING;
68
history_opt->required = NO;
69
history_opt->description =
70
_("Text to append to the next line of the map's metadata file");
72
units_opt = G_define_option();
73
units_opt->key = "unit";
74
units_opt->type = TYPE_STRING;
75
units_opt->required = NO;
76
units_opt->description = _("The map data unit");
78
vunits_opt = G_define_option();
79
vunits_opt->key = "vunit";
80
vunits_opt->type = TYPE_STRING;
81
vunits_opt->required = NO;
82
vunits_opt->description = _("The vertical unit of the map");
84
datasrc1_opt = G_define_option();
85
datasrc1_opt->key = "source1";
86
datasrc1_opt->key_desc = "phrase";
87
datasrc1_opt->type = TYPE_STRING;
88
datasrc1_opt->required = NO;
89
datasrc1_opt->description = _("Text to use for data source, line 1");
91
datasrc2_opt = G_define_option();
92
datasrc2_opt->key = "source2";
93
datasrc2_opt->key_desc = "phrase";
94
datasrc2_opt->type = TYPE_STRING;
95
datasrc2_opt->required = NO;
96
datasrc2_opt->description = _("Text to use for data source, line 2");
98
datadesc_opt = G_define_option();
99
datadesc_opt->key = "description";
100
datadesc_opt->key_desc = "phrase";
101
datadesc_opt->type = TYPE_STRING;
102
datadesc_opt->required = NO;
103
datadesc_opt->description =
104
_("Text to use for data description or keyword(s)");
106
map_opt = G_define_option();
107
map_opt->key = "raster";
108
map_opt->type = TYPE_STRING;
109
map_opt->required = NO;
110
map_opt->gisprompt = "old,cell,raster";
111
map_opt->description = _("Raster map from which to copy category table");
113
load_opt = G_define_standard_option(G_OPT_F_INPUT);
114
load_opt->key = "loadhistory";
115
load_opt->required = NO;
116
load_opt->description = _("Text file from which to load history");
118
save_opt = G_define_standard_option(G_OPT_F_OUTPUT);
119
save_opt->key = "savehistory";
120
save_opt->required = NO;
121
save_opt->description = _("Text file in which to save history");
123
stats_flag = G_define_flag();
124
stats_flag->key = 's';
125
stats_flag->description = _("Update range");
127
/* Parse command-line options */
128
if (G_parser(argc, argv))
131
/* Make sure raster exists and set mapset */
132
infile = raster->answer;
133
mapset = G_find_raster3d(infile, G_mapset()); /* current mapset only for editing */
134
if (!mapset || strcmp(mapset, G_mapset()) != 0)
135
G_fatal_error(_("3D raster map <%s> not found"), infile);
137
if (title_opt->answer) {
138
strncpy(title, title_opt->answer, MAX_TITLE_LEN);
139
title[MAX_TITLE_LEN - 1] = '\0'; /* strncpy doesn't null terminate over-sized input */
141
G_debug(3, "map title= [%s] (%d chars)", title, (int)strlen(title));
143
Rast3d_read_history(raster->answer, "", &hist);
144
Rast_set_history(&hist, HIST_TITLE, title);
145
Rast3d_write_history(raster->answer, &hist);
148
if (save_opt->answer) {
149
FILE *fp = fopen(save_opt->answer, "w");
153
G_fatal_error(_("Unable to open output file <%s>"), save_opt->answer);
155
Rast3d_read_history(raster->answer, "", &hist);
157
for (i = 0; i < Rast_history_length(&hist); i++)
158
fprintf(fp, "%s\n", Rast_history_line(&hist, i));
163
if (load_opt->answer) {
164
FILE *fp = fopen(load_opt->answer, "r");
167
G_fatal_error(_("Unable to open input file <%s>"), load_opt->answer);
169
Rast3d_read_history(raster->answer, "", &hist);
171
Rast_clear_history(&hist);
175
if (!G_getl2(buf, sizeof(buf), fp))
177
Rast_append_history(&hist, buf);
182
Rast3d_write_history(raster->answer, &hist);
185
if (history_opt->answer) {
186
Rast3d_read_history(raster->answer, "", &hist);
188
/* two less than defined as if only one less a newline gets appended in the hist file. bug? */
189
/* Should be RECORD_LEN, but r.info truncates at > 71 chars */
190
if (strlen(history_opt->answer) > 71) {
193
for (i = 0; i < strlen(history_opt->answer); i += 71) {
196
strncpy(buf, &history_opt->answer[i], sizeof(buf)-1);
197
buf[sizeof(buf)-1] = '\0';
199
Rast_append_history(&hist, buf);
203
Rast_append_history(&hist, history_opt->answer);
205
Rast3d_write_history(raster->answer, &hist);
208
if(units_opt->answer || vunits_opt->answer) {
211
map = Rast3d_open_cell_old(raster->answer, G_mapset(), \
212
RASTER3D_DEFAULT_WINDOW, RASTER3D_TILE_SAME_AS_FILE, RASTER3D_USE_CACHE_DEFAULT);
214
/* Modify the units */
215
if (units_opt->answer)
216
Rast3d_set_unit(map, units_opt->answer);
218
if (vunits_opt->answer)
219
Rast3d_set_vertical_unit(map, vunits_opt->answer);
221
Rast3d_rewrite_header(map);
226
if (datasrc1_opt->answer || datasrc2_opt->answer || datadesc_opt->answer) {
227
Rast3d_read_history(raster->answer, "", &hist);
229
if (datasrc1_opt->answer)
230
Rast_set_history(&hist, HIST_DATSRC_1, datasrc1_opt->answer);
232
if (datasrc2_opt->answer)
233
Rast_set_history(&hist, HIST_DATSRC_2, datasrc2_opt->answer);
235
if (datadesc_opt->answer)
236
Rast_set_history(&hist, HIST_KEYWRD, datadesc_opt->answer);
238
Rast3d_write_history(raster->answer, &hist);
241
if (map_opt->answer) { /* use cats from another map */
243
struct Categories cats;
245
fd = Rast_open_old(infile, "");
246
Rast_init_cats("", &cats);
247
if (Rast3d_read_cats(map_opt->answer, "", &cats) < 0)
248
G_fatal_error(_("Unable to read category file of raster map <%s>"),
251
Rast3d_write_cats(infile, &cats);
252
G_message(_("cats table for [%s] set to %s"),
253
infile, map_opt->answer);
255
Rast_free_cats(&cats);
258
/* Check the histogram and range */
259
if (stats_flag->answer)
260
check_stats(raster->answer);