GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
opencell.c
Go to the documentation of this file.
1 
15 #include <rpc/types.h>
16 #include <rpc/xdr.h>
17 #include <unistd.h>
18 #include <string.h>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <fcntl.h>
22 #include <grass/config.h>
23 #include "G.h"
24 #include <grass/gis.h>
25 #include <grass/glocale.h>
26 
27 static int allocate_compress_buf(int);
28 
29 static struct fileinfo *new_fileinfo(int fd)
30 {
31  int oldsize = G__.fileinfo_count;
32  int newsize = oldsize;
33  int i;
34 
35  if (fd < oldsize)
36  return &G__.fileinfo[fd];
37 
38  newsize *= 2;
39  if (newsize <= fd)
40  newsize = fd + 20;
41 
42  G__.fileinfo = G_realloc(G__.fileinfo, newsize * sizeof(struct fileinfo));
43 
44  /* Mark all cell files as closed */
45  for (i = oldsize; i < newsize; i++) {
46  memset(&G__.fileinfo[i], 0, sizeof(struct fileinfo));
47  G__.fileinfo[i].open_mode = -1;
48  }
49 
50  G__.fileinfo_count = newsize;
51 
52  return &G__.fileinfo[fd];
53 }
54 
55 
72 static int G__open_raster_new(const char *name, int open_mode);
73 
100 int G_open_cell_old(const char *name, const char *mapset)
101 {
102  int fd;
103 
104  if ((fd = G__open_cell_old(name, mapset)) < 0) {
105  G_warning(_("Unable to open raster map <%s@%s>"), name, mapset);
106  return fd;
107  }
108 
109  /* turn on auto masking, if not already on */
111  /*
112  if(G__.auto_mask <= 0)
113  G__.mask_buf = G_allocate_cell_buf();
114  now we don't ever free it!, so no need to allocate it (Olga)
115  */
116  /* mask_buf is used for reading MASK file when mask is set and
117  for reading map rows when the null file doesn't exist */
118 
119  return fd;
120 }
121 
149 int G__open_cell_old(const char *name, const char *mapset)
150 {
151  struct fileinfo *fcb;
152  int fd;
153  char cell_dir[100];
154  const char *r_name;
155  const char *r_mapset;
156  struct Cell_head cellhd;
157  int CELL_nbytes = 0; /* bytes per cell in CELL map */
158  int INTERN_SIZE;
159  int reclass_flag, i;
160  int MAP_NBYTES;
161  RASTER_MAP_TYPE MAP_TYPE;
162  struct Reclass reclass;
163  const char *xmapset;
164  struct GDAL_link *gdal;
165 
166  /* make sure window is set */
167  G__init_window();
168 
169  xmapset = G_find_cell2(name, mapset);
170  if (!xmapset) {
171  G_warning(_("Unable to find <%s@%s>"), name, mapset);
172  return -1;
173  }
174  mapset = xmapset;
175 
176  /* Check for reclassification */
177  reclass_flag = G_get_reclass(name, mapset, &reclass);
178 
179  switch (reclass_flag) {
180  case 0:
181  r_name = name;
182  r_mapset = mapset;
183  break;
184  case 1:
185  r_name = reclass.name;
186  r_mapset = reclass.mapset;
187  if (G_find_cell2(r_name, r_mapset) == NULL) {
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);
191  return -1;
192  }
193  break;
194  default: /* Error reading cellhd/reclass file */
195  return -1;
196  }
197 
198  /* read the cell header */
199  if (G_get_cellhd(r_name, r_mapset, &cellhd) < 0)
200  return -1;
201 
202  /* now check the type */
203  MAP_TYPE = G_raster_map_type(r_name, r_mapset);
204  if (MAP_TYPE < 0)
205  return -1;
206 
207  if (MAP_TYPE == CELL_TYPE)
208  /* set the number of bytes for CELL map */
209  {
210  CELL_nbytes = cellhd.format + 1;
211  if (CELL_nbytes < 1) {
212  G_warning(_("Raster map <%s@%s>: format field in header file invalid"),
213  r_name, r_mapset);
214  return -1;
215  }
216  }
217 
218  if (cellhd.proj != G__.window.proj) {
219  G_warning(_("Raster map <%s@%s> is in different projection than current region. "
220  "Found raster map <%s@%s>, should be <%s>."),
221  name, mapset, name, G__projection_name(cellhd.proj),
223  return -1;
224  }
225  if (cellhd.zone != G__.window.zone) {
226  G_warning(_("Raster map <%s@%s> is in different zone (%d) than current region (%d)"),
227  name, mapset, cellhd.zone, G__.window.zone);
228  return -1;
229  }
230 
231  /* when map is int warn if too large cell size */
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);
235  return -1;
236  }
237 
238  /* record number of bytes per cell */
239  if (MAP_TYPE == FCELL_TYPE) {
240  strcpy(cell_dir, "fcell");
241  INTERN_SIZE = sizeof(FCELL);
242  MAP_NBYTES = XDR_FLOAT_NBYTES;
243  }
244  else if (MAP_TYPE == DCELL_TYPE) {
245  strcpy(cell_dir, "fcell");
246  INTERN_SIZE = sizeof(DCELL);
247  MAP_NBYTES = XDR_DOUBLE_NBYTES;
248  }
249  else { /* integer */
250 
251  strcpy(cell_dir, "cell");
252  INTERN_SIZE = sizeof(CELL);
253  MAP_NBYTES = CELL_nbytes;
254  }
255 
256  gdal = G_get_gdal_link(r_name, r_mapset);
257  if (gdal) {
258 #ifdef HAVE_GDAL
259  /* dummy descriptor to reserve the fileinfo slot */
260  fd = open(G_DEV_NULL, O_RDONLY);
261 #else
262  G_warning(_("map <%s@%s> is a GDAL link but GRASS is compiled without GDAL support"),
263  r_name, r_mapset);
264  return -1;
265 #endif
266  }
267  else
268  /* now actually open file for reading */
269  fd = G_open_old(cell_dir, r_name, r_mapset);
270 
271  if (fd < 0)
272  return -1;
273 
274  fcb = new_fileinfo(fd);
275 
276  fcb->map_type = MAP_TYPE;
277 
278  /* Save cell header */
279  G_copy((char *)&fcb->cellhd, (char *)&cellhd, sizeof(cellhd));
280 
281  /* allocate null bitstream buffers for reading null rows */
282  for (i = 0; i < NULL_ROWS_INMEM; i++)
283  fcb->NULL_ROWS[i] = G__allocate_null_bits(G__.window.cols);
284  fcb->null_work_buf = G__allocate_null_bits(fcb->cellhd.cols);
285  /* initialize : no NULL rows in memory */
286  fcb->min_null_row = (-1) * NULL_ROWS_INMEM;
287 
288  /* mark closed */
289  fcb->open_mode = -1;
290 
291  /* save name and mapset */
292  {
293  char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
294 
295  if (G__name_is_fully_qualified(name, xname, xmapset))
296  fcb->name = G_store(xname);
297  else
298  fcb->name = G_store(name);
299  }
300  fcb->mapset = G_store(mapset);
301 
302  /* mark no data row in memory */
303  fcb->cur_row = -1;
304  /* fcb->null_cur_row is not used for reading, only for writing */
305  fcb->null_cur_row = -1;
306 
307  /* if reclass, copy reclass structure */
308  if ((fcb->reclass_flag = reclass_flag))
309  G_copy(&fcb->reclass, &reclass, sizeof(reclass));
310 
311  fcb->gdal = gdal;
312  if (!gdal)
313  /* check for compressed data format, making initial reads if necessary */
314  if (G__check_format(fd) < 0) {
315  close(fd); /* warning issued by check_format() */
316  return -1;
317  }
318 
319  /* create the mapping from cell file to window */
321 
322  /*
323  * allocate the data buffer
324  * number of bytes per cell is cellhd.format+1
325  */
326 
327  /* for reading fcb->data is allocated to be fcb->cellhd.cols * fcb->nbytes
328  (= XDR_FLOAT/DOUBLE_NBYTES) and G__.work_buf to be G__.window.cols *
329  sizeof(CELL or DCELL or FCELL) */
330  fcb->data = (unsigned char *)G_calloc(fcb->cellhd.cols, MAP_NBYTES);
331 
332  G__reallocate_work_buf(INTERN_SIZE);
336  /* work_buf is used as intermediate buf for conversions */
337  /*
338  * allocate/enlarge the compressed data buffer needed by get_map_row()
339  */
340  allocate_compress_buf(fd);
341 
342  /* initialize/read in quant rules for float point maps */
343  if (fcb->map_type != CELL_TYPE) {
344  if (fcb->reclass_flag)
345  G_read_quant(fcb->reclass.name, fcb->reclass.mapset,
346  &(fcb->quant));
347  else
348  G_read_quant(fcb->name, fcb->mapset, &(fcb->quant));
349  }
350 
351  /* now mark open for read: this must follow create_window_mapping() */
352  fcb->open_mode = OPEN_OLD;
353  fcb->io_error = 0;
354  fcb->map_type = MAP_TYPE;
355  fcb->nbytes = MAP_NBYTES;
356  fcb->null_file_exists = -1;
357 
358  if (fcb->map_type != CELL_TYPE)
359  xdrmem_create(&fcb->xdrstream, (caddr_t) fcb->data,
360  (u_int) (fcb->nbytes * fcb->cellhd.cols), XDR_DECODE);
361 
362  return fd;
363 }
364 
365 /*****************************************************************/
366 
367 static int WRITE_NBYTES = sizeof(CELL);
368 
369 /* bytes per cell for current map */
370 
371 static int NBYTES = sizeof(CELL);
372 
373 /* bytes per cell for writing integer maps */
374 
375 static RASTER_MAP_TYPE WRITE_MAP_TYPE = CELL_TYPE;
376 
377 /* a type of current map */
378 
379 static int COMPRESSION_TYPE = 0;
380 
381 #define FP_NBYTES G__.fp_nbytes
382 /* bytes per cell for writing floating point maps */
383 #define FP_TYPE G__.fp_type
384 /* a type of floating maps to be open */
385 static int FP_TYPE_SET = 0; /* wether or not the f.p. type was set explicitly
386  by calling G_set_fp_type() */
387 
388 static char cell_dir[100];
389 
417 int G_open_cell_new(const char *name)
418 {
419  WRITE_MAP_TYPE = CELL_TYPE;
420  strcpy(cell_dir, "cell");
421  /* bytes per cell for current map */
422  WRITE_NBYTES = NBYTES;
423  return G__open_raster_new(name, OPEN_NEW_COMPRESSED);
424 }
425 
438 int G_open_cell_new_random(const char *name)
439 {
440  WRITE_MAP_TYPE = CELL_TYPE;
441  /* bytes per cell for current map */
442  WRITE_NBYTES = NBYTES;
443  strcpy(cell_dir, "cell");
444  return G__open_raster_new(name, OPEN_NEW_RANDOM);
445 }
446 
458 {
459  WRITE_MAP_TYPE = CELL_TYPE; /* a type of current map */
460  strcpy(cell_dir, "cell");
461  /* bytes per cell for current map */
462  WRITE_NBYTES = NBYTES;
463  return G__open_raster_new(name, OPEN_NEW_UNCOMPRESSED);
464 }
465 
477 {
479 
480  return 0;
481 }
482 
498 /* sets the format for integer raster map */
499 {
500  if (WRITE_MAP_TYPE == CELL_TYPE) {
501  NBYTES = n + 1;
502  if (NBYTES <= 0)
503  NBYTES = 1;
504  if ((unsigned int) NBYTES > sizeof(CELL))
505  NBYTES = sizeof(CELL);
506  }
507 
508  return 0;
509 }
510 
519 {
520  unsigned int i;
521 
522  if (v >= 0)
523  for (i = 0; i < sizeof(CELL); i++)
524  if (!(v /= 256))
525  return i;
526  return sizeof(CELL) - 1;
527 }
528 
546 int G_open_fp_cell_new(const char *name)
547 {
548  /* use current float. type for writing float point maps */
549  /* if the FP type was NOT explicitly set by G_set_fp_type()
550  use environment variable */
551  if (!FP_TYPE_SET) {
552  if (getenv("GRASS_FP_DOUBLE")) {
553  FP_TYPE = DCELL_TYPE;
555  }
556  else {
557  FP_TYPE = FCELL_TYPE;
559  }
560  }
561  WRITE_MAP_TYPE = FP_TYPE;
562  WRITE_NBYTES = FP_NBYTES;
563 
564  strcpy(cell_dir, "fcell");
565  return G__open_raster_new(name, OPEN_NEW_COMPRESSED);
566 }
567 
579 {
580  /* use current float. type for writing float point maps */
581  if (!FP_TYPE_SET) {
582  if (getenv("GRASS_FP_DOUBLE")) {
583  FP_TYPE = DCELL_TYPE;
585  }
586  else {
587  FP_TYPE = FCELL_TYPE;
589  }
590  }
591  WRITE_MAP_TYPE = FP_TYPE;
592  WRITE_NBYTES = FP_NBYTES;
593 
594  strcpy(cell_dir, "fcell");
595  return G__open_raster_new(name, OPEN_NEW_UNCOMPRESSED);
596 }
597 
598 static int
599 clean_check_raster_name(const char *inmap, char **outmap, char **outmapset)
600 {
601  /* Remove mapset part of name if exists. Also, if mapset
602  * part exists, make sure it matches current mapset.
603  */
604  int status = 0;
605  char *ptr;
606  char *buf;
607 
608  buf = G_store(inmap);
609  if ((ptr = strpbrk(buf, "@")) != NULL) {
610  *ptr = '\0';
611  ptr++;
612  *outmapset = G_store(G_mapset());
613  if ((status = strcmp(ptr, *outmapset))) {
614  G_free(buf);
615  G_free(*outmapset);
616  }
617  else {
618  *outmap = G_store(buf);
619  G_free(buf);
620  }
621  }
622  else {
623  *outmap = buf;
624  *outmapset = G_store(G_mapset());
625  }
626  return status;
627 }
628 
629 /* opens a f-cell or cell file depending on WRITE_MAP_TYPE */
630 static int G__open_raster_new(const char *name, int open_mode)
631 {
632  char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
633  struct fileinfo *fcb;
634  int i, null_fd, fd;
635  char *tempname;
636  char *map;
637  char *mapset;
638 
639  /* check for fully-qualfied name */
640  if (G__name_is_fully_qualified(name, xname, xmapset)) {
641  if (strcmp(xmapset, G_mapset()) != 0)
642  G_fatal_error(_("Raster map <%s> is not in the current mapset (%s)"),
643  name, G_mapset());
644  name = xname;
645  }
646 
647  /* check for legal grass name */
648  if (G_legal_filename(name) < 0) {
649  G_warning(_("<%s> is an illegal file name"),
650  name);
651  return -1;
652  }
653 
654  if (clean_check_raster_name(name, &map, &mapset) != 0) {
655  G_warning(_("<%s>: bad mapset"), name);
656  return -1;
657  }
658 
659  /* make sure window is set */
660  G__init_window();
661 
662  /* open a tempfile name */
663  tempname = G_tempfile();
664  fd = creat(tempname, 0666);
665  if (fd < 0) {
666  G_warning(_("No temp files available: %s"), strerror(errno));
667  G_free(tempname);
668  G_free(map);
669  G_free(mapset);
670  return -1;
671  }
672 
673  fcb = new_fileinfo(fd);
674  /*
675  * since we are bypassing the normal open logic
676  * must create the cell element
677  */
678  G__make_mapset_element(cell_dir);
679 
680  /* mark closed */
681  fcb->map_type = WRITE_MAP_TYPE;
682  fcb->open_mode = -1;
683 
684  /* for writing fcb->data is allocated to be G__.window.cols *
685  sizeof(CELL or DCELL or FCELL) and G__.work_buf to be G__.window.cols *
686  fcb->nbytes (= XDR_FLOAT/DOUBLE_NBYTES) */
687  fcb->data = (unsigned char *)G_calloc(G__.window.cols,
688  G_raster_size(fcb->map_type));
689 
691  /* we need null buffer to automatically write embeded nulls in put_row */
692 
693  if (open_mode == OPEN_NEW_COMPRESSED && !COMPRESSION_TYPE)
694  COMPRESSION_TYPE = getenv("GRASS_INT_ZLIB") ? 2 : 1;
695 
696  /*
697  * copy current window into cell header
698  * set format to cell/supercell
699  * for compressed writing
700  * allocate space to hold the row address array
701  * allocate/enlarge both the compress_buf and the work_buf
702  */
703  G_copy((char *)&fcb->cellhd, (char *)&G__.window, sizeof(fcb->cellhd));
704 
705  if (open_mode == OPEN_NEW_COMPRESSED && fcb->map_type == CELL_TYPE) {
706  fcb->row_ptr = G_calloc(fcb->cellhd.rows + 1, sizeof(off_t));
707  G_zero(fcb->row_ptr, (fcb->cellhd.rows + 1) * sizeof(off_t));
708  G__write_row_ptrs(fd);
709  fcb->cellhd.compressed = COMPRESSION_TYPE;
710 
711  allocate_compress_buf(fd);
712  fcb->nbytes = 1; /* to the minimum */
713  G__reallocate_work_buf(sizeof(CELL));
716  }
717  else {
718  fcb->nbytes = WRITE_NBYTES;
719  if (open_mode == OPEN_NEW_COMPRESSED) {
720  fcb->row_ptr = G_calloc(fcb->cellhd.rows + 1, sizeof(off_t));
721  G_zero(fcb->row_ptr, (fcb->cellhd.rows + 1) * sizeof(off_t));
722  G__write_row_ptrs(fd);
723  fcb->cellhd.compressed = COMPRESSION_TYPE;
724  }
725  else
726  fcb->cellhd.compressed = 0;
730 
731  if (fcb->map_type != CELL_TYPE) {
732  G_quant_init(&(fcb->quant));
733  }
734 
735  if (open_mode == OPEN_NEW_RANDOM) {
736  G_warning(_("Unable to write embedded null values "
737  "for raster map open for random access"));
738  if (fcb->map_type == CELL_TYPE)
739  G_write_zeros(fd,
740  (long)WRITE_NBYTES * fcb->cellhd.cols *
741  fcb->cellhd.rows);
742  else if (fcb->map_type == FCELL_TYPE) {
744  (fd, fcb->cellhd.rows, fcb->cellhd.cols) < 0)
745  return -1;
746  }
747  else {
749  (fd, fcb->cellhd.rows, fcb->cellhd.cols) < 0)
750  return -1;
751  }
752  }
753  }
754 
755  /* save name and mapset, and tempfile name */
756  fcb->name = map;
757  fcb->mapset = mapset;
758  fcb->temp_name = tempname;
759 
760  /* next row to be written (in order) is zero */
761  fcb->cur_row = 0;
762 
763  /* open a null tempfile name */
764  tempname = G_tempfile();
765  null_fd = creat(tempname, 0666);
766  if (null_fd < 0) {
767  G_warning(_("G__open_raster_new(): no temp files available"));
768  G_free(tempname);
769  G_free(fcb->name);
770  G_free(fcb->mapset);
771  G_free(fcb->temp_name);
772  close(fd);
773  return -1;
774  }
775 
776  fcb->null_temp_name = tempname;
777  close(null_fd);
778 
779  /* next row to be written (in order) is zero */
780  fcb->null_cur_row = 0;
781 
782  /* allocate null bitstream buffers for writing */
783  for (i = 0; i < NULL_ROWS_INMEM; i++)
784  fcb->NULL_ROWS[i] = G__allocate_null_bits(fcb->cellhd.cols);
785  fcb->min_null_row = (-1) * NULL_ROWS_INMEM;
786  fcb->null_work_buf = G__allocate_null_bits(fcb->cellhd.cols);
787 
788  /* init cell stats */
789  /* now works only for int maps */
790  if (fcb->map_type == CELL_TYPE)
791  if ((fcb->want_histogram = G__.want_histogram))
792  G_init_cell_stats(&fcb->statf);
793 
794  /* init range and if map is double/float init d/f_range */
795  G_init_range(&fcb->range);
796 
797  if (fcb->map_type != CELL_TYPE)
798  G_init_fp_range(&fcb->fp_range);
799 
800  /* mark file as open for write */
801  fcb->open_mode = open_mode;
802  fcb->io_error = 0;
803 
804  return fd;
805 }
806 
816 static int allocate_compress_buf(int fd)
817 {
818  struct fileinfo *fcb = &G__.fileinfo[fd];
819  int n;
820 
821  n = fcb->cellhd.cols * (sizeof(CELL) + 1) + 1;
822  if (fcb->cellhd.compressed && fcb->map_type == CELL_TYPE &&
823  (n > G__.compressed_buf_size)) {
824  if (G__.compressed_buf_size <= 0)
825  G__.compressed_buf = (unsigned char *)G_malloc(n);
826  else
828  (unsigned char *)G_realloc((char *)G__.compressed_buf, n);
830  }
831 
832  return 0;
833 }
834 
842 int G__reallocate_work_buf(int bytes_per_cell)
843 {
844  int n;
845 
846  n = G__.window.cols * (bytes_per_cell + 1) + 1;
847  if (n > G__.work_buf_size) {
848  if (G__.work_buf_size <= 0)
849  G__.work_buf = (unsigned char *)G_malloc(n);
850  else
851  G__.work_buf =
852  (unsigned char *)G_realloc((char *)G__.work_buf, n);
853  G__.work_buf_size = n;
854  }
855 
856  return 0;
857 }
858 
866 {
867  int n;
868  n = (G__.window.cols + 1) * sizeof(char);
869  if (n > G__.null_buf_size) {
870  if (G__.null_buf_size <= 0)
871  G__.null_buf = (char *)G_malloc(n);
872  else
873  G__.null_buf = (char *)G_realloc(G__.null_buf, n);
874  G__.null_buf_size = n;
875  }
876 
877  return 0;
878 }
879 
886 {
887  int n;
888 
889  n = (G__.window.cols + 1) * sizeof(CELL);
890  if (n > G__.mask_buf_size) {
891  if (G__.mask_buf_size <= 0)
892  G__.mask_buf = (CELL *) G_malloc(n);
893  else
894  G__.mask_buf = (CELL *) G_realloc((char *)G__.mask_buf, n);
895  G__.mask_buf_size = n;
896  }
897 
898  return 0;
899 }
900 
907 {
908  int n;
909 
910  n = (G__.window.cols + 1) * sizeof(CELL);
911  if (n > G__.temp_buf_size) {
912  if (G__.temp_buf_size <= 0)
913  G__.temp_buf = (CELL *) G_malloc(n);
914  else
915  G__.temp_buf = (CELL *) G_realloc((char *)G__.temp_buf, n);
916  G__.temp_buf_size = n;
917  }
918 
919  return 0;
920 }
921 
922 
937 int G_set_fp_type(RASTER_MAP_TYPE map_type)
938 {
939  FP_TYPE_SET = 1;
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"));
942  return -1;
943  }
944  FP_TYPE = map_type;
945  if (map_type == DCELL_TYPE)
947  else
949 
950  return 1;
951 }
952 
953 
954 #define FORMAT_FILE "f_format"
955 
956 
969 int G_raster_map_is_fp(const char *name, const char *mapset)
970 {
971  char path[GPATH_MAX];
972  const char *xmapset;
973 
974  xmapset = G_find_cell2(name, mapset);
975  if (!xmapset) {
976  G_warning(_("Unable to find '%s' in '%s'"), name, mapset);
977  return -1;
978  }
979  G__file_name(path, "fcell", name, xmapset);
980  if (access(path, 0) == 0)
981  return 1;
982  G__file_name(path, "g3dcell", name, xmapset);
983  if (access(path, 0) == 0)
984  return 1;
985 
986  return 0;
987 }
988 
1001 RASTER_MAP_TYPE G_raster_map_type(const char *name, const char *mapset)
1002 {
1003  char path[GPATH_MAX];
1004  const char *xmapset;
1005 
1006  xmapset = G_find_cell2(name, mapset);
1007  if (!xmapset) {
1008  if (mapset && *mapset)
1009  G_warning(_("Raster map <%s> not found in mapset <%s>"), name, mapset);
1010  else
1011  G_warning(_("Raster map <%s> not found"), name);
1012  return -1;
1013  }
1014  G__file_name(path, "fcell", name, xmapset);
1015 
1016  if (access(path, 0) == 0)
1017  return G__check_fp_type(name, xmapset);
1018 
1019  G__file_name(path, "g3dcell", name, xmapset);
1020 
1021  if (access(path, 0) == 0)
1022  return DCELL_TYPE;
1023 
1024  return CELL_TYPE;
1025 }
1026 
1038 RASTER_MAP_TYPE G_get_raster_map_type(int fd)
1039 {
1040  struct fileinfo *fcb = &G__.fileinfo[fd];
1041 
1042  return fcb->map_type;
1043 }
1044 
1053 RASTER_MAP_TYPE G__check_fp_type(const char *name, const char *mapset)
1054 {
1055  char path[GPATH_MAX];
1056  struct Key_Value *format_keys;
1057  int in_stat;
1058  char *str, *str1;
1059  RASTER_MAP_TYPE map_type;
1060  const char *xmapset;
1061 
1062  xmapset = G_find_cell2(name, mapset);
1063  if (!xmapset) {
1064  G_warning(_("Unable to find '%s' in '%s'"), name, mapset);
1065  return -1;
1066  }
1067  G__file_name_misc(path, "cell_misc", FORMAT_FILE, name, xmapset);
1068 
1069  if (access(path, 0) != 0) {
1070  G_warning(_("Unable to find '%s'"), path);
1071  return -1;
1072  }
1073  format_keys = G_read_key_value_file(path, &in_stat);
1074  if (in_stat != 0) {
1075  G_warning(_("Unable to open '%s'"), path);
1076  return -1;
1077  }
1078  if ((str = G_find_key_value("type", format_keys)) != NULL) {
1079  G_strip(str);
1080  if (strcmp(str, "double") == 0)
1081  map_type = DCELL_TYPE;
1082  else if (strcmp(str, "float") == 0)
1083  map_type = FCELL_TYPE;
1084  else {
1085  G_warning(_("Invalid type: field '%s' in file '%s'"),
1086  str, path);
1087  G_free_key_value(format_keys);
1088  return -1;
1089  }
1090  }
1091  else {
1092  G_free_key_value(format_keys);
1093  return -1;
1094  }
1095 
1096  if ((str1 = G_find_key_value("byte_order", format_keys)) != NULL) {
1097  G_strip(str1);
1098  if (strcmp(str1, "xdr") != 0)
1099  G_warning(_("Raster map <%s> is not xdr: byte_order: %s"),
1100  name, str);
1101  /* here read and translate byte order if not using xdr */
1102  }
1103  G_free_key_value(format_keys);
1104  return map_type;
1105 }
1106 
1127 int G_open_raster_new(const char *name, RASTER_MAP_TYPE wr_type)
1128 {
1129  if (wr_type == CELL_TYPE)
1130  return G_open_cell_new(name);
1131 
1132  G_set_fp_type(wr_type);
1133  return G_open_fp_cell_new(name);
1134 }
1135 
1147 int G_open_raster_new_uncompressed(const char *name, RASTER_MAP_TYPE wr_type)
1148 {
1149  if (wr_type == CELL_TYPE)
1150  return G_open_cell_new_uncompressed(name);
1151 
1152  G_set_fp_type(wr_type);
1153  return G_open_fp_cell_new_uncompressed(name);
1154 }
1155 
1171 int G_set_quant_rules(int fd, struct Quant *q)
1172 {
1173  struct fileinfo *fcb = &G__.fileinfo[fd];
1174  CELL cell;
1175  DCELL dcell;
1176  struct Quant_table *p;
1177 
1178  if (fcb->open_mode != OPEN_OLD) {
1179  G_warning(_("G_set_quant_rules() can be called only for "
1180  "raster maps opened for reading"));
1181  return -1;
1182  }
1183  /* copy all info from q to fcb->quant) */
1184  G_quant_init(&fcb->quant);
1185  if (q->truncate_only) {
1186  G_quant_truncate(&fcb->quant);
1187  return 0;
1188  }
1189  for (p = &(q->table[q->nofRules - 1]); p >= q->table; p--)
1190  G_quant_add_rule(&fcb->quant, p->dLow, p->dHigh, p->cLow, p->cHigh);
1191  if (G_quant_get_neg_infinite_rule(q, &dcell, &cell) > 0)
1192  G_quant_set_neg_infinite_rule(&fcb->quant, dcell, cell);
1193  if (G_quant_get_pos_infinite_rule(q, &dcell, &cell) > 0)
1194  G_quant_set_pos_infinite_rule(&fcb->quant, dcell, cell);
1195 
1196  return 0;
1197 }
char * G_mapset(void)
current mapset name
Definition: mapset.c:31
char * G_find_key_value(const char *key, const struct Key_Value *kv)
Find given key.
Definition: key_value1.c:128
int want_histogram
Definition: G.h:92
int nbytes
Definition: G.h:58
void G_free(void *buf)
Free allocated memory.
Definition: gis/alloc.c:142
int G_open_fp_cell_new(const char *name)
Opens new fcell file in a database.
Definition: opencell.c:546
int G_open_cell_new(const char *name)
Opens a new cell file in a database (compressed)
Definition: opencell.c:417
unsigned char * work_buf
Definition: G.h:87
int want_histogram
Definition: G.h:49
int null_buf_size
Definition: G.h:89
RASTER_MAP_TYPE map_type
Definition: G.h:59
char * G_store(const char *s)
Copy string to allocated memory.
Definition: store.c:32
void G_quant_set_neg_infinite_rule(struct Quant *q, DCELL dLeft, CELL c)
Definition: quant.c:595
char xmapset[512]
Definition: g3dcats.c:89
#define OPEN_NEW_RANDOM
Definition: G.h:103
int G_cellvalue_format(CELL v)
Get cell value format.
Definition: opencell.c:518
char * G__file_name_misc(char *path, const char *dir, const char *element, const char *name, const char *mapset)
Definition: file_name.c:70
char * null_buf
Definition: G.h:83
int G_get_cellhd(const char *name, const char *mapset, struct Cell_head *cellhd)
Read the raster header.
Definition: get_cellhd.c:45
int G__check_format(int fd)
Definition: gis/format.c:66
tuple q
Definition: forms.py:2019
char * name
Definition: G.h:63
int G_quant_get_neg_infinite_rule(const struct Quant *q, DCELL *dLeft, CELL *c)
Definition: quant.c:611
off_t * row_ptr
Definition: G.h:51
FILE * fd
Definition: g3dcolor.c:368
string name
Definition: render.py:1314
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.
Definition: opencell.c:1053
int G__write_row_ptrs(int fd)
Definition: gis/format.c:162
int G_read_quant(const char *name, const char *mapset, struct Quant *quant)
reads quantization rules for &quot;name&quot; in &quot;mapset&quot; and stores them in the quantization structure &quot;quant&quot;...
Definition: quant_rw.c:245
int G_copy(void *a, const void *b, int n)
Copies n bytes starting at address b into address a.
Definition: gis/copy.c:30
int G_open_cell_old(const char *name, const char *mapset)
Open an existing integer raster map (cell)
Definition: opencell.c:100
int io_error
Definition: G.h:65
#define FP_NBYTES
Definition: opencell.c:381
int G__random_f_initialize_0(int fd, int nofRows, int nofCols)
Definition: init_map.c:54
int fileinfo_count
Definition: G.h:94
int mask_buf_size
Definition: G.h:90
unsigned char * null_work_buf
Definition: G.h:68
char * temp_name
Definition: G.h:60
int compressed_buf_size
Definition: G.h:86
int G_free_key_value(struct Key_Value *kv)
Free allocated Key_Value structure.
Definition: key_value1.c:145
RASTER_MAP_TYPE G_get_raster_map_type(int fd)
Determine raster type from descriptor.
Definition: opencell.c:1038
unsigned char * data
Definition: G.h:57
char * G_tempfile(void)
Returns a temporary file name.
Definition: tempfile.c:47
int G__reallocate_null_buf(void)
Allocate/enlarge the null data buffer needed by get_map_row() and for conversion in put_row...
Definition: opencell.c:865
#define FORMAT_FILE
Definition: opencell.c:954
int G_open_raster_new_uncompressed(const char *name, RASTER_MAP_TYPE wr_type)
Opens a new raster map (uncompressed)
Definition: opencell.c:1147
#define FP_TYPE
Definition: opencell.c:383
struct GDAL_link * gdal
Definition: G.h:71
int work_buf_size
Definition: G.h:88
struct Range range
Definition: G.h:47
int temp_buf_size
Definition: G.h:91
struct Reclass reclass
Definition: G.h:45
char * getenv()
int G_raster_map_is_fp(const char *name, const char *mapset)
Check if raster map is floating-point.
Definition: opencell.c:969
#define OPEN_NEW_UNCOMPRESSED
Definition: G.h:102
int G_set_fp_type(RASTER_MAP_TYPE map_type)
Set raster map floating-point data format.
Definition: opencell.c:937
int G_zero(void *buf, int i)
Zero out a buffer, buf, of length i.
Definition: gis/zero.c:29
struct Cell_head window
Definition: G.h:78
int G__reallocate_temp_buf(void)
Allocate/enlarge the temporary buffer needed by G_get_raster_row[_nomask].
Definition: opencell.c:906
Definition: G.h:74
int G_write_zeros(int fd, size_t n)
Writes n bytes of 9 to file descriptor fd
Definition: writ_zeros.c:29
int G_set_quant_rules(int fd, struct Quant *q)
Sets quant translation rules for raster map opened for reading.
Definition: opencell.c:1171
struct Cell_stats statf
Definition: G.h:46
char * G__file_name(char *path, const char *element, const char *name, const char *mapset)
Builds full path names to GIS data files.
Definition: file_name.c:33
struct Key_Value * G_read_key_value_file(const char *file, int *stat)
Read key/values pairs from file.
Definition: key_value3.c:54
char * G_find_cell2(const char *name, const char *mapset)
find a raster map (look but don&#39;t touch)
Definition: find_cell.c:83
size_t G_raster_size(RASTER_MAP_TYPE data_type)
Returns size of a raster CELL in bytes.
Definition: alloc_cell.c:38
char * mapset
Definition: G.h:64
int G_strip(char *buf)
Removes all leading and trailing white space from string.
Definition: strings.c:389
int open_mode
Definition: G.h:43
int cur_row
Definition: G.h:54
flag
Definition: tools.py:1403
int G_set_cell_format(int n)
Sets the format for subsequent opens on new integer cell files (uncompressed and random only)...
Definition: opencell.c:497
unsigned char * G__allocate_null_bits(int cols)
Allocates memory for null bits.
Definition: alloc_cell.c:156
int G__open_cell_old(const char *name, const char *mapset)
Lower level function, open cell files, supercell files, and the MASK file.
Definition: opencell.c:149
char * null_temp_name
Definition: G.h:61
int G_want_histogram(int flag)
Save histogram for newly create raster map (cell)
Definition: opencell.c:476
int G_open_old(const char *element, const char *name, const char *mapset)
Open a database file for reading.
Definition: gis/open.c:147
char buf[GNAME_MAX+sizeof(G3D_DIRECTORY)+2]
Definition: g3drange.c:62
int G__random_d_initialize_0(int fd, int nofRows, int nofCols)
Definition: init_map.c:14
void G_quant_add_rule(struct Quant *q, DCELL dLow, DCELL dHigh, CELL cLow, CELL cHigh)
Definition: quant.c:655
int G_init_range(struct Range *range)
initialize range structure
Definition: range.c:579
int null_file_exists
Definition: G.h:62
return NULL
Definition: dbfopen.c:1394
struct fileinfo * fileinfo
Definition: G.h:95
XDR xdrstream
Definition: G.h:66
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)
Definition: opencell.c:438
#define OPEN_OLD
Definition: G.h:100
unsigned char * compressed_buf
Definition: G.h:85
int G_open_fp_cell_new_uncompressed(const char *name)
Opens new fcell file in a database (uncompressed)
Definition: opencell.c:578
int G_open_cell_new_uncompressed(const char *name)
Opens a new cell file in a database (uncompressed)
Definition: opencell.c:457
int G_quant_init(struct Quant *quant)
Initializes the q struct.
Definition: quant.c:422
struct Cell_head cellhd
Definition: G.h:44
int min_null_row
Definition: G.h:69
#define XDR_DOUBLE_NBYTES
Definition: G.h:10
int G__reallocate_mask_buf(void)
Allocate/enlarge the mask buffer needed by get_map_row()
Definition: opencell.c:885
Definition: G.h:41
CELL * temp_buf
Definition: G.h:84
struct GDAL_link * G_get_gdal_link(const char *name, const char *mapset)
Definition: gdal.c:155
int G__create_window_mapping(int fd)
Create window mapping.
Definition: window_map.c:36
#define NULL_ROWS_INMEM
Definition: G.h:11
int null_cur_row
Definition: G.h:55
CELL * mask_buf
Definition: G.h:82
int G__make_mapset_element(const char *p_element)
Create element in the current mapset.
Definition: mapset_msc.c:34
int G__reallocate_work_buf(int bytes_per_cell)
Allocate/enlarge the work data buffer needed by get_map_row and put_map_row()
Definition: opencell.c:842
char * G__projection_name(int n)
Definition: proj2.c:36
int G_get_reclass(const char *name, const char *mapset, struct Reclass *reclass)
Definition: reclass.c:117
struct Quant quant
Definition: G.h:70
int errno
G_init_fp_range(drange)
int G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
int G__init_window(void)
Initialize window.
Definition: window_map.c:320
RASTER_MAP_TYPE G_raster_map_type(const char *name, const char *mapset)
Determine raster data type.
Definition: opencell.c:1001
void G_quant_set_pos_infinite_rule(struct Quant *q, DCELL dRight, CELL c)
Definition: quant.c:625
int G_init_cell_stats(struct Cell_stats *s)
initialize cell stats
Definition: cell_stats.c:34
char xname[512]
Definition: g3dcats.c:89
int n
Definition: dataquad.c:291
unsigned char * NULL_ROWS[NULL_ROWS_INMEM]
Definition: G.h:67
struct FPRange fp_range
Definition: G.h:48
#define XDR_FLOAT_NBYTES
Definition: G.h:9
int G_open_raster_new(const char *name, RASTER_MAP_TYPE wr_type)
Opens a new raster map.
Definition: opencell.c:1127
int G_quant_get_pos_infinite_rule(const struct Quant *q, DCELL *dRight, CELL *c)
Definition: quant.c:641
int G__check_for_auto_masking(void)
Checks for auto masking.
Definition: auto_mask.c:34
#define OPEN_NEW_COMPRESSED
Definition: G.h:101
int G_quant_truncate(struct Quant *quant)
sets the quant for q rules to perform simple truncation on floats.
Definition: quant.c:471
int reclass_flag
Definition: G.h:50
int G__name_is_fully_qualified(const char *fullname, char *name, char *mapset)
Check if map name is fully qualified (map @ mapset)
Definition: nme_in_mps.c:57