SAGA API Version 9.12
Loading...
Searching...
No Matches
grid_operation.cpp
Go to the documentation of this file.
1
3// //
4// SAGA //
5// //
6// System for Automated Geoscientific Analyses //
7// //
8// Application Programming Interface //
9// //
10// Library: SAGA_API //
11// //
12//-------------------------------------------------------//
13// //
14// grid_operation.cpp //
15// //
16// Copyright (C) 2005 by Olaf Conrad //
17// //
18//-------------------------------------------------------//
19// //
20// This file is part of 'SAGA - System for Automated //
21// Geoscientific Analyses'. //
22// //
23// This library is free software; you can redistribute //
24// it and/or modify it under the terms of the GNU Lesser //
25// General Public License as published by the Free //
26// Software Foundation, either version 2.1 of the //
27// License, or (at your option) any later version. //
28// //
29// This library is distributed in the hope that it will //
30// be useful, but WITHOUT ANY WARRANTY; without even the //
31// implied warranty of MERCHANTABILITY or FITNESS FOR A //
32// PARTICULAR PURPOSE. See the GNU Lesser General Public //
33// License for more details. //
34// //
35// You should have received a copy of the GNU Lesser //
36// General Public License along with this program; if //
37// not, see <http://www.gnu.org/licenses/>. //
38// //
39//-------------------------------------------------------//
40// //
41// contact: Olaf Conrad //
42// Institute of Geography //
43// University of Goettingen //
44// Goldschmidtstr. 5 //
45// 37077 Goettingen //
46// Germany //
47// //
48// e-mail: oconrad@saga-gis.org //
49// //
51
52//---------------------------------------------------------
53#include <memory.h>
54
55#include "grid.h"
56
57
59// //
60// Assignments //
61// //
63
64//---------------------------------------------------------
66{
67 double Value = Get_NoData_Value();
68
69 #pragma omp parallel for
70 for(int y=0; y<Get_NY(); y++) for(int x=0; x<Get_NX(); x++)
71 {
72 Set_Value(x, y, Value, false);
73 }
74
75 return( true );
76}
77
78//---------------------------------------------------------
79bool CSG_Grid::Assign(double Value)
80{
81 if( !is_Valid() )
82 {
83 return( false );
84 }
85
86 if( Value == 0. && !is_Cached() )
87 {
88 #pragma omp parallel for
89 for(int y=0; y<Get_NY(); y++)
90 {
91 memset(m_Values[y], 0, Get_nLineBytes());
92 }
93
95 }
96 else
97 {
98 #pragma omp parallel for
99 for(int y=0; y<Get_NY(); y++) for(int x=0; x<Get_NX(); x++)
100 {
101 Set_Value(x, y, Value);
102 }
103 }
104
105 //-----------------------------------------------------
106 Get_History().Destroy(); m_Statistics.Invalidate();
107
108 return( true );
109}
110
111
113// //
115
116//---------------------------------------------------------
117bool CSG_Grid::Assign(CSG_Data_Object *pObject, bool bProgress)
118{
119 return( pObject && Assign(pObject->asGrid(), CSG_Grid_Resampling::Undefined) );
120}
121
122bool CSG_Grid::Assign(CSG_Grid *pGrid, CSG_Grid_Resampling Method, bool bProgress)
123{
124 if( !is_Valid() || !pGrid || !pGrid->is_Valid() || is_Intersecting(pGrid->Get_Extent()) == INTERSECTION_None )
125 {
126 return( false );
127 }
128
129 bool bResult = false;
130
131 //---------------------------------------------------------
132 if( m_System == pGrid->m_System )
133 {
134 #pragma omp parallel for
135 for(int y=0; y<Get_NY(); y++) for(int x=0; x<Get_NX(); x++)
136 {
137 if( pGrid->is_NoData(x, y) )
138 {
139 Set_NoData(x, y);
140 }
141 else
142 {
143 Set_Value(x, y, pGrid->asDouble(x, y));
144 }
145 }
146
147 bResult = true; bProgress = false;
148 }
149
150 //---------------------------------------------------------
151 else if( Get_Cellsize() == pGrid->Get_Cellsize() // No-Scaling...
152 && fmod(Get_XMin() - pGrid->Get_XMin(), Get_Cellsize()) == 0.
153 && fmod(Get_YMin() - pGrid->Get_YMin(), Get_Cellsize()) == 0. )
154 {
155 bResult = _Assign_Interpolated(pGrid, CSG_Grid_Resampling::NearestNeighbour, bProgress);
156 }
157
158 //---------------------------------------------------------
159 else switch( Method )
160 {
164 case CSG_Grid_Resampling::Bicubic_2 : bResult = _Assign_Interpolated(pGrid, Method , bProgress); break;
165
167 case CSG_Grid_Resampling::Mean_Cells : bResult = _Assign_MeanValue (pGrid, Method != CSG_Grid_Resampling::Mean_Nodes, bProgress); break;
168
170 case CSG_Grid_Resampling::Maximum : bResult = _Assign_ExtremeValue(pGrid, Method == CSG_Grid_Resampling::Maximum , bProgress); break;
171
172 case CSG_Grid_Resampling::Majority : bResult = _Assign_Majority (pGrid , bProgress); break;
173
174 default : bResult = Get_Cellsize() < pGrid->Get_Cellsize()
175 ? _Assign_Interpolated(pGrid, CSG_Grid_Resampling::Bicubic_2, bProgress) // Down-Scaling...
176 : _Assign_MeanValue (pGrid, true , bProgress); // Up-Scaling...
177 break;
178 }
179
180 //---------------------------------------------------------
181 if( bResult )
182 {
183 Set_Unit(pGrid->Get_Unit());
184
185 if( pGrid->Get_Projection().is_Okay() )
186 {
187 Get_Projection() = pGrid->Get_Projection();
188 }
189
190 Get_History() = pGrid->Get_History();
191 }
192
193 //---------------------------------------------------------
194 if( bProgress )
195 {
197 }
198
199 return( bResult );
200}
201
202//---------------------------------------------------------
203bool CSG_Grid::_Assign_Interpolated(CSG_Grid *pGrid, CSG_Grid_Resampling Interpolation, bool bProgress)
204{
205 for(int y=0; y<Get_NY() && (!bProgress || SG_UI_Process_Set_Progress(y, Get_NY())); y++)
206 {
207 double py = Get_YMin() + y * Get_Cellsize();
208
209 #pragma omp parallel for
210 for(int x=0; x<Get_NX(); x++)
211 {
212 double px = Get_XMin() + x * Get_Cellsize(), z;
213
214 if( pGrid->Get_Value(px, py, z, Interpolation) )
215 {
216 Set_Value(x, y, z);
217 }
218 else
219 {
220 Set_NoData(x, y);
221 }
222 }
223 }
224
225 return( true );
226}
227
228//---------------------------------------------------------
229bool CSG_Grid::_Assign_ExtremeValue(CSG_Grid *pGrid, bool bMaximum, bool bProgress)
230{
231 if( Get_Cellsize() < pGrid->Get_Cellsize() )
232 {
233 return( false );
234 }
235
236 //-----------------------------------------------------
238
239 double d = pGrid->Get_Cellsize() / Get_Cellsize();
240
241 double ax = 0.5 + (pGrid->Get_XMin() - Get_XMin()) / Get_Cellsize();
242 double py = 0.5 + (pGrid->Get_YMin() - Get_YMin()) / Get_Cellsize();
243
244 for(int y=0; y<pGrid->Get_NY() && (!bProgress || SG_UI_Process_Set_Progress(y, pGrid->Get_NY())); y++, py+=d)
245 {
246 int iy = (int)floor(py);
247
248 if( iy >= 0 && iy < Get_NY() )
249 {
250 #pragma omp parallel for
251 for(int x=0; x<pGrid->Get_NX(); x++)
252 {
253 if( !pGrid->is_NoData(x, y) )
254 {
255 int ix = (int)floor(ax + x * d);
256
257 if( ix >= 0 && ix < Get_NX() )
258 {
259 double z = pGrid->asDouble(x, y);
260
261 if( is_NoData(ix, iy)
262 || (bMaximum == true && z > asDouble(ix, iy))
263 || (bMaximum == false && z < asDouble(ix, iy)) )
264 {
265 Set_Value(ix, iy, z);
266 }
267 }
268 }
269 }
270 }
271 }
272
273 //-----------------------------------------------------
274 return( true );
275}
276
277//---------------------------------------------------------
278bool CSG_Grid::_Assign_MeanValue(CSG_Grid *pGrid, bool bAreaProportional, bool bProgress)
279{
280 if( Get_Cellsize() < pGrid->Get_Cellsize() )
281 {
282 return( false );
283 }
284
285 //-----------------------------------------------------
286 double d = Get_Cellsize() / pGrid->Get_Cellsize();
287
288 double ox = (Get_XMin(true) - pGrid->Get_XMin()) / pGrid->Get_Cellsize();
289 double py = (Get_YMin(true) - pGrid->Get_YMin()) / pGrid->Get_Cellsize();
290
291 for(int y=0; y<Get_NY() && (!bProgress || SG_UI_Process_Set_Progress(y, Get_NY())); y++, py+=d)
292 {
293 int ay = (int)(bAreaProportional ? floor(py ) : ceil (py ));
294 int by = (int)(bAreaProportional ? ceil (py + d) : floor(py + d));
295
296 //-------------------------------------------------
297 #pragma omp parallel for
298 for(int x=0; x<Get_NX(); x++)
299 {
300 double px = ox + x * d;
301
302 int ax = (int)(bAreaProportional ? floor(px ) : ceil (px ));
303 int bx = (int)(bAreaProportional ? ceil (px + d) : floor(px + d));
304
305 CSG_Rect rMean(px, py, px + d, py + d); CSG_Simple_Statistics s;
306
307 for(int iy=ay; iy<=by; iy++)
308 {
309 if( iy >= 0 && iy < pGrid->Get_NY() )
310 {
311 for(int ix=ax; ix<=bx; ix++)
312 {
313 if( ix >= 0 && ix < pGrid->Get_NX() && !pGrid->is_NoData(ix, iy) )
314 {
315 if( bAreaProportional )
316 {
317 CSG_Rect r(ix - 0.5, iy - 0.5, ix + 0.5, iy + 0.5);
318
319 if( r.Intersect(rMean) )
320 {
321 s.Add_Value(pGrid->asDouble(ix, iy), r.Get_Area());
322 }
323 }
324 else
325 {
326 s.Add_Value(pGrid->asDouble(ix, iy));
327 }
328 }
329 }
330 }
331 }
332
333 //---------------------------------------------
334 if( s.Get_Count() > 0 )
335 {
336 Set_Value(x, y, s.Get_Mean());
337 }
338 else
339 {
340 Set_NoData(x, y);
341 }
342 }
343 }
344
345 //-----------------------------------------------------
346 return( true );
347}
348
349
351// //
353
354//---------------------------------------------------------
355bool CSG_Grid::_Assign_Majority(CSG_Grid *pGrid, bool bProgress)
356{
357 if( Get_Cellsize() < pGrid->Get_Cellsize() )
358 {
359 return( false );
360 }
361
362 //-----------------------------------------------------
364
366
367 //-----------------------------------------------------
368 int ay, by = (int)(1. + (((0 - 0.5) * Get_Cellsize() + Get_YMin()) - pGrid->Get_YMin()) / pGrid->Get_Cellsize());
369
370 for(int y=0; y<Get_NY() && (!bProgress || SG_UI_Process_Set_Progress(y, Get_NY())); y++)
371 {
372 ay = by; by = (int)(1. + (((y + 0.5) * Get_Cellsize() + Get_YMin()) - pGrid->Get_YMin()) / pGrid->Get_Cellsize());
373
374 if( ay < pGrid->Get_NY() && by > 0 )
375 {
376 if( ay < 0 ) { ay = 0; } if( by > pGrid->Get_NY() ) { by = pGrid->Get_NY(); }
377
378 int ax, bx = (int)(1. + (((0 - 0.5) * Get_Cellsize() + Get_XMin()) - pGrid->Get_XMin()) / pGrid->Get_Cellsize());
379
380 for(int x=0; x<Get_NX(); x++)
381 {
382 ax = bx; bx = (int)(1. + (((x + 0.5) * Get_Cellsize() + Get_XMin()) - pGrid->Get_XMin()) / pGrid->Get_Cellsize());
383
384 if( ax < pGrid->Get_NX() && bx > 0 )
385 {
386 CSG_Unique_Number_Statistics s;
387
388 if( ax < 0 ) { ax = 0; } if( bx > pGrid->Get_NX() ) { bx = pGrid->Get_NX(); }
389
390 for(int iy=ay; iy<by; iy++) for(int ix=ax; ix<bx; ix++)
391 {
392 if( !pGrid->is_NoData(ix, iy) )
393 {
394 s += pGrid->asDouble(ix, iy);
395 }
396 }
397
398 int n; double z;
399
400 if( s.Get_Majority(z, n) )//&& n > 1 )
401 {
402 Set_Value(x, y, z);
403 }
404 }
405 }
406 }
407 }
408
409 //-----------------------------------------------------
410 return( true );
411}
412
413
415// //
416// Operators //
417// //
419
420//---------------------------------------------------------
422{
424
425 return( *this );
426}
427
429{
430 Assign(Value);
431
432 return( *this );
433}
434
435//---------------------------------------------------------
437{
438 CSG_Grid g(*this);
439
440 return( g._Operation_Arithmetic(Grid, GRID_OPERATION_Addition) );
441}
442
444{
445 CSG_Grid g(*this);
446
447 return( g._Operation_Arithmetic(Value, GRID_OPERATION_Addition) );
448}
449
451{
452 return( _Operation_Arithmetic(Grid, GRID_OPERATION_Addition) );
453}
454
456{
457 return( _Operation_Arithmetic(Value, GRID_OPERATION_Addition) );
458}
459
461{
462 return( _Operation_Arithmetic(Grid, GRID_OPERATION_Addition) );
463}
464
465CSG_Grid & CSG_Grid::Add (double Value)
466{
467 return( _Operation_Arithmetic(Value, GRID_OPERATION_Addition) );
468}
469
470//---------------------------------------------------------
472{
473 CSG_Grid g(*this);
474
475 return( g._Operation_Arithmetic(Grid, GRID_OPERATION_Subtraction) );
476}
477
479{
480 CSG_Grid g(*this);
481
482 return( g._Operation_Arithmetic(Value, GRID_OPERATION_Subtraction) );
483}
484
486{
487 return( _Operation_Arithmetic(Grid, GRID_OPERATION_Subtraction) );
488}
489
491{
492 return( _Operation_Arithmetic(Value, GRID_OPERATION_Subtraction) );
493}
494
496{
497 return( _Operation_Arithmetic(Grid, GRID_OPERATION_Subtraction) );
498}
499
501{
502 return( _Operation_Arithmetic(Value, GRID_OPERATION_Subtraction) );
503}
504
505//---------------------------------------------------------
507{
508 CSG_Grid g(*this);
509
510 return( g._Operation_Arithmetic(Grid, GRID_OPERATION_Multiplication) );
511}
512
514{
515 CSG_Grid g(*this);
516
517 return( g._Operation_Arithmetic(Value, GRID_OPERATION_Multiplication) );
518}
519
521{
522 return( _Operation_Arithmetic(Grid, GRID_OPERATION_Multiplication) );
523}
524
526{
527 return( _Operation_Arithmetic(Value, GRID_OPERATION_Multiplication) );
528}
529
531{
532 return( _Operation_Arithmetic(Grid, GRID_OPERATION_Multiplication) );
533}
534
536{
537 return( _Operation_Arithmetic(Value, GRID_OPERATION_Multiplication) );
538}
539
540//---------------------------------------------------------
542{
543 CSG_Grid g(*this);
544
545 return( g._Operation_Arithmetic(Grid, GRID_OPERATION_Division) );
546}
547
549{
550 CSG_Grid g(*this);
551
552 return( g._Operation_Arithmetic(Value, GRID_OPERATION_Division) );
553}
554
556{
557 return( _Operation_Arithmetic(Grid, GRID_OPERATION_Division) );
558}
559
561{
562 return( _Operation_Arithmetic(Value, GRID_OPERATION_Division) );
563}
564
566{
567 return( _Operation_Arithmetic(Grid, GRID_OPERATION_Division) );
568}
569
571{
572 return( _Operation_Arithmetic(Value, GRID_OPERATION_Division) );
573}
574
575
577// //
578// Operations //
579// //
581
582//---------------------------------------------------------
583CSG_Grid & CSG_Grid::_Operation_Arithmetic(const CSG_Grid &Grid, TSG_Grid_Operation Operation)
584{
585 if( is_Intersecting(Grid.Get_Extent()) )
586 {
587 CSG_Grid_Resampling Interpolation =
588 Get_Cellsize() == Grid.Get_Cellsize() && fmod(Get_XMin() - Grid.Get_XMin(), Get_Cellsize()) == 0.
589 && Get_Cellsize() == Grid.Get_Cellsize() && fmod(Get_YMin() - Grid.Get_YMin(), Get_Cellsize()) == 0.
592
593 #pragma omp parallel for
594 for(int y=0; y<Get_NY(); y++)
595 {
596 double yWorld = Get_YMin() + y * Get_Cellsize();
597
598 for(int x=0; x<Get_NX(); x++)
599 {
600 if( !is_NoData(x, y) )
601 {
602 double xWorld = Get_XMin() + x * Get_Cellsize(), Value;
603
604 if( Grid.Get_Value(xWorld, yWorld, Value, Interpolation) )
605 {
606 switch( Operation )
607 {
608 case GRID_OPERATION_Addition : Add_Value(x, y, Value); break;
609 case GRID_OPERATION_Subtraction : Add_Value(x, y, -Value); break;
610 case GRID_OPERATION_Multiplication: Mul_Value(x, y, Value); break;
612 if( Value != 0. )
613 {
614 Mul_Value(x, y, 1. / Value);
615 }
616 else
617 {
618 Set_NoData(x, y);
619 }
620 break;
621 }
622 }
623 }
624 }
625 }
626 }
627
628 return( *this );
629}
630
631//---------------------------------------------------------
632CSG_Grid & CSG_Grid::_Operation_Arithmetic(double Value, TSG_Grid_Operation Operation)
633{
634 switch( Operation )
635 {
636 case GRID_OPERATION_Addition : if( Value == 0. ) { return( *this ); } break;
637 case GRID_OPERATION_Subtraction : if( Value == 0. ) { return( *this ); } Value = -Value; break;
638 case GRID_OPERATION_Multiplication: if( Value == 1. ) { return( *this ); } break;
639 case GRID_OPERATION_Division : if( Value == 0. ) { return( *this ); } Value = 1. / Value; break;
640 }
641
642 //-----------------------------------------------------
643 #pragma omp parallel for
644 for(int y=0; y<Get_NY(); y++) for(int x=0; x<Get_NX(); x++)
645 {
646 if( !is_NoData(x, y) )
647 {
648 switch( Operation )
649 {
651 case GRID_OPERATION_Subtraction : Add_Value(x, y, Value); break;
653 case GRID_OPERATION_Division : Mul_Value(x, y, Value); break;
654 }
655 }
656 }
657
658 //-----------------------------------------------------
659 return( *this );
660}
661
662
664// //
666
667//---------------------------------------------------------
669{
670 if( is_Valid() && Get_Range() > 0. )
671 {
672 double Min = Get_Min(), Max = Get_Max();
673
674 #pragma omp parallel for
675 for(int y=0; y<Get_NY(); y++) for(int x=0; x<Get_NX(); x++)
676 {
677 if( !is_NoData(x, y) )
678 {
679 Set_Value(x, y, Max - (asDouble(x, y) - Min));
680 }
681 }
682 }
683}
684
685//---------------------------------------------------------
687{
688 if( is_Valid() )
689 {
690 #pragma omp parallel for
691 for(int x=0; x<Get_NX(); x++) for(int yA=0, yB=Get_NY()-1; yA<yB; yA++, yB--)
692 {
693 double v = asDouble(x, yA); Set_Value(x, yA, asDouble(x, yB)); Set_Value(x, yB, v);
694 }
695 }
696}
697
698//---------------------------------------------------------
700{
701 if( is_Valid() )
702 {
703 #pragma omp parallel for
704 for(int y=0; y<Get_NY(); y++) for(int xA=0, xB=Get_NX()-1; xA<xB; xA++, xB--)
705 {
706 double v = asDouble(xA, y); Set_Value(xA, y, asDouble(xB, y)); Set_Value(xB, y, v);
707 }
708 }
709}
710
711
713// //
715
716//---------------------------------------------------------
718{
719 if( is_Valid() && Get_Range() > 0. )
720 {
721 double Min = Get_Min(), Range = Get_Range();
722
723 #pragma omp parallel for
724 for(int y=0; y<Get_NY(); y++) for(int x=0; x<Get_NX(); x++)
725 {
726 if( !is_NoData(x, y) )
727 {
728 Set_Value(x, y, (asDouble(x, y) - Min) / Range);
729 }
730 }
731
732 return( true );
733 }
734
735 return( false );
736}
737
738//---------------------------------------------------------
740{
741 if( is_Valid() && Minimum < Maximum )
742 {
743 #pragma omp parallel for
744 for(int y=0; y<Get_NY(); y++) for(int x=0; x<Get_NX(); x++)
745 {
746 if( !is_NoData(x, y) )
747 {
748 Set_Value(x, y, Minimum + asDouble(x, y) * (Maximum - Minimum));
749 }
750 }
751
752 return( true );
753 }
754
755 return( false );
756}
757
758//---------------------------------------------------------
760{
761 if( is_Valid() && Get_StdDev() > 0. )
762 {
763 double Mean = Get_Mean(), StdDev = Get_StdDev();
764
765 #pragma omp parallel for
766 for(int y=0; y<Get_NY(); y++) for(int x=0; x<Get_NX(); x++)
767 {
768 if( !is_NoData(x, y) )
769 {
770 Set_Value(x, y, (asDouble(x, y) - Mean) / StdDev);
771 }
772 }
773
774 return( true );
775 }
776
777 return( false );
778}
779
780//---------------------------------------------------------
781bool CSG_Grid::DeStandardise(double Mean, double StdDev)
782{
783 if( is_Valid() && StdDev > 0. )
784 {
785 #pragma omp parallel for
786 for(int y=0; y<Get_NY(); y++) for(int x=0; x<Get_NX(); x++)
787 {
788 if( !is_NoData(x, y) )
789 {
790 Set_Value(x, y, Mean + asDouble(x, y) * StdDev);
791 }
792 }
793
794 return( true );
795 }
796
797 return( false );
798}
799
800
802// //
803// //
804// //
806
807//---------------------------------------------------------
819//---------------------------------------------------------
820int CSG_Grid::Get_Gradient_NeighborDir(int x, int y, bool bDown, bool bNoEdges) const
821{
822 int Direction = -1;
823
824 if( is_InGrid(x, y) )
825 {
826 double z = asDouble(x, y), dzMax = 0.;
827
828 for(int i=0; i<8; i++)
829 {
830 int ix = m_System.Get_xTo(i, x), iy = m_System.Get_yTo(i, y);
831
832 if( is_InGrid(ix, iy) )
833 {
834 double dz = (z - asDouble(ix, iy)) / m_System.Get_Length(i);
835
836 if( (!bDown || dz > 0.) && (Direction < 0 || dzMax < dz) )
837 {
838 Direction = i; dzMax = dz;
839 }
840 }
841 else if( bNoEdges )
842 {
843 return( -1 );
844 }
845 }
846 }
847
848 return( Direction );
849}
850
851//---------------------------------------------------------
859//---------------------------------------------------------
860bool CSG_Grid::Get_Gradient(int x, int y, double &Slope, double &Aspect) const
861{
862 if( is_InGrid(x, y) )
863 {
864 double z = asDouble(x, y), dz[4];
865
866 for(int i=0, iDir=0, ix, iy; i<4; i++, iDir+=2)
867 {
868 if( is_InGrid(
869 ix = m_System.Get_xTo (iDir, x),
870 iy = m_System.Get_yTo (iDir, y)) )
871 {
872 dz[i] = asDouble(ix, iy) - z;
873 }
874 else if( is_InGrid(
875 ix = m_System.Get_xFrom(iDir, x),
876 iy = m_System.Get_yFrom(iDir, y)) )
877 {
878 dz[i] = z - asDouble(ix, iy);
879 }
880 else
881 {
882 dz[i] = 0.;
883 }
884 }
885
886 double G = (dz[0] - dz[2]) / (2. * Get_Cellsize());
887 double H = (dz[1] - dz[3]) / (2. * Get_Cellsize());
888
889 Slope = atan(sqrt(G*G + H*H));
890 Aspect = G != 0. ? M_PI_180 + atan2(H, G) : H > 0. ? M_PI_270 : H < 0. ? M_PI_090 : -1.;
891
892 return( true );
893 }
894
895 Slope = 0.;
896 Aspect = -1.;
897
898 return( false );
899}
900
901//---------------------------------------------------------
908//---------------------------------------------------------
909bool CSG_Grid::Get_Gradient(double x, double y, double &Slope, double &Aspect, CSG_Grid_Resampling Interpolation) const
910{
911 double z, iz, dz[4];
912
913 if( Get_Value(x, y, z, Interpolation) )
914 {
915 for(int i=0, iDir=0; i<4; i++, iDir+=2)
916 {
917 if( Get_Value(
918 x + Get_Cellsize() * m_System.Get_xTo (iDir),
919 y + Get_Cellsize() * m_System.Get_yTo (iDir), iz, Interpolation) )
920 {
921 dz[i] = iz - z;
922 }
923 else if( Get_Value(
924 x + Get_Cellsize() * m_System.Get_xFrom(iDir),
925 y + Get_Cellsize() * m_System.Get_yFrom(iDir), iz, Interpolation) )
926 {
927 dz[i] = z - iz;
928 }
929 else
930 {
931 dz[i] = 0.;
932 }
933 }
934
935 double G = (dz[0] - dz[2]) / (2. * Get_Cellsize());
936 double H = (dz[1] - dz[3]) / (2. * Get_Cellsize());
937
938 Slope = atan(sqrt(G*G + H*H));
939 Aspect = G != 0. ? M_PI_180 + atan2(H, G) : H > 0. ? M_PI_270 : H < 0. ? M_PI_090 : -1.;
940
941 return( true );
942 }
943
944 Slope = 0.;
945 Aspect = -1.;
946
947 return( false );
948}
949
950//---------------------------------------------------------
957//---------------------------------------------------------
958bool CSG_Grid::Get_Gradient(const TSG_Point &p, double &Incline, double &Azimuth, CSG_Grid_Resampling Interpolation) const
959{
960 return( Get_Gradient(p.x, p.y, Incline, Azimuth, Interpolation) );
961}
962
963
965// //
966// //
967// //
969
970//---------------------------------------------------------
bool SG_UI_Process_Set_Ready(void)
bool SG_UI_Process_Set_Progress(int Position, int Range)
double Get_NoData_Value(bool bUpper=false) const
Definition dataobject.h:256
CSG_MetaData & Get_History(void)
Definition dataobject.h:239
virtual bool Set_NoData_Value(double Value)
CSG_Projection & Get_Projection(void)
class CSG_Grid * asGrid(bool bPolymorph=false) const
int Get_NY(void) const
Definition grid.h:564
virtual CSG_Grid & operator*=(const CSG_Grid &Grid)
double Get_Max(void)
Definition grid.cpp:1027
bool DeNormalise(double Minimum, double Maximum)
double Get_YMin(bool bCells=false) const
Definition grid.h:576
bool is_Cached(void) const
Definition grid.h:633
void Invert(void)
virtual CSG_Grid & operator+=(const CSG_Grid &Grid)
virtual CSG_Grid & Subtract(const CSG_Grid &Grid)
double Get_Mean(void)
Definition grid.cpp:1017
TSG_Intersection is_Intersecting(const CSG_Rect &Extent) const
Definition grid.cpp:452
bool Standardise(void)
double Get_XMin(bool bCells=false) const
Definition grid.h:572
void Mirror(void)
virtual void Set_NoData(int x, int y)
Definition grid.h:730
virtual CSG_Grid operator-(const CSG_Grid &Grid) const
bool DeStandardise(double Mean, double StdDev)
virtual CSG_Grid & operator=(const CSG_Grid &Grid)
virtual CSG_Grid & Add(const CSG_Grid &Grid)
double Get_Cellsize(void) const
Definition grid.h:567
virtual void Mul_Value(int x, int y, double Value)
Definition grid.h:839
virtual bool is_NoData(int x, int y) const
Definition grid.h:727
virtual void Set_Modified(bool bModified=true)
Definition grid.h:664
int Get_Gradient_NeighborDir(int x, int y, bool bDown=true, bool bNoEdges=true) const
virtual CSG_Grid operator*(const CSG_Grid &Grid) const
bool Normalise(void)
CSG_Grid(void)
Definition grid.cpp:136
virtual bool is_Valid(void) const
Definition grid.cpp:441
int Get_nLineBytes(void) const
Definition grid.h:550
const CSG_String & Get_Unit(void) const
Definition grid.h:553
virtual CSG_Grid & Divide(const CSG_Grid &Grid)
virtual double asDouble(sLong i, bool bScaled=true) const
Definition grid.h:793
virtual void Add_Value(int x, int y, double Value)
Definition grid.h:836
void Flip(void)
virtual CSG_Grid operator/(const CSG_Grid &Grid) const
double Get_Min(void)
Definition grid.cpp:1022
virtual CSG_Grid operator+(const CSG_Grid &Grid) const
virtual bool Assign(double Value=0.)
virtual CSG_Grid & Multiply(const CSG_Grid &Grid)
double Get_Value(double x, double y, CSG_Grid_Resampling Resampling=CSG_Grid_Resampling::Bicubic_2, bool bByteWise=false) const
Definition grid.cpp:539
void Set_Unit(const CSG_String &Unit)
Definition grid.cpp:400
virtual const CSG_Rect & Get_Extent(void)
Definition grid.h:561
int Get_NX(void) const
Definition grid.h:563
bool is_InGrid(int x, int y, bool bCheckNoData=true) const
Definition grid.h:621
double Get_StdDev(void)
Definition grid.cpp:1037
virtual void Set_Value(sLong i, double Value, bool bScaled=true)
Definition grid.h:843
virtual CSG_Grid & operator/=(const CSG_Grid &Grid)
bool Assign_NoData(void)
virtual CSG_Grid & operator-=(const CSG_Grid &Grid)
double Get_Range(void)
Definition grid.cpp:1032
bool Get_Gradient(int x, int y, double &Slope, double &Aspect) const
void Destroy(void)
Definition metadata.cpp:140
bool is_Okay(void) const
Definition geo_tools.h:863
double Get_Mean(void)
Definition mat_tools.h:753
sLong Get_Count(void) const
Definition mat_tools.h:745
void Add_Value(double Value, double Weight=1.)
virtual bool Get_Majority(double &Value) const
Definition mat_tools.h:870
struct SSG_Point TSG_Point
@ INTERSECTION_None
Definition geo_tools.h:102
TSG_Grid_Operation
Definition grid.h:194
@ GRID_OPERATION_Multiplication
Definition grid.h:197
@ GRID_OPERATION_Division
Definition grid.h:198
@ GRID_OPERATION_Addition
Definition grid.h:195
@ GRID_OPERATION_Subtraction
Definition grid.h:196
CSG_Grid_Resampling
Definition grid.h:156
#define M_PI_090
Definition mat_tools.h:100
#define M_PI_180
Definition mat_tools.h:102
#define M_PI_270
Definition mat_tools.h:104
double x
Definition geo_tools.h:129
double y
Definition geo_tools.h:129