GRASS GIS 7 Programmer's Manual  7.5.svn(2017)-r71942
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
gvl_file.c
Go to the documentation of this file.
1 /*!
2  \file lib/ogsf/gvl_file.c
3 
4  \brief OGSF library - loading and manipulating volumes (lower level functions)
5 
6  GRASS OpenGL gsurf OGSF Library
7 
8  (C) 1999-2008 by the GRASS Development Team
9 
10  This program is free software under the
11  GNU General Public License (>=v2).
12  Read the file COPYING that comes with GRASS
13  for details.
14 
15  \author Tomas Paudits (February 2004)
16  \author Doxygenized by Martin Landa <landa.martin gmail.com> (May 2008)
17  */
18 
19 #include <string.h>
20 #include <stdlib.h>
21 
22 #include <grass/gis.h>
23 #include <grass/ogsf.h>
24 #include <grass/raster3d.h>
25 #include <grass/glocale.h>
26 
27 #define LUCKY 33
28 
29 #define MODE_DIRECT 0
30 #define MODE_SLICE 1
31 #define MODE_FULL 2
32 #define MODE_PRELOAD 3
33 
34 #define MODE_DEFAULT 0
35 
36 #define STATUS_READY 0
37 #define STATUS_BUSY 1
38 
39 /*!
40  \brief structure for slice mode reading from volume file
41  */
42 typedef struct
43 {
44  int num, skip;
45  int crnt, base;
46 
47  void *slice[MAX_VOL_SLICES];
48 } slice_data;
49 
50 static geovol_file *Data[MAX_VOL_FILES];
51 static geovol_file Df[MAX_VOL_FILES]; /* trying to avoid allocation */
52 
53 static int Numfiles = 0;
54 
55 static int Cur_id = LUCKY;
56 static int Cur_max;
57 
58 static int Rows, Cols, Depths;
59 
60 /* local functions prototypes */
61 void *open_g3d_file(const char *, IFLAG *, double *, double *);
62 int close_g3d_file(void *);
63 
64 /*!
65  \brief Initialize volume files
66 
67  \return 1
68  */
69 static int init_volfiles(void)
70 {
71  int i;
72  RASTER3D_Region *w3;
73 
74  for (i = 0; i < MAX_VOL_FILES; i++) {
75  /* avoiding dynamic allocation */
76  Data[i] = &(Df[i]);
77  }
78 
79  Cur_max = MAX_VOL_FILES;
80 
81  /* get window */
82  w3 = GVL_get_window();
83 
84  /* set cols, rows, depths from window */
85  Cols = w3->cols;
86  Rows = w3->rows;
87  Depths = w3->depths;
88 
89  return (1);
90 }
91 
92 /*!
93  \brief Check number of files
94 
95  \return 0
96  */
97 static int check_num_volfiles(void)
98 {
99  if (Numfiles < Cur_max) {
100  return (0);
101  }
102 
103  G_fatal_error(_("Maximum number of datafiles exceeded"));
104 
105  /* This return statement keeps compilers happy, it is never executed */
106  return (0);
107 }
108 
109 /*!
110  \brief Get geovol_file structure for given handle
111 
112  \param id
113 
114  \return pointer to geovol_file struct
115  \return NULL on failure
116  */
118 {
119  int i;
120 
121  for (i = 0; i < Numfiles; i++) {
122  if (Data[i]->data_id == id) {
123  return (Data[i]);
124  }
125  }
126 
127  return (NULL);
128 }
129 
130 /*!
131  \brief Find file with name and type in geovol_file array an return handle
132 
133  \param name file name
134  \param begin
135 
136  \param data id
137  \param -1 not found
138  */
139 int find_datah(const char *name, IFLAG type, int begin)
140 {
141  static int i;
142  int start;
143 
144  start = begin ? 0 : i + 1;
145 
146  for (i = start; i < Numfiles; i++) {
147  if (!strcmp(Data[i]->file_name, name)) {
148  if (Data[i]->file_type == type) {
149  return (Data[i]->data_id);
150  }
151  }
152  }
153 
154  return (-1);
155 }
156 
157 /*!
158  \brief Get file name for given handle
159 
160  \param id handle id
161 
162  \return file name
163  \return NULL on failure
164  */
165 char *gvl_file_get_name(int id)
166 {
167  int i;
168  geovol_file *fvf;
169  static char retstr[GPATH_MAX];
170 
171  for (i = 0; i < Numfiles; i++) {
172  if (Data[i]->data_id == id) {
173  fvf = Data[i];
174  strcpy(retstr, fvf->file_name);
175 
176  return (retstr);
177  }
178  }
179 
180  return (NULL);
181 }
182 
183 /*!
184  \brief Get file type for given handle
185 
186  \param vf pointer to geovol_file struct
187 
188  \return file type
189  */
191 {
192  return (vf->file_type);
193 }
194 
195 /*!
196  \brief Get data type for given handle
197 
198  \param vf pointer to geovol_file struct
199 
200  \return data type
201  */
203 {
204  return (vf->data_type);
205 }
206 
207 /*!
208  \brief Get minimum and maximum value in volume file
209 
210  \param vf pointer to geovol_file struct
211  \param[out] min min value
212  \param[out] max max value
213  */
214 void gvl_file_get_min_max(geovol_file * vf, double *min, double *max)
215 {
216  *min = vf->min;
217  *max = vf->max;
218 }
219 
220 /*!
221  \brief Open 3d raster file
222 
223  \param name file name
224  \param file_type file type
225  \param data_type data type
226  \param[out] min min value
227  \param[out] max max value
228 
229  \return pointer to file
230  \return NULL on failure
231  */
232 void *open_volfile(const char *name, IFLAG file_type, IFLAG * data_type,
233  double *min, double *max)
234 {
235  if (file_type == VOL_FTYPE_RASTER3D) {
236  return open_g3d_file(name, data_type, min, max);
237  }
238 
239  return (NULL);
240 }
241 
242 /*!
243  \brief Close volume file
244 
245  \param map volume filename
246  \param type file type
247 
248  \return
249  \return -1 on failure
250  */
251 int close_volfile(void *map, IFLAG type)
252 {
253  if (type == VOL_FTYPE_RASTER3D) {
254  return close_g3d_file(map);
255  }
256 
257  return (-1);
258 }
259 
260 /*!
261  \brief Get handle for given file name and type
262 
263  \param name volume filename
264  \param file_type file type
265 
266  \return data id
267  \return -1 on failure
268  */
269 int gvl_file_newh(const char *name, IFLAG file_type)
270 {
271  geovol_file *new;
272  static int first = 1;
273  int i, id;
274  void *m;
275  IFLAG data_type;
276  double min, max;
277 
278  if (first) {
279  if (0 > init_volfiles()) {
280  return (-1);
281  }
282 
283  first = 0;
284  }
285 
286  if (0 <= (id = find_datah(name, file_type, 1))) {
287  for (i = 0; i < Numfiles; i++) {
288  if (Data[i]->data_id == id) {
289  new = Data[i];
290  new->count++;
291 
292  return (id);
293  }
294  }
295  }
296 
297  if (0 > check_num_volfiles()) {
298  return (-1);
299  }
300 
301  if (!name) {
302  return (-1);
303  }
304 
305  if ((m = open_volfile(name, file_type, &data_type, &min, &max)) == NULL) {
306  return (-1);
307  }
308 
309  new = Data[Numfiles];
310 
311  if (new) {
312  Numfiles++;
313  new->data_id = Cur_id++;
314 
315  new->file_name = G_store(name);
316  new->file_type = file_type;
317  new->count = 1;
318  new->map = m;
319  new->min = min;
320  new->max = max;
321  new->data_type = data_type;
322 
323  new->status = STATUS_READY;
324  new->buff = NULL;
325 
326  new->mode = 255;
328 
329  return (new->data_id);
330  }
331 
332  return (-1);
333 }
334 
335 /*!
336  \brief Free allocated buffers
337 
338  \param vf pointer to geovol_file struct
339 
340  \return 1
341  */
343 {
344  if (vf->mode == MODE_SLICE) {
345  G_free(vf->buff);
346  vf->buff = NULL;
347  }
348 
349  if (vf->mode == MODE_PRELOAD) {
350  G_free(vf->buff);
351  vf->buff = NULL;
352  }
353 
354  return (1);
355 }
356 
357 /*!
358  \brief Free geovol_file structure for given handle
359 
360  \param id
361 
362  \return
363  */
365 {
366  int i, j, found = -1;
367  geovol_file *fvf;
368 
369  G_debug(5, "gvl_file_free_datah(): id=%d", id);
370 
371  for (i = 0; i < Numfiles; i++) {
372  if (Data[i]->data_id == id) {
373  found = 1;
374  fvf = Data[i];
375 
376  if (fvf->count > 1) {
377  fvf->count--;
378  }
379  else {
380  close_volfile(fvf->map, fvf->file_type);
381  free_volfile_buffs(fvf);
382 
383  G_free(fvf->file_name);
384  fvf->file_name = NULL;
385 
386  fvf->data_id = 0;
387 
388  for (j = i; j < (Numfiles - 1); j++) {
389  Data[j] = Data[j + 1];
390  }
391 
392  Data[j] = fvf;
393 
394  --Numfiles;
395  }
396  }
397  }
398 
399  return (found);
400 }
401 
402 /******************************************************************/
403 /* reading from RASTER3D raster volume files */
404 
405 /******************************************************************/
406 
407 /*!
408  \brief Open 3d raster file
409 
410  \param filename file name
411  \param type data type
412  \param[out] min min value
413  \param[out] max max value
414 
415  \returns pointer to data
416  */
417 void *open_g3d_file(const char *filename, IFLAG * type, double *min,
418  double *max)
419 {
420  const char *mapset;
421  int itype;
422  void *map;
423 
424  /* search for g3d file a return his mapset */
425  mapset = G_find_raster3d(filename, "");
426  if (!mapset) {
427  G_warning(_("3D raster map <%s> not found"), filename);
428  return (NULL);
429  }
430 
431  /* open g3d file */
432  map =
435  if (!map) {
436  G_warning(_("Unable to open 3D raster map <%s>"), filename);
437  return (NULL);
438  }
439 
440  /* load range into range structure of map */
441  if (!Rast3d_range_load(map)) {
442  G_warning(_("Unable to read range of 3D raster map <%s>"), filename);
443  return (NULL);
444  }
445 
446  Rast3d_range_min_max(map, min, max);
447 
448  /* get file data type */
449  itype = Rast3d_file_type_map(map);
450  if (itype == FCELL_TYPE)
451  *type = VOL_DTYPE_FLOAT;
452  if (itype == DCELL_TYPE)
453  *type = VOL_DTYPE_DOUBLE;
454 
455  return (map);
456 }
457 
458 /*!
459  \brief Close g3d file
460 
461  \param map 3d raster map
462 
463  \return -1 on failure
464  \return 1 on success
465  */
466 int close_g3d_file(void *map)
467 {
468  /* close opened g3d file */
469  if (Rast3d_close((RASTER3D_Map *) map) != 1) {
470  G_warning(_("Unable to close 3D raster map <%s>"),
471  ((RASTER3D_Map *) map)->fileName);
472  return (-1);
473  }
474 
475  return (1);
476 }
477 
478 /*!
479  \brief Eead value from g3d file
480 
481  \param type data type
482  \param map 3D raster map
483  \param x,y,z real coordinates
484  \param[out] value data value
485 
486  \return -1 on failure
487  \return 1 on success
488  */
489 int read_g3d_value(IFLAG type, void *map, int x, int y, int z, void *value)
490 {
491  switch (type) {
492  /* float data type */
493  case (VOL_DTYPE_FLOAT):
494  *((float *)value) = Rast3d_get_float(map, x, y, z);
495  break;
496 
497  /* double data type */
498  case (VOL_DTYPE_DOUBLE):
499  *((double *)value) = Rast3d_get_double(map, x, y, z);
500  break;
501 
502  /* unsupported data type */
503  default:
504  return (-1);
505  }
506 
507  return (1);
508 }
509 
510 /*!
511  \brief Read slice of values at level from g3d file
512 
513  \param type data type
514  \param map 3D raster map
515  \param level
516  \param[out] data
517 
518  \return -1 on failure
519  \return 0 on success
520  */
521 int read_g3d_slice(IFLAG type, void *map, int level, void *data)
522 {
523  int x, y;
524 
525  switch (type) {
526  /* float data type */
527  case (VOL_DTYPE_FLOAT):
528  for (x = 0; x < Cols; x++) {
529  for (y = 0; y < Rows; y++) {
530  ((float *)data)[x + y * Cols] =
531  Rast3d_get_float(map, x, y, level);
532  }
533  }
534 
535  break;
536 
537  /* double data type */
538  case (VOL_DTYPE_DOUBLE):
539  for (x = 0; x < Cols; x++) {
540  for (y = 0; y < Rows; y++) {
541  ((double *)data)[x + y * Cols] =
542  Rast3d_get_double(map, x, y, level);
543  }
544  }
545 
546  break;
547 
548  /* unsupported data type */
549  default:
550  return (-1);
551  }
552 
553  return (1);
554 }
555 
556 /*!
557  \brief Read all values from g3d file
558 
559  \param type data type
560  \param map 3D raster map
561  \param[out] data data buffer
562 
563  \return -1 on failure
564  \return 1 on success
565  */
566 int read_g3d_vol(IFLAG type, void *map, void *data)
567 {
568  int x, y, z;
569 
570  switch (type) {
571  /* float data type */
572  case (VOL_DTYPE_FLOAT):
573  for (x = 0; x < Cols; x++) {
574  for (y = 0; y < Rows; y++) {
575  for (z = 0; z < Depths; z++) {
576  ((float *)data)[x + y * Cols + z * Rows * Cols] =
577  Rast3d_get_float(map, x, y, z);
578  }
579  }
580  }
581 
582  break;
583 
584  /* double data type */
585  case (VOL_DTYPE_DOUBLE):
586  for (x = 0; x < Cols; x++) {
587  for (y = 0; y < Rows; y++) {
588  for (z = 0; z < Depths; z++) {
589  ((double *)data)[x + y * Cols + z * Rows * Cols] =
590  Rast3d_get_double(map, x, y, z);
591  }
592  }
593  }
594 
595  break;
596 
597  /* unsupported data type */
598  default:
599  return (-1);
600  }
601 
602  return (1);
603 }
604 
605 /*!
606  \brief Check for null value
607 
608  \param type data type
609  \param value
610 
611  \return 1 if value is null
612  \return 0 if value is not null
613  \return -1 on failure (unsupported data type
614  */
615 int is_null_g3d_value(IFLAG type, void *value)
616 {
617  switch (type) {
618  /* float data type */
619  case (VOL_DTYPE_FLOAT):
620  return Rast3d_is_null_value_num(value, FCELL_TYPE);
621  break;
622 
623  /* double data type */
624  case (VOL_DTYPE_DOUBLE):
625  return Rast3d_is_null_value_num(value, DCELL_TYPE);
626  break;
627 
628  /* unsupported data type */
629  default:
630  return (-1);
631  }
632 
633  return (-1);
634 }
635 
636 /******************************************************************/
637 /* reading from buffer */
638 
639 /******************************************************************/
640 
641 /*!
642  \brief Get value from buffer
643 
644  \param type data type
645  \param data data buffer
646  \param offset
647  \param value
648 
649  \return -1 on failure (unsupported data type)
650  \return 1 on success
651  */
652 int get_buff_value(IFLAG type, void *data, int offset, void *value)
653 {
654  switch (type) {
655  /* float data type */
656  case (VOL_DTYPE_FLOAT):
657  *((float *)value) = ((float *)data)[offset];
658  break;
659 
660  /* double data type */
661  case (VOL_DTYPE_DOUBLE):
662  *((double *)value) = ((double *)data)[offset];
663  break;
664 
665  /* unsupported data type */
666  default:
667  return (-1);
668  }
669 
670  return (1);
671 }
672 
673 /******************************************************************/
674 /* direct mode reading from volume file */
675 
676 /******************************************************************/
677 
678 /*!
679  \brief Read value direct from volume file
680 
681  \param vf pointer to geovol_file struct
682  \param x,y,z real point
683  \param[out] value data value
684 
685  \return -1 on failure
686  \return 1 on success
687  */
688 int get_direct_value(geovol_file * vf, int x, int y, int z, void *value)
689 {
690  switch (vf->file_type) {
691  /* RASTER3D file type */
692  case (VOL_FTYPE_RASTER3D):
693  if (0 > read_g3d_value(vf->data_type, vf->map, x, y, z, value))
694  return (-1);
695  break;
696 
697  default:
698  return (-1);
699  }
700 
701  return (1);
702 }
703 
704 /******************************************************************/
705 /* full mode reading from volume file */
706 
707 /******************************************************************/
708 
709 /*!
710  \brief Allocate buffer memory for full mode reading
711 
712  \param vf pointer to geovol_file
713 
714  \return -1 on failure
715  \return 1 on success
716  */
718 {
719  switch (vf->data_type) {
720  /* float data type */
721  case (VOL_DTYPE_FLOAT):
722  if ((vf->buff =
723  (float *)G_malloc(sizeof(float) * Cols * Rows * Depths)) == NULL)
724  return (-1);
725  break;
726 
727  /* double data type */
728  case (VOL_DTYPE_DOUBLE):
729  if ((vf->buff =
730  (double *)G_malloc(sizeof(double) * Cols * Rows * Depths)) ==
731  NULL)
732  return (-1);
733  break;
734 
735  /* unsupported data type */
736  default:
737  return (-1);
738  }
739 
740  return (1);
741 }
742 
743 /*!
744  \brief Free memory buffer memory
745 
746  \param vf pointer to geovol_file struct
747 
748  \return 1
749  */
751 {
752  G_free(vf->buff);
753 
754  return (1);
755 }
756 
757 /*!
758  \brief Read all values from volume file
759 
760  \param vf pointer to geovol_file struct
761 
762  \return -1 on failure
763  \return 1 on success
764  */
766 {
767  switch (vf->file_type) {
768  /* RASTER3D file format */
769  case (VOL_FTYPE_RASTER3D):
770  if (0 > read_g3d_vol(vf->data_type, vf->map, vf->buff))
771  return (-1);
772  break;
773  /* unsupported file format */
774  default:
775  return (-1);
776  }
777 
778  return (1);
779 }
780 
781 /*!
782  \brief Get value from volume buffer
783 
784  \param vf pointer to geovol_file struct
785  \param x,y,z real point
786  \param[out] value data value
787 
788  \return 1
789  */
790 int get_vol_value(geovol_file * vf, int x, int y, int z, void *value)
791 {
792  get_buff_value(vf->data_type, vf->buff, z * Rows * Cols + y * Cols + x,
793  value);
794 
795  return (1);
796 }
797 
798 /******************************************************************/
799 /* slice mode reading from volume file */
800 
801 /******************************************************************/
802 
803 /*!
804  \brief Allocate buffer for slice mode reading
805 
806  \param vf pointer to geovol_file struct
807 
808  \return -1 on failure
809  \return 1 on success
810  */
812 {
813  int i;
814  slice_data *sd = (slice_data *) vf->buff;
815 
816  switch (vf->data_type) {
817  /* float data type */
818  case (VOL_DTYPE_FLOAT):
819  for (i = 0; i < sd->num; i++) {
820  if ((sd->slice[i] =
821  (float *)G_malloc(sizeof(float) * Cols * Rows)) == NULL)
822  return (-1);
823  }
824  break;
825 
826  /* double data type */
827  case (VOL_DTYPE_DOUBLE):
828  for (i = 0; i < sd->num; i++) {
829  if ((sd->slice[i] =
830  (double *)G_malloc(sizeof(double) * Cols * Rows)) == NULL)
831  return (-1);
832  }
833  break;
834 
835  /* unsupported data type */
836  default:
837  return (-1);
838  }
839 
840  return (1);
841 }
842 
843 /*!
844  \brief Free buffer for slice mode reading
845 
846  \param vf pointer to geovol_file struct
847 
848  \return 1
849  */
851 {
852  int i;
853  slice_data *sd = (slice_data *) vf->buff;
854 
855  for (i = 0; i < sd->num; i++) {
856  G_free(sd->slice[i]);
857  }
858 
859  return (1);
860 }
861 
862 /*!
863  \brief Read slice of values at level from volume file
864 
865  \param vf pointer to geovol_file struct
866  \param s
867  \param l
868 
869  \return -1 on failure
870  \return 1 on success
871  */
872 int read_slice(geovol_file * vf, int s, int l)
873 {
874  /* get slice structure */
875  slice_data *sd = (slice_data *) vf->buff;
876 
877  switch (vf->file_type) {
878  /* RASTER3D file format */
879  case (VOL_FTYPE_RASTER3D):
880  if (0 > read_g3d_slice(vf->data_type, vf->map, l, sd->slice[s]))
881  return (-1);
882  break;
883  /* unsupported file format */
884  default:
885  return (-1);
886  }
887 
888  return (1);
889 }
890 
891 /*!
892  \brief Read new slice into buffer
893 
894  \param vf pointer to geovol_file struct
895  */
897 {
898  void *tmp;
899  int i;
900 
901  slice_data *sd = (slice_data *) vf->buff;
902 
903  /* change pointers to slices */
904  tmp = sd->slice[0];
905  for (i = 0; i < sd->num - 1; i++) {
906  sd->slice[i] = sd->slice[i + 1];
907  }
908  sd->slice[sd->num - 1] = tmp;
909 
910  /* read new slice data */
911  read_slice(vf, sd->num, sd->crnt + 1 + (sd->num - sd->base));
912 
913  /* increase current slice value */
914  sd->crnt++;
915 }
916 
917 /*!
918  \brief Get value from slice buffer
919 
920  \param vf pointer to geovol_file struct
921  \param x,y,z real point
922  \param[out] value data value
923 
924  \return -1 on failure
925  \return 1 on success
926  */
927 int get_slice_value(geovol_file * vf, int x, int y, int z, void *value)
928 {
929  slice_data *sd = (slice_data *) vf->buff;
930 
931  /* value is in loaded slices */
932  if ((z >= sd->crnt - (sd->base - 1)) &&
933  (z <= sd->crnt + sd->num - sd->base)) {
934 
935  get_buff_value(vf->data_type, sd->slice[(z - sd->crnt)], y * Cols + x,
936  value);
937  }
938 
939  /* if value isn't in loaded slices, need read new data slice */
940  else if (z == sd->crnt - (sd->base - 1) + 1) {
941  shift_slices(vf);
942  get_buff_value(vf->data_type, sd->slice[(z - sd->crnt)], y * Cols + x,
943  value);
944  }
945 
946  /* value out of range */
947  else {
948  return (-1);
949  }
950 
951  return (1);
952 }
953 
954 /******************************************************************/
955 /* reading from volume file */
956 
957 /******************************************************************/
958 
959 /*!
960  \brief Start read - allocate memory buffer a read first data into buffer
961 
962  \param vf pointer to geovol_file struct
963 
964  \return -1 on failure
965  \return 1 on success
966  */
968 {
969  int i;
970  slice_data *sd;
971 
972  /* check status */
973  if (vf->status == STATUS_BUSY)
974  return (-1);
975 
976  switch (vf->mode) {
977  /* read whole volume into memory */
978  case (MODE_FULL):
979  /* allocate memory */
980  if (0 > alloc_vol_buff(vf))
981  return (-1);
982 
983  /* read volume */
984  read_vol(vf);
985  break;
986 
987  /* read first data slices into memory */
988  case (MODE_SLICE):
989  /* allocate slices buffer memory */
990  if (0 > alloc_slice_buff(vf))
991  return (-1);
992 
993  /* read volume */
994  sd = (slice_data *) vf->buff;
995  /* set current slice to 0 */
996  sd->crnt = 0;
997 
998  /* read first slices into buffer */
999  for (i = 0; i < (sd->num - sd->base + 1); i++)
1000  read_slice(vf, (sd->base - 1) + i, i);
1001  break;
1002  }
1003 
1004  /* set status */
1005  vf->status = STATUS_BUSY;
1006 
1007  return (1);
1008 }
1009 
1010 /*!
1011  \brief End read - free buffer memory
1012 
1013  \param vf pointer to geovol_file struct
1014 
1015  \return -1 on failure
1016  \return 1 on success
1017  */
1019 {
1020  /* check status */
1021  if (vf->status == STATUS_READY)
1022  return (-1);
1023 
1024  switch (vf->mode) {
1025  case (MODE_FULL):
1026  if (0 > free_vol_buff(vf))
1027  return (-1);
1028  break;
1029 
1030  case (MODE_SLICE):
1031  /* allocate slices buffer memory */
1032  if (0 > free_slice_buff(vf))
1033  return (-1);
1034  }
1035 
1036  /* set status */
1037  vf->status = STATUS_READY;
1038 
1039  return (1);
1040 }
1041 
1042 /*!
1043  \brief Get value for volume file at x, y, z
1044 
1045  \param vf pointer to geovol_file struct
1046 
1047  \return -1 on failure
1048  \return 1 on success
1049  */
1050 int gvl_file_get_value(geovol_file * vf, int x, int y, int z, void *value)
1051 {
1052  /* check status */
1053  if (vf->status != STATUS_BUSY) {
1054  return (-1);
1055  }
1056 
1057  switch (vf->mode) {
1058  /* read value direct from g3d file */
1059  case (MODE_DIRECT):
1060  if (0 > get_direct_value(vf, x, y, z, value))
1061  return (-1);
1062  break;
1063 
1064  case (MODE_SLICE):
1065  if (0 > get_slice_value(vf, x, y, z, value))
1066  return (-1);
1067  break;
1068 
1069  case (MODE_FULL):
1070  case (MODE_PRELOAD):
1071  if (0 > get_vol_value(vf, x, y, z, value))
1072  return (-1);
1073  break;
1074  }
1075  return (1);
1076 }
1077 
1078 /*!
1079  \brief Check for null value
1080 
1081  \param vf pointer to geovol_file struct
1082  \param value data value
1083 
1084  \return -1 on failure
1085  \return 1 on success
1086  */
1087 int gvl_file_is_null_value(geovol_file * vf, void *value)
1088 {
1089  switch (vf->file_type) {
1090  /* RASTER3D file format */
1091  case (VOL_FTYPE_RASTER3D):
1092  return is_null_g3d_value(vf->file_type, value);
1093  break;
1094  /* unsupported file format */
1095  default:
1096  return (-1);
1097  }
1098 
1099  return (-1);
1100 }
1101 
1102 /******************************************************************/
1103 /* set parameters for reading volumes */
1104 
1105 /******************************************************************/
1106 
1107 /*!
1108  \brief Set read mode
1109 
1110  \param vf pointer to geovol_file struct
1111  \param mode read mode
1112 
1113  \return -1 on failure
1114  \return 1 on success
1115  */
1117 {
1118  slice_data *sd;
1119 
1120  if (vf->status == STATUS_BUSY)
1121  return (-1);
1122 
1123  if (vf->mode == mode)
1124  return (1);
1125 
1126  if (vf->mode == MODE_SLICE)
1127  G_free(vf->buff);
1128 
1129  if (vf->mode == MODE_PRELOAD)
1130  G_free(vf->buff);
1131 
1132  if (mode == MODE_SLICE) {
1133  if ((vf->buff = (slice_data *) G_malloc(sizeof(slice_data))) == NULL)
1134  return (-1);
1135 
1136  sd = (slice_data *) vf->buff;
1137  sd->num = 1;
1138  sd->crnt = 0;
1139  sd->base = 1;
1140  }
1141 
1142  if (mode == MODE_PRELOAD) {
1143  /* allocate memory */
1144  if (0 > alloc_vol_buff(vf))
1145  return (-1);
1146 
1147  /* read volume */
1148  read_vol(vf);
1149  }
1150 
1151  vf->mode = mode;
1152 
1153  return (1);
1154 }
1155 
1156 /*!
1157  \brief Set parameters for slice reading
1158 
1159  \param vf pointer to geovol_file struct
1160  \param n
1161  \param b
1162 
1163  \return -1 on failure
1164  \return 1 on success
1165  */
1167 {
1168  slice_data *sd;
1169 
1170  if (vf->status == STATUS_BUSY)
1171  return (-1);
1172 
1173  if (!(vf->mode == MODE_SLICE))
1174  return (-1);
1175 
1176  sd = (slice_data *) vf->buff;
1177  sd->num = n;
1178  sd->base = b;
1179 
1180  return (1);
1181 }
int is_null_g3d_value(IFLAG type, void *value)
Check for null value.
Definition: gvl_file.c:615
int Rows
Definition: gvl_calc.c:73
#define RASTER3D_USE_CACHE_DEFAULT
Definition: raster3d.h:20
void G_free(void *buf)
Free allocated memory.
Definition: gis/alloc.c:149
int close_g3d_file(void *)
Close g3d file.
Definition: gvl_file.c:466
IFLAG file_type
Definition: ogsf.h:389
char * file_name
Definition: ogsf.h:391
#define MAX_VOL_SLICES
Definition: ogsf.h:50
if(!YY_CURRENT_BUFFER)
Definition: sqlp.yy.c:803
int Rast3d_file_type_map(RASTER3D_Map *map)
Returns the type with which tiles of map are stored on file.
Definition: headerinfo.c:281
int Rast3d_close(RASTER3D_Map *map)
Close 3D raster map files.
int alloc_vol_buff(geovol_file *vf)
Allocate buffer memory for full mode reading.
Definition: gvl_file.c:717
geovol_file * gvl_file_get_volfile(int id)
Get geovol_file structure for given handle.
Definition: gvl_file.c:117
#define min(x, y)
Definition: draw2.c:31
float Rast3d_get_float(RASTER3D_Map *map, int x, int y, int z)
Is equivalent to Rast3d_get_value (map, x, y, z, &amp;value, FCELL_TYPE); return value.
Definition: getvalue.c:43
IFLAG status
Definition: ogsf.h:397
#define RASTER3D_DEFAULT_WINDOW
Definition: raster3d.h:29
char * G_store(const char *s)
Copy string to allocated memory.
Definition: strings.c:86
int gvl_file_get_data_type(geovol_file *vf)
Get data type for given handle.
Definition: gvl_file.c:202
#define MODE_DIRECT
Definition: gvl_file.c:29
int read_vol(geovol_file *vf)
Read all values from volume file.
Definition: gvl_file.c:765
char * gvl_file_get_name(int id)
Get file name for given handle.
Definition: gvl_file.c:165
int gvl_file_get_file_type(geovol_file *vf)
Get file type for given handle.
Definition: gvl_file.c:190
#define NULL
Definition: ccmath.h:32
#define x
double min
Definition: ogsf.h:395
#define max(x, y)
Definition: draw2.c:32
int get_buff_value(IFLAG type, void *data, int offset, void *value)
Get value from buffer.
Definition: gvl_file.c:652
switch(read_cats(name, mapset, pcats))
#define VOL_DTYPE_FLOAT
Definition: ogsf.h:131
int Rast3d_range_load(RASTER3D_Map *map)
Loads the range into the range structure of map.
Definition: d/range.c:112
int close_volfile(void *map, IFLAG type)
Close volume file.
Definition: gvl_file.c:251
void G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
Definition: gis/error.c:159
#define MODE_SLICE
Definition: gvl_file.c:30
#define STATUS_BUSY
Definition: gvl_file.c:37
int read_g3d_value(IFLAG type, void *map, int x, int y, int z, void *value)
Eead value from g3d file.
Definition: gvl_file.c:489
double l
Definition: r_raster.c:39
#define LUCKY
Definition: gvl_file.c:27
int gvl_file_set_slices_param(geovol_file *vf, int n, int b)
Set parameters for slice reading.
Definition: gvl_file.c:1166
int get_slice_value(geovol_file *vf, int x, int y, int z, void *value)
Get value from slice buffer.
Definition: gvl_file.c:927
#define DCELL_TYPE
Definition: raster.h:13
void * map
Definition: ogsf.h:394
#define MAX_VOL_FILES
Definition: ogsf.h:51
int free_volfile_buffs(geovol_file *vf)
Free allocated buffers.
Definition: gvl_file.c:342
double b
Definition: r_raster.c:39
for(cat=0;;cat++)
int gvl_file_newh(const char *name, IFLAG file_type)
Get handle for given file name and type.
Definition: gvl_file.c:269
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition: debug.c:65
void * buff
Definition: ogsf.h:400
int alloc_slice_buff(geovol_file *vf)
Allocate buffer for slice mode reading.
Definition: gvl_file.c:811
unsigned int count
Definition: ogsf.h:390
void shift_slices(geovol_file *vf)
Read new slice into buffer.
Definition: gvl_file.c:896
const char * G_find_raster3d(const char *name, const char *mapset)
Search for a 3D raster map in current search path or in a specified mapset.
Definition: find_rast3d.c:28
int Rast3d_is_null_value_num(const void *n, int type)
Definition: null.c:12
#define GPATH_MAX
Definition: gis.h:151
int gvl_file_start_read(geovol_file *vf)
Start read - allocate memory buffer a read first data into buffer.
Definition: gvl_file.c:967
void * GVL_get_window()
Get window.
Definition: gvl2.c:98
int get_direct_value(geovol_file *vf, int x, int y, int z, void *value)
Read value direct from volume file.
Definition: gvl_file.c:688
int gvl_file_free_datah(int id)
Free geovol_file structure for given handle.
Definition: gvl_file.c:364
double Rast3d_get_double(RASTER3D_Map *map, int x, int y, int z)
Is equivalent to Rast3d_get_value (map, x, y, z, &amp;value, DCELL_TYPE); return value.
Definition: getvalue.c:66
IFLAG mode
Definition: ogsf.h:398
int get_vol_value(geovol_file *vf, int x, int y, int z, void *value)
Get value from volume buffer.
Definition: gvl_file.c:790
#define MODE_FULL
Definition: gvl_file.c:31
double max
Definition: ogsf.h:395
void * open_g3d_file(const char *, IFLAG *, double *, double *)
Open 3d raster file.
Definition: gvl_file.c:417
int gvl_file_end_read(geovol_file *vf)
End read - free buffer memory.
Definition: gvl_file.c:1018
void Rast3d_range_min_max(RASTER3D_Map *map, double *min, double *max)
Returns in min and max the minimum and maximum values of the range.
Definition: d/range.c:135
#define RASTER3D_TILE_SAME_AS_FILE
Definition: raster3d.h:12
#define STATUS_READY
Definition: gvl_file.c:36
int read_g3d_vol(IFLAG type, void *map, void *data)
Read all values from g3d file.
Definition: gvl_file.c:566
int gvl_file_is_null_value(geovol_file *vf, void *value)
Check for null value.
Definition: gvl_file.c:1087
#define _(str)
Definition: glocale.h:13
void gvl_file_get_min_max(geovol_file *vf, double *min, double *max)
Get minimum and maximum value in volume file.
Definition: gvl_file.c:214
#define FCELL_TYPE
Definition: raster.h:12
return
Definition: raster3d/cats.c:89
long num
Definition: raster3d/cats.c:85
#define MODE_PRELOAD
Definition: gvl_file.c:32
int free_slice_buff(geovol_file *vf)
Free buffer for slice mode reading.
Definition: gvl_file.c:850
void * open_volfile(const char *name, IFLAG file_type, IFLAG *data_type, double *min, double *max)
Open 3d raster file.
Definition: gvl_file.c:232
const char * name
Definition: named_colr.c:7
#define VOL_FTYPE_RASTER3D
Definition: ogsf.h:128
#define VOL_DTYPE_DOUBLE
Definition: ogsf.h:132
int free_vol_buff(geovol_file *vf)
Free memory buffer memory.
Definition: gvl_file.c:750
#define MODE_DEFAULT
Definition: gvl_file.c:34
int Cols
Definition: gvl_calc.c:73
int find_datah(const char *name, IFLAG type, int begin)
Find file with name and type in geovol_file array an return handle.
Definition: gvl_file.c:139
int gvl_file_set_mode(geovol_file *vf, IFLAG mode)
Set read mode.
Definition: gvl_file.c:1116
int read_g3d_slice(IFLAG type, void *map, int level, void *data)
Read slice of values at level from g3d file.
Definition: gvl_file.c:521
int data_id
Definition: ogsf.h:388
void * Rast3d_open_cell_old(const char *name, const char *mapset, RASTER3D_Region *window, int typeIntern, int cache)
Opens existing g3d-file name in mapset. Tiles are stored in memory with type which must be any of FCE...
Definition: raster3d/open.c:77
void G_warning(const char *msg,...)
Print a warning message to stderr.
Definition: gis/error.c:203
#define IFLAG
Definition: ogsf.h:69
int gvl_file_get_value(geovol_file *vf, int x, int y, int z, void *value)
Get value for volume file at x, y, z.
Definition: gvl_file.c:1050
IFLAG data_type
Definition: ogsf.h:393
int Depths
Definition: gvl_calc.c:73
int read_slice(geovol_file *vf, int s, int l)
Read slice of values at level from volume file.
Definition: gvl_file.c:872