GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gis/put_row.c
Go to the documentation of this file.
1 
2 /**********************************************************************
3  *
4  * G_zeros_r_nulls(zeros_r_nulls)
5  * int zeros_r_nulls the last argument of put_data()
6  *
7  * zeros_r_nulls > 0 zero values of buf to be written into files
8  * are null values by default.
9  *
10  * zeros_r_nulls == 0 zero values are just zero itself.
11  *
12  * zeros_r_nulls < 0 do not set. return current setting.
13  * 1: set
14  * 0: not set
15  *
16  * Return setting values in all cases.
17  *
18  * *** NOTE ***
19  * Use only to change a default behavior for zero of G_put_map_row[_random].
20  *
21  **********************************************************************
22  *
23  * G_put_[c/f/d]_raster_row(fd, buf)
24  * int fd file descriptor of the opened map
25  * [F/D]CELL *buf buffer holding row info to be written
26  *
27  * Writes the next row for the cell file opened on 'fd' from 'buf'
28  * All writes go into NEW files that exactly match the current window.
29  * The file must have been opened with G_open_cell_new()
30  * and be written sequentially, ie no skipping rows
31  *
32  * when the null values are embeded into the data, corresponding cells are
33  * changed to 0's and the corresponding null value row is written into null
34  * file.
35  *
36  * *** NOTE ***
37  * A map cannot be copied using G_get_raster_row() and G_put_raster_row().
38  * The former resamples the data of the original map into a row buffer
39  * that matches the current window. The later writes out rows associated
40  * with the window.
41  *
42  * returns: 1 if successful
43  * -1 on fail
44  *
45  * Keeps track of the minimum and maximum cell value for use in updating
46  * the range file upon close of the cell file.
47  * HOWEVER when nulls are not embeded, the cells are considered 0's as far
48  * as updating range is concerned, even if the corresponding cell is null
49  * in the resulting null file, so programmer should be carefult to set all
50  * the null values using G_set_null_value() or G_insert_[d/f_]null_values()
51  *
52  **********************************************************************
53  *
54  * G_put_map_row(fd, buf)
55  * int fd file descriptor of the opened map
56  * CELL *buf buffer holding row info to be written
57  *
58  * Writes the next row for the cell file opened on 'fd' from 'buf'
59  * All writes go into NEW files that exactly match the current window.
60  * The file must have been opened with G_open_cell_new()
61  * and be written sequentially, ie no skipping rows
62  *
63  * NULLS are written into null bitmap file for all cells which are zero,
64  * and cells which have null value (these cells are converted to 0's before
65  * writing)
66  *
67  * *** NOTE ***
68  * A map cannot be copied using G_get_map_row() and G_put_map_row().
69  * The former resamples the data of the original map into a row buffer
70  * that matches the current window. The later writes out rows associated
71  * with the window.
72  *
73  * returns: 1 if successful
74  * -1 on fail
75  *
76  * Keeps track of the minimum and maximum cell value for use in updating
77  * the range file upon close of the cell file.
78  *
79  **********************************************************************
80  *
81  * G_put_map_row_random(fd, buf, row, col, ncells)
82  * int fd File descriptor where data is to be written
83  * CELL *buf Buffer holding data
84  * int row Map row where data is to be written
85  * int col Column where data begins
86  * int ncells Number of columns of data to be written
87  *
88  * Writes parts of rows into open cell file.
89  *
90  * Cell file must have been opened with G_open_cell_new_random()
91  * except it can't write null file.
92  *
93  * returns: 0 if successful
94  * -1 on fail
95  *
96  * behaves the same as G_put_map_row()
97  *
98  **********************************************************************
99  *
100  * Note: there is no G_put_[c/f/d]_raster_row_random() because even though
101  * it is possible to randomly write floating and integer rows, it is not
102  * possible to rand. write null data, so the null file can't
103  * be updated correctly.
104  *
105  ***********************************************************************
106  *
107  * G__put_null_value_row(fd, buf, row, col, ncells)
108  * int fd File descriptor where data is to be written
109  * char *buf Buffer holding null data
110  * int row Map row where data is to be written
111  * int col Column where data begins
112  * int ncells Number of columns of data to be written
113  *
114  * converts a buffer of zero's and ones to bitstream and stores this
115  * bitstream in memory. (the null rows from memory are written into null
116  * file after the limit is reached, and the place for new null rows
117  * to be kept in memory is freed. Should not be used by application
118  * programs.
119  *
120  * returns: 0 if successful
121  * -1 on fail
122  **********************************************************************/
123 
124 #include <string.h>
125 
126 #include <sys/types.h>
127 #include <sys/stat.h>
128 #include <unistd.h>
129 #include <fcntl.h>
130 
131 #include <grass/config.h>
132 
133 #include "G.h"
134 #include <grass/glocale.h>
135 
136 static int _zeros_r_nulls = 1;
137 
138 static int put_raster_data(int, const void *, int, int, int, int,
139  RASTER_MAP_TYPE);
140 static int put_data(int, const CELL *, int, int, int, int);
141 static int check_open(const char *, int, int);
142 static int adjust(int, int *, int *);
143 static void write_error(int, int);
144 static int same(const unsigned char *, const unsigned char *, int);
145 static int seek_random(int, int, int);
146 static void set_file_pointer(int, int);
147 static int put_fp_data(int, const void *, int, int, int, RASTER_MAP_TYPE);
148 static int put_null_data(int, const char *, int);
149 static int convert_and_write_if(int, const CELL *);
150 static int convert_and_write_id(int, const CELL *);
151 static int convert_and_write_df(int, const DCELL *);
152 static int convert_and_write_fd(int, const FCELL *);
153 static int put_raster_row(int fd, const void *buf, RASTER_MAP_TYPE data_type,
154  int zeros_r_nulls);
155 
156 /*--------------------------------------------------------------------------*/
157 
158 /*--------------------------------------------------------------------------*/
159 
160 /*--------------------------------------------------------------------------*/
161 
162 int G_zeros_r_nulls(int zeros_r_nulls)
163 {
164  if (zeros_r_nulls >= 0)
165  _zeros_r_nulls = zeros_r_nulls > 0;
166 
167  return _zeros_r_nulls;
168 }
169 
170 int G_put_map_row_random(int fd, const CELL * buf, int row, int col, int n)
171 {
172  struct fileinfo *fcb = &G__.fileinfo[fd];
173 
174  if (!check_open("G_put_map_row_random", fd, 1))
175  return -1;
176 
177  buf += adjust(fd, &col, &n);
178  switch (put_data(fd, buf, row, col, n, _zeros_r_nulls)) {
179  case -1:
180  return -1;
181  case 0:
182  return 1;
183  }
184 
185  /* only for integer maps */
186  if (fcb->want_histogram)
187  G_update_cell_stats(buf, n, &fcb->statf);
188 
189  G_row_update_range(buf, n, &fcb->range);
190 
191  return 1;
192 }
193 
194 int G__put_null_value_row(int fd, const char *buf)
195 {
196  struct fileinfo *fcb = &G__.fileinfo[fd];
197 
198  switch (put_null_data(fd, buf, fcb->null_cur_row)) {
199  case -1:
200  return -1;
201  case 0:
202  return 1;
203  }
204 
205  fcb->null_cur_row++;
206 
207  return 1;
208 }
209 
210 int G_put_map_row(int fd, const CELL * buf)
211 {
212  struct fileinfo *fcb = &G__.fileinfo[fd];
213 
214  if (fcb->map_type != CELL_TYPE) {
215  G_fatal_error(_("G_put_map_row: %s is not integer! Use G_put_[f/d]_raster_row()!"),
216  fcb->name);
217  return -1;
218  }
219 
220  return put_raster_row(fd, buf, CELL_TYPE, _zeros_r_nulls);
221 }
222 
223 int G_put_raster_row(int fd, const void *buf, RASTER_MAP_TYPE data_type)
224 {
225  return put_raster_row(fd, buf, data_type, 0);
226 }
227 
228 int G_put_c_raster_row(int fd, const CELL * buf)
229 {
230  return G_put_raster_row(fd, buf, CELL_TYPE);
231 }
232 
233 int G_put_f_raster_row(int fd, const FCELL * buf)
234 {
235  return G_put_raster_row(fd, buf, FCELL_TYPE);
236 }
237 
238 int G_put_d_raster_row(int fd, const DCELL * buf)
239 {
240  return G_put_raster_row(fd, buf, DCELL_TYPE);
241 }
242 
243 /*--------------------------------------------------------------------------*/
244 
245 static int check_open(const char *me, int fd, int random)
246 {
247  struct fileinfo *fcb = &G__.fileinfo[fd];
248 
249  switch (fcb->open_mode) {
250  case OPEN_OLD:
251  G_warning(_("%s: map [%s] not open for write - request ignored"), me,
252  fcb->name);
253  break;
254  case OPEN_NEW_COMPRESSED:
256  if (!random)
257  return 1;
258 
259  G_warning(_("%s: map [%s] not open for random write - request ignored"),
260  me, fcb->name);
261  break;
262  case OPEN_NEW_RANDOM:
263  if (random)
264  return 1;
265 
266  G_warning(_("%s: map [%s] not open for sequential write - request ignored"),
267  me, fcb->name);
268  break;
269  default:
270  G_warning(_("%s: unopened file descriptor - request ignored"), me);
271  break;
272  }
273 
274  return 0;
275 }
276 
277 /*******************************************************
278 * adjust the column,n so that it is within the window
279 * returns the adjustment to buffer that must be made
280 * if col,n is adjusted
281 *
282 * if n comes back <= zero, do not write
283 *******************************************************/
284 static int adjust(int fd, int *col, int *n)
285 {
286  struct fileinfo *fcb = &G__.fileinfo[fd];
287  int adj = 0;
288  int last = *col + *n;
289 
290  if (*col < 0) {
291  adj = -(*col);
292  *col = 0;
293  }
294 
295  if (last > fcb->cellhd.cols)
296  last = fcb->cellhd.cols;
297 
298  *n = last - *col;
299 
300  return adj;
301 }
302 
303 static void write_error(int fd, int row)
304 {
305  struct fileinfo *fcb = &G__.fileinfo[fd];
306 
307  if (fcb->io_error)
308  return;
309 
310  G_warning(_("map [%s] - unable to write row %d"), fcb->name, row);
311 
312  fcb->io_error = 1;
313 
314  return;
315 }
316 
317 /*--------------------------------------------------------------------------*/
318 
319 int G__write_data(int fd, int row, int n)
320 {
321  struct fileinfo *fcb = &G__.fileinfo[fd];
322  ssize_t nwrite = fcb->nbytes * n;
323 
324  if (write(fd, G__.work_buf, nwrite) != nwrite) {
325  write_error(fd, row);
326  return -1;
327  }
328 
329  return 0;
330 }
331 
332 int G__write_data_compressed(int fd, int row, int n)
333 {
334  struct fileinfo *fcb = &G__.fileinfo[fd];
335  int nwrite = fcb->nbytes * n;
336 
337  if (G_zlib_write(fd, G__.work_buf, nwrite) < 0) {
338  write_error(fd, row);
339  return -1;
340  }
341 
342  return 0;
343 }
344 
345 /*--------------------------------------------------------------------------*/
346 
347 static int seek_random(int fd, int row, int col)
348 {
349  struct fileinfo *fcb = &G__.fileinfo[fd];
350  off_t offset = ((off_t) fcb->cellhd.cols * row + col) * fcb->nbytes;
351 
352  if (lseek(fd, offset, SEEK_SET) < 0) {
353  write_error(fd, row);
354  return -1;
355  }
356 
357  return 0;
358 }
359 
360 /*--------------------------------------------------------------------------*/
361 
362 static void set_file_pointer(int fd, int row)
363 {
364  struct fileinfo *fcb = &G__.fileinfo[fd];
365 
366  fcb->row_ptr[row] = lseek(fd, 0L, SEEK_CUR);
367 }
368 
369 /*--------------------------------------------------------------------------*/
370 
371 /*--------------------------------------------------------------------------*/
372 
373 /*--------------------------------------------------------------------------*/
374 
375 static int convert_float(XDR * xdrs, const FCELL * rast, int row, int col,
376  int n, int random)
377 {
378  int i;
379 
380  for (i = 0; i < n; i++) {
381  FCELL f;
382 
383  /* substitute embeded null vals by 0's */
384  if (G_is_f_null_value(&rast[i])) {
385  f = 0.;
386  if (!random)
387  G__.null_buf[col + i] = 1;
388  }
389  else
390  f = rast[i];
391 
392  if (!xdr_float(xdrs, &f)) {
393  G_warning(_("xdr_float failed for index %d of row %d"), i, row);
394  return -1;
395  }
396  }
397 
398  return 0;
399 }
400 
401 static int convert_double(XDR * xdrs, const DCELL * rast, int row, int col,
402  int n, int random)
403 {
404  int i;
405 
406  for (i = 0; i < n; i++) {
407  DCELL d;
408 
409  /* substitute embeded null vals by 0's */
410  if (G_is_d_null_value(&rast[i])) {
411  d = 0.;
412  if (!random)
413  G__.null_buf[col + i] = 1;
414  }
415  else
416  d = rast[i];
417 
418  if (!xdr_double(xdrs, &d)) {
419  G_warning(_("xdr_double failed for index %d of row %d"), i, row);
420  return -1;
421  }
422  }
423 
424  return 0;
425 }
426 
427 /*--------------------------------------------------------------------------*/
428 
429 /* writes data to fcell file for either full or partial rows */
430 
431 static int put_fp_data(int fd, const void *rast, int row, int col, int n,
432  RASTER_MAP_TYPE data_type)
433 {
434  struct fileinfo *fcb = &G__.fileinfo[fd];
435  int random = (fcb->open_mode == OPEN_NEW_RANDOM);
436  int compressed = (fcb->open_mode == OPEN_NEW_COMPRESSED);
437  XDR *xdrs = &fcb->xdrstream;
438 
439  if (row < 0 || row >= fcb->cellhd.rows)
440  return 0;
441 
442  if (n <= 0)
443  return 0;
444 
445  if (random) {
446  if (seek_random(fd, row, col) == -1)
447  return -1;
448  }
449  else if (compressed)
450  set_file_pointer(fd, row);
451 
452  xdrmem_create(xdrs, (caddr_t) G__.work_buf,
453  (u_int) (fcb->nbytes * fcb->cellhd.cols), XDR_ENCODE);
454  xdr_setpos(xdrs, 0);
455 
456  if (data_type == FCELL_TYPE) {
457  if (convert_float(xdrs, rast, row, col, n, random) < 0)
458  return -1;
459  }
460  else {
461  if (convert_double(xdrs, rast, row, col, n, random) < 0)
462  return -1;
463  }
464 
465  xdr_destroy(&fcb->xdrstream);
466 
467  if (compressed) {
468  if (G__write_data_compressed(fd, row, n) == -1)
469  return -1;
470  }
471  else if (G__write_data(fd, row, n) == -1)
472  return -1;
473 
474  return 1;
475 }
476 
477 /*--------------------------------------------------------------------------*/
478 
479 /*--------------------------------------------------------------------------*/
480 
481 /*--------------------------------------------------------------------------*/
482 
483 static void convert_int(unsigned char *wk, const CELL * rast, int col, int n,
484  int random, int len, int zeros_r_nulls)
485 {
486  int i;
487 
488  /* transform CELL data into non-machine dependent multi-byte format */
489 
490  for (i = 0; i < n; i++) {
491  CELL v = rast[i];
492  int neg;
493  int k;
494 
495  /* substitute embeded null vals by 0's */
496  if (G_is_c_null_value(&v)) {
497  v = 0;
498  if (!random)
499  G__.null_buf[col + i] = 1;
500  }
501  else if (!random && zeros_r_nulls && !v)
502  G__.null_buf[col + i] = 1;
503 
504  /* negatives */
505  if (v < 0) {
506  neg = 1;
507  v = -v;
508  }
509  else
510  neg = 0;
511 
512  /* copy byte by byte */
513  for (k = len - 1; k >= 0; k--) {
514  wk[k] = v & 0xff;
515  v >>= 8;
516  }
517 
518  /* set negative bit in first byte */
519  if (neg)
520  wk[0] |= 0x80;
521 
522  wk += len;
523  }
524 }
525 
526 static int count_bytes(const unsigned char *wk, int n, int len)
527 {
528  int i, j;
529 
530  for (i = 0; i < len - 1; i++)
531  for (j = 0; j < n; j++)
532  if (wk[j * len + i] != 0)
533  return len - i;
534 
535  return 1;
536 }
537 
538 static void trim_bytes(unsigned char *wk, int n, int slen, int trim)
539 {
540  unsigned char *wk2 = wk;
541  int i, j;
542 
543  for (i = 0; i < n; i++) {
544  for (j = 0; j < trim; j++)
545  wk++;
546  for (; j < slen; j++)
547  *wk2++ = *wk++;
548  }
549 }
550 
551 static int same(const unsigned char *x, const unsigned char *y, int n)
552 {
553  return (memcmp(x, y, n) == 0);
554 }
555 
556 static int count_run(const unsigned char *src, int n, int nbytes)
557 {
558  const unsigned char *cur = src + nbytes;
559  int i;
560 
561  for (i = 1; i < n; i++) {
562  if (i == 255 || !same(cur, src, nbytes))
563  return i;
564 
565  cur += nbytes;
566  }
567 
568  return n;
569 }
570 
571 static int rle_compress(unsigned char *dst, unsigned char *src, int n,
572  int nbytes)
573 {
574  int nwrite = 0;
575  int total = nbytes * n;
576 
577  while (n > 0) {
578  int count;
579 
580  nwrite += nbytes + 1;
581  if (nwrite >= total)
582  return 0;
583 
584  count = count_run(src, n, nbytes);
585 
586  *dst++ = count;
587  memcpy(dst, src, nbytes);
588  dst += nbytes;
589 
590  src += count * nbytes;
591  n -= count;
592  }
593 
594  return nwrite;
595 }
596 
597 static int zlib_compress(unsigned char *dst, unsigned char *src, int n,
598  int nbytes)
599 {
600  int total = nbytes * n;
601  int nwrite = G_zlib_compress(G__.work_buf + 1, total,
602  G__.compressed_buf + 1,
604 
605  return (nwrite >= total) ? 0 : nwrite;
606 }
607 
608 /*--------------------------------------------------------------------------*/
609 
610 static int put_data(int fd, const CELL * cell, int row, int col, int n,
611  int zeros_r_nulls)
612 {
613  struct fileinfo *fcb = &G__.fileinfo[fd];
614  int random = (fcb->open_mode == OPEN_NEW_RANDOM);
615  int compressed = fcb->cellhd.compressed;
616  int len = compressed ? sizeof(CELL) : fcb->nbytes;
617  unsigned char *wk = G__.work_buf;
618  ssize_t nwrite;
619 
620  if (row < 0 || row >= fcb->cellhd.rows)
621  return 0;
622 
623  if (n <= 0)
624  return 0;
625 
626  if (random) {
627  if (seek_random(fd, row, col) == -1)
628  return -1;
629  }
630  else if (compressed)
631  set_file_pointer(fd, row);
632 
633  if (compressed)
634  wk++;
635 
636  convert_int(wk, cell, col, n, random, len, zeros_r_nulls);
637 
638  if (compressed) {
639  unsigned char *wk = G__.work_buf + 1;
640  int nbytes = count_bytes(wk, n, len);
641 
642  if (fcb->nbytes < nbytes)
643  fcb->nbytes = nbytes;
644 
645  /* first trim away zero high bytes */
646  if (nbytes < len)
647  trim_bytes(wk, n, len, len - nbytes);
648 
650 
651  /* then compress the data */
652  nwrite = compressed == 1
653  ? rle_compress(G__.compressed_buf + 1, G__.work_buf + 1, n,
654  nbytes)
655  : zlib_compress(G__.compressed_buf + 1, G__.work_buf + 1, n,
656  nbytes);
657 
658  if (nwrite > 0) {
659  nwrite++;
660 
661  if (write(fd, G__.compressed_buf, nwrite) != nwrite) {
662  write_error(fd, row);
663  return -1;
664  }
665  }
666  else {
667  nwrite = nbytes * n + 1;
668  if (write(fd, G__.work_buf, nwrite) != nwrite) {
669  write_error(fd, row);
670  return -1;
671  }
672  }
673  }
674  else {
675  nwrite = fcb->nbytes * n;
676 
677  if (write(fd, G__.work_buf, nwrite) != nwrite) {
678  write_error(fd, row);
679  return -1;
680  }
681  }
682 
683  return 1;
684 }
685 
686 /*--------------------------------------------------------------------------*/
687 
688 /*--------------------------------------------------------------------------*/
689 
690 /*--------------------------------------------------------------------------*/
691 
692 static int put_raster_data(int fd, const void *rast, int row, int col, int n,
693  int zeros_r_nulls, RASTER_MAP_TYPE map_type)
694 {
695  return (map_type == CELL_TYPE)
696  ? put_data(fd, rast, row, col, n, zeros_r_nulls)
697  : put_fp_data(fd, rast, row, col, n, map_type);
698 }
699 
700 /*--------------------------------------------------------------------------*/
701 
702 /*--------------------------------------------------------------------------*/
703 
704 /*--------------------------------------------------------------------------*/
705 
706 static int put_null_data(int fd, const char *flags, int row)
707 {
708  struct fileinfo *fcb = &G__.fileinfo[fd];
709  int null_fd, i;
710 
711  if (fcb->min_null_row + NULL_ROWS_INMEM <= row) {
712  /* the row is out of the range of rows stored in memory */
713  /* write out all the rows kept in memory, and initialize memory
714  for keeping new NULL_ROWS_INMEM rows */
715 
716  if (fcb->min_null_row >= 0) {
717  null_fd = G__open_null_write(fd);
718  if (null_fd < 0)
719  return -1;
720 
721  for (i = 0; i < NULL_ROWS_INMEM; i++) {
722  /* fcb->cellhd.rows doesn't have to be a miultiple of NULL_ROWS_INMEM */
723  if (i + fcb->min_null_row >= fcb->cellhd.rows)
724  break;
725 
726  if (G__write_null_bits(null_fd, fcb->NULL_ROWS[i],
727  i + fcb->min_null_row,
728  fcb->cellhd.cols, fd) < 0)
729  return -1;
730 
731  } /* done writing out memory rows */
732  if (null_fd >= 0)
733  close(null_fd);
734  }
735 
736  /* now initialize memory to store new NULL_ROWS_INMEM rows */
738  /* init memory to store next NULL_ROWS_INMEM rows */
739  }
740 
741  /* remember the null row for i for the future writing */
742  G__convert_01_flags(flags, fcb->NULL_ROWS[row - fcb->min_null_row],
743  fcb->cellhd.cols);
744 
745  return 1;
746 }
747 
749 {
750  struct fileinfo *fcb = &G__.fileinfo[fd];
751  int null_fd;
752 
753  if (access(fcb->null_temp_name, 0) != 0) {
754  G_warning(_("unable to find a temporary null file %s"),
755  fcb->null_temp_name);
756  return -1;
757  }
758 
759  null_fd = open(fcb->null_temp_name, O_WRONLY);
760  if (null_fd < 0)
761  return -1;
762 
763  return null_fd;
764 }
765 
766 int G__write_null_bits(int null_fd, const unsigned char *flags, int row,
767  int cols, int fd)
768 {
769  off_t offset;
770  size_t size;
771 
772  size = G__null_bitstream_size(cols);
773  offset = (off_t) size *row;
774 
775  if (lseek(null_fd, offset, SEEK_SET) < 0) {
776  G_warning(_("error writing null row %d"), row);
777  return -1;
778  }
779 
780  if (write(null_fd, flags, size) != size) {
781  G_warning(_("error writing null row %d"), row);
782  return -1;
783  }
784 
785  return 1;
786 }
787 
788 /*--------------------------------------------------------------------------*/
789 
790 /*--------------------------------------------------------------------------*/
791 
792 /*--------------------------------------------------------------------------*/
793 
794 static int convert_and_write_if(int fd, const CELL * buf)
795 {
796  struct fileinfo *fcb = &G__.fileinfo[fd];
797  FCELL *p = (FCELL *) fcb->data;
798  int i;
799 
800  for (i = 0; i < fcb->cellhd.cols; i++)
801  if (G_is_c_null_value(&buf[i]))
802  G_set_f_null_value(&p[i], 1);
803  else
804  p[i] = (FCELL) buf[i];
805 
806  return G_put_f_raster_row(fd, p);
807 }
808 
809 static int convert_and_write_df(int fd, const DCELL * buf)
810 {
811  struct fileinfo *fcb = &G__.fileinfo[fd];
812  FCELL *p = (FCELL *) fcb->data;
813  int i;
814 
815  for (i = 0; i < fcb->cellhd.cols; i++)
816  if (G_is_d_null_value(&buf[i]))
817  G_set_f_null_value(&p[i], 1);
818  else
819  p[i] = (FCELL) buf[i];
820 
821  return G_put_f_raster_row(fd, p);
822 }
823 
824 static int convert_and_write_id(int fd, const CELL * buf)
825 {
826  struct fileinfo *fcb = &G__.fileinfo[fd];
827  DCELL *p = (DCELL *) fcb->data;
828  int i;
829 
830  for (i = 0; i < fcb->cellhd.cols; i++)
831  if (G_is_c_null_value(&buf[i]))
832  G_set_d_null_value(&p[i], 1);
833  else
834  p[i] = (DCELL) buf[i];
835 
836  return G_put_d_raster_row(fd, p);
837 }
838 
839 static int convert_and_write_fd(int fd, const FCELL * buf)
840 {
841  struct fileinfo *fcb = &G__.fileinfo[fd];
842  DCELL *p = (DCELL *) fcb->data;
843  int i;
844 
845  for (i = 0; i < fcb->cellhd.cols; i++)
846  if (G_is_f_null_value(&buf[i]))
847  G_set_d_null_value(&p[i], 1);
848  else
849  p[i] = (DCELL) buf[i];
850 
851  return G_put_d_raster_row(fd, p);
852 }
853 
854 static int convert_and_write_fi(int fd, const FCELL * buf)
855 {
856  struct fileinfo *fcb = &G__.fileinfo[fd];
857  CELL *p = (CELL *) fcb->data;
858  int i;
859 
860  for (i = 0; i < fcb->cellhd.cols; i++)
861  if (G_is_f_null_value(&buf[i]))
862  G_set_c_null_value(&p[i], 1);
863  else
864  p[i] = (CELL) buf[i];
865 
866  return G_put_c_raster_row(fd, p);
867 }
868 
869 static int convert_and_write_di(int fd, const DCELL * buf)
870 {
871  struct fileinfo *fcb = &G__.fileinfo[fd];
872  CELL *p = (CELL *) fcb->data;
873  int i;
874 
875  for (i = 0; i < fcb->cellhd.cols; i++)
876  if (G_is_d_null_value(&buf[i]))
877  G_set_c_null_value(&p[i], 1);
878  else
879  p[i] = (CELL) buf[i];
880 
881  return G_put_c_raster_row(fd, p);
882 }
883 
884 /*--------------------------------------------------------------------------*/
885 
886 static int put_raster_row(int fd, const void *buf, RASTER_MAP_TYPE data_type,
887  int zeros_r_nulls)
888 {
889  struct fileinfo *fcb = &G__.fileinfo[fd];
890 
891  static int (*convert_and_write_FtypeOtype[3][3]) () = {
892  {
893  NULL, convert_and_write_if, convert_and_write_id}, {
894  convert_and_write_fi, NULL, convert_and_write_fd}, {
895  convert_and_write_di, convert_and_write_df, NULL}
896  };
897 
898  if (!check_open("put_raster_row", fd, 0))
899  return -1;
900 
901  if (fcb->map_type != data_type)
902  return convert_and_write_FtypeOtype[data_type][fcb->map_type] (fd,
903  buf);
904 
905  G_zero(G__.null_buf, fcb->cellhd.cols * sizeof(char));
906 
907  switch (put_raster_data
908  (fd, buf, fcb->cur_row, 0, fcb->cellhd.cols, zeros_r_nulls,
909  data_type)) {
910  case -1:
911  return -1;
912  case 0:
913  return 1;
914  }
915 
916  /* only for integer maps */
917  if (data_type == CELL_TYPE) {
918  if (fcb->want_histogram)
919  G_update_cell_stats(buf, fcb->cellhd.cols, &fcb->statf);
920  G__row_update_range(buf, fcb->cellhd.cols, &fcb->range,
921  zeros_r_nulls);
922  }
923  else
924  G_row_update_fp_range(buf, fcb->cellhd.cols, &fcb->fp_range,
925  data_type);
926 
927  fcb->cur_row++;
928 
929  /* write the null row for the data row */
930  return G__put_null_value_row(fd, G__.null_buf);
931 }
int G_is_c_null_value(const CELL *cellVal)
Returns 1 if cell is NULL, 0 otherwise. This will test if the value cell is the largest int...
Definition: null_val.c:244
int G_put_raster_row(int fd, const void *buf, RASTER_MAP_TYPE data_type)
Definition: gis/put_row.c:223
int nbytes
Definition: G.h:58
unsigned char * work_buf
Definition: G.h:87
int want_histogram
Definition: G.h:49
RASTER_MAP_TYPE map_type
Definition: G.h:59
int G__write_data_compressed(int fd, int row, int n)
Definition: gis/put_row.c:332
#define OPEN_NEW_RANDOM
Definition: G.h:103
char * null_buf
Definition: G.h:83
char * name
Definition: G.h:63
int G__open_null_write(int fd)
Definition: gis/put_row.c:748
off_t * row_ptr
Definition: G.h:51
FILE * fd
Definition: g3dcolor.c:368
void G_set_d_null_value(DCELL *dcellVals, int numVals)
Definition: null_val.c:176
int io_error
Definition: G.h:65
int G__put_null_value_row(int fd, const char *buf)
Definition: gis/put_row.c:194
int compressed_buf_size
Definition: G.h:86
int count
unsigned char * data
Definition: G.h:57
int G_zeros_r_nulls(int zeros_r_nulls)
Definition: gis/put_row.c:162
int y
Definition: plot.c:34
struct Range range
Definition: G.h:47
int G_row_update_fp_range(const void *rast, int n, struct FPRange *range, RASTER_MAP_TYPE data_type)
Definition: range.c:524
tuple size
value.Bind(wx.EVT_TEXT, self.OnVolumeIsosurfMap)
Definition: tools.py:2334
#define OPEN_NEW_UNCOMPRESSED
Definition: G.h:102
int G_zero(void *buf, int i)
Zero out a buffer, buf, of length i.
Definition: gis/zero.c:29
Definition: G.h:74
struct Cell_stats statf
Definition: G.h:46
int G__row_update_range(const CELL *cell, int n, struct Range *range, int ignore_zeros)
Definition: range.c:498
int G__write_data(int fd, int row, int n)
Definition: gis/put_row.c:319
int G_is_d_null_value(const DCELL *dcellVal)
Returns 1 if dcell is NULL, 0 otherwise. This will test if the value dcell is a NaN. Same test as in G_is_f_null_value().
Definition: null_val.c:306
int G_is_f_null_value(const FCELL *fcellVal)
Returns 1 if fcell is NULL, 0 otherwise. This will test if the value fcell is a NaN. It isn&#39;t good enough to test for a particular NaN bit pattern since the machine code may change this bit pattern to a different NaN. The test will be.
Definition: null_val.c:281
int G_row_update_range(const CELL *cell, int n, struct Range *range)
update range structure
Definition: range.c:489
int G__write_null_bits(int null_fd, const unsigned char *flags, int row, int cols, int fd)
Definition: gis/put_row.c:766
int G__convert_01_flags(const char *zero_ones, unsigned char *flags, int n)
Definition: null_val.c:524
int open_mode
Definition: G.h:43
int G_put_map_row_random(int fd, const CELL *buf, int row, int col, int n)
Definition: gis/put_row.c:170
int G_put_d_raster_row(int fd, const DCELL *buf)
Definition: gis/put_row.c:238
int cur_row
Definition: G.h:54
int
Definition: g3dcolor.c:48
int G_update_cell_stats(const CELL *cell, int n, struct Cell_stats *s)
add data to cell stats
Definition: cell_stats.c:67
int G_put_map_row(int fd, const CELL *buf)
Definition: gis/put_row.c:210
if(!YY_CURRENT_BUFFER)
Definition: lex.yy.c:799
char * null_temp_name
Definition: G.h:61
void G_set_f_null_value(FCELL *fcellVals, int numVals)
Definition: null_val.c:158
int G__null_bitstream_size(int cols)
Determines null bitstream size.
Definition: alloc_cell.c:171
char buf[GNAME_MAX+sizeof(G3D_DIRECTORY)+2]
Definition: g3drange.c: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)
#define OPEN_OLD
Definition: G.h:100
unsigned char * compressed_buf
Definition: G.h:85
tuple cols
struct Cell_head cellhd
Definition: G.h:44
int min_null_row
Definition: G.h:69
Definition: G.h:41
#define NULL_ROWS_INMEM
Definition: G.h:11
for(cat=0;;cat++)
Definition: g3dcats.c:140
int null_cur_row
Definition: G.h:55
void G_set_c_null_value(CELL *cellVals, int numVals)
Definition: null_val.c:142
int G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
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
int G_put_c_raster_row(int fd, const CELL *buf)
Definition: gis/put_row.c:228
#define OPEN_NEW_COMPRESSED
Definition: G.h:101
int G_put_f_raster_row(int fd, const FCELL *buf)
Definition: gis/put_row.c:233