~ubuntu-branches/ubuntu/wily/grass/wily

« back to all changes in this revision

Viewing changes to raster/r.out.gdal/export_band.c

Tags: 7.0.0~rc1+ds1-1~exp1
* New upstream release candidate.
* Repack upstream tarball, remove precompiled Python objects.
* Add upstream metadata.
* Update gbp.conf and Vcs-Git URL to use the experimental branch.
* Update watch file for GRASS 7.0.
* Drop build dependencies for Tcl/Tk, add build dependencies:
  python-numpy, libnetcdf-dev, netcdf-bin, libblas-dev, liblapack-dev
* Update Vcs-Browser URL to use cgit instead of gitweb.
* Update paths to use grass70.
* Add configure options: --with-netcdf, --with-blas, --with-lapack,
  remove --with-tcltk-includes.
* Update patches for GRASS 7.
* Update copyright file, changes:
  - Update copyright years
  - Group files by license
  - Remove unused license sections
* Add patches for various typos.
* Fix desktop file with patch instead of d/rules.
* Use minimal dh rules.
* Bump Standards-Version to 3.9.6, no changes.
* Use dpkg-maintscript-helper to replace directories with symlinks.
  (closes: #776349)
* Update my email to use @debian.org address.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
*****************************************************************************/
16
16
 
17
17
#include <grass/gis.h>
 
18
#include <grass/raster.h>
18
19
#include <grass/glocale.h>
 
20
 
19
21
#include "cpl_string.h"
20
22
#include "gdal.h"
21
23
#include "local_proto.h"
33
35
                double nodataval, const char *nodatakey,
34
36
                int default_nodataval)
35
37
{
36
 
    int bHaveMinMax;
37
38
    double dfCellMin;
38
39
    double dfCellMax;
39
 
    struct FPRange sRange;
40
40
    int fd;
41
41
    int cols = cellhead->cols;
42
42
    int rows = cellhead->rows;
43
43
    int ret = 0;
44
44
 
45
45
    /* Open GRASS raster */
46
 
    fd = G_open_cell_old(name, mapset);
47
 
    if (fd < 0) {
48
 
        G_warning(_("Unable to open raster map <%s>"), name);
49
 
        return -1;
50
 
    }
51
 
 
52
 
    /* Get min/max values. */
53
 
    if (G_read_fp_range(name, mapset, &sRange) == -1) {
54
 
        bHaveMinMax = FALSE;
55
 
    }
56
 
    else {
57
 
        bHaveMinMax = TRUE;
58
 
        G_get_fp_range_min_max(&sRange, &dfCellMin, &dfCellMax);
59
 
    }
 
46
    fd = Rast_open_old(name, mapset);
60
47
 
61
48
    /* Create GRASS raster buffer */
62
 
    void *bufer = G_allocate_raster_buf(maptype);
 
49
    void *bufer = Rast_allocate_buf(maptype);
63
50
 
64
51
    if (bufer == NULL) {
65
52
        G_warning(_("Unable to allocate buffer for reading raster map"));
66
53
        return -1;
67
54
    }
68
 
    char *nulls = (char *)G_malloc(cols);
69
55
 
70
 
    if (nulls == NULL) {
71
 
        G_warning(_("Unable to allocate buffer for reading raster map"));
72
 
        return -1;
73
 
    }
 
56
    /* the following routine must be kept identical to export_band */
74
57
 
75
58
    /* Copy data form GRASS raster to GDAL raster */
76
59
    int row, col;
90
73
 
91
74
        for (row = 0; row < rows; row++) {
92
75
 
93
 
            if (G_get_raster_row(fd, bufer, row, maptype) < 0) {
94
 
                G_warning(_("Unable to read raster map <%s> row %d"),
95
 
                          name, row);
96
 
                return -1;
97
 
            }
98
 
            G_get_null_value_row(fd, nulls, row);
 
76
            Rast_get_row(fd, bufer, row, maptype);
99
77
            for (col = 0; col < cols; col++) {
100
 
                if (nulls[col]) {
101
 
                    ((FCELL *) bufer)[col] = fnullval;
 
78
                if (Rast_is_f_null_value(&((FCELL *) bufer)[col])) {
102
79
                    n_nulls++;
103
80
                }
104
81
                else {
122
99
 
123
100
        for (row = 0; row < rows; row++) {
124
101
 
125
 
            if (G_get_raster_row(fd, bufer, row, maptype) < 0) {
126
 
                G_warning(_("Unable to read raster map <%s> row %d"),
127
 
                          name, row);
128
 
                return -1;
129
 
            }
130
 
            G_get_null_value_row(fd, nulls, row);
 
102
            Rast_get_row(fd, bufer, row, maptype);
131
103
            for (col = 0; col < cols; col++) {
132
 
                if (nulls[col]) {
 
104
                if (Rast_is_d_null_value(&((DCELL *) bufer)[col])) {
133
105
                    ((DCELL *) bufer)[col] = dnullval;
134
106
                    n_nulls++;
135
107
                }
154
126
 
155
127
        for (row = 0; row < rows; row++) {
156
128
 
157
 
            if (G_get_raster_row(fd, bufer, row, maptype) < 0) {
158
 
                G_warning(_("Unable to read raster map <%s> row %d"),
159
 
                          name, row);
160
 
                return -1;
161
 
            }
162
 
            G_get_null_value_row(fd, nulls, row);
 
129
            Rast_get_row(fd, bufer, row, maptype);
163
130
            for (col = 0; col < cols; col++) {
164
 
                if (nulls[col]) {
 
131
                if (Rast_is_c_null_value(&((CELL *) bufer)[col])) {
165
132
                    ((CELL *) bufer)[col] = inullval;
166
133
                    n_nulls++;
167
134
                }
186
153
        G_warning("Raster export results in data loss.");
187
154
        ret = -2;
188
155
    }
 
156
    G_message(_("Using GDAL data type <%s>"), GDALGetDataTypeName(export_datatype));
189
157
 
190
158
    /* a default nodata value was used and NULL cells were present */
191
159
    if (n_nulls && default_nodataval) {
196
164
                                (int)nodataval, nodatakey);
197
165
        else
198
166
            G_important_message(_("Input raster map contains cells with NULL-value (no-data). "
199
 
                                 "The value %f will be used to represent no-data values in the input map. "
 
167
                                 "The value %g will be used to represent no-data values in the input map. "
200
168
                                 "You can specify a nodata value with the %s option."),
201
169
                                nodataval, nodatakey);
202
170
    }
220
188
        ret = -1;
221
189
    }
222
190
 
 
191
    Rast_close(fd);
 
192
 
 
193
    G_free(bufer);
 
194
 
223
195
    return ret;
224
196
}
225
197
 
227
199
 * returns 0 on success
228
200
 * -1 on raster data read/write error
229
201
 * */
230
 
int export_band(GDALDatasetH hMEMDS, GDALDataType export_datatype, int band,
 
202
int export_band(GDALDatasetH hMEMDS, int band,
231
203
                const char *name, const char *mapset,
232
204
                struct Cell_head *cellhead, RASTER_MAP_TYPE maptype,
233
 
                double nodataval, const char *nodatakey,
234
 
                int suppress_main_colortable, int default_nodataval)
 
205
                double nodataval, int suppress_main_colortable)
235
206
{
236
207
    struct Colors sGrassColors;
237
208
    GDALColorTableH hCT;
244
215
    int cols = cellhead->cols;
245
216
    int rows = cellhead->rows;
246
217
    int ret = 0;
 
218
    char value[200];
247
219
 
248
220
    /* Open GRASS raster */
249
 
    fd = G_open_cell_old(name, mapset);
250
 
    if (fd < 0) {
251
 
        G_warning(_("Unable to open raster map <%s>"), name);
252
 
        return -1;
253
 
    }
 
221
    fd = Rast_open_old(name, mapset);
254
222
 
255
223
    /* Get raster band  */
256
224
    GDALRasterBandH hBand = GDALGetRasterBand(hMEMDS, band);
261
229
    }
262
230
 
263
231
    /* Get min/max values. */
264
 
    if (G_read_fp_range(name, mapset, &sRange) == -1) {
 
232
    if (Rast_read_fp_range(name, mapset, &sRange) == -1) {
265
233
        bHaveMinMax = FALSE;
266
234
    }
267
235
    else {
268
236
        bHaveMinMax = TRUE;
269
 
        G_get_fp_range_min_max(&sRange, &dfCellMin, &dfCellMax);
 
237
        Rast_get_fp_range_min_max(&sRange, &dfCellMin, &dfCellMax);
270
238
    }
271
239
 
272
 
    /* suppress useless warnings */
273
 
    CPLPushErrorHandler(CPLQuietErrorHandler);
274
 
    GDALSetRasterColorInterpretation(hBand, GPI_RGB);
275
 
    CPLPopErrorHandler();
 
240
    sprintf(value, "GRASS GIS %s", GRASS_VERSION_NUMBER);
 
241
    GDALSetMetadataItem(hBand, "Generated_with", value, NULL);
276
242
 
277
243
    /* use default color rules if no color rules are given */
278
 
    if (G_read_colors(name, mapset, &sGrassColors) >= 0) {
 
244
    if (Rast_read_colors(name, mapset, &sGrassColors) >= 0) {
279
245
        int maxcolor, i;
280
246
        CELL min, max;
281
 
        char key[200], value[200];
 
247
        char key[200];
282
248
        int rcount;
283
249
 
284
 
        G_get_color_range(&min, &max, &sGrassColors);
 
250
        Rast_get_c_color_range(&min, &max, &sGrassColors);
285
251
        if (bHaveMinMax) {
286
252
            if (max < dfCellMax) {
287
253
                maxcolor = max;
306
272
            }
307
273
        }
308
274
 
309
 
        rcount = G_colors_count(&sGrassColors);
 
275
        rcount = Rast_colors_count(&sGrassColors);
310
276
 
311
277
        G_debug(3, "dfCellMin: %f, dfCellMax: %f, maxcolor: %d", dfCellMin,
312
278
                dfCellMax, maxcolor);
318
284
                int nRed, nGreen, nBlue;
319
285
                GDALColorEntry sColor;
320
286
 
321
 
                if (G_get_color(iColor, &nRed, &nGreen, &nBlue,
 
287
                if (Rast_get_c_color(&iColor, &nRed, &nGreen, &nBlue,
322
288
                                     &sGrassColors)) {
323
289
                    sColor.c1 = nRed;
324
290
                    sColor.c2 = nGreen;
326
292
                    sColor.c4 = 255;
327
293
 
328
294
                    G_debug(3,
329
 
                            "G_get_color: Y, rcount %d, nRed %d, nGreen %d, nBlue %d",
 
295
                            "Rast_get_c_color: Y, rcount %d, nRed %d, nGreen %d, nBlue %d",
330
296
                            rcount, nRed, nGreen, nBlue);
331
297
                    GDALSetColorEntry(hCT, iColor, &sColor);
332
298
                }
337
303
                    sColor.c4 = 0;
338
304
 
339
305
                    G_debug(3,
340
 
                            "G_get_color: N, rcount %d, nRed %d, nGreen %d, nBlue %d",
 
306
                            "Rast_get_c_color: N, rcount %d, nRed %d, nGreen %d, nBlue %d",
341
307
                            rcount, nRed, nGreen, nBlue);
342
308
                    GDALSetColorEntry(hCT, iColor, &sColor);
343
309
                }
360
326
            DCELL val1, val2;
361
327
            unsigned char r1, g1, b1, r2, g2, b2;
362
328
 
363
 
            G_get_f_color_rule(&val1, &r1, &g1, &b1, &val2, &r2, &g2, &b2,
 
329
            Rast_get_fp_color_rule(&val1, &r1, &g1, &b1, &val2, &r2, &g2, &b2,
364
330
                               &sGrassColors, i);
365
331
 
366
332
 
372
338
    }
373
339
 
374
340
    /* Create GRASS raster buffer */
375
 
    void *bufer = G_allocate_raster_buf(maptype);
 
341
    void *bufer = Rast_allocate_buf(maptype);
376
342
 
377
343
    if (bufer == NULL) {
378
344
        G_warning(_("Unable to allocate buffer for reading raster map"));
379
345
        return -1;
380
346
    }
381
 
    char *nulls = (char *)G_malloc(cols);
382
347
 
383
 
    if (nulls == NULL) {
384
 
        G_warning(_("Unable to allocate buffer for reading raster map"));
385
 
        return -1;
386
 
    }
 
348
    /* the following routine must be kept identical to exact_checks */
387
349
 
388
350
    /* Copy data form GRASS raster to GDAL raster */
389
351
    int row, col;
402
364
 
403
365
        for (row = 0; row < rows; row++) {
404
366
 
405
 
            if (G_get_raster_row(fd, bufer, row, maptype) < 0) {
406
 
                G_warning(_("Unable to read raster map <%s> row %d"),
407
 
                          name, row);
408
 
                return -1;
409
 
            }
410
 
            G_get_null_value_row(fd, nulls, row);
 
367
            Rast_get_row(fd, bufer, row, maptype);
411
368
            for (col = 0; col < cols; col++) {
412
 
                if (nulls[col]) {
 
369
                if (Rast_is_f_null_value(&((FCELL *) bufer)[col])) {
413
370
                    ((FCELL *) bufer)[col] = fnullval;
414
371
                    if (n_nulls == 0) {
415
372
                        GDALSetRasterNoDataValue(hBand, nodataval);
436
393
 
437
394
        for (row = 0; row < rows; row++) {
438
395
 
439
 
            if (G_get_raster_row(fd, bufer, row, maptype) < 0) {
440
 
                G_warning(_("Unable to read raster map <%s> row %d"),
441
 
                          name, row);
442
 
                return -1;
443
 
            }
444
 
            G_get_null_value_row(fd, nulls, row);
 
396
            Rast_get_row(fd, bufer, row, maptype);
445
397
            for (col = 0; col < cols; col++) {
446
 
                if (nulls[col]) {
 
398
                if (Rast_is_d_null_value(&((DCELL *) bufer)[col])) {
447
399
                    ((DCELL *) bufer)[col] = dnullval;
448
400
                    if (n_nulls == 0) {
449
401
                        GDALSetRasterNoDataValue(hBand, nodataval);
470
422
 
471
423
        for (row = 0; row < rows; row++) {
472
424
 
473
 
            if (G_get_raster_row(fd, bufer, row, maptype) < 0) {
474
 
                G_warning(_("Unable to read raster map <%s> row %d"),
475
 
                          name, row);
476
 
                return -1;
477
 
            }
478
 
            G_get_null_value_row(fd, nulls, row);
 
425
            Rast_get_row(fd, bufer, row, maptype);
479
426
            for (col = 0; col < cols; col++) {
480
 
                if (nulls[col]) {
 
427
                if (Rast_is_c_null_value(&((CELL *) bufer)[col])) {
481
428
                    ((CELL *) bufer)[col] = inullval;
482
429
                    if (n_nulls == 0) {
483
430
                        GDALSetRasterNoDataValue(hBand, nodataval);
496
443
        }
497
444
    }
498
445
 
 
446
    Rast_close(fd);
 
447
 
 
448
    G_free(bufer);
 
449
 
499
450
    return ret;
500
451
}
501
452
 
502
453
int exact_range_check(double min, double max, GDALDataType datatype,
503
454
                      const char *name)
504
455
{
 
456
 
505
457
    switch (datatype) {
506
458
    case GDT_Byte:
507
459
        if (min < TYPE_BYTE_MIN || max > TYPE_BYTE_MAX) {