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

« back to all changes in this revision

Viewing changes to raster/r.out.arc/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
 
#include <stdlib.h>
2
 
#include <string.h>
3
 
#include <math.h>
4
 
#include <stdio.h>
5
 
#include <grass/gis.h>
6
 
#include <grass/glocale.h>
7
 
 
8
 
/*
9
 
 ****************************************************************************
10
 
 *
11
 
 * MODULE:       r.out.arc
12
 
 * AUTHOR(S):    Original author: Michael Shapiro (r.out.ascii)
13
 
 *               modified to r.out.arc by Markus Neteler, Univ. of Hannover
14
 
 *               neteler geog.uni-hannover.de (11/99)
15
 
 * PURPOSE:      r.out.arc: writes ARC/INFO ASCII GRID file
16
 
 * COPYRIGHT:    (C) 2000 by the GRASS Development Team
17
 
 *
18
 
 *               This program is free software under the GNU General Public
19
 
 *              License (>=v2). Read the file COPYING that comes with GRASS
20
 
 *              for details.
21
 
 *
22
 
 *****************************************************************************/
23
 
 
24
 
int main(int argc, char *argv[])
25
 
{
26
 
    void *raster, *ptr;
27
 
 
28
 
    /*
29
 
       char  *null_row;
30
 
     */
31
 
    RASTER_MAP_TYPE out_type, map_type;
32
 
    char *outfile;
33
 
    char *mapset;
34
 
    char null_str[80];
35
 
    char cell_buf[300];
36
 
    int fd;
37
 
    int row, col;
38
 
    int nrows, ncols, dp;
39
 
    int do_stdout;
40
 
    FILE *fp;
41
 
    double cellsize;
42
 
    struct GModule *module;
43
 
    struct
44
 
    {
45
 
        struct Option *map;
46
 
        struct Option *output;
47
 
        struct Option *dp;
48
 
        struct Option *null;
49
 
    } parm;
50
 
    struct
51
 
    {
52
 
        struct Flag *noheader;
53
 
        struct Flag *singleline;
54
 
        struct Flag *ccenter;
55
 
    } flag;
56
 
 
57
 
    G_gisinit(argv[0]);
58
 
 
59
 
    module = G_define_module();
60
 
    module->keywords = _("raster, export");
61
 
    module->description =
62
 
        _("Converts a raster map layer into an ESRI ARCGRID file.");
63
 
 
64
 
    /* Define the different options */
65
 
    parm.map = G_define_standard_option(G_OPT_R_INPUT);
66
 
 
67
 
    parm.output = G_define_standard_option(G_OPT_F_OUTPUT);
68
 
    parm.output->description =
69
 
        _("Name for output ARC-GRID map (use out=- for stdout)");
70
 
 
71
 
    parm.dp = G_define_option();
72
 
    parm.dp->key = "dp";
73
 
    parm.dp->type = TYPE_INTEGER;
74
 
    parm.dp->required = NO;
75
 
    parm.dp->answer = "8";
76
 
    parm.dp->description = _("Number of decimal places");
77
 
 
78
 
    flag.noheader = G_define_flag();
79
 
    flag.noheader->key = 'h';
80
 
    flag.noheader->description = _("Suppress printing of header information");
81
 
 
82
 
    /* Added to optionally produce a single line output.     -- emes -- 12.10.92 */
83
 
    flag.singleline = G_define_flag();
84
 
    flag.singleline->key = '1';
85
 
    flag.singleline->description =
86
 
        _("List one entry per line instead of full row");
87
 
 
88
 
    /* use cell center in header instead of cell corner */
89
 
    flag.ccenter = G_define_flag();
90
 
    flag.ccenter->key = 'c';
91
 
    flag.ccenter->description =
92
 
        _("Use cell center reference in header instead of cell corner");
93
 
 
94
 
    if (G_parser(argc, argv))
95
 
        exit(EXIT_FAILURE);
96
 
 
97
 
 
98
 
    sscanf(parm.dp->answer, "%d", &dp);
99
 
    if (dp > 20 || dp < 0)
100
 
        G_fatal_error("dp has to be from 0 to 20");
101
 
 
102
 
    outfile = parm.output->answer;
103
 
    if ((strcmp("-", outfile)) == 0)
104
 
        do_stdout = 1;
105
 
    else
106
 
        do_stdout = 0;
107
 
 
108
 
    sprintf(null_str, "-9999");
109
 
 
110
 
    mapset = G_find_cell(parm.map->answer, "");
111
 
    if (mapset == NULL)
112
 
        G_fatal_error(_("Raster map <%s> not found"), parm.map->answer);
113
 
 
114
 
    fd = G_open_cell_old(parm.map->answer, mapset);
115
 
    if (fd < 0)
116
 
        G_fatal_error(_("Unable to open raster map <%s>"), parm.map->answer);
117
 
 
118
 
    map_type = G_get_raster_map_type(fd);
119
 
    out_type = map_type;
120
 
 
121
 
    /*
122
 
       null_row = G_allocate_null_buf();
123
 
     */
124
 
    raster = G_allocate_raster_buf(out_type);
125
 
 
126
 
    nrows = G_window_rows();
127
 
    ncols = G_window_cols();
128
 
 
129
 
    /* open arc file for writing */
130
 
    if (do_stdout)
131
 
        fp = stdout;
132
 
    else if (NULL == (fp = fopen(outfile, "w")))
133
 
        G_fatal_error(_("Unable to open file <%s>"), outfile);
134
 
 
135
 
    if (!flag.noheader->answer) {
136
 
        struct Cell_head region;
137
 
        char buf[128];
138
 
 
139
 
        G_get_window(&region);
140
 
        fprintf(fp, "ncols %d\n", region.cols);
141
 
        fprintf(fp, "nrows %d\n", region.rows);
142
 
        cellsize = fabs(region.east - region.west) / region.cols;
143
 
 
144
 
        if (G_projection() != PROJECTION_LL) {  /* Is Projection != LL (3) */
145
 
            if (!flag.ccenter->answer) {
146
 
                G_format_easting(region.west, buf, region.proj);
147
 
                fprintf(fp, "xllcorner %s\n", buf);
148
 
                G_format_northing(region.south, buf, region.proj);
149
 
                fprintf(fp, "yllcorner %s\n", buf);
150
 
            }
151
 
            else {
152
 
                G_format_easting(region.west + cellsize / 2., buf, region.proj);
153
 
                fprintf(fp, "xllcenter %s\n", buf);
154
 
                G_format_northing(region.south + cellsize / 2., buf, region.proj);
155
 
                fprintf(fp, "yllcenter %s\n", buf);
156
 
            }
157
 
        }
158
 
        else {                  /* yes, lat/long */
159
 
            G_format_easting(region.west, buf, -1);
160
 
            fprintf(fp, "xllcorner %s\n", buf);
161
 
 
162
 
            G_format_northing(region.south, buf, -1);
163
 
            fprintf(fp, "yllcorner %s\n", buf);
164
 
        }
165
 
 
166
 
        G_format_resolution(cellsize, buf, -1);
167
 
        fprintf(fp, "cellsize %s\n", buf);
168
 
        fprintf(fp, "NODATA_value %s\n", null_str);
169
 
    }
170
 
 
171
 
    for (row = 0; row < nrows; row++) {
172
 
        G_percent(row, nrows, 2);
173
 
        if (G_get_raster_row(fd, raster, row, out_type) < 0)
174
 
            exit(EXIT_FAILURE);
175
 
        /*
176
 
           if (G_get_null_value_row(fd, null_row, row) < 0)
177
 
           exit(EXIT_FAILURE);
178
 
         */
179
 
        for (col = 0, ptr = raster; col < ncols; col++,
180
 
             ptr = G_incr_void_ptr(ptr, G_raster_size(out_type))) {
181
 
            if (!G_is_null_value(ptr, out_type)) {
182
 
                if (out_type == CELL_TYPE)
183
 
                    fprintf(fp, "%d", *((CELL *) ptr));
184
 
 
185
 
                else if (out_type == FCELL_TYPE) {
186
 
                    sprintf(cell_buf, "%.*f", dp, *((FCELL *) ptr));
187
 
                    G_trim_decimal(cell_buf);
188
 
                    fprintf(fp, "%s", cell_buf);
189
 
                }
190
 
                else if (out_type == DCELL_TYPE) {
191
 
                    sprintf(cell_buf, "%.*f", dp, *((DCELL *) ptr));
192
 
                    G_trim_decimal(cell_buf);
193
 
                    fprintf(fp, "%s", cell_buf);
194
 
                }
195
 
            }
196
 
            else
197
 
                fprintf(fp, "%s", null_str);
198
 
 
199
 
            if (!flag.singleline->answer)
200
 
                fprintf(fp, " ");
201
 
            else
202
 
                fprintf(fp, "\n");
203
 
        }
204
 
 
205
 
        if (!flag.singleline->answer)
206
 
            fprintf(fp, "\n");
207
 
 
208
 
        /*
209
 
           for (col = 0; col < ncols; col++)
210
 
           fprintf (fp,"%d ", null_row[col]);
211
 
           fprintf (fp,"\n");
212
 
         */
213
 
    }
214
 
 
215
 
    /* make sure it got to 100% */
216
 
    G_percent(1, 1, 2);
217
 
 
218
 
    G_close_cell(fd);
219
 
    fclose(fp);
220
 
 
221
 
    exit(EXIT_SUCCESS);
222
 
}