6
#include <grass/glocale.h>
9
****************************************************************************
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
18
* This program is free software under the GNU General Public
19
* License (>=v2). Read the file COPYING that comes with GRASS
22
*****************************************************************************/
24
int main(int argc, char *argv[])
31
RASTER_MAP_TYPE out_type, map_type;
42
struct GModule *module;
46
struct Option *output;
52
struct Flag *noheader;
53
struct Flag *singleline;
59
module = G_define_module();
60
module->keywords = _("raster, export");
62
_("Converts a raster map layer into an ESRI ARCGRID file.");
64
/* Define the different options */
65
parm.map = G_define_standard_option(G_OPT_R_INPUT);
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)");
71
parm.dp = G_define_option();
73
parm.dp->type = TYPE_INTEGER;
74
parm.dp->required = NO;
75
parm.dp->answer = "8";
76
parm.dp->description = _("Number of decimal places");
78
flag.noheader = G_define_flag();
79
flag.noheader->key = 'h';
80
flag.noheader->description = _("Suppress printing of header information");
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");
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");
94
if (G_parser(argc, argv))
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");
102
outfile = parm.output->answer;
103
if ((strcmp("-", outfile)) == 0)
108
sprintf(null_str, "-9999");
110
mapset = G_find_cell(parm.map->answer, "");
112
G_fatal_error(_("Raster map <%s> not found"), parm.map->answer);
114
fd = G_open_cell_old(parm.map->answer, mapset);
116
G_fatal_error(_("Unable to open raster map <%s>"), parm.map->answer);
118
map_type = G_get_raster_map_type(fd);
122
null_row = G_allocate_null_buf();
124
raster = G_allocate_raster_buf(out_type);
126
nrows = G_window_rows();
127
ncols = G_window_cols();
129
/* open arc file for writing */
132
else if (NULL == (fp = fopen(outfile, "w")))
133
G_fatal_error(_("Unable to open file <%s>"), outfile);
135
if (!flag.noheader->answer) {
136
struct Cell_head region;
139
G_get_window(®ion);
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;
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);
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);
158
else { /* yes, lat/long */
159
G_format_easting(region.west, buf, -1);
160
fprintf(fp, "xllcorner %s\n", buf);
162
G_format_northing(region.south, buf, -1);
163
fprintf(fp, "yllcorner %s\n", buf);
166
G_format_resolution(cellsize, buf, -1);
167
fprintf(fp, "cellsize %s\n", buf);
168
fprintf(fp, "NODATA_value %s\n", null_str);
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)
176
if (G_get_null_value_row(fd, null_row, row) < 0)
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));
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);
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);
197
fprintf(fp, "%s", null_str);
199
if (!flag.singleline->answer)
205
if (!flag.singleline->answer)
209
for (col = 0; col < ncols; col++)
210
fprintf (fp,"%d ", null_row[col]);
215
/* make sure it got to 100% */