~ubuntu-branches/ubuntu/vivid/grass/vivid-proposed

« back to all changes in this revision

Viewing changes to raster3d/r3.support/main.c

  • Committer: Package Import Robot
  • Author(s): Bas Couwenberg
  • Date: 2015-02-20 23:12:08 UTC
  • mfrom: (8.2.6 experimental)
  • Revision ID: package-import@ubuntu.com-20150220231208-1u6qvqm84v430b10
Tags: 7.0.0-1~exp1
* New upstream release.
* Update python-ctypes-ternary.patch to use if/else instead of and/or.
* Drop check4dev patch, rely on upstream check.
* Add build dependency on libpq-dev to grass-dev for libpq-fe.h.
* Drop patches applied upstream, refresh remaining patches.
* Update symlinks for images switched from jpg to png.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 **********************************************************************
 
3
 *
 
4
 * MODULE:       r3.support (GRASS core)
 
5
 *
 
6
 * AUTHOR(S):    Soeren Gebbert vTI/AK based on r.support
 
7
 *               Original r.support by Michael Shapiro - CERL
 
8
 *
 
9
 * PURPOSE:      Build support files for raster map
 
10
 *               - Edit header, units
 
11
 *               - Update status (range)
 
12
 *
 
13
 * COPYRIGHT:    (C) 2000-2007 by the GRASS Development Team
 
14
 *
 
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.
 
18
 *
 
19
 **********************************************************************/
 
20
 
 
21
#include <unistd.h>
 
22
#include <stdlib.h>
 
23
#include <string.h>
 
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"
 
29
 
 
30
#define MAX_TITLE_LEN 1022
 
31
 
 
32
int main(int argc, char *argv[])
 
33
{
 
34
    const char *mapset;
 
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;
 
41
    const char *infile;
 
42
    char title[MAX_TITLE_LEN + 1];
 
43
    struct History hist;
 
44
 
 
45
    /* Initialize GIS engine */
 
46
    G_gisinit(argv[0]);
 
47
 
 
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.");
 
54
 
 
55
    raster = G_define_standard_option(G_OPT_R3_MAP);
 
56
 
 
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");
 
63
 
 
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");
 
71
 
 
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");
 
77
 
 
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");
 
83
    
 
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");
 
90
 
 
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");
 
97
 
 
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)");
 
105
 
 
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");
 
112
 
 
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");
 
117
 
 
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");
 
122
 
 
123
    stats_flag = G_define_flag();
 
124
    stats_flag->key = 's';
 
125
    stats_flag->description = _("Update range");
 
126
 
 
127
    /* Parse command-line options */
 
128
    if (G_parser(argc, argv))
 
129
        exit(EXIT_FAILURE);
 
130
 
 
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);
 
136
 
 
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 */
 
140
        G_strip(title);
 
141
        G_debug(3, "map title= [%s]  (%d chars)", title, (int)strlen(title));
 
142
        
 
143
        Rast3d_read_history(raster->answer, "", &hist);
 
144
        Rast_set_history(&hist, HIST_TITLE, title);
 
145
        Rast3d_write_history(raster->answer, &hist);
 
146
    }
 
147
 
 
148
    if (save_opt->answer) {
 
149
        FILE *fp = fopen(save_opt->answer, "w");
 
150
        int i;
 
151
 
 
152
        if (!fp)
 
153
            G_fatal_error(_("Unable to open output file <%s>"), save_opt->answer);
 
154
 
 
155
        Rast3d_read_history(raster->answer, "", &hist);
 
156
 
 
157
        for (i = 0; i < Rast_history_length(&hist); i++)
 
158
            fprintf(fp, "%s\n", Rast_history_line(&hist, i));
 
159
 
 
160
        fclose(fp);
 
161
    }
 
162
 
 
163
    if (load_opt->answer) {
 
164
        FILE *fp = fopen(load_opt->answer, "r");
 
165
 
 
166
        if (!fp)
 
167
            G_fatal_error(_("Unable to open input file <%s>"), load_opt->answer);
 
168
 
 
169
        Rast3d_read_history(raster->answer, "", &hist);
 
170
 
 
171
        Rast_clear_history(&hist);
 
172
 
 
173
        for (;;) {
 
174
            char buf[80];
 
175
            if (!G_getl2(buf, sizeof(buf), fp))
 
176
                break;
 
177
            Rast_append_history(&hist, buf);
 
178
        }
 
179
 
 
180
        fclose(fp);
 
181
 
 
182
        Rast3d_write_history(raster->answer, &hist);
 
183
    }
 
184
 
 
185
    if (history_opt->answer) {
 
186
        Rast3d_read_history(raster->answer, "", &hist);
 
187
 
 
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) {
 
191
            int i;
 
192
 
 
193
            for (i = 0; i < strlen(history_opt->answer); i += 71) {
 
194
                char buf[72];
 
195
 
 
196
                strncpy(buf, &history_opt->answer[i], sizeof(buf)-1);
 
197
                buf[sizeof(buf)-1] = '\0';
 
198
 
 
199
                Rast_append_history(&hist, buf);
 
200
            }
 
201
        }
 
202
        else
 
203
            Rast_append_history(&hist, history_opt->answer);
 
204
 
 
205
        Rast3d_write_history(raster->answer, &hist);
 
206
    }
 
207
    
 
208
    if(units_opt->answer || vunits_opt->answer) {
 
209
        RASTER3D_Map *map;
 
210
        
 
211
        map = Rast3d_open_cell_old(raster->answer, G_mapset(), \
 
212
                RASTER3D_DEFAULT_WINDOW, RASTER3D_TILE_SAME_AS_FILE, RASTER3D_USE_CACHE_DEFAULT);
 
213
        
 
214
        /* Modify the units */
 
215
        if (units_opt->answer)
 
216
            Rast3d_set_unit(map, units_opt->answer);
 
217
 
 
218
        if (vunits_opt->answer)
 
219
            Rast3d_set_vertical_unit(map, vunits_opt->answer);
 
220
        
 
221
        Rast3d_rewrite_header(map);
 
222
        Rast3d_close(map);
 
223
    
 
224
    }
 
225
 
 
226
    if (datasrc1_opt->answer || datasrc2_opt->answer || datadesc_opt->answer) {
 
227
        Rast3d_read_history(raster->answer, "", &hist);
 
228
 
 
229
        if (datasrc1_opt->answer)
 
230
            Rast_set_history(&hist, HIST_DATSRC_1, datasrc1_opt->answer);
 
231
 
 
232
        if (datasrc2_opt->answer)
 
233
            Rast_set_history(&hist, HIST_DATSRC_2, datasrc2_opt->answer);
 
234
 
 
235
        if (datadesc_opt->answer)
 
236
            Rast_set_history(&hist, HIST_KEYWRD, datadesc_opt->answer);
 
237
 
 
238
        Rast3d_write_history(raster->answer, &hist);
 
239
    }
 
240
 
 
241
    if (map_opt->answer) {      /* use cats from another map */
 
242
        int fd;
 
243
        struct Categories cats;
 
244
 
 
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>"),
 
249
                          map_opt->answer);
 
250
 
 
251
        Rast3d_write_cats(infile, &cats);
 
252
        G_message(_("cats table for [%s] set to %s"),
 
253
                  infile, map_opt->answer);
 
254
        Rast_close(fd);
 
255
        Rast_free_cats(&cats);
 
256
    }
 
257
 
 
258
    /* Check the histogram and range */
 
259
    if (stats_flag->answer)
 
260
        check_stats(raster->answer);
 
261
 
 
262
    return EXIT_SUCCESS;
 
263
}