15 #include <rpc/types.h>
19 #include <sys/types.h>
22 #include <grass/config.h>
24 #include <grass/gis.h>
25 #include <grass/glocale.h>
27 static int allocate_compress_buf(
int);
29 static struct fileinfo *new_fileinfo(
int fd)
32 int newsize = oldsize;
45 for (i = oldsize; i < newsize; i++) {
72 static int G__open_raster_new(
const char *
name,
int open_mode);
105 G_warning(_(
"Unable to open raster map <%s@%s>"), name, mapset);
155 const char *r_mapset;
156 struct Cell_head cellhd;
161 RASTER_MAP_TYPE MAP_TYPE;
162 struct Reclass reclass;
171 G_warning(_(
"Unable to find <%s@%s>"), name, mapset);
179 switch (reclass_flag) {
185 r_name = reclass.name;
186 r_mapset = reclass.mapset;
188 G_warning(_(
"Unable to open raster map <%s@%s> since it is a reclass "
189 "of raster map <%s@%s> which does not exist"),
190 name, mapset, r_name, r_mapset);
207 if (MAP_TYPE == CELL_TYPE)
210 CELL_nbytes = cellhd.format + 1;
211 if (CELL_nbytes < 1) {
212 G_warning(_(
"Raster map <%s@%s>: format field in header file invalid"),
219 G_warning(_(
"Raster map <%s@%s> is in different projection than current region. "
220 "Found raster map <%s@%s>, should be <%s>."),
226 G_warning(_(
"Raster map <%s@%s> is in different zone (%d) than current region (%d)"),
227 name, mapset, cellhd.zone,
G__.
window.zone);
232 if (MAP_TYPE == CELL_TYPE && (
unsigned int) CELL_nbytes >
sizeof(CELL)) {
233 G_warning(_(
"Raster map <%s@%s>: bytes per cell (%d) too large"),
234 name, mapset, CELL_nbytes);
239 if (MAP_TYPE == FCELL_TYPE) {
240 strcpy(cell_dir,
"fcell");
241 INTERN_SIZE =
sizeof(FCELL);
244 else if (MAP_TYPE == DCELL_TYPE) {
245 strcpy(cell_dir,
"fcell");
246 INTERN_SIZE =
sizeof(DCELL);
251 strcpy(cell_dir,
"cell");
252 INTERN_SIZE =
sizeof(CELL);
253 MAP_NBYTES = CELL_nbytes;
260 fd = open(G_DEV_NULL, O_RDONLY);
262 G_warning(_(
"map <%s@%s> is a GDAL link but GRASS is compiled without GDAL support"),
274 fcb = new_fileinfo(fd);
279 G_copy((
char *)&fcb->
cellhd, (
char *)&cellhd,
sizeof(cellhd));
293 char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
330 fcb->
data = (
unsigned char *)G_calloc(fcb->
cellhd.cols, MAP_NBYTES);
340 allocate_compress_buf(fd);
367 static int WRITE_NBYTES =
sizeof(CELL);
371 static int NBYTES =
sizeof(CELL);
375 static RASTER_MAP_TYPE WRITE_MAP_TYPE = CELL_TYPE;
379 static int COMPRESSION_TYPE = 0;
381 #define FP_NBYTES G__.fp_nbytes
383 #define FP_TYPE G__.fp_type
385 static int FP_TYPE_SET = 0;
388 static char cell_dir[100];
419 WRITE_MAP_TYPE = CELL_TYPE;
420 strcpy(cell_dir,
"cell");
422 WRITE_NBYTES = NBYTES;
440 WRITE_MAP_TYPE = CELL_TYPE;
442 WRITE_NBYTES = NBYTES;
443 strcpy(cell_dir,
"cell");
459 WRITE_MAP_TYPE = CELL_TYPE;
460 strcpy(cell_dir,
"cell");
462 WRITE_NBYTES = NBYTES;
500 if (WRITE_MAP_TYPE == CELL_TYPE) {
504 if ((
unsigned int) NBYTES >
sizeof(CELL))
505 NBYTES =
sizeof(CELL);
523 for (i = 0; i <
sizeof(CELL); i++)
526 return sizeof(CELL) - 1;
552 if (
getenv(
"GRASS_FP_DOUBLE")) {
564 strcpy(cell_dir,
"fcell");
582 if (
getenv(
"GRASS_FP_DOUBLE")) {
594 strcpy(cell_dir,
"fcell");
599 clean_check_raster_name(
const char *inmap,
char **outmap,
char **outmapset)
609 if ((ptr = strpbrk(buf,
"@")) !=
NULL) {
613 if ((status = strcmp(ptr, *outmapset))) {
630 static int G__open_raster_new(
const char *
name,
int open_mode)
632 char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
641 if (strcmp(xmapset,
G_mapset()) != 0)
642 G_fatal_error(_(
"Raster map <%s> is not in the current mapset (%s)"),
649 G_warning(_(
"<%s> is an illegal file name"),
654 if (clean_check_raster_name(name, &map, &mapset) != 0) {
664 fd = creat(tempname, 0666);
666 G_warning(_(
"G__open_raster_new(): no temp files available"));
673 fcb = new_fileinfo(fd);
694 COMPRESSION_TYPE =
getenv(
"GRASS_INT_ZLIB") ? 2 : 1;
706 fcb->
row_ptr = G_calloc(fcb->
cellhd.rows + 1,
sizeof(off_t));
709 fcb->
cellhd.compressed = COMPRESSION_TYPE;
711 allocate_compress_buf(fd);
718 fcb->
nbytes = WRITE_NBYTES;
720 fcb->
row_ptr = G_calloc(fcb->
cellhd.rows + 1,
sizeof(off_t));
723 fcb->
cellhd.compressed = COMPRESSION_TYPE;
726 fcb->
cellhd.compressed = 0;
736 G_warning(_(
"Unable to write embedded null values "
737 "for raster map open for random access"));
740 (
long)WRITE_NBYTES * fcb->
cellhd.cols *
742 else if (fcb->
map_type == FCELL_TYPE) {
765 null_fd = creat(tempname, 0666);
767 G_warning(_(
"G__open_raster_new(): no temp files available"));
816 static int allocate_compress_buf(
int fd)
821 n = fcb->
cellhd.cols * (
sizeof(CELL) + 1) + 1;
846 n =
G__.
window.cols * (bytes_per_cell + 1) + 1;
852 (
unsigned char *)G_realloc((
char *)
G__.
work_buf, n);
868 n = (
G__.
window.cols + 1) *
sizeof(
char);
889 n = (
G__.
window.cols + 1) *
sizeof(CELL);
910 n = (
G__.
window.cols + 1) *
sizeof(CELL);
940 if (map_type != FCELL_TYPE && map_type != DCELL_TYPE) {
941 G_warning(_(
"G_set_fp_type(): can only be called with FCELL_TYPE or DCELL_TYPE"));
945 if (map_type == DCELL_TYPE)
954 #define FORMAT_FILE "f_format"
971 char path[GPATH_MAX];
976 G_warning(_(
"Unable to find '%s' in '%s'"), name, mapset);
980 if (access(path, 0) == 0)
983 if (access(path, 0) == 0)
1003 char path[GPATH_MAX];
1004 const char *xmapset;
1008 if (mapset && *mapset)
1009 G_warning(_(
"Raster map <%s> not found in mapset <%s>"), name, mapset);
1011 G_warning(_(
"Raster map <%s> not found"), name);
1016 if (access(path, 0) == 0)
1021 if (access(path, 0) == 0)
1055 char path[GPATH_MAX];
1056 struct Key_Value *format_keys;
1059 RASTER_MAP_TYPE map_type;
1060 const char *xmapset;
1064 G_warning(_(
"Unable to find '%s' in '%s'"), name, mapset);
1069 if (access(path, 0) != 0) {
1070 G_warning(_(
"Unable to find '%s'"), path);
1075 G_warning(_(
"Unable to open '%s'"), path);
1080 if (strcmp(str,
"double") == 0)
1081 map_type = DCELL_TYPE;
1082 else if (strcmp(str,
"float") == 0)
1083 map_type = FCELL_TYPE;
1085 G_warning(_(
"Invalid type: field '%s' in file '%s'"),
1098 if (strcmp(str1,
"xdr") != 0)
1099 G_warning(_(
"Raster map <%s> is not xdr: byte_order: %s"),
1129 if (wr_type == CELL_TYPE)
1149 if (wr_type == CELL_TYPE)
1176 struct Quant_table *p;
1179 G_warning(_(
"G_set_quant_rules() can be called only for "
1180 "raster maps opened for reading"));
1185 if (q->truncate_only) {
1189 for (p = &(q->table[q->nofRules - 1]); p >= q->table; p--)