GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
null_val.c
Go to the documentation of this file.
1 /*
2  *
3  *****************************************************************************
4  *
5  * MODULE: GRASS gis library
6  * AUTHOR(S): Original author unknown - probably CERL
7  * Justin Hickey - Thailand - jhickey@hpcc.nectec.or.th
8  * PURPOSE: To provide functionality to handle NULL values for data types
9  * CELL, FCELL, and DCELL. May need more...
10  * COPYRIGHT: (C) 2000 by the GRASS Development Team
11  *
12  * This program is free software under the GNU General Public
13  * License (>=v2). Read the file COPYING that comes with GRASS
14  * for details.
15  *
16  *****************************************************************************/
17 
18 /*============================= Include Files ==============================*/
19 
20 /* System include files */
21 #include <string.h>
22 
23 /* Grass and local include files */
24 #include <grass/gis.h>
25 #include <grass/glocale.h>
26 
27 static int EmbedGivenNulls(void *, char *, RASTER_MAP_TYPE, int);
28 
29 /****************************************************************************
30 * int EmbedGivenNulls (void *cell, char *nulls, RASTER_MAP_TYPE map_type,
31 * int ncols)
32 *
33 * PURPOSE: To insert null values into a map. Needs more.....
34 * INPUT VARS: cell => ??
35 * nulls => ??
36 * map_type => type of raster - CELL, FCELL, DCELL
37 * ncols => ??
38 * RETURN VAL: ??
39 *****************************************************************************/
40 static int EmbedGivenNulls(void *cell, char *nulls, RASTER_MAP_TYPE map_type,
41  int ncols)
42 {
43  CELL *c;
44  FCELL *f;
45  DCELL *d;
46  int i;
47 
48  c = (CELL *) cell;
49  f = (FCELL *) cell;
50  d = (DCELL *) cell;
51 
52  for (i = 0; i < ncols; i++) {
53  if (nulls[i]) {
54  switch (map_type) {
55  case CELL_TYPE:
56  G_set_c_null_value((CELL *) (c + i), 1);
57  break;
58 
59  case FCELL_TYPE:
60  G_set_f_null_value((FCELL *) (f + i), 1);
61  break;
62 
63  case DCELL_TYPE:
64  G_set_d_null_value((DCELL *) (d + i), 1);
65  break;
66 
67  default:
68  G_warning(_("EmbedGivenNulls: wrong data type!"));
69  }
70  }
71  }
72 
73  return 1;
74 }
75 
76 /*========================== Library Functions =============================*/
77 
78 /****************************************************************************
79 * void G__set_null_value (void *rast, int numVals, int null_is_zero,
80 * RASTER_MAP_TYPE data_type)
81 *
82 * PURPOSE: To set one or more raster values to null. It also sets null
83 * to zero if null_is_zero is TRUE.
84 * INPUT VARS: rast => pointer to values to set to null
85 * numVals => number of values to set to null
86 * null_is_zero => flag to indicate if NULL = 0
87 * data_type => type of raster - CELL, FCELL, DCELL
88 * RETURN VAL: none
89 *****************************************************************************/
90 void G__set_null_value(void *rast, int numVals, int null_is_zero,
91  RASTER_MAP_TYPE data_type)
92 {
93  if (null_is_zero) {
94  G_zero((char *)rast, numVals * G_raster_size(data_type));
95  return;
96  }
97 
98  G_set_null_value(rast, numVals, data_type);
99 
100  return;
101 }
102 
103 /****************************************************************************
104 * void G_set_null_value (void *buf, int numVals, RASTER_MAP_TYPE data_type)
105 *
106 * PURPOSE: To set one or more raster values to null.
107 * INPUT VARS: buf => pointer to values to set to null
108 * numVals => number of values to set to null
109 * data_type => type of raster - CELL, FCELL, DCELL
110 * RETURN VAL: none
111 *****************************************************************************/
112 void G_set_null_value(void *buf, int numVals, RASTER_MAP_TYPE data_type)
113 {
114  switch (data_type) {
115  case CELL_TYPE:
116  G_set_c_null_value((CELL *) buf, numVals);
117  break;
118 
119  case FCELL_TYPE:
120  G_set_f_null_value((FCELL *) buf, numVals);
121  break;
122 
123  case DCELL_TYPE:
124  G_set_d_null_value((DCELL *) buf, numVals);
125  break;
126 
127  default:
128  G_warning(_("G_set_null_value: wrong data type!"));
129  }
130 
131  return;
132 }
133 
134 /****************************************************************************
135 * void G_set_c_null_value (CELL *cellVals, int numVals)
136 *
137 * PURPOSE: To set a number of CELL raster values to NULL.
138 * INPUT VARS: cellVals => pointer to CELL values to set to null
139 * numVals => number of values to set to null
140 * RETURN VAL: none
141 *****************************************************************************/
142 void G_set_c_null_value(CELL *cellVals, int numVals)
143 {
144  int i; /* counter */
145 
146  for (i = 0; i < numVals; i++)
147  cellVals[i] = (int) 0x80000000;
148 }
149 
150 /****************************************************************************
151 * void G_set_f_null_value (FCELL *fcellVals, int numVals)
152 *
153 * PURPOSE: To set a number of FCELL raster values to NULL.
154 * INPUT VARS: fcellVals => pointer to FCELL values to set to null
155 * numVals => number of values to set to null
156 * RETURN VAL: none
157 *****************************************************************************/
158 void G_set_f_null_value(FCELL *fcellVals, int numVals)
159 {
160  static const unsigned char null_bits[4] = {
161  0xFF, 0xFF, 0xFF, 0xFF};
162  int i;
163 
164  for (i = 0; i < numVals; i++)
165  memcpy(&fcellVals[i], null_bits, sizeof(null_bits));
166 }
167 
168 /****************************************************************************
169 * void G_set_d_null_value (DCELL *dcellVals, int numVals)
170 *
171 * PURPOSE: To set a number of DCELL raster values to NULL.
172 * INPUT VARS: dcellVals => pointer to DCELL values to set to null
173 * numVals => number of values to set to null
174 * RETURN VAL: none
175 *****************************************************************************/
176 void G_set_d_null_value(DCELL *dcellVals, int numVals)
177 {
178  static const unsigned char null_bits[8] = {
179  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
180  int i;
181 
182  for (i = 0; i < numVals; i++)
183  memcpy(&dcellVals[i], null_bits, sizeof(null_bits));
184 }
185 
186 /****************************************************************************
187 * int G_is_null_value (void *rast, RASTER_MAP_TYPE data_type)
188 *
189 * PURPOSE: To check if a raster value is set to NULL
190 * INPUT VARS: rast => raster value to check
191 * data_type => type of raster - CELL, FCELL, DCELL
192 * RETURN VAL: TRUE if raster value is NULL FALSE otherwise
193 *****************************************************************************/
194 
207 int G_is_null_value(const void *rast, RASTER_MAP_TYPE data_type)
208 {
209  switch (data_type) {
210  case CELL_TYPE:
211  return (G_is_c_null_value((CELL *) rast));
212 
213  case FCELL_TYPE:
214  return (G_is_f_null_value((FCELL *) rast));
215 
216  case DCELL_TYPE:
217  return (G_is_d_null_value((DCELL *) rast));
218 
219  default:
220  G_warning("G_is_null_value: wrong data type!");
221  return FALSE;
222  }
223 }
224 
225 /****************************************************************************
226 *
227 * int G_is_c_null_value (CELL *cellVal)
228 *
229 * PURPOSE: To check if a CELL raster value is set to NULL
230 * INPUT VARS: cellVal => CELL raster value to check
231 * RETURN VAL: TRUE if CELL raster value is NULL FALSE otherwise
232 *****************************************************************************/
233 
244 int G_is_c_null_value(const CELL * cellVal)
245 {
246  /* Check if the CELL value matches the null pattern */
247  return *cellVal == (CELL) 0x80000000;
248 }
249 
250 /****************************************************************************
251 *
252 * int G_is_f_null_value (FCELL *fcellVal)
253 *
254 * PURPOSE: To check if a FCELL raster value is set to NULL
255 * INPUT VARS: fcellVal => FCELL raster value to check
256 * RETURN VAL: TRUE if FCELL raster value is NULL FALSE otherwise
257 *****************************************************************************/
258 
281 int G_is_f_null_value(const FCELL * fcellVal)
282 {
283  return *fcellVal != *fcellVal;
284 }
285 
286 /****************************************************************************
287 *
288 * int G_is_d_null_value (DCELL *dcellVal)
289 *
290 * PURPOSE: To check if a DCELL raster value is set to NULL
291 * INPUT VARS: dcellVal => DCELL raster value to check
292 * RETURN VAL: TRUE if DCELL raster value is NULL FALSE otherwise
293 *****************************************************************************/
294 
306 int G_is_d_null_value(const DCELL * dcellVal)
307 {
308  return *dcellVal != *dcellVal;
309 }
310 
311 /****************************************************************************
312 *
313 * int G_insert_null_values (void *rast, char *null_row, int ncols,
314 * RASTER_MAP_TYPE data_type)
315 *
316 * PURPOSE: To insert null values into a map. Needs more.....
317 * INPUT VARS: rast => ??
318 * null_row => ??
319 * ncols => ??
320 * data_type => type of raster - CELL, FCELL, DCELL
321 * RETURN VAL: ??
322 *****************************************************************************/
323 
341 int G_insert_null_values(void *rast, char *null_row, int ncols,
342  RASTER_MAP_TYPE data_type)
343 {
344  return (EmbedGivenNulls(rast, null_row, data_type, ncols));
345 }
346 
347 /****************************************************************************
348 *
349 * int G_insert_c_null_values (CELL *cellVal, char *null_row, int ncols)
350 *
351 * PURPOSE: To insert null values into a CELL map. Needs more.....
352 * INPUT VARS: cellVal => ??
353 * null_row => ??
354 * ncols => ??
355 * RETURN VAL: ??
356 *****************************************************************************/
357 
370 int G_insert_c_null_values(CELL * cellVal, char *null_row, int ncols)
371 {
372  return (EmbedGivenNulls((void *)cellVal, null_row, CELL_TYPE, ncols));
373 }
374 
375 /****************************************************************************
376 *
377 * int G_insert_f_null_values (FCELL *fcellVal, char *null_row, int ncols)
378 *
379 * PURPOSE: To insert null values into a FCELL map. Needs more.....
380 * INPUT VARS: fcellVal => ??
381 * null_row => ??
382 * ncols => ??
383 * RETURN VAL: ??
384 *****************************************************************************/
385 
398 int G_insert_f_null_values(FCELL * fcellVal, char *null_row, int ncols)
399 {
400  return (EmbedGivenNulls((void *)fcellVal, null_row, FCELL_TYPE, ncols));
401 }
402 
403 /****************************************************************************
404 *
405 * int G_insert_d_null_values (DCELL *dcellVal, char *null_row, int ncols)
406 *
407 * PURPOSE: To insert null values into a DCELL map. Needs more.....
408 * INPUT VARS: dcellVal => ??
409 * null_row => ??
410 * ncols => ??
411 * RETURN VAL: ??
412 *****************************************************************************/
413 
426 int G_insert_d_null_values(DCELL * dcellVal, char *null_row, int ncols)
427 {
428  return (EmbedGivenNulls((void *)dcellVal, null_row, DCELL_TYPE, ncols));
429 }
430 
431 /****************************************************************************
432 * int G__check_null_bit (unsigned char *flags, int bit_num, int n)
433 *
434 * PURPOSE: To...
435 * INPUT VARS: flags => ??
436 * bit_num => ??
437 * n => ??
438 * RETURN VAL: ??
439 *****************************************************************************/
440 int G__check_null_bit(const unsigned char *flags, int bit_num, int n)
441 {
442  int ind;
443  int offset;
444 
445  /* find the index of the unsigned char in which this bit appears */
446  ind = G__null_bitstream_size(bit_num + 1) - 1;
447 
448  /* find how many unsigned chars the buffer with bit_num+1 (counting from 0
449  has and subtract 1 to get unsigned char index */
450  if (ind > G__null_bitstream_size(n) - 1) {
451  G_warning("G__check_null_bit: can't access index %d. "
452  "Size of flags is %d (bit # is %d",
453  ind, G__null_bitstream_size(n) - 1, bit_num);
454  return -1;
455  }
456 
457  offset = (ind + 1) * 8 - bit_num - 1;
458 
459  return ((flags[ind] & ((unsigned char)1 << offset)) != 0);
460 }
461 
462 /****************************************************************************
463 * int G__set_flags_from_01_random (char *zero_ones, unsigned char *flags,
464 * int col, int n, int ncols)
465 *
466 * PURPOSE: given array of 0/1 of length n starting from column col
467 * set the corresponding bits of flags; total number of bits
468 * in flags is ncols
469 * INPUT VARS: zero_ones => ??
470 * flags => ??
471 * col => ??
472 * n => ??
473 * ncols => ??
474 * RETURN VAL: ??
475 *****************************************************************************/
476 int G__set_flags_from_01_random(const char *zero_ones, unsigned char *flags,
477  int col, int n, int ncols)
478 {
479  unsigned char v;
480  int count;
481  int size;
482  int i, k;
483 
484  if (col == 0 && n == ncols) {
485  G__convert_01_flags(zero_ones, flags, n);
486  return 0;
487  }
488 
489  count = 0;
490  size = G__null_bitstream_size(ncols);
491 
492  for (i = 0; i < size; i++) {
493  v = 0;
494  k = 8;
495 
496  while (k-- > 0) {
497  if (count >= col && count < (col + n)) {
498  v = v | ((unsigned char)zero_ones[count - col] << k);
499  }
500  else if (count < ncols) {
501  v = v |
502  ((unsigned char)G__check_null_bit(flags, count, ncols) << k);
503  }
504 
505  /* otherwise keep this bit the same as it was */
506  count++;
507  }
508 
509  flags[i] = v;
510  }
511 
512  return 1;
513 }
514 
515 /****************************************************************************
516 * int G__convert_01_flags (char *zero_ones, unsigned char *flags, int n)
517 *
518 * PURPOSE: To...
519 * INPUT VARS: zero_ones => ??
520 * flags => ??
521 * n => ??
522 * RETURN VAL: ??
523 *****************************************************************************/
524 int G__convert_01_flags(const char *zero_ones, unsigned char *flags, int n)
525 {
526  unsigned char *v;
527  int count;
528  int size;
529  int i, k;
530 
531  /* pad the flags with 0's to make size multiple of 8 */
532  v = flags;
533  size = G__null_bitstream_size(n);
534  count = 0;
535 
536  for (i = 0; i < size; i++) {
537  *v = 0;
538  k = 8;
539 
540  while (k-- > 0) {
541  if (count < n) {
542  *v = *v | ((unsigned char)zero_ones[count] << k);
543  }
544 
545  count++;
546  }
547 
548  v++;
549  }
550 
551  return 0;
552 }
553 
554 /****************************************************************************
555 * int G__convert_flags_01 (char *zero_ones, unsigned char *flags, int n)
556 *
557 * PURPOSE: To...
558 * INPUT VARS: zero_ones => ??
559 * flags => ??
560 * n => ??
561 * RETURN VAL: ??
562 *****************************************************************************/
563 int G__convert_flags_01(char *zero_ones, const unsigned char *flags, int n)
564 {
565  const unsigned char *v;
566  int count;
567  int size;
568  int i, k;
569 
570  count = 0;
571  v = flags;
572  size = G__null_bitstream_size(n);
573 
574  for (i = 0; i < size; i++) {
575  k = 8;
576 
577  while (k-- > 0) {
578  if (count < n) {
579  zero_ones[count] = ((*v & ((unsigned char)1 << k)) != 0);
580  count++;
581  }
582  }
583 
584  v++;
585  }
586 
587  return 0;
588 }
589 
590 /****************************************************************************
591 * int G__init_null_bits (unsigned char *flags, int cols)
592 *
593 * PURPOSE: To...
594 * INPUT VARS: flags => ??
595 * cols => ??
596 * RETURN VAL: ??
597 *****************************************************************************/
598 int G__init_null_bits(unsigned char *flags, int cols)
599 {
600  unsigned char *v;
601  int size;
602  int i;
603 
604  /* pad the flags with 0's to make size multiple of 8 */
605  v = flags;
606  size = G__null_bitstream_size(cols);
607 
608  for (i = 0; i < size; i++) {
609  if ((i + 1) * 8 <= cols) {
610  *v = (unsigned char)255;
611  }
612  else {
613  *v = (unsigned char)255 << ((i + 1) * 8 - cols);
614  }
615 
616  v++;
617  }
618 
619  return 0;
620 }
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
void G__set_null_value(void *rast, int numVals, int null_is_zero, RASTER_MAP_TYPE data_type)
Definition: null_val.c:90
#define FALSE
Definition: dbfopen.c:117
void G_set_d_null_value(DCELL *dcellVals, int numVals)
Definition: null_val.c:176
int count
int G__set_flags_from_01_random(const char *zero_ones, unsigned char *flags, int col, int n, int ncols)
Definition: null_val.c:476
tuple size
value.Bind(wx.EVT_TEXT, self.OnVolumeIsosurfMap)
Definition: tools.py:2334
int G_zero(void *buf, int i)
Zero out a buffer, buf, of length i.
Definition: gis/zero.c:29
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_insert_null_values(void *rast, char *null_row, int ncols, RASTER_MAP_TYPE data_type)
Insert NULL value.
Definition: null_val.c:341
size_t G_raster_size(RASTER_MAP_TYPE data_type)
Returns size of a raster CELL in bytes.
Definition: alloc_cell.c:38
int G__convert_01_flags(const char *zero_ones, unsigned char *flags, int n)
Definition: null_val.c:524
int G__init_null_bits(unsigned char *flags, int cols)
Definition: null_val.c:598
int G_insert_d_null_values(DCELL *dcellVal, char *null_row, int ncols)
Insert DCELL NULL value.
Definition: null_val.c:426
int G__convert_flags_01(char *zero_ones, const unsigned char *flags, int n)
Definition: null_val.c:563
int G_insert_f_null_values(FCELL *fcellVal, char *null_row, int ncols)
Insert FCELL NULL value.
Definition: null_val.c:398
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
void G_set_null_value(void *buf, int numVals, RASTER_MAP_TYPE data_type)
Definition: null_val.c:112
G_warning("category support for [%s] in mapset [%s] %s", name, mapset, type)
tuple cols
void G_set_c_null_value(CELL *cellVals, int numVals)
Definition: null_val.c:142
int G__check_null_bit(const unsigned char *flags, int bit_num, int n)
Definition: null_val.c:440
int n
Definition: dataquad.c:291
int G_is_null_value(const void *rast, RASTER_MAP_TYPE data_type)
If the data_type is CELL_TYPE, calls G_is_c_null_value ((CELL *) rast); If the data_type is FCELL_TYP...
Definition: null_val.c:207
int G_insert_c_null_values(CELL *cellVal, char *null_row, int ncols)
Insert CELL NULL value.
Definition: null_val.c:370