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);
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)
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);
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;
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];
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;
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--)
char * G_mapset(void)
current mapset name
char * G_find_key_value(const char *key, const struct Key_Value *kv)
Find given key.
void G_free(void *buf)
Free allocated memory.
int G_open_fp_cell_new(const char *name)
Opens new fcell file in a database.
int G_open_cell_new(const char *name)
Opens a new cell file in a database (compressed)
char * G_store(const char *s)
Copy string to allocated memory.
void G_quant_set_neg_infinite_rule(struct Quant *q, DCELL dLeft, CELL c)
int G_cellvalue_format(CELL v)
Get cell value format.
char * G__file_name_misc(char *path, const char *dir, const char *element, const char *name, const char *mapset)
int G_get_cellhd(const char *name, const char *mapset, struct Cell_head *cellhd)
Read the raster header.
int G_quant_get_neg_infinite_rule(const struct Quant *q, DCELL *dLeft, CELL *c)
RASTER_MAP_TYPE G__check_fp_type(const char *name, const char *mapset)
Determines whether the floating points cell file has double or float type.
int G_read_quant(const char *name, const char *mapset, struct Quant *quant)
reads quantization rules for "name" in "mapset" and stores them in the quantization structure "quant"...
int G_copy(void *a, const void *b, int n)
Copies n bytes starting at address b into address a.
int G_open_cell_old(const char *name, const char *mapset)
Open an existing integer raster map (cell)
int G__random_f_initialize_0(int fd, int nofRows, int nofCols)
unsigned char * null_work_buf
int G_free_key_value(struct Key_Value *kv)
Free allocated Key_Value structure.
RASTER_MAP_TYPE G_get_raster_map_type(int fd)
Determine raster type from descriptor.
char * G_tempfile(void)
Returns a temporary file name.
int G__reallocate_null_buf(void)
Allocate/enlarge the null data buffer needed by get_map_row() and for conversion in put_row...
int G_open_raster_new_uncompressed(const char *name, RASTER_MAP_TYPE wr_type)
Opens a new raster map (uncompressed)
int G_raster_map_is_fp(const char *name, const char *mapset)
Check if raster map is floating-point.
#define OPEN_NEW_UNCOMPRESSED
int G_set_fp_type(RASTER_MAP_TYPE map_type)
Set raster map floating-point data format.
int G_zero(void *buf, int i)
Zero out a buffer, buf, of length i.
int G__reallocate_temp_buf(void)
Allocate/enlarge the temporary buffer needed by G_get_raster_row[_nomask].
int G_write_zeros(int fd, size_t n)
Writes n bytes of 9 to file descriptor fd
int G_set_quant_rules(int fd, struct Quant *q)
Sets quant translation rules for raster map opened for reading.
char * G__file_name(char *path, const char *element, const char *name, const char *mapset)
Builds full path names to GIS data files.
struct Key_Value * G_read_key_value_file(const char *file, int *stat)
Read key/values pairs from file.
char * G_find_cell2(const char *name, const char *mapset)
find a raster map (look but don't touch)
size_t G_raster_size(RASTER_MAP_TYPE data_type)
Returns size of a raster CELL in bytes.
int G_strip(char *buf)
Removes all leading and trailing white space from string.
int G_set_cell_format(int n)
Sets the format for subsequent opens on new integer cell files (uncompressed and random only)...
unsigned char * G__allocate_null_bits(int cols)
Allocates memory for null bits.
int G__open_cell_old(const char *name, const char *mapset)
Lower level function, open cell files, supercell files, and the MASK file.
int G_want_histogram(int flag)
Save histogram for newly create raster map (cell)
int G_open_old(const char *element, const char *name, const char *mapset)
Open a database file for reading.
char buf[GNAME_MAX+sizeof(G3D_DIRECTORY)+2]
int G__random_d_initialize_0(int fd, int nofRows, int nofCols)
void G_quant_add_rule(struct Quant *q, DCELL dLow, DCELL dHigh, CELL cLow, CELL cHigh)
int G_init_range(struct Range *range)
initialize range structure
struct fileinfo * fileinfo
G_warning("category support for [%s] in mapset [%s] %s", name, mapset, type)
int G_open_cell_new_random(const char *name)
Opens a new cell file in a database (random mode)
unsigned char * compressed_buf
int G_open_fp_cell_new_uncompressed(const char *name)
Opens new fcell file in a database (uncompressed)
int G_open_cell_new_uncompressed(const char *name)
Opens a new cell file in a database (uncompressed)
int G_quant_init(struct Quant *quant)
Initializes the q struct.
#define XDR_DOUBLE_NBYTES
int G__reallocate_mask_buf(void)
Allocate/enlarge the mask buffer needed by get_map_row()
struct GDAL_link * G_get_gdal_link(const char *name, const char *mapset)
int G__create_window_mapping(int fd)
Create window mapping.
int G__make_mapset_element(const char *p_element)
Create element in the current mapset.
int G__reallocate_work_buf(int bytes_per_cell)
Allocate/enlarge the work data buffer needed by get_map_row and put_map_row()
char * G__projection_name(int n)
int G_get_reclass(const char *name, const char *mapset, struct Reclass *reclass)
int G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
int G__init_window(void)
Initialize window.
RASTER_MAP_TYPE G_raster_map_type(const char *name, const char *mapset)
Determine raster data type.
void G_quant_set_pos_infinite_rule(struct Quant *q, DCELL dRight, CELL c)
int G_init_cell_stats(struct Cell_stats *s)
initialize cell stats
int G_legal_filename(const char *s)
Check for legal database file name.
unsigned char * NULL_ROWS[NULL_ROWS_INMEM]
int G_open_raster_new(const char *name, RASTER_MAP_TYPE wr_type)
Opens a new raster map.
int G_quant_get_pos_infinite_rule(const struct Quant *q, DCELL *dRight, CELL *c)
int G__check_for_auto_masking(void)
Checks for auto masking.
#define OPEN_NEW_COMPRESSED
int G_quant_truncate(struct Quant *quant)
sets the quant for q rules to perform simple truncation on floats.
int G__name_is_fully_qualified(const char *fullname, char *name, char *mapset)
Check if map name is fully qualified (map @ mapset)