Geoinformatica  0.90
Operations.pm
Go to the documentation of this file.
1 #** @file Dialogs.pm
2 # @brief Adds overloaded operations to Geo::Raster
3 # @note Many methods may convert an integer raster into a floating
4 # point raster if the operation requires.
5 # @note All operations, which involve more than one raster, require
6 # that the rasters are overlayable.
7 #*
8 
9 package Geo::Raster;
10 
11 use strict;
12 use overload (
13  'fallback' => undef,
14  # not having "" overloaded makes print "$raster" to print "1"
15  '""' => 'as_string',
16  'bool' => 'bool',
17  '=' => 'shallow_copy',
18  'neg' => 'neg',
19  '+' => 'plus',
20  '-' => 'minus',
21  '*' => 'times',
22  '/' => 'over',
23  '%' => 'modulo',
24  '**' => 'power',
25  '+=' => 'add',
26  '-=' => 'subtract',
27  '*=' => 'multiply_by',
28  '/=' => 'divide_by',
29  '%=' => 'modulus_with',
30  '**=' => 'to_power_of',
31  '<' => 'lt',
32  '>' => 'gt',
33  '<=' => 'le',
34  '>=' => 'ge',
35  '==' => 'eq',
36  '!=' => 'ne',
37  '<=>' => 'cmp',
38  'atan2' => 'atan2',
39  'cos' => 'cos',
40  'sin' => 'sin',
41  'exp' => 'exp',
42  'abs' => 'abs',
43  'log' => 'log',
44  'sqrt' => 'sqrt',
45  );
46 use Scalar::Util 'blessed';
47 
48 sub as_string {
49  my $self = shift;
50  return $self;
51 }
52 
53 sub bool {
54  return 1;
55 }
56 
57 sub shallow_copy {
58  my $self = shift;
59  return $self;
60 }
61 
62 #** @method Geo::Raster neg()
63 #
64 # @brief Unary minus. Multiplies this raster with -1.
65 #
66 # @return A negated (multiplied by -1) raster.
67 #*
68 sub neg {
69  my $self = shift;
70  my $copy = Geo::Raster->new($self);
71  ral_grid_mult_integer($copy->{GRID}, -1);
72  return $copy;
73 }
74 
75 sub _typeconversion {
76  my($self,$other) = @_;
77  my $type = ral_grid_get_datatype($self->{GRID});
78  if (ref($other)) {
79  if (blessed($other) and $other->isa('Geo::Raster')) {
80  return $Geo::Raster::REAL_GRID if
81  ral_grid_get_datatype($other->{GRID}) == $Geo::Raster::REAL_GRID or
82  $type == $Geo::Raster::REAL_GRID;
83  return $Geo::Raster::INTEGER_GRID;
84  } else {
85  croak "$other is not a grid\n";
86  }
87  } else {
88  # perlfaq4: is scalar an integer ?
89  return $type if $other =~ /^-?\d+$/;
90 
91  # perlfaq4: is scalar a C float ?
92  if ($other =~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/) {
93  return $Geo::Raster::REAL_GRID if $type == $Geo::Raster::INTEGER_GRID;
94  return $type;
95  }
96  croak "$other is not numeric\n";
97  }
98 }
99 
100 
101 #** @method Geo::Raster plus($value)
102 #
103 # @brief Adds a number globally to the raster.
104 #
105 # Example:
106 # @code
107 # $b = $a + 3.14159;
108 # @endcode
109 # is the same as
110 # @code
111 # $b = $a->plus(3.14159);
112 # @endcode
113 #
114 # @param[in] value An integer or a floating point number to add to the
115 # cell values of this raster.
116 # @return the resulting raster.
117 #*
118 
119 #** @method Geo::Raster plus(Geo::Raster second)
120 #
121 # @brief Adds a raster to this raster.
122 #
123 # Example:
124 # @code
125 # $c = $a + $b;
126 # @endcode
127 # is the same as
128 # @code
129 # $c = $a->plus($b);
130 # @endcode
131 #
132 # @param[in] second A raster.
133 # @return the resulting raster.
134 #*
135 sub plus {
136  my($self, $second) = @_;
137  my $datatype = $self->_typeconversion($second);
138  my $copy = Geo::Raster->new(datatype=>$datatype, copy=>$self);
139  if (ref($second)) {
140  ral_grid_add_grid($copy->{GRID}, $second->{GRID});
141  } else {
142  my $dt = ral_grid_get_datatype($copy->{GRID});
143  if ($dt == $Geo::Raster::INTEGER_GRID and $second =~ /^-?\d+$/) {
144  ral_grid_add_integer($copy->{GRID}, $second);
145  } else {
146  ral_grid_add_real($copy->{GRID}, $second);
147  }
148  }
149  return $copy;
150 }
151 
152 #** @method Geo::Raster minus($value, $reversed)
153 #
154 # @brief Subtracts a value globally from this raster.
155 #
156 # Example:
157 # @code
158 # $b = $a - 3.14159;
159 # @endcode
160 # is the same as
161 # @code
162 # $b = -1*(3.14159 - $a);
163 # @endcode
164 #
165 # @param[in] value A value to subtract
166 # @param[in] reversed (optional) Whether to perform value - raster
167 # computation instead of raster - value. When operator '-' is used,
168 # this value is automatically set by Perl when appropriate.
169 # @return the resulting raster.
170 #*
171 
172 #** @method Geo::Raster minus(Geo::Raster second, $reversed)
173 #
174 # @brief Subtracts a raster from this raster.
175 #
176 # Example:
177 # @code
178 # $c = $b - $a;
179 # @endcode
180 # is the same as
181 # @code
182 # $c = $a->minus($b, 1);
183 # @endcode
184 #
185 # @param[in] second A raster to be subtracted.
186 # @param[in] reversed (optional) Whether to perform value - raster
187 # computation instead of raster - value. When operator '-' is used,
188 # this value is automatically set by Perl when appropriate.
189 # @return the resulting raster.
190 #*
191 sub minus {
192  my($self, $second, $reversed) = @_;
193  my $datatype = $self->_typeconversion($second);
194  my $copy = Geo::Raster->new(datatype=>$datatype, copy=>$self);
195  if (ref($second)) {
196  ($copy, $second) = ($second, $copy) if $reversed;
197  ral_grid_sub_grid($copy->{GRID}, $second->{GRID});
198  } else {
199  if ($reversed) {
200  ral_grid_mult_integer($copy->{GRID}, -1);
201  } else {
202  $second *= -1;
203  }
204 
205  if (ral_grid_get_datatype($copy->{GRID}) == $Geo::Raster::INTEGER_GRID and $second =~ /^-?\d+$/) {
206  # Second parameter is an integer.
207  ral_grid_add_integer($copy->{GRID}, $second);
208  } else {
209  # Second parameter is a real.
210  ral_grid_add_real($copy->{GRID}, $second);
211  }
212  }
213  return $copy;
214 }
215 
216 #** @method Geo::Raster times($value)
217 #
218 # @brief Multiplies the cells of this raster with a value.
219 #
220 # Example:
221 # @code
222 # $b = $a * 3.14159;
223 # @endcode
224 # is the same as
225 # @code
226 # $b = $a->times(3.14159);
227 # @endcode
228 #
229 # @param[in] value The multiplier.
230 # @return a new raster.
231 #*
232 
233 #** @method Geo::Raster times(Geo::Raster second)
234 #
235 # @brief Multiplies the cell values of this raster with the cell values of another raster.
236 #
237 # Example:
238 # @code
239 # $c = $a * $b;
240 # @endcode
241 # is the same as
242 # @code
243 # $c = $a->times($b);
244 # @endcode
245 #
246 # The effect of raster multiplication is
247 # @code
248 # for all cells: c[cell] = a[cell]*b[cell]
249 # @endcode
250 #
251 # @param[in] second The multiplier raster.
252 # @return a new raster.
253 #*
254 sub times {
255  my($self, $second) = @_;
256  my $datatype = $self->_typeconversion($second);
257  my $copy = Geo::Raster->new(datatype=>$datatype, copy=>$self);
258  if (ref($second)) {
259  ral_grid_mult_grid($copy->{GRID}, $second->{GRID});
260  } else {
261  if (ral_grid_get_datatype($copy->{GRID}) == $Geo::Raster::INTEGER_GRID and $second =~ /^-?\d+$/) {
262  ral_grid_mult_integer($copy->{GRID},$second);
263  } else {
264  ral_grid_mult_real($copy->{GRID},$second);
265  }
266  }
267  return $copy;
268 }
269 
270 #** @method Geo::Raster over($value, $reversed)
271 #
272 # @brief Divides the cell values of this raster with a value.
273 #
274 # Example:
275 # @code
276 # $b = $a / 3.14159;
277 # @endcode
278 # is the same as
279 # @code
280 # $b = 1/(3.14159 / $a);
281 # @endcode
282 #
283 # @param[in] value The divisor.
284 # @param[in] reversed (optional) Whether to perform value / raster
285 # computation instead of raster / value. When operator '/' is used,
286 # this value is automatically set by Perl when appropriate.
287 # @return the resulting raster.
288 #*
289 
290 #** @method Geo::Raster over(Geo::Raster second, $reversed)
291 #
292 # @brief Divides this raster with another raster.
293 #
294 # Example:
295 # @code
296 # $c = $a / $b;
297 # @endcode
298 # is the same as
299 # @code
300 # $c = $a->over($b);
301 # @endcode
302 # The effect of raster division is
303 # @code
304 # for all cells: c[cell] = a[cell]/b[cell]
305 # @endcode
306 #
307 # @param[in] second A raster
308 # @param[in] reversed (optional) Whether to perform value / raster
309 # computation instead of raster / value. When operator '/' is used,
310 # this value is automatically set by Perl when appropriate.
311 # @return the resulting raster.
312 #*
313 sub over {
314  my($self, $second, $reversed) = @_;
315  my $copy = new Geo::Raster datatype=>$Geo::Raster::REAL_GRID, copy=>$self;
316  if (ref($second)) {
317  ($copy, $second) = ($second, $copy) if $reversed;
318  ral_grid_div_grid($copy->{GRID}, $second->{GRID});
319  } else {
320  if ($reversed) {
321  if (ral_grid_get_datatype($copy->{GRID}) == $Geo::Raster::INTEGER_GRID and $second =~ /^-?\d+$/) {
322  ral_integer_div_grid($second, $copy->{GRID});
323  } else {
324  ral_real_div_grid($second, $copy->{GRID});
325  }
326  } else {
327  if (ral_grid_get_datatype($copy->{GRID}) == $Geo::Raster::INTEGER_GRID and $second =~ /^-?\d+$/) {
328  ral_grid_div_integer($copy->{GRID}, $second);
329  } else {
330  ral_grid_div_real($copy->{GRID}, $second);
331  }
332  }
333  }
334  return $copy;
335 }
336 
337 sub over2 {
338  my($self, $second, $reversed) = @_;
339  my $copy;
340  if($reversed) {
341  $copy = new Geo::Raster datatype=>$Geo::Raster::REAL_GRID, copy=>$second;
342  } else {
343  $copy = new Geo::Raster datatype=>$Geo::Raster::REAL_GRID, copy=>$self;
344  }
345 
346  if (ref($second)) {
347  # ($copy, $second) = ($second, $copy) if $reversed;
348  if($reversed) {
349  ral_grid_div_grid($copy->{GRID}, $self->{GRID});
350  } els {
351  ral_grid_div_grid($copy->{GRID}, $second->{GRID});
352  }
353  } else {
354  if ($reversed) {
355  if (ral_grid_get_datatype($copy->{GRID}) == $Geo::Raster::INTEGER_GRID and $second =~ /^-?\d+$/) {
356  ral_integer_div_grid($second, $copy->{GRID});
357  } else {
358  ral_real_div_grid($second, $copy->{GRID});
359  }
360  } else {
361  if (ral_grid_get_datatype($copy->{GRID}) == $Geo::Raster::INTEGER_GRID and $second =~ /^-?\d+$/) {
362  ral_grid_div_integer($copy->{GRID}, $second);
363  } else {
364  ral_grid_div_real($copy->{GRID}, $second);
365  }
366  }
367  }
368  return $copy;
369 }
370 
371 #** @method Geo::Raster modulo($value, $reversed)
372 #
373 # @brief Computes the modulus (remainder of division, Perl operator %)
374 # of this raster and an integer number.
375 #
376 # Example:
377 # @code
378 # $b = $a % 3;
379 # @endcode
380 # is the same as
381 # @code
382 # $b = $a->modulo(3);
383 # @endcode
384 #
385 # @param[in] value An integer number.
386 # @param[in] reversed (optional) Whether to perform value % raster
387 # computation instead of raster % value. When operator '%' is used,
388 # this value is automatically set by Perl when appropriate.
389 # @note This raster must be an integer raster.
390 # @return the resulting raster.
391 #*
392 
393 #** @method Geo::Raster modulo(Geo::Raster second, $reversed)
394 #
395 # @brief Computes the modulus (remainder of division, Perl operator %)
396 # of this raster and an integer raster.
397 #
398 # Example:
399 # @code
400 # $c = $a % $b;
401 # @endcode
402 # is the same as
403 # @code
404 # $c = $a->modulo($b);
405 # @endcode
406 #
407 # @param[in] second An integer raster.
408 # @param[in] reversed (optional) Whether to perform second % raster
409 # computation instead of raster % second. When operator '%' is used,
410 # this value is automatically set by Perl when appropriate.
411 # @note This raster must be an integer raster.
412 # @return the resulting raster.
413 #*
414 sub modulo {
415  my($self, $second, $reversed) = @_;
416  my $copy = new Geo::Raster($self);
417  if (ref($second)) {
418  ($copy, $second) = ($second, $copy) if $reversed;
419  ral_grid_modulus_grid($copy->{GRID}, $second->{GRID});
420  } else {
421  if ($reversed) {
422  ral_integer_modulus_grid($second, $copy->{GRID});
423  } else {
424  ral_grid_modulus_integer($copy->{GRID}, $second);
425  }
426  }
427  return $copy;
428 }
429 
430 #** @method Geo::Raster power($exponent, $reversed)
431 #
432 # @brief Computes the power (Perl operator **) of this raster and an
433 # exponent.
434 #
435 # Example:
436 # @code
437 # $b = $a ** 3.14159;
438 # @endcode
439 # is the same as
440 # @code
441 # $b = $a->power(3.14159);
442 # @endcode
443 #
444 # @param[in] exponent A number.
445 # @param[in] reversed (optional) Whether to perform exponent ** raster
446 # computation instead of raster ** exponent. When operator '**' is used,
447 # this value is automatically set by Perl when appropriate.
448 # @return the resulting raster.
449 #*
450 
451 #** @method Geo::Raster power(Geo::Raster exponent, $reversed)
452 #
453 # @brief Computes the power (Perl operator **) of this raster an
454 # exponent raster.
455 #
456 # Example:
457 # @code
458 # $c = $a ** $b;
459 # @endcode
460 # is the same as
461 # @code
462 # $c = $a->power($b);
463 # @endcode
464 #
465 # @param[in] exponent A raster.
466 # @param[in] reversed (optional) Whether to perform exponent ** raster
467 # computation instead of raster ** exponent. When operator '**' is used,
468 # this value is automatically set by Perl when appropriate.
469 # @return the resulting raster.
470 #*
471 sub power {
472  my($self, $second, $reversed) = @_;
473  my $datatype = $self->_typeconversion($second);
474  return unless defined($datatype);
475  my $copy = Geo::Raster->new(datatype=>$datatype, copy=>$self);
476  if (ref($second)) {
477  ($copy, $second) = ($second, $copy) if $reversed;
478  ral_grid_power_grid($copy->{GRID}, $second->{GRID});
479  } else {
480  if ($reversed) {
481  ral_realpower_grid($second, $copy->{GRID});
482  } else {
483  ral_grid_power_real($copy->{GRID}, $second);
484  }
485  }
486  return $copy;
487 }
488 
489 #** @method add($value)
490 #
491 # @brief Adds a number in-place to the cell values of this raster.
492 #
493 # Example:
494 # @code
495 # $a += 3.14159;
496 # @endcode
497 # is the same as
498 # @code
499 # $a->add(3.14159);
500 # @endcode
501 #
502 # @param[in] value The number to add.
503 #*
504 
505 #** @method Geo::Raster add(Geo::Raster second)
506 #
507 # @brief Adds another raster to this raster.
508 #
509 # Example of addition
510 # @code
511 # $a += $b;
512 # @endcode
513 # is the same as
514 # @code
515 # $a->add($b);
516 # @endcode
517 #
518 # @param[in] second A raster to add.
519 #*
520 sub add {
521  my($self, $second) = @_;
522  my $datatype = $self->_typeconversion($second);
523  return unless defined($datatype);
524  $self->_new_grid(ral_grid_create_copy($self->{GRID}, $datatype))
525  if $datatype != ral_grid_get_datatype($self->{GRID});
526  if (ref($second)) {
527  ral_grid_add_grid($self->{GRID}, $second->{GRID});
528  } else {
529  if (ral_grid_get_datatype($self->{GRID}) == $Geo::Raster::INTEGER_GRID and $second =~ /^-?\d+$/) {
530  ral_grid_add_integer($self->{GRID}, $second);
531  } else {
532  ral_grid_add_real($self->{GRID}, $second);
533  }
534  }
535  return $self;
536 }
537 
538 #** @method Geo::Raster subtract($value)
539 #
540 # @brief Subtracts a number from the cell values.
541 #
542 # Example of subtraction
543 # @code
544 # $a -= 3.14159;
545 # @endcode
546 # is the same as
547 # @code
548 # $a->subtract(3.14159);
549 # @endcode
550 #
551 # @param[in] value A number that is subtracted from all cells of this raster.
552 #*
553 
554 #** @method Geo::Raster subtract(Geo::Raster second)
555 #
556 # @brief Subtracts from the cell value the respective cell values of the given raster.
557 #
558 # Example of subtraction
559 # @code
560 # $a -= $b;
561 # @endcode
562 # is the same as
563 # @code
564 # $a->subtract($b);
565 # @endcode
566 #
567 # @param[in] second A raster, whose cell values are to be subtracted
568 # from the cell values of this raster.
569 #*
570 sub subtract {
571  my($self, $second) = @_;
572  my $datatype = $self->_typeconversion($second);
573  return unless defined($datatype);
574  $self->_new_grid(ral_grid_create_copy($self->{GRID}, $datatype)) if $datatype != ral_grid_get_datatype($self->{GRID});
575  if (ref($second)) {
576  ral_grid_sub_grid($self->{GRID}, $second->{GRID});
577  } else {
578  if (ral_grid_get_datatype($self->{GRID}) == $Geo::Raster::INTEGER_GRID and $second =~ /^-?\d+$/) {
579  ral_grid_add_integer($self->{GRID}, -$second);
580  } else {
581  ral_grid_add_real($self->{GRID}, -$second);
582  }
583  }
584  return $self;
585 }
586 
587 #** @method Geo::Raster multiply_by($value)
588 #
589 # @brief Multiplies the cell values of this raster with the given number.
590 #
591 # Example:
592 # @code
593 # $a *= 3.14159;
594 # @endcode
595 # is the same as
596 # @code
597 # $a->multiply_by(3.14159);
598 # @endcode
599 #
600 # @param[in] value The multiplier.
601 #*
602 
603 #** @method Geo::Raster multiply_by(Geo::Raster second)
604 #
605 # @brief Multiplies the cell values of this raster with the respective
606 # cell values of the given raster.
607 #
608 # Example of multiplication
609 # @code
610 # $a *= $b;
611 # @endcode
612 # is the same as
613 # @code
614 # $a->multiply_by($b);
615 # @endcode
616 #
617 # @param[in] second A raster.
618 #*
619 sub multiply_by {
620  my($self, $second) = @_;
621  my $datatype = $self->_typeconversion($second);
622  return unless defined($datatype);
623  $self->_new_grid(ral_grid_create_copy($self->{GRID}, $datatype)) if $datatype != ral_grid_get_datatype($self->{GRID});
624  if (ref($second)) {
625  ral_grid_mult_grid($self->{GRID}, $second->{GRID});
626  } else {
627  if (ral_grid_get_datatype($self->{GRID}) == $Geo::Raster::INTEGER_GRID and $second =~ /^-?\d+$/) {
628  ral_grid_mult_integer($self->{GRID}, $second);
629  } else {
630  ral_grid_mult_real($self->{GRID}, $second);
631  }
632  }
633  return $self;
634 }
635 
636 #** @method Geo::Raster divide_by($value)
637 #
638 # @brief Divides the cell values of this raster with the given number.
639 #
640 # Example:
641 # @code
642 # $a /= 3.14159;
643 # @endcode
644 # is the same as
645 # @code
646 # $a->divide_by(3.14159);
647 # @endcode
648 #
649 # @param[in] value A number.
650 #*
651 
652 #** @method Geo::Raster divide_by(Geo::Raster second)
653 #
654 # @brief Divides the cell values of this raster with the respective
655 # cell values of the other raster.
656 #
657 # Example:
658 # @code
659 # $a /= $b;
660 # @endcode
661 # is the same as
662 # @code
663 # $a->divide_by($b);
664 # @endcode
665 #
666 # @param[in] second A raster.
667 #*
668 sub divide_by {
669  my($self, $second) = @_;
670  $self->_new_grid(ral_grid_create_copy($self->{GRID}, $Geo::Raster::REAL_GRID));
671  if (ref($second)) {
672  ral_grid_div_grid($self->{GRID}, $second->{GRID});
673  } else {
674  if (ral_grid_get_datatype($self->{GRID}) == $Geo::Raster::INTEGER_GRID and $second =~ /^-?\d+$/) {
675  ral_grid_div_integer($self->{GRID}, $second);
676  } else {
677  ral_grid_div_real($self->{GRID}, $second);
678  }
679  }
680  return $self;
681 }
682 
683 #** @method Geo::Raster modulus_with($value)
684 #
685 # @brief Computes the modulus of each cell value and the given number
686 # and assigns that to the cell.
687 #
688 # Example:
689 # @code
690 # $a %= 3;
691 # @endcode
692 # is the same as
693 # @code
694 # $a->modulus_with(3);
695 # @endcode
696 #
697 # @param[in] value An integer number.
698 # @note Defined only for integer rasters.
699 #*
700 
701 #** @method Geo::Raster modulus_with(Geo::Raster second)
702 #
703 # @brief Computes the modulus of each cell value of this raster and
704 # the respective cell value of the given integer raster.
705 #
706 # Example:
707 # @code
708 # $a %= $b;
709 # @endcode
710 # is the same as
711 # @code
712 # $a->modulus_with($b);
713 # @endcode
714 #
715 # @param[in] second An integer raster.
716 # @note Defined only for integer rasters.
717 #*
718 sub modulus_with {
719  my($self, $second) = @_;
720  if (ref($second)) {
721  ral_grid_modulus_grid($self->{GRID}, $second->{GRID});
722  } else {
723  ral_grid_modulus_integer($self->{GRID}, $second);
724  }
725  return $self;
726 }
727 
728 #** @method Geo::Raster to_power_of($power)
729 #
730 # @brief Raises the cell values of this raster to the given power.
731 #
732 # Example:
733 # @code
734 # $a **= 3.14159;
735 # @endcode
736 # is the same as
737 # @code
738 # $a->to_power_of(3.14159);
739 # @endcode
740 #
741 # @param[in] power The exponent.
742 #*
743 
744 #** @method Geo::Raster to_power_of(Geo::Raster second)
745 #
746 # @brief Raises the cell values to the power of the respective cell
747 # values of the given raster.
748 #
749 # Example:
750 # @code
751 # $a **= $b;
752 # @endcode
753 # is the same as
754 # @code
755 # $a->to_power_of($b);
756 # @endcode
757 #
758 # @param[in] second A raster, whose cell values are used as exponents.
759 #*
760 sub to_power_of {
761  my($self, $second) = @_;
762  my $datatype = $self->_typeconversion($second);
763  return unless defined($datatype);
764  $self->_new_grid(ral_grid_create_copy($self->{GRID}, $datatype)) if $datatype != ral_grid_get_datatype($self->{GRID});
765  if (ref($second)) {
766  ral_grid_power_grid($self->{GRID}, $second->{GRID});
767  } else {
768  ral_grid_power_real($self->{GRID}, $second);
769  }
770  return $self;
771 }
772 
773 #** @method Geo::Raster atan2(Geo::Raster second)
774 #
775 # @brief Calculates at each cell the arc-tangent of this and the
776 # second raster.
777 #
778 # @param[in] second A raster.
779 # @return a new raster. In void context changes this raster.
780 #*
781 sub atan2 {
782  my($self, $second) = @_;
783  if (ref($self) and ref($second)) {
784  if (defined wantarray) {
785  $self = new Geo::Raster datatype=>$Geo::Raster::REAL_GRID, copy=>$self;
786  } elsif (ral_grid_get_datatype($self->{GRID}) == $Geo::Raster::INTEGER_GRID) {
787  $self->_new_grid(ral_grid_create_copy($self->{GRID}, $Geo::Raster::REAL_GRID));
788  }
789  ral_grid_atan2($self->{GRID}, $second->{GRID});
790  return $self;
791  } else {
792  croak "don't mix scalars and rasters in atan2, please";
793  }
794 }
795 
796 #** @method Geo::Raster cos()
797 #
798 # @brief Calculates the cosine at each cell.
799 #
800 # @return a new raster. In void context changes this raster.
801 # @note The resulting raster will always have as datatype real.
802 #*
803 sub cos {
804  my $self = shift;
805  if (defined wantarray) {
806  $self = new Geo::Raster datatype=>$Geo::Raster::REAL_GRID, copy=>$self;
807  } elsif (ral_grid_get_datatype($self->{GRID}) == $Geo::Raster::INTEGER_GRID) {
808  $self->_new_grid(ral_grid_create_copy($self->{GRID}, $Geo::Raster::REAL_GRID));
809  }
810  ral_grid_cos($self->{GRID});
811  return $self;
812 }
813 
814 #** @method Geo::Raster sin()
815 #
816 # @brief Calculates the sine at each cell.
817 #
818 # @return a new raster. In void context changes this raster.
819 #*
820 sub sin {
821  my $self = shift;
822  if (defined wantarray) {
823  $self = new Geo::Raster datatype=>$Geo::Raster::REAL_GRID, copy=>$self;
824  } elsif (ral_grid_get_datatype($self->{GRID}) == $Geo::Raster::INTEGER_GRID) {
825  $self->_new_grid(ral_grid_create_copy($self->{GRID}, $Geo::Raster::REAL_GRID));
826  }
827  ral_grid_sin($self->{GRID});
828  return $self;
829 }
830 
831 #** @method Geo::Raster exp()
832 #
833 # @brief Calculates the exponential function at each cell.
834 #
835 # @return a new raster. In void context changes this raster.
836 #*
837 sub exp {
838  my $self = shift;
839  if (defined wantarray) {
840  $self = new Geo::Raster datatype=>$Geo::Raster::REAL_GRID, copy=>$self;
841  } elsif (ral_grid_get_datatype($self->{GRID}) == $Geo::Raster::INTEGER_GRID) {
842  $self->_new_grid(ral_grid_create_copy($self->{GRID}, $Geo::Raster::REAL_GRID));
843  }
844  ral_grid_exp($self->{GRID});
845  return $self;
846 }
847 
848 #** @method Geo::Raster abs()
849 #
850 # @brief Calculates the absolute value at each cell.
851 #
852 # @return a new raster. In void context changes this raster.
853 #*
854 sub abs {
855  my $self = shift;
856  if (defined wantarray) {
857  my $copy = new Geo::Raster($self);
858  ral_grid_abs($copy->{GRID});
859  return $copy;
860  } else {
861  ral_grid_abs($self->{GRID});
862  }
863 }
864 
865 #** @method Geo::Raster sqrt()
866 #
867 # @brief Calculates the square root at each cell.
868 #
869 # @return a new raster. In void context changes this raster.
870 #*
871 sub sqrt {
872  my $self = shift;
873  if (defined wantarray) {
874  $self = new Geo::Raster datatype=>$Geo::Raster::REAL_GRID, copy=>$self;
875  } elsif (ral_grid_get_datatype($self->{GRID}) == $Geo::Raster::INTEGER_GRID) {
876  $self->_new_grid(ral_grid_create_copy($self->{GRID}, $Geo::Raster::REAL_GRID));
877  }
878  ral_grid_sqrt($self->{GRID});
879  return $self;
880 }
881 
882 #** @method Geo::Raster round()
883 #
884 # @brief Rounds the value at each cell.
885 #
886 # @return a new integer raster. In void context changes this raster.
887 #*
888 sub round {
889  my $self = shift;
890  if (ref($self)) {
891  my $grid = ral_grid_round($self->{GRID});
892  return unless $grid;
893  if (defined wantarray) {
894  my $new = new Geo::Raster $grid;
895  return $new;
896  } else {
897  $self->_new_grid($grid);
898  }
899  } else {
900  return $self < 0 ? POSIX::floor($self - 0.5) : POSIX::floor($self + 0.5);
901  }
902 }
903 
904 {
905  no warnings 'redefine';
906 
907 #** @method Geo::Raster acos()
908 #
909 # @brief Calculates the arc-cosine at each cell.
910 #
911 # @return a new raster. In void context changes this raster.
912 #*
913 sub acos {
914  my $self = shift;
915  if (defined wantarray) {
916  $self = new Geo::Raster datatype=>$Geo::Raster::REAL_GRID, copy=>$self;
917  } elsif (ral_grid_get_datatype($self->{GRID}) == $Geo::Raster::INTEGER_GRID) {
918  $self->_new_grid(ral_grid_create_copy($self->{GRID}, $Geo::Raster::REAL_GRID));
919  }
920  ral_grid_acos($self->{GRID});
921  return $self;
922 }
923 
924 #** @method Geo::Raster atan()
925 #
926 # @brief Calculates the arc-tangent at each cell.
927 #
928 # @return a new raster. In void context changes this raster.
929 #*
930 sub atan {
931  my $self = shift;
932  if (defined wantarray) {
933  $self = new Geo::Raster datatype=>$Geo::Raster::REAL_GRID, copy=>$self;
934  } elsif (ral_grid_get_datatype($self->{GRID}) == $Geo::Raster::INTEGER_GRID) {
935  $self->_new_grid(ral_grid_create_copy($self->{GRID}, $Geo::Raster::REAL_GRID));
936  }
937  ral_grid_atan($self->{GRID});
938  return $self;
939 }
940 
941 #** @method Geo::Raster ceil()
942 #
943 # @brief Calculates at each cell the smallest integer not less than
944 # the value.
945 #
946 # @return a new integer raster. In void context changes this raster.
947 #*
948 sub ceil {
949  my $self = shift;
950  if (ref($self)) {
951  $self = new Geo::Raster($self) if defined wantarray;
952  ral_grid_ceil($self->{GRID});
953  return $self;
954  } else {
955  return POSIX::ceil($self);
956  }
957 }
958 
959 #** @method Geo::Raster cosh()
960 #
961 # @brief Calculates at each cell the hyperbolic cosine of the value.
962 #
963 # @return a new raster. In void context changes this raster.
964 #*
965 sub cosh {
966  my $self = shift;
967  if (defined wantarray) {
968  $self = new Geo::Raster datatype=>$Geo::Raster::REAL_GRID, copy=>$self;
969  } elsif (ral_grid_get_datatype($self->{GRID}) == $Geo::Raster::INTEGER_GRID) {
970  $self->_new_grid(ral_grid_create_copy($self->{GRID}, $Geo::Raster::REAL_GRID));
971  }
972  ral_grid_cosh($self->{GRID});
973  return $self;
974 }
975 
976 #** @method Geo::Raster floor()
977 #
978 # @brief Calculates at each cell the largest integer less than or
979 # equal to the value.
980 #
981 # @return a new integer raster. In void context changes this raster.
982 #*
983 sub floor {
984  my $self = shift;
985  if (ref($self)) {
986  $self = new Geo::Raster($self) if defined wantarray;
987  ral_grid_floor($self->{GRID});
988  return $self;
989  } else {
990  return POSIX::floor($self);
991  }
992 }
993 
994 #** @method Geo::Raster log()
995 #
996 # @brief Calculates the logarithm at each cell.
997 #
998 # @return a new raster. In void context changes this raster.
999 #*
1000 sub log {
1001  my $self = shift;
1002  if (defined wantarray) {
1003  $self = new Geo::Raster datatype=>$Geo::Raster::REAL_GRID, copy=>$self;
1004  } elsif (ral_grid_get_datatype($self->{GRID}) == $Geo::Raster::INTEGER_GRID) {
1005  $self->_new_grid(ral_grid_create_copy($self->{GRID}, $Geo::Raster::REAL_GRID));
1006  }
1007  ral_grid_log($self->{GRID});
1008  return $self;
1009 }
1010 
1011 #** @method Geo::Raster log10()
1012 #
1013 # @brief Calculates the base-10 logarithm at each cell.
1014 #
1015 # @return a new raster. In void context changes this raster.
1016 #*
1017 sub log10 {
1018  my $self = shift;
1019  if (defined wantarray) {
1020  $self = new Geo::Raster datatype=>$Geo::Raster::REAL_GRID, copy=>$self;
1021  } elsif (ral_grid_get_datatype($self->{GRID}) == $Geo::Raster::INTEGER_GRID) {
1022  $self->_new_grid(ral_grid_create_copy($self->{GRID}, $Geo::Raster::REAL_GRID));
1023  }
1024  ral_grid_log10($self->{GRID});
1025  return $self;
1026 }
1027 
1028 ## @fn log_base($base, $value)
1029 #
1030 # @brief Calculates the logarithm with a desired base.
1031 # @param base Desired logarithm base.
1032 # @param value Value for which the logarithm is calculated.
1033 # @return the result of the logarithm function.
1034 #*
1035 sub log_base {
1036  my ($base, $value) = @_;
1037  return CORE::log($value)/CORE::log($base);
1038 }
1039 
1040 #** @method Geo::Raster sinh()
1041 #
1042 # @brief Calculates the hyperbolic sine at each cell.
1043 #
1044 # @return a new raster. In void context changes this raster.
1045 #*
1046 sub sinh {
1047  my $self = shift;
1048  if (defined wantarray) {
1049  $self = new Geo::Raster datatype=>$Geo::Raster::REAL_GRID, copy=>$self;
1050  } elsif (ral_grid_get_datatype($self->{GRID}) == $Geo::Raster::INTEGER_GRID) {
1051  $self->_new_grid(ral_grid_create_copy($self->{GRID}, $Geo::Raster::REAL_GRID));
1052  }
1053  ral_grid_sinh($self->{GRID});
1054  return $self;
1055 }
1056 
1057 #** @method Geo::Raster tan()
1058 #
1059 # @brief Calculates the tangent at each cell.
1060 #
1061 # @return a new raster. In void context changes this raster.
1062 #*
1063 sub tan {
1064  my $self = shift;
1065  if (defined wantarray) {
1066  $self = new Geo::Raster datatype=>$Geo::Raster::REAL_GRID, copy=>$self;
1067  } elsif (ral_grid_get_datatype($self->{GRID}) == $Geo::Raster::INTEGER_GRID) {
1068  $self->_new_grid(ral_grid_create_copy($self->{GRID}, $Geo::Raster::REAL_GRID));
1069  }
1070  ral_grid_tan($self->{GRID});
1071  return $self;
1072 }
1073 
1074 #** @method Geo::Raster tanh()
1075 #
1076 # @brief Calculates the hyperbolic tangent at each cell.
1077 #
1078 # @return a new raster. In void context changes this raster.
1079 #*
1080 sub tanh {
1081  my $self = shift;
1082  if (defined wantarray) {
1083  $self = new Geo::Raster datatype=>$Geo::Raster::REAL_GRID, copy=>$self;
1084  } elsif (ral_grid_get_datatype($self->{GRID}) == $Geo::Raster::INTEGER_GRID) {
1085  $self->_new_grid(ral_grid_create_copy($self->{GRID}, $Geo::Raster::REAL_GRID));
1086  }
1087  ral_grid_tanh($self->{GRID});
1088  return $self;
1089 }
1090 }
1091 
1092 #** @method Geo::Raster lt($second, $reversed)
1093 #
1094 # @brief Performs at each cell the "less than" comparison
1095 # on this raster and a number.
1096 #
1097 # Example:
1098 # @code
1099 # $b = $a < 3.14159;
1100 # @endcode
1101 # is the same as
1102 # @code
1103 # $b = $a->lt(3);
1104 # @endcode
1105 #
1106 # @param[in] second A number to compare against.
1107 # @param[in] reversed (optional) Whether to perform this < second
1108 # computation instead of second < this. When operator '<' is used,
1109 # this value is automatically set by Perl when appropriate.
1110 # @return a new integer raster, which is 0 where this raster is greater
1111 # than or equal to the second, and 1 where it is less than the
1112 # second. In void context changes this raster.
1113 #*
1114 
1115 #** @method Geo::Raster lt(Geo::Raster second, $reversed)
1116 #
1117 # @brief Performs at each cell the "less than" comparison
1118 # on this and the second raster.
1119 #
1120 # Example:
1121 # @code
1122 # $c = $a < $b;
1123 # @endcode
1124 # is the same as
1125 # @code
1126 # $c = $a->lt($b);
1127 # @endcode
1128 #
1129 # @param[in] second A raster to compare against.
1130 # @param[in] reversed (optional) Whether to perform this < second
1131 # computation instead of second < this. When operator '<' is used,
1132 # this value is automatically set by Perl when appropriate.
1133 # @return a new integer raster, which is 0 where this raster is greater
1134 # than or equal to the second, and 1 where it is less than the
1135 # second. In void context changes this raster.
1136 #*
1137 sub lt {
1138  my($self, $second, $reversed) = @_;
1139  $self = Geo::Raster->new($self) if defined wantarray;
1140  if (ref($second)) {
1141  ral_grid_lt_grid($self->{GRID}, $second->{GRID});
1142  } else {
1143  if ($reversed) {
1144  if (ral_grid_get_datatype($self->{GRID}) == $Geo::Raster::INTEGER_GRID and $second =~ /^-?\d+$/) {
1145  ral_grid_gt_integer($self->{GRID}, $second);
1146  } else {
1147  ral_grid_gt_real($self->{GRID}, $second);
1148  }
1149  } else {
1150  if (ral_grid_get_datatype($self->{GRID}) == $Geo::Raster::INTEGER_GRID and $second =~ /^-?\d+$/) {
1151  ral_grid_lt_integer($self->{GRID}, $second);
1152  } else {
1153  ral_grid_lt_real($self->{GRID}, $second);
1154  }
1155  }
1156  }
1157  return $self if defined wantarray;
1158 }
1159 
1160 #** @method Geo::Raster gt($second, $reversed)
1161 #
1162 # @brief Performs at each cell the "greater than" comparison
1163 # on this raster and a number.
1164 #
1165 # Example:
1166 # @code
1167 # $b = $a > 3.14159;
1168 # @endcode
1169 # is the same as
1170 # @code
1171 # $b = $a->gt(3);
1172 # @endcode
1173 #
1174 # @param[in] second A number to compare against.
1175 # @param[in] reversed (optional) Whether to perform this > second
1176 # computation instead of second > this. When operator '>' is used,
1177 # this value is automatically set by Perl when appropriate.
1178 # @return a new integer raster, which is 0 where this raster is less
1179 # than or equal to the second, and 1 where it is greater than the
1180 # second. In void context changes this raster.
1181 #*
1182 
1183 #** @method Geo::Raster gt(Geo::Raster second, $reversed)
1184 #
1185 # @brief Performs at each cell the "greater than" comparison
1186 # on this and the second raster.
1187 #
1188 # Example:
1189 # @code
1190 # $c = $a > $b;
1191 # @endcode
1192 # is the same as
1193 # @code
1194 # $c = $a->gt($b);
1195 # @endcode
1196 #
1197 # @param[in] second A raster to compare against.
1198 # @param[in] reversed (optional) Whether to perform this > second
1199 # computation instead of second > this. When operator '>' is used,
1200 # this value is automatically set by Perl when appropriate.
1201 # @return a new integer raster, which is 0 where this raster is less
1202 # than or equal to the second, and 1 where it is greater than the
1203 # second. In void context changes this raster.
1204 #*
1205 sub gt {
1206  my($self, $second, $reversed) = @_;
1207  $self = Geo::Raster->new($self) if defined wantarray;
1208  if (ref($second)) {
1209  ral_grid_gt_grid($self->{GRID}, $second->{GRID});
1210  } else {
1211  if ($reversed) {
1212  if (ral_grid_get_datatype($self->{GRID}) == $Geo::Raster::INTEGER_GRID and $second =~ /^-?\d+$/) {
1213  ral_grid_lt_integer($self->{GRID}, $second);
1214  } else {
1215  ral_grid_lt_real($self->{GRID}, $second);
1216  }
1217  } else {
1218  if (ral_grid_get_datatype($self->{GRID}) == $Geo::Raster::INTEGER_GRID and $second =~ /^-?\d+$/) {
1219  ral_grid_gt_integer($self->{GRID}, $second);
1220  } else {
1221  ral_grid_gt_real($self->{GRID}, $second);
1222  }
1223  }
1224  }
1225  return $self if defined wantarray;
1226 }
1227 
1228 #** @method Geo::Raster le($second, $reversed)
1229 #
1230 # @brief Performs at each cell the "less than or equal to" comparison
1231 # on this raster and a number.
1232 #
1233 # Example:
1234 # @code
1235 # $b = $a <= 3.14159;
1236 # @endcode
1237 # is the same as
1238 # @code
1239 # $b = $a->le(3);
1240 # @endcode
1241 #
1242 # @param[in] second A number to compare against.
1243 # @param[in] reversed (optional) Whether to perform this <= second
1244 # computation instead of second <= this. When operator '<=' is used,
1245 # this value is automatically set by Perl when appropriate.
1246 # @return a new integer raster, which is 0 where this raster is greater
1247 # than the second, and 1 where it is less than or equal to the second. In void
1248 # context changes this raster.
1249 #*
1250 
1251 #** @method Geo::Raster le(Geo::Raster second, $reversed)
1252 #
1253 # @brief Performs at each cell the "greater than or equal to" comparison
1254 # on this and the second raster.
1255 #
1256 # Example:
1257 # @code
1258 # $c = $a <= $b;
1259 # @endcode
1260 # is the same as
1261 # @code
1262 # $c = $a->le($b);
1263 # @endcode
1264 #
1265 # @param[in] second A raster to compare against.
1266 # @param[in] reversed (optional) Whether to perform this <= second
1267 # computation instead of second <= this. When operator '<=' is used,
1268 # this value is automatically set by Perl when appropriate.
1269 # @return a new integer raster, which is 0 where this raster is greater
1270 # than the second, and 1 where it is less than or equal to the second. In void
1271 # context changes this raster.
1272 #*
1273 sub le {
1274  my($self, $second, $reversed) = @_;
1275  $self = Geo::Raster->new($self) if defined wantarray;
1276  if (ref($second)) {
1277  ral_grid_le_grid($self->{GRID}, $second->{GRID});
1278  } else {
1279  if ($reversed) {
1280  if (ral_grid_get_datatype($self->{GRID}) == $Geo::Raster::INTEGER_GRID and $second =~ /^-?\d+$/) {
1281  ral_grid_ge_integer($self->{GRID}, $second);
1282  } else {
1283  ral_grid_ge_real($self->{GRID}, $second);
1284  }
1285  } else {
1286  if (ral_grid_get_datatype($self->{GRID}) == $Geo::Raster::INTEGER_GRID and $second =~ /^-?\d+$/) {
1287  ral_grid_le_integer($self->{GRID}, $second);
1288  } else {
1289  ral_grid_le_real($self->{GRID}, $second);
1290  }
1291  }
1292  }
1293  return $self if defined wantarray;
1294 }
1295 
1296 #** @method Geo::Raster ge($second, $reversed)
1297 #
1298 # @brief Performs at each cell the "greater than or equal to" comparison
1299 # on this raster and a number.
1300 #
1301 # Example:
1302 # @code
1303 # $b = $a >= 3.14159;
1304 # @endcode
1305 # is the same as
1306 # @code
1307 # $b = $a->ge(3);
1308 # @endcode
1309 #
1310 # @param[in] second A number to compare against.
1311 # @param[in] reversed (optional) Whether to perform this >= second
1312 # computation instead of second >= this. When operator '>=' is used,
1313 # this value is automatically set by Perl when appropriate.
1314 # @return a new integer raster, which is 0 where this raster is less
1315 # than the second, and 1 where it is greater than the second. In void
1316 # context changes this raster.
1317 #*
1318 
1319 #** @method Geo::Raster ge(Geo::Raster second, $reversed)
1320 #
1321 # @brief Performs at each cell the "greater than or equal to" comparison
1322 # on this and the second raster.
1323 #
1324 # Example:
1325 # @code
1326 # $c = $a >= $b;
1327 # @endcode
1328 # is the same as
1329 # @code
1330 # $c = $a->ge($b);
1331 # @endcode
1332 #
1333 # @param[in] second A raster to compare against.
1334 # @param[in] reversed (optional) Whether to perform this >= second
1335 # computation instead of second >= this. When operator '>=' is used,
1336 # this value is automatically set by Perl when appropriate.
1337 # @return a new integer raster, which is 0 where this raster is less
1338 # than the second, and 1 where it is greater than the second. In void
1339 # context changes this raster.
1340 #*
1341 sub ge {
1342  my($self, $second, $reversed) = @_;
1343  $self = Geo::Raster->new($self) if defined wantarray;
1344  if (ref($second)) {
1345  ral_grid_ge_grid($self->{GRID}, $second->{GRID});
1346  } else {
1347  if ($reversed) {
1348  if (ral_grid_get_datatype($self->{GRID}) == $Geo::Raster::INTEGER_GRID and $second =~ /^-?\d+$/) {
1349  ral_grid_le_integer($self->{GRID}, $second);
1350  } else {
1351  ral_grid_le_real($self->{GRID}, $second);
1352  }
1353  } else {
1354  if (ral_grid_get_datatype($self->{GRID}) == $Geo::Raster::INTEGER_GRID and $second =~ /^-?\d+$/) {
1355  ral_grid_ge_integer($self->{GRID}, $second);
1356  } else {
1357  ral_grid_ge_real($self->{GRID}, $second);
1358  }
1359  }
1360  }
1361  return $self if defined wantarray;
1362 }
1363 
1364 #** @method Geo::Raster eq($second)
1365 #
1366 # @brief Performs at each cell the "equal to" comparison on this raster
1367 # and a number.
1368 #
1369 # Example:
1370 # @code
1371 # $b = $a == 3;
1372 # @endcode
1373 # is the same as
1374 # @code
1375 # $b = $a->eq(3);
1376 # @endcode
1377 #
1378 # @param[in] second A number to compare against.
1379 # @return a new integer raster, which is 0 where this raster is not equal
1380 # to the second, and 1 where they are equal. In void context
1381 # changes this raster.
1382 #*
1383 
1384 #** @method Geo::Raster eq(Geo::Raster second)
1385 #
1386 # @brief Performs at each cell the "equal to" comparison on this and the second
1387 # raster.
1388 #
1389 # Example:
1390 # @code
1391 # $c = $a == $b;
1392 # @endcode
1393 # is the same as
1394 # @code
1395 # $c = $a->eq($b);
1396 # @endcode
1397 #
1398 # @param[in] second A raster to compare against.
1399 # @return a new integer raster, which is 0 where this raster is not equal
1400 # ot the second, and 1 where they are equal. In void context
1401 # changes this raster.
1402 #*
1403 sub eq {
1404  my $self = shift;
1405  my $second = shift;
1406  $self = Geo::Raster->new($self) if defined wantarray;
1407  if (ref($second)) {
1408  ral_grid_eq_grid($self->{GRID}, $second->{GRID});
1409  } else {
1410  if (ral_grid_get_datatype($self->{GRID}) == $Geo::Raster::INTEGER_GRID and $second =~ /^-?\d+$/) {
1411  ral_grid_eq_integer($self->{GRID}, $second);
1412  } else {
1413  ral_grid_eq_real($self->{GRID}, $second);
1414  }
1415  }
1416  return $self if defined wantarray;
1417 }
1418 
1419 #** @method Geo::Raster ne($second)
1420 #
1421 # @brief Performs at each cell the "not equal to" comparison on this raster
1422 # and a number.
1423 #
1424 # Example:
1425 # @code
1426 # $b = $a != 3;
1427 # @endcode
1428 # is the same as
1429 # @code
1430 # $b = $a->ne(3);
1431 # @endcode
1432 #
1433 # @param[in] second A number to compare against.
1434 # @return a new integer raster, which is 0 where this raster is equal
1435 # to the second, and 1 where they are not equal. In void context
1436 # changes this raster.
1437 #*
1438 
1439 #** @method Geo::Raster ne(Geo::Raster second)
1440 #
1441 # @brief Performs at each cell the "not equal to" comparison on this and the second
1442 # raster.
1443 #
1444 # Example:
1445 # @code
1446 # $c = $a != $b;
1447 # @endcode
1448 # is the same as
1449 # @code
1450 # $c = $a->ne($b);
1451 # @endcode
1452 #
1453 # @param[in] second A raster to compare against.
1454 # @return a new integer raster, which is 0 where this raster is equal
1455 # to the second, and 1 where they are not equal. In void context
1456 # changes this raster.
1457 #*
1458 sub ne {
1459  my $self = shift;
1460  my $second = shift;
1461  $self = Geo::Raster->new($self) if defined wantarray;
1462  if (ref($second)) {
1463  ral_grid_ne_grid($self->{GRID}, $second->{GRID});
1464  } else {
1465  if (ral_grid_get_datatype($self->{GRID}) == $Geo::Raster::INTEGER_GRID and $second =~ /^-?\d+$/) {
1466  ral_grid_ne_integer($self->{GRID}, $second);
1467  } else {
1468  ral_grid_ne_real($self->{GRID}, $second);
1469  }
1470  }
1471  return $self if defined wantarray;
1472 }
1473 
1474 #** @method Geo::Raster cmp($second, $reversed)
1475 #
1476 # @brief Performs at each cell the numeric comparison on this raster
1477 # and a number.
1478 #
1479 # Example:
1480 # @code
1481 # $b = $a <=> 3.14159;
1482 # @endcode
1483 # is the same as
1484 # @code
1485 # $b = $a->cmp(3.14159);
1486 # @endcode
1487 #
1488 # @param[in] second A number to compare against.
1489 # @param[in] reversed (optional) Whether to perform this <=> second
1490 # computation instead of second <=> this. When operator '<=>' is used,
1491 # this value is automatically set by Perl when appropriate.
1492 # @return a new integer raster, which is -1 where this raster is
1493 # smaller than the second, 0 where they are equal, and 1 where the
1494 # second is larger. In void context changes this raster.
1495 #*
1496 
1497 #** @method Geo::Raster cmp(Geo::Raster second, $reversed)
1498 #
1499 # @brief Performs at each cell the numeric comparison on this and the second
1500 # raster.
1501 #
1502 # Example:
1503 # @code
1504 # $c = $a <=> $b;
1505 # @endcode
1506 # is the same as
1507 # @code
1508 # $c = $a->cmp($b);
1509 # @endcode
1510 #
1511 # @param[in] second A raster to compare against.
1512 # @param[in] reversed (optional) Whether to perform this <=> second
1513 # computation instead of second <=> this. When operator '<=>' is used,
1514 # this value is automatically set by Perl when appropriate.
1515 # @return a new integer raster, which is -1 where this raster is
1516 # smaller than the second, 0 where they are equal, and 1 where the
1517 # second is larger. In void context changes this raster.
1518 #*
1519 sub cmp {
1520  my($self, $second, $reversed) = @_;
1521  $self = Geo::Raster->new($self) if defined wantarray;
1522  if (ref($second)) {
1523  ral_grid_cmp_grid($self->{GRID}, $second->{GRID});
1524  } else {
1525  if (ral_grid_get_datatype($self->{GRID}) == $Geo::Raster::INTEGER_GRID and $second =~ /^-?\d+$/) {
1526  ral_grid_cmp_integer($self->{GRID}, $second);
1527  } else {
1528  ral_grid_cmp_real($self->{GRID}, $second);
1529  }
1530  if ($reversed) {
1531  if (ral_grid_get_datatype($self->{GRID}) == $Geo::Raster::INTEGER_GRID and $second =~ /^-?\d+$/) {
1532  ral_grid_mult_integer($self->{GRID}, -1);
1533  } else {
1534  ral_grid_mult_real($self->{GRID}, -1);
1535  }
1536  }
1537  }
1538  return $self if defined wantarray;
1539 }
1540 
1541 #** @method Geo::Raster not()
1542 #
1543 # @brief Evaluate at each cell the logical not.
1544 #
1545 # @return a new raster. In void context changes this raster.
1546 # @note Defined only for integer rasters.
1547 #*
1548 sub not {
1549  my $self = shift;
1550  $self = Geo::Raster->new($self) if defined wantarray;
1551  ral_grid_not($self->{GRID});
1552  return $self if defined wantarray;
1553 }
1554 
1555 #** @method Geo::Raster and(Geo::Raster second)
1556 #
1557 # @brief Evaluate at each cell the logical and on this and the second
1558 # raster.
1559 #
1560 # The truth table of logical and:
1561 #<table>
1562 #<tr><th>this</th><th>second</th><th>result</th></tr>
1563 #<tr><td>false</td><td>false</td><td>false</td></tr>
1564 #<tr><td>false</td><td>true</td><td>false</td></tr>
1565 #<tr><td>true</td><td>false</td><td>false</td></tr>
1566 #<tr><td>true</td><td>true</td><td>true</td></tr>
1567 #</table>
1568 # If either cell has nodata (undefined) value, the result is undefined.
1569 #
1570 # @param[in] second A raster.
1571 # @return a new raster. In void context changes this raster.
1572 # @note Defined only for integer rasters.
1573 #*
1574 sub and {
1575  my $self = shift;
1576  my $second = shift;
1577  $self = Geo::Raster->new($self) if defined wantarray;
1578  ral_grid_and_grid($self->{GRID}, $second->{GRID});
1579  return $self if defined wantarray;
1580 }
1581 
1582 #** @method Geo::Raster or(Geo::Raster second)
1583 #
1584 # @brief Evaluate at each cell the logical or on this and the second
1585 # raster.
1586 #
1587 # The truth table of logical or:
1588 #<table>
1589 #<tr><th>this</th><th>second</th><th>result</th></tr>
1590 #<tr><td>false</td><td>false</td><td>false</td></tr>
1591 #<tr><td>false</td><td>true</td><td>true</td></tr>
1592 #<tr><td>true</td><td>false</td><td>true</td></tr>
1593 #<tr><td>true</td><td>true</td><td>true</td></tr>
1594 #</table>
1595 # If either cell has nodata (undefined) value, the result is undefined.
1596 #
1597 # @param[in] second A raster.
1598 # @return a new raster. In void context changes this raster.
1599 # @note Defined only for integer rasters.
1600 #*
1601 sub or {
1602  my $self = shift;
1603  my $second = shift;
1604  $self = Geo::Raster->new($self) if defined wantarray;
1605  ral_grid_or_grid($self->{GRID}, $second->{GRID});
1606  return $self if defined wantarray;
1607 }
1608 
1609 #** @method Geo::Raster nor($second)
1610 #
1611 # @brief Evaluates at each cell the logical nor on this and the second
1612 # raster.
1613 #
1614 # The truth table of logical nor:
1615 #<table>
1616 #<tr><th>this</th><th>second</th><th>result</th></tr>
1617 #<tr><td>false</td><td>false</td><td>true</td></tr>
1618 #<tr><td>false</td><td>true</td><td>false</td></tr>
1619 #<tr><td>true</td><td>false</td><td>false</td></tr>
1620 #<tr><td>true</td><td>true</td><td>false</td></tr>
1621 #</table>
1622 # If either cell has nodata (undefined) value, the result is undefined.
1623 #
1624 # @param[in] second A raster, whose cell values are used to calculate
1625 # the logical inverse of disjunction.
1626 # @return a new raster. In void context changes this raster.
1627 # @note Defined only for integer rasters.
1628 #*
1629 sub nor {
1630  my $self = shift;
1631  my $second = shift;
1632  $self = Geo::Raster->new($self) if defined wantarray;
1633  ral_grid_or_grid($self->{GRID}, $second->{GRID});
1634  $self->not();
1635  return $self if defined wantarray;
1636 }
1637 
1638 1;
int RAL_CALL ral_grid_or_grid(ral_grid *gd1, ral_grid *gd2)
A class for geospatial rasters.
Definition: Algorithms.pm:9
public Geo::Raster new()
Create a new raster.
int RAL_CALL ral_grid_not(ral_grid *gd)
private method _new_grid()
ral_grid_handle RAL_CALL ral_grid_create_copy(ral_grid *gd, int datatype)
int RAL_CALL ral_grid_and_grid(ral_grid *gd1, ral_grid *gd2)