00001
00015 #include <rpc/types.h>
00016 #include <rpc/xdr.h>
00017 #include <unistd.h>
00018 #include <string.h>
00019 #include <sys/types.h>
00020 #include <sys/stat.h>
00021 #include <fcntl.h>
00022 #include <grass/config.h>
00023 #include "G.h"
00024 #include <grass/gis.h>
00025 #include <grass/glocale.h>
00026
00027 static int allocate_compress_buf(int);
00028
00029 static struct fileinfo *new_fileinfo(int fd)
00030 {
00031 int oldsize = G__.fileinfo_count;
00032 int newsize = oldsize;
00033 int i;
00034
00035 if (fd < oldsize)
00036 return &G__.fileinfo[fd];
00037
00038 newsize *= 2;
00039 if (newsize <= fd)
00040 newsize = fd + 20;
00041
00042 G__.fileinfo = G_realloc(G__.fileinfo, newsize * sizeof(struct fileinfo));
00043
00044
00045 for (i = oldsize; i < newsize; i++) {
00046 memset(&G__.fileinfo[i], 0, sizeof(struct fileinfo));
00047 G__.fileinfo[i].open_mode = -1;
00048 }
00049
00050 G__.fileinfo_count = newsize;
00051
00052 return &G__.fileinfo[fd];
00053 }
00054
00055
00072 static int G__open_raster_new(const char *name, int open_mode);
00073
00100 int G_open_cell_old(const char *name, const char *mapset)
00101 {
00102 int fd;
00103
00104 if ((fd = G__open_cell_old(name, mapset)) < 0) {
00105 G_warning(_("Unable to open raster map <%s@%s>"), name, mapset);
00106 return fd;
00107 }
00108
00109
00110 G__check_for_auto_masking();
00111
00112
00113
00114
00115
00116
00117
00118
00119 return fd;
00120 }
00121
00149 int G__open_cell_old(const char *name, const char *mapset)
00150 {
00151 struct fileinfo *fcb;
00152 int fd;
00153 char cell_dir[100];
00154 const char *r_name;
00155 const char *r_mapset;
00156 struct Cell_head cellhd;
00157 int CELL_nbytes = 0;
00158 int INTERN_SIZE;
00159 int reclass_flag, i;
00160 int MAP_NBYTES;
00161 RASTER_MAP_TYPE MAP_TYPE;
00162 struct Reclass reclass;
00163 const char *xmapset;
00164 struct GDAL_link *gdal;
00165
00166
00167 G__init_window();
00168
00169 xmapset = G_find_cell2(name, mapset);
00170 if (!xmapset) {
00171 G_warning(_("Unable to find <%s@%s>"), name, mapset);
00172 return -1;
00173 }
00174 mapset = xmapset;
00175
00176
00177 reclass_flag = G_get_reclass(name, mapset, &reclass);
00178
00179 switch (reclass_flag) {
00180 case 0:
00181 r_name = name;
00182 r_mapset = mapset;
00183 break;
00184 case 1:
00185 r_name = reclass.name;
00186 r_mapset = reclass.mapset;
00187 if (G_find_cell2(r_name, r_mapset) == NULL) {
00188 G_warning(_("Unable to open raster map <%s@%s> since it is a reclass "
00189 "of raster map <%s@%s> which does not exist"),
00190 name, mapset, r_name, r_mapset);
00191 return -1;
00192 }
00193 break;
00194 default:
00195 return -1;
00196 }
00197
00198
00199 if (G_get_cellhd(r_name, r_mapset, &cellhd) < 0)
00200 return -1;
00201
00202
00203 MAP_TYPE = G_raster_map_type(r_name, r_mapset);
00204 if (MAP_TYPE < 0)
00205 return -1;
00206
00207 if (MAP_TYPE == CELL_TYPE)
00208
00209 {
00210 CELL_nbytes = cellhd.format + 1;
00211 if (CELL_nbytes < 1) {
00212 G_warning(_("Raster map <%s@%s>: format field in header file invalid"),
00213 r_name, r_mapset);
00214 return -1;
00215 }
00216 }
00217
00218 if (cellhd.proj != G__.window.proj) {
00219 G_warning(_("Raster map <%s@%s> is in different projection than current region. "
00220 "Found raster map <%s@%s>, should be <%s>."),
00221 name, mapset, name, G__projection_name(cellhd.proj),
00222 G__projection_name(G__.window.proj));
00223 return -1;
00224 }
00225 if (cellhd.zone != G__.window.zone) {
00226 G_warning(_("Raster map <%s@%s> is in different zone (%d) than current region (%d)"),
00227 name, mapset, cellhd.zone, G__.window.zone);
00228 return -1;
00229 }
00230
00231
00232 if (MAP_TYPE == CELL_TYPE && (unsigned int) CELL_nbytes > sizeof(CELL)) {
00233 G_warning(_("Raster map <%s@%s>: bytes per cell (%d) too large"),
00234 name, mapset, CELL_nbytes);
00235 return -1;
00236 }
00237
00238
00239 if (MAP_TYPE == FCELL_TYPE) {
00240 strcpy(cell_dir, "fcell");
00241 INTERN_SIZE = sizeof(FCELL);
00242 MAP_NBYTES = XDR_FLOAT_NBYTES;
00243 }
00244 else if (MAP_TYPE == DCELL_TYPE) {
00245 strcpy(cell_dir, "fcell");
00246 INTERN_SIZE = sizeof(DCELL);
00247 MAP_NBYTES = XDR_DOUBLE_NBYTES;
00248 }
00249 else {
00250
00251 strcpy(cell_dir, "cell");
00252 INTERN_SIZE = sizeof(CELL);
00253 MAP_NBYTES = CELL_nbytes;
00254 }
00255
00256 gdal = G_get_gdal_link(r_name, r_mapset);
00257 if (gdal) {
00258 #ifdef HAVE_GDAL
00259
00260 fd = open(G_DEV_NULL, O_RDONLY);
00261 #else
00262 G_warning(_("map <%s@%s> is a GDAL link but GRASS is compiled without GDAL support"),
00263 r_name, r_mapset);
00264 return -1;
00265 #endif
00266 }
00267 else
00268
00269 fd = G_open_old(cell_dir, r_name, r_mapset);
00270
00271 if (fd < 0)
00272 return -1;
00273
00274 fcb = new_fileinfo(fd);
00275
00276 fcb->map_type = MAP_TYPE;
00277
00278
00279 G_copy((char *)&fcb->cellhd, (char *)&cellhd, sizeof(cellhd));
00280
00281
00282 for (i = 0; i < NULL_ROWS_INMEM; i++)
00283 fcb->NULL_ROWS[i] = G__allocate_null_bits(G__.window.cols);
00284 fcb->null_work_buf = G__allocate_null_bits(fcb->cellhd.cols);
00285
00286 fcb->min_null_row = (-1) * NULL_ROWS_INMEM;
00287
00288
00289 fcb->open_mode = -1;
00290
00291
00292 {
00293 char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
00294
00295 if (G__name_is_fully_qualified(name, xname, xmapset))
00296 fcb->name = G_store(xname);
00297 else
00298 fcb->name = G_store(name);
00299 }
00300 fcb->mapset = G_store(mapset);
00301
00302
00303 fcb->cur_row = -1;
00304
00305 fcb->null_cur_row = -1;
00306
00307
00308 if ((fcb->reclass_flag = reclass_flag))
00309 G_copy(&fcb->reclass, &reclass, sizeof(reclass));
00310
00311 fcb->gdal = gdal;
00312 if (!gdal)
00313
00314 if (G__check_format(fd) < 0) {
00315 close(fd);
00316 return -1;
00317 }
00318
00319
00320 G__create_window_mapping(fd);
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330 fcb->data = (unsigned char *)G_calloc(fcb->cellhd.cols, MAP_NBYTES);
00331
00332 G__reallocate_work_buf(INTERN_SIZE);
00333 G__reallocate_mask_buf();
00334 G__reallocate_null_buf();
00335 G__reallocate_temp_buf();
00336
00337
00338
00339
00340 allocate_compress_buf(fd);
00341
00342
00343 if (fcb->map_type != CELL_TYPE) {
00344 if (fcb->reclass_flag)
00345 G_read_quant(fcb->reclass.name, fcb->reclass.mapset,
00346 &(fcb->quant));
00347 else
00348 G_read_quant(fcb->name, fcb->mapset, &(fcb->quant));
00349 }
00350
00351
00352 fcb->open_mode = OPEN_OLD;
00353 fcb->io_error = 0;
00354 fcb->map_type = MAP_TYPE;
00355 fcb->nbytes = MAP_NBYTES;
00356 fcb->null_file_exists = -1;
00357
00358 if (fcb->map_type != CELL_TYPE)
00359 xdrmem_create(&fcb->xdrstream, (caddr_t) fcb->data,
00360 (u_int) (fcb->nbytes * fcb->cellhd.cols), XDR_DECODE);
00361
00362 return fd;
00363 }
00364
00365
00366
00367 static int WRITE_NBYTES = sizeof(CELL);
00368
00369
00370
00371 static int NBYTES = sizeof(CELL);
00372
00373
00374
00375 static RASTER_MAP_TYPE WRITE_MAP_TYPE = CELL_TYPE;
00376
00377
00378
00379 static int COMPRESSION_TYPE = 0;
00380
00381 #define FP_NBYTES G__.fp_nbytes
00382
00383 #define FP_TYPE G__.fp_type
00384
00385 static int FP_TYPE_SET = 0;
00386
00387
00388 static char cell_dir[100];
00389
00417 int G_open_cell_new(const char *name)
00418 {
00419 WRITE_MAP_TYPE = CELL_TYPE;
00420 strcpy(cell_dir, "cell");
00421
00422 WRITE_NBYTES = NBYTES;
00423 return G__open_raster_new(name, OPEN_NEW_COMPRESSED);
00424 }
00425
00438 int G_open_cell_new_random(const char *name)
00439 {
00440 WRITE_MAP_TYPE = CELL_TYPE;
00441
00442 WRITE_NBYTES = NBYTES;
00443 strcpy(cell_dir, "cell");
00444 return G__open_raster_new(name, OPEN_NEW_RANDOM);
00445 }
00446
00457 int G_open_cell_new_uncompressed(const char *name)
00458 {
00459 WRITE_MAP_TYPE = CELL_TYPE;
00460 strcpy(cell_dir, "cell");
00461
00462 WRITE_NBYTES = NBYTES;
00463 return G__open_raster_new(name, OPEN_NEW_UNCOMPRESSED);
00464 }
00465
00476 int G_want_histogram(int flag)
00477 {
00478 G__.want_histogram = flag;
00479
00480 return 0;
00481 }
00482
00497 int G_set_cell_format(int n)
00498
00499 {
00500 if (WRITE_MAP_TYPE == CELL_TYPE) {
00501 NBYTES = n + 1;
00502 if (NBYTES <= 0)
00503 NBYTES = 1;
00504 if ((unsigned int) NBYTES > sizeof(CELL))
00505 NBYTES = sizeof(CELL);
00506 }
00507
00508 return 0;
00509 }
00510
00518 int G_cellvalue_format(CELL v)
00519 {
00520 unsigned int i;
00521
00522 if (v >= 0)
00523 for (i = 0; i < sizeof(CELL); i++)
00524 if (!(v /= 256))
00525 return i;
00526 return sizeof(CELL) - 1;
00527 }
00528
00546 int G_open_fp_cell_new(const char *name)
00547 {
00548
00549
00550
00551 if (!FP_TYPE_SET) {
00552 if (getenv("GRASS_FP_DOUBLE")) {
00553 FP_TYPE = DCELL_TYPE;
00554 FP_NBYTES = XDR_DOUBLE_NBYTES;
00555 }
00556 else {
00557 FP_TYPE = FCELL_TYPE;
00558 FP_NBYTES = XDR_FLOAT_NBYTES;
00559 }
00560 }
00561 WRITE_MAP_TYPE = FP_TYPE;
00562 WRITE_NBYTES = FP_NBYTES;
00563
00564 strcpy(cell_dir, "fcell");
00565 return G__open_raster_new(name, OPEN_NEW_COMPRESSED);
00566 }
00567
00578 int G_open_fp_cell_new_uncompressed(const char *name)
00579 {
00580
00581 if (!FP_TYPE_SET) {
00582 if (getenv("GRASS_FP_DOUBLE")) {
00583 FP_TYPE = DCELL_TYPE;
00584 FP_NBYTES = XDR_DOUBLE_NBYTES;
00585 }
00586 else {
00587 FP_TYPE = FCELL_TYPE;
00588 FP_NBYTES = XDR_FLOAT_NBYTES;
00589 }
00590 }
00591 WRITE_MAP_TYPE = FP_TYPE;
00592 WRITE_NBYTES = FP_NBYTES;
00593
00594 strcpy(cell_dir, "fcell");
00595 return G__open_raster_new(name, OPEN_NEW_UNCOMPRESSED);
00596 }
00597
00598 static int
00599 clean_check_raster_name(const char *inmap, char **outmap, char **outmapset)
00600 {
00601
00602
00603
00604 int status = 0;
00605 char *ptr;
00606 char *buf;
00607
00608 buf = G_store(inmap);
00609 if ((ptr = strpbrk(buf, "@")) != NULL) {
00610 *ptr = '\0';
00611 ptr++;
00612 *outmapset = G_store(G_mapset());
00613 if ((status = strcmp(ptr, *outmapset))) {
00614 G_free(buf);
00615 G_free(*outmapset);
00616 }
00617 else {
00618 *outmap = G_store(buf);
00619 G_free(buf);
00620 }
00621 }
00622 else {
00623 *outmap = buf;
00624 *outmapset = G_store(G_mapset());
00625 }
00626 return status;
00627 }
00628
00629
00630 static int G__open_raster_new(const char *name, int open_mode)
00631 {
00632 char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
00633 struct fileinfo *fcb;
00634 int i, null_fd, fd;
00635 char *tempname;
00636 char *map;
00637 char *mapset;
00638
00639
00640 if (G__name_is_fully_qualified(name, xname, xmapset)) {
00641 if (strcmp(xmapset, G_mapset()) != 0)
00642 G_fatal_error(_("Raster map <%s> is not in the current mapset (%s)"),
00643 name, G_mapset());
00644 name = xname;
00645 }
00646
00647
00648 if (G_legal_filename(name) < 0) {
00649 G_warning(_("<%s> is an illegal file name"),
00650 name);
00651 return -1;
00652 }
00653
00654 if (clean_check_raster_name(name, &map, &mapset) != 0) {
00655 G_warning(_("<%s>: bad mapset"), name);
00656 return -1;
00657 }
00658
00659
00660 G__init_window();
00661
00662
00663 tempname = G_tempfile();
00664 fd = creat(tempname, 0666);
00665 if (fd < 0) {
00666 G_warning(_("G__open_raster_new(): no temp files available"));
00667 G_free(tempname);
00668 G_free(map);
00669 G_free(mapset);
00670 return -1;
00671 }
00672
00673 fcb = new_fileinfo(fd);
00674
00675
00676
00677
00678 G__make_mapset_element(cell_dir);
00679
00680
00681 fcb->map_type = WRITE_MAP_TYPE;
00682 fcb->open_mode = -1;
00683
00684
00685
00686
00687 fcb->data = (unsigned char *)G_calloc(G__.window.cols,
00688 G_raster_size(fcb->map_type));
00689
00690 G__reallocate_null_buf();
00691
00692
00693 if (open_mode == OPEN_NEW_COMPRESSED && !COMPRESSION_TYPE)
00694 COMPRESSION_TYPE = getenv("GRASS_INT_ZLIB") ? 2 : 1;
00695
00696
00697
00698
00699
00700
00701
00702
00703 G_copy((char *)&fcb->cellhd, (char *)&G__.window, sizeof(fcb->cellhd));
00704
00705 if (open_mode == OPEN_NEW_COMPRESSED && fcb->map_type == CELL_TYPE) {
00706 fcb->row_ptr = G_calloc(fcb->cellhd.rows + 1, sizeof(off_t));
00707 G_zero(fcb->row_ptr, (fcb->cellhd.rows + 1) * sizeof(off_t));
00708 G__write_row_ptrs(fd);
00709 fcb->cellhd.compressed = COMPRESSION_TYPE;
00710
00711 allocate_compress_buf(fd);
00712 fcb->nbytes = 1;
00713 G__reallocate_work_buf(sizeof(CELL));
00714 G__reallocate_mask_buf();
00715 G__reallocate_temp_buf();
00716 }
00717 else {
00718 fcb->nbytes = WRITE_NBYTES;
00719 if (open_mode == OPEN_NEW_COMPRESSED) {
00720 fcb->row_ptr = G_calloc(fcb->cellhd.rows + 1, sizeof(off_t));
00721 G_zero(fcb->row_ptr, (fcb->cellhd.rows + 1) * sizeof(off_t));
00722 G__write_row_ptrs(fd);
00723 fcb->cellhd.compressed = COMPRESSION_TYPE;
00724 }
00725 else
00726 fcb->cellhd.compressed = 0;
00727 G__reallocate_work_buf(fcb->nbytes);
00728 G__reallocate_mask_buf();
00729 G__reallocate_temp_buf();
00730
00731 if (fcb->map_type != CELL_TYPE) {
00732 G_quant_init(&(fcb->quant));
00733 }
00734
00735 if (open_mode == OPEN_NEW_RANDOM) {
00736 G_warning(_("Unable to write embedded null values "
00737 "for raster map open for random access"));
00738 if (fcb->map_type == CELL_TYPE)
00739 G_write_zeros(fd,
00740 (long)WRITE_NBYTES * fcb->cellhd.cols *
00741 fcb->cellhd.rows);
00742 else if (fcb->map_type == FCELL_TYPE) {
00743 if (G__random_f_initialize_0
00744 (fd, fcb->cellhd.rows, fcb->cellhd.cols) < 0)
00745 return -1;
00746 }
00747 else {
00748 if (G__random_d_initialize_0
00749 (fd, fcb->cellhd.rows, fcb->cellhd.cols) < 0)
00750 return -1;
00751 }
00752 }
00753 }
00754
00755
00756 fcb->name = map;
00757 fcb->mapset = mapset;
00758 fcb->temp_name = tempname;
00759
00760
00761 fcb->cur_row = 0;
00762
00763
00764 tempname = G_tempfile();
00765 null_fd = creat(tempname, 0666);
00766 if (null_fd < 0) {
00767 G_warning(_("G__open_raster_new(): no temp files available"));
00768 G_free(tempname);
00769 G_free(fcb->name);
00770 G_free(fcb->mapset);
00771 G_free(fcb->temp_name);
00772 close(fd);
00773 return -1;
00774 }
00775
00776 fcb->null_temp_name = tempname;
00777 close(null_fd);
00778
00779
00780 fcb->null_cur_row = 0;
00781
00782
00783 for (i = 0; i < NULL_ROWS_INMEM; i++)
00784 fcb->NULL_ROWS[i] = G__allocate_null_bits(fcb->cellhd.cols);
00785 fcb->min_null_row = (-1) * NULL_ROWS_INMEM;
00786 fcb->null_work_buf = G__allocate_null_bits(fcb->cellhd.cols);
00787
00788
00789
00790 if (fcb->map_type == CELL_TYPE)
00791 if ((fcb->want_histogram = G__.want_histogram))
00792 G_init_cell_stats(&fcb->statf);
00793
00794
00795 G_init_range(&fcb->range);
00796
00797 if (fcb->map_type != CELL_TYPE)
00798 G_init_fp_range(&fcb->fp_range);
00799
00800
00801 fcb->open_mode = open_mode;
00802 fcb->io_error = 0;
00803
00804 return fd;
00805 }
00806
00816 static int allocate_compress_buf(int fd)
00817 {
00818 struct fileinfo *fcb = &G__.fileinfo[fd];
00819 int n;
00820
00821 n = fcb->cellhd.cols * (sizeof(CELL) + 1) + 1;
00822 if (fcb->cellhd.compressed && fcb->map_type == CELL_TYPE &&
00823 (n > G__.compressed_buf_size)) {
00824 if (G__.compressed_buf_size <= 0)
00825 G__.compressed_buf = (unsigned char *)G_malloc(n);
00826 else
00827 G__.compressed_buf =
00828 (unsigned char *)G_realloc((char *)G__.compressed_buf, n);
00829 G__.compressed_buf_size = n;
00830 }
00831
00832 return 0;
00833 }
00834
00842 int G__reallocate_work_buf(int bytes_per_cell)
00843 {
00844 int n;
00845
00846 n = G__.window.cols * (bytes_per_cell + 1) + 1;
00847 if (n > G__.work_buf_size) {
00848 if (G__.work_buf_size <= 0)
00849 G__.work_buf = (unsigned char *)G_malloc(n);
00850 else
00851 G__.work_buf =
00852 (unsigned char *)G_realloc((char *)G__.work_buf, n);
00853 G__.work_buf_size = n;
00854 }
00855
00856 return 0;
00857 }
00858
00865 int G__reallocate_null_buf(void)
00866 {
00867 int n;
00868 n = (G__.window.cols + 1) * sizeof(char);
00869 if (n > G__.null_buf_size) {
00870 if (G__.null_buf_size <= 0)
00871 G__.null_buf = (char *)G_malloc(n);
00872 else
00873 G__.null_buf = (char *)G_realloc(G__.null_buf, n);
00874 G__.null_buf_size = n;
00875 }
00876
00877 return 0;
00878 }
00879
00885 int G__reallocate_mask_buf(void)
00886 {
00887 int n;
00888
00889 n = (G__.window.cols + 1) * sizeof(CELL);
00890 if (n > G__.mask_buf_size) {
00891 if (G__.mask_buf_size <= 0)
00892 G__.mask_buf = (CELL *) G_malloc(n);
00893 else
00894 G__.mask_buf = (CELL *) G_realloc((char *)G__.mask_buf, n);
00895 G__.mask_buf_size = n;
00896 }
00897
00898 return 0;
00899 }
00900
00906 int G__reallocate_temp_buf(void)
00907 {
00908 int n;
00909
00910 n = (G__.window.cols + 1) * sizeof(CELL);
00911 if (n > G__.temp_buf_size) {
00912 if (G__.temp_buf_size <= 0)
00913 G__.temp_buf = (CELL *) G_malloc(n);
00914 else
00915 G__.temp_buf = (CELL *) G_realloc((char *)G__.temp_buf, n);
00916 G__.temp_buf_size = n;
00917 }
00918
00919 return 0;
00920 }
00921
00922
00937 int G_set_fp_type(RASTER_MAP_TYPE map_type)
00938 {
00939 FP_TYPE_SET = 1;
00940 if (map_type != FCELL_TYPE && map_type != DCELL_TYPE) {
00941 G_warning(_("G_set_fp_type(): can only be called with FCELL_TYPE or DCELL_TYPE"));
00942 return -1;
00943 }
00944 FP_TYPE = map_type;
00945 if (map_type == DCELL_TYPE)
00946 FP_NBYTES = XDR_DOUBLE_NBYTES;
00947 else
00948 FP_NBYTES = XDR_FLOAT_NBYTES;
00949
00950 return 1;
00951 }
00952
00953
00954 #define FORMAT_FILE "f_format"
00955
00956
00969 int G_raster_map_is_fp(const char *name, const char *mapset)
00970 {
00971 char path[GPATH_MAX];
00972 const char *xmapset;
00973
00974 xmapset = G_find_cell2(name, mapset);
00975 if (!xmapset) {
00976 G_warning(_("Unable to find '%s' in '%s'"), name, mapset);
00977 return -1;
00978 }
00979 G__file_name(path, "fcell", name, xmapset);
00980 if (access(path, 0) == 0)
00981 return 1;
00982 G__file_name(path, "g3dcell", name, xmapset);
00983 if (access(path, 0) == 0)
00984 return 1;
00985
00986 return 0;
00987 }
00988
01001 RASTER_MAP_TYPE G_raster_map_type(const char *name, const char *mapset)
01002 {
01003 char path[GPATH_MAX];
01004 const char *xmapset;
01005
01006 xmapset = G_find_cell2(name, mapset);
01007 if (!xmapset) {
01008 if (mapset && *mapset)
01009 G_warning(_("Raster map <%s> not found in mapset <%s>"), name, mapset);
01010 else
01011 G_warning(_("Raster map <%s> not found"), name);
01012 return -1;
01013 }
01014 G__file_name(path, "fcell", name, xmapset);
01015
01016 if (access(path, 0) == 0)
01017 return G__check_fp_type(name, xmapset);
01018
01019 G__file_name(path, "g3dcell", name, xmapset);
01020
01021 if (access(path, 0) == 0)
01022 return DCELL_TYPE;
01023
01024 return CELL_TYPE;
01025 }
01026
01038 RASTER_MAP_TYPE G_get_raster_map_type(int fd)
01039 {
01040 struct fileinfo *fcb = &G__.fileinfo[fd];
01041
01042 return fcb->map_type;
01043 }
01044
01053 RASTER_MAP_TYPE G__check_fp_type(const char *name, const char *mapset)
01054 {
01055 char path[GPATH_MAX];
01056 struct Key_Value *format_keys;
01057 int in_stat;
01058 char *str, *str1;
01059 RASTER_MAP_TYPE map_type;
01060 const char *xmapset;
01061
01062 xmapset = G_find_cell2(name, mapset);
01063 if (!xmapset) {
01064 G_warning(_("Unable to find '%s' in '%s'"), name, mapset);
01065 return -1;
01066 }
01067 G__file_name_misc(path, "cell_misc", FORMAT_FILE, name, xmapset);
01068
01069 if (access(path, 0) != 0) {
01070 G_warning(_("Unable to find '%s'"), path);
01071 return -1;
01072 }
01073 format_keys = G_read_key_value_file(path, &in_stat);
01074 if (in_stat != 0) {
01075 G_warning(_("Unable to open '%s'"), path);
01076 return -1;
01077 }
01078 if ((str = G_find_key_value("type", format_keys)) != NULL) {
01079 G_strip(str);
01080 if (strcmp(str, "double") == 0)
01081 map_type = DCELL_TYPE;
01082 else if (strcmp(str, "float") == 0)
01083 map_type = FCELL_TYPE;
01084 else {
01085 G_warning(_("Invalid type: field '%s' in file '%s'"),
01086 str, path);
01087 G_free_key_value(format_keys);
01088 return -1;
01089 }
01090 }
01091 else {
01092 G_free_key_value(format_keys);
01093 return -1;
01094 }
01095
01096 if ((str1 = G_find_key_value("byte_order", format_keys)) != NULL) {
01097 G_strip(str1);
01098 if (strcmp(str1, "xdr") != 0)
01099 G_warning(_("Raster map <%s> is not xdr: byte_order: %s"),
01100 name, str);
01101
01102 }
01103 G_free_key_value(format_keys);
01104 return map_type;
01105 }
01106
01127 int G_open_raster_new(const char *name, RASTER_MAP_TYPE wr_type)
01128 {
01129 if (wr_type == CELL_TYPE)
01130 return G_open_cell_new(name);
01131
01132 G_set_fp_type(wr_type);
01133 return G_open_fp_cell_new(name);
01134 }
01135
01147 int G_open_raster_new_uncompressed(const char *name, RASTER_MAP_TYPE wr_type)
01148 {
01149 if (wr_type == CELL_TYPE)
01150 return G_open_cell_new_uncompressed(name);
01151
01152 G_set_fp_type(wr_type);
01153 return G_open_fp_cell_new_uncompressed(name);
01154 }
01155
01171 int G_set_quant_rules(int fd, struct Quant *q)
01172 {
01173 struct fileinfo *fcb = &G__.fileinfo[fd];
01174 CELL cell;
01175 DCELL dcell;
01176 struct Quant_table *p;
01177
01178 if (fcb->open_mode != OPEN_OLD) {
01179 G_warning(_("G_set_quant_rules() can be called only for "
01180 "raster maps opened for reading"));
01181 return -1;
01182 }
01183
01184 G_quant_init(&fcb->quant);
01185 if (q->truncate_only) {
01186 G_quant_truncate(&fcb->quant);
01187 return 0;
01188 }
01189 for (p = &(q->table[q->nofRules - 1]); p >= q->table; p--)
01190 G_quant_add_rule(&fcb->quant, p->dLow, p->dHigh, p->cLow, p->cHigh);
01191 if (G_quant_get_neg_infinite_rule(q, &dcell, &cell) > 0)
01192 G_quant_set_neg_infinite_rule(&fcb->quant, dcell, cell);
01193 if (G_quant_get_pos_infinite_rule(q, &dcell, &cell) > 0)
01194 G_quant_set_pos_infinite_rule(&fcb->quant, dcell, cell);
01195
01196 return 0;
01197 }