/
gdalgrid.cpp
4161 lines (3706 loc) · 155 KB
/
gdalgrid.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************
*
* Project: GDAL Gridding API.
* Purpose: Implementation of GDAL scattered data gridder.
* Author: Andrey Kiselev, dron@ak4719.spb.edu
*
******************************************************************************
* Copyright (c) 2007, Andrey Kiselev <dron@ak4719.spb.edu>
* Copyright (c) 2009-2013, Even Rouault <even dot rouault at spatialys.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
****************************************************************************/
#include "cpl_port.h"
#include "gdalgrid.h"
#include "gdalgrid_priv.h"
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <limits>
#include <map>
#include <utility>
#include <algorithm>
#include "cpl_conv.h"
#include "cpl_cpu_features.h"
#include "cpl_error.h"
#include "cpl_multiproc.h"
#include "cpl_progress.h"
#include "cpl_quad_tree.h"
#include "cpl_string.h"
#include "cpl_vsi.h"
#include "cpl_worker_thread_pool.h"
#include "gdal.h"
CPL_CVSID("$Id$")
constexpr double TO_RADIANS = M_PI / 180.0;
/************************************************************************/
/* GDALGridGetPointBounds() */
/************************************************************************/
static void GDALGridGetPointBounds( const void* hFeature, CPLRectObj* pBounds )
{
const GDALGridPoint* psPoint = static_cast<const GDALGridPoint*>(hFeature);
GDALGridXYArrays* psXYArrays = psPoint->psXYArrays;
const int i = psPoint->i;
const double dfX = psXYArrays->padfX[i];
const double dfY = psXYArrays->padfY[i];
pBounds->minx = dfX;
pBounds->miny = dfY;
pBounds->maxx = dfX;
pBounds->maxy = dfY;
}
/************************************************************************/
/* GDALGridInverseDistanceToAPower() */
/************************************************************************/
/**
* Inverse distance to a power.
*
* The Inverse Distance to a Power gridding method is a weighted average
* interpolator. You should supply the input arrays with the scattered data
* values including coordinates of every data point and output grid geometry.
* The function will compute interpolated value for the given position in
* output grid.
*
* For every grid node the resulting value \f$Z\f$ will be calculated using
* formula:
*
* \f[
* Z=\frac{\sum_{i=1}^n{\frac{Z_i}{r_i^p}}}{\sum_{i=1}^n{\frac{1}{r_i^p}}}
* \f]
*
* where
* <ul>
* <li> \f$Z_i\f$ is a known value at point \f$i\f$,
* <li> \f$r_i\f$ is an Euclidean distance from the grid node
* to point \f$i\f$,
* <li> \f$p\f$ is a weighting power,
* <li> \f$n\f$ is a total number of points in search ellipse.
* </ul>
*
* In this method the weighting factor \f$w\f$ is
*
* \f[
* w=\frac{1}{r^p}
* \f]
*
* @param poOptionsIn Algorithm parameters. This should point to
* GDALGridInverseDistanceToAPowerOptions object.
* @param nPoints Number of elements in input arrays.
* @param padfX Input array of X coordinates.
* @param padfY Input array of Y coordinates.
* @param padfZ Input array of Z values.
* @param dfXPoint X coordinate of the point to compute.
* @param dfYPoint Y coordinate of the point to compute.
* @param pdfValue Pointer to variable where the computed grid node value
* will be returned.
* @param hExtraParamsIn extra parameters (unused)
*
* @return CE_None on success or CE_Failure if something goes wrong.
*/
CPLErr
GDALGridInverseDistanceToAPower( const void *poOptionsIn, GUInt32 nPoints,
const double *padfX, const double *padfY,
const double *padfZ,
double dfXPoint, double dfYPoint,
double *pdfValue,
CPL_UNUSED void* hExtraParamsIn)
{
// TODO: For optimization purposes pre-computed parameters should be moved
// out of this routine to the calling function.
const GDALGridInverseDistanceToAPowerOptions * const poOptions =
static_cast<const GDALGridInverseDistanceToAPowerOptions *>(
poOptionsIn);
// Pre-compute search ellipse parameters.
const double dfRadius1Square = poOptions->dfRadius1 * poOptions->dfRadius1;
const double dfRadius2Square = poOptions->dfRadius2 * poOptions->dfRadius2;
const double dfR12Square = dfRadius1Square * dfRadius2Square;
// Compute coefficients for coordinate system rotation.
const double dfAngle = TO_RADIANS * poOptions->dfAngle;
const bool bRotated = dfAngle != 0.0;
const double dfCoeff1 = bRotated ? cos(dfAngle) : 0.0;
const double dfCoeff2 = bRotated ? sin(dfAngle) : 0.0;
const double dfPowerDiv2 = poOptions->dfPower / 2;
const double dfSmoothing = poOptions->dfSmoothing;
const GUInt32 nMaxPoints = poOptions->nMaxPoints;
double dfNominator = 0.0;
double dfDenominator = 0.0;
GUInt32 n = 0;
for( GUInt32 i = 0; i < nPoints; i++ )
{
double dfRX = padfX[i] - dfXPoint;
double dfRY = padfY[i] - dfYPoint;
const double dfR2 =
dfRX * dfRX + dfRY * dfRY + dfSmoothing * dfSmoothing;
if( bRotated )
{
const double dfRXRotated = dfRX * dfCoeff1 + dfRY * dfCoeff2;
const double dfRYRotated = dfRY * dfCoeff1 - dfRX * dfCoeff2;
dfRX = dfRXRotated;
dfRY = dfRYRotated;
}
// Is this point located inside the search ellipse?
if( dfRadius2Square * dfRX * dfRX + dfRadius1Square * dfRY * dfRY <= dfR12Square )
{
// If the test point is close to the grid node, use the point
// value directly as a node value to avoid singularity.
if( dfR2 < 0.0000000000001 )
{
*pdfValue = padfZ[i];
return CE_None;
}
const double dfW = pow( dfR2, dfPowerDiv2 );
const double dfInvW = 1.0 / dfW;
dfNominator += dfInvW * padfZ[i];
dfDenominator += dfInvW;
n++;
if( nMaxPoints > 0 && n > nMaxPoints )
break;
}
}
if( n < poOptions->nMinPoints || dfDenominator == 0.0 )
{
*pdfValue = poOptions->dfNoDataValue;
}
else
{
*pdfValue = dfNominator / dfDenominator;
}
return CE_None;
}
/************************************************************************/
/* GDALGridInverseDistanceToAPowerNearestNeighbor() */
/************************************************************************/
/**
* Inverse distance to a power with nearest neighbor search, ideal when
* max_points used.
*
* The Inverse Distance to a Power gridding method is a weighted average
* interpolator. You should supply the input arrays with the scattered data
* values including coordinates of every data point and output grid geometry.
* The function will compute interpolated value for the given position in
* output grid.
*
* For every grid node the resulting value \f$Z\f$ will be calculated using
* formula for nearest matches:
*
* \f[
* Z=\frac{\sum_{i=1}^n{\frac{Z_i}{r_i^p}}}{\sum_{i=1}^n{\frac{1}{r_i^p}}}
* \f]
*
* where
* <ul>
* <li> \f$Z_i\f$ is a known value at point \f$i\f$,
* <li> \f$r_i\f$ is an Euclidean distance from the grid node
* to point \f$i\f$ (with an optional smoothing parameter \f$s\f$),
* <li> \f$p\f$ is a weighting power,
* <li> \f$n\f$ is a total number of points in search ellipse.
* </ul>
*
* In this method the weighting factor \f$w\f$ is
*
* \f[
* w=\frac{1}{r^p}
* \f]
*
* @param poOptionsIn Algorithm parameters. This should point to
* GDALGridInverseDistanceToAPowerNearestNeighborOptions object.
* @param nPoints Number of elements in input arrays.
* @param padfX Input array of X coordinates.
* @param padfY Input array of Y coordinates.
* @param padfZ Input array of Z values.
* @param dfXPoint X coordinate of the point to compute.
* @param dfYPoint Y coordinate of the point to compute.
* @param pdfValue Pointer to variable where the computed grid node value
* will be returned.
* @param hExtraParamsIn extra parameters.
*
* @return CE_None on success or CE_Failure if something goes wrong.
*/
CPLErr
GDALGridInverseDistanceToAPowerNearestNeighbor(
const void *poOptionsIn, GUInt32 nPoints,
const double *padfX, const double *padfY,
const double *padfZ,
double dfXPoint, double dfYPoint,
double *pdfValue,
void* hExtraParamsIn )
{
CPL_IGNORE_RET_VAL(nPoints);
const
GDALGridInverseDistanceToAPowerNearestNeighborOptions *const poOptions =
static_cast<
const GDALGridInverseDistanceToAPowerNearestNeighborOptions *>(
poOptionsIn);
const double dfRadius = poOptions->dfRadius;
const double dfSmoothing = poOptions->dfSmoothing;
const double dfSmoothing2 = dfSmoothing * dfSmoothing;
const GUInt32 nMaxPoints = poOptions->nMaxPoints;
GDALGridExtraParameters* psExtraParams =
static_cast<GDALGridExtraParameters *>(hExtraParamsIn);
const CPLQuadTree* phQuadTree = psExtraParams->hQuadTree;
CPLAssert(phQuadTree);
const double dfRPower2 = psExtraParams->dfRadiusPower2PreComp;
const double dfPowerDiv2 = psExtraParams->dfPowerDiv2PreComp;
std::multimap<double, double> oMapDistanceToZValues;
const double dfSearchRadius = dfRadius;
CPLRectObj sAoi;
sAoi.minx = dfXPoint - dfSearchRadius;
sAoi.miny = dfYPoint - dfSearchRadius;
sAoi.maxx = dfXPoint + dfSearchRadius;
sAoi.maxy = dfYPoint + dfSearchRadius;
int nFeatureCount = 0;
GDALGridPoint** papsPoints = reinterpret_cast<GDALGridPoint **>(
CPLQuadTreeSearch(phQuadTree, &sAoi, &nFeatureCount) );
if( nFeatureCount != 0 )
{
for( int k = 0; k < nFeatureCount; k++ )
{
const int i = papsPoints[k]->i;
const double dfRX = padfX[i] - dfXPoint;
const double dfRY = padfY[i] - dfYPoint;
const double dfR2 = dfRX * dfRX + dfRY * dfRY;
// real distance + smoothing
const double dfRsmoothed2 = dfR2 + dfSmoothing2;
if( dfRsmoothed2 < 0.0000000000001 )
{
*pdfValue = padfZ[i];
CPLFree(papsPoints);
return CE_None;
}
// is point within real distance?
if( dfR2 <= dfRPower2 )
{
oMapDistanceToZValues.insert(
std::make_pair(dfRsmoothed2, padfZ[i]) );
}
}
}
CPLFree(papsPoints);
double dfNominator = 0.0;
double dfDenominator = 0.0;
GUInt32 n = 0;
// Examine all "neighbors" within the radius (sorted by distance via the
// multimap), and use the closest n points based on distance until the max
// is reached.
for( std::multimap<double, double>::iterator oMapDistanceToZValuesIter =
oMapDistanceToZValues.begin();
oMapDistanceToZValuesIter != oMapDistanceToZValues.end();
++oMapDistanceToZValuesIter)
{
const double dfR2 = oMapDistanceToZValuesIter->first;
const double dfZ = oMapDistanceToZValuesIter->second;
const double dfW = pow(dfR2, dfPowerDiv2);
const double dfInvW = 1.0 / dfW;
dfNominator += dfInvW * dfZ;
dfDenominator += dfInvW;
n++;
if( nMaxPoints > 0 && n >= nMaxPoints )
{
break;
}
}
if( n < poOptions->nMinPoints || dfDenominator == 0.0 )
{
*pdfValue = poOptions->dfNoDataValue;
}
else
{
*pdfValue = dfNominator / dfDenominator;
}
return CE_None;
}
/************************************************************************/
/* GDALGridInverseDistanceToAPowerNearestNeighborPerQuadrant() */
/************************************************************************/
/**
* Inverse distance to a power with nearest neighbor search, with a per-quadrant
* search logic.
*/
static CPLErr
GDALGridInverseDistanceToAPowerNearestNeighborPerQuadrant(
const void *poOptionsIn, GUInt32 /*nPoints*/,
const double *padfX, const double *padfY,
const double *padfZ,
double dfXPoint, double dfYPoint,
double *pdfValue,
void* hExtraParamsIn )
{
const
GDALGridInverseDistanceToAPowerNearestNeighborOptions *const poOptions =
static_cast<
const GDALGridInverseDistanceToAPowerNearestNeighborOptions *>(
poOptionsIn);
const double dfRadius = poOptions->dfRadius;
const double dfSmoothing = poOptions->dfSmoothing;
const double dfSmoothing2 = dfSmoothing * dfSmoothing;
const GUInt32 nMaxPoints = poOptions->nMaxPoints;
const GUInt32 nMinPointsPerQuadrant = poOptions->nMinPointsPerQuadrant;
const GUInt32 nMaxPointsPerQuadrant = poOptions->nMaxPointsPerQuadrant;
GDALGridExtraParameters* psExtraParams =
static_cast<GDALGridExtraParameters *>(hExtraParamsIn);
const CPLQuadTree* phQuadTree = psExtraParams->hQuadTree;
CPLAssert(phQuadTree);
const double dfRPower2 = psExtraParams->dfRadiusPower2PreComp;
const double dfPowerDiv2 = psExtraParams->dfPowerDiv2PreComp;
std::multimap<double, double> oMapDistanceToZValuesPerQuadrant[4];
const double dfSearchRadius = dfRadius;
CPLRectObj sAoi;
sAoi.minx = dfXPoint - dfSearchRadius;
sAoi.miny = dfYPoint - dfSearchRadius;
sAoi.maxx = dfXPoint + dfSearchRadius;
sAoi.maxy = dfYPoint + dfSearchRadius;
int nFeatureCount = 0;
GDALGridPoint** papsPoints = reinterpret_cast<GDALGridPoint **>(
CPLQuadTreeSearch(phQuadTree, &sAoi, &nFeatureCount) );
if( nFeatureCount != 0 )
{
for( int k = 0; k < nFeatureCount; k++ )
{
const int i = papsPoints[k]->i;
const double dfRX = padfX[i] - dfXPoint;
const double dfRY = padfY[i] - dfYPoint;
const double dfR2 = dfRX * dfRX + dfRY * dfRY;
// real distance + smoothing
const double dfRsmoothed2 = dfR2 + dfSmoothing2;
if( dfRsmoothed2 < 0.0000000000001 )
{
*pdfValue = padfZ[i];
CPLFree(papsPoints);
return CE_None;
}
// is point within real distance?
if( dfR2 <= dfRPower2 )
{
const int iQuadrant =
((dfRX >= 0) ? 1 : 0) |
(((dfRY >= 0) ? 1 : 0) << 1);
oMapDistanceToZValuesPerQuadrant[iQuadrant].insert(
std::make_pair(dfRsmoothed2, padfZ[i]) );
}
}
}
CPLFree(papsPoints);
std::multimap<double, double>::iterator aoIter[] = {
oMapDistanceToZValuesPerQuadrant[0].begin(),
oMapDistanceToZValuesPerQuadrant[1].begin(),
oMapDistanceToZValuesPerQuadrant[2].begin(),
oMapDistanceToZValuesPerQuadrant[3].begin(),
};
constexpr int ALL_QUADRANT_FLAGS = 1 + 2 + 4 + 8;
// Examine all "neighbors" within the radius (sorted by distance via the
// multimap), and use the closest n points based on distance until the max
// is reached.
// Do that by fetching the nearest point in quadrant 0, then the nearest
// point in quadrant 1, 2 and 3, and starting againg with the next nearest
// point in quarant 0, etc.
int nQuadrantIterFinishedFlag = 0;
GUInt32 anPerQuadrant[4] = {0};
double dfNominator = 0.0;
double dfDenominator = 0.0;
GUInt32 n = 0;
for( int iQuadrant = 0; /* true */ ; iQuadrant = (iQuadrant + 1) % 4 )
{
if( aoIter[iQuadrant] == oMapDistanceToZValuesPerQuadrant[iQuadrant].end() ||
(nMaxPointsPerQuadrant > 0 && anPerQuadrant[iQuadrant] >= nMaxPointsPerQuadrant) )
{
nQuadrantIterFinishedFlag |= 1 << iQuadrant;
if( nQuadrantIterFinishedFlag == ALL_QUADRANT_FLAGS )
break;
continue;
}
const double dfR2 = aoIter[iQuadrant]->first;
const double dfZ = aoIter[iQuadrant]->second;
++ aoIter[iQuadrant];
const double dfW = pow(dfR2, dfPowerDiv2);
const double dfInvW = 1.0 / dfW;
dfNominator += dfInvW * dfZ;
dfDenominator += dfInvW;
n++;
anPerQuadrant[iQuadrant] ++;
if( nMaxPoints > 0 && n >= nMaxPoints )
{
break;
}
}
if( nMinPointsPerQuadrant > 0 &&
(anPerQuadrant[0] < nMinPointsPerQuadrant ||
anPerQuadrant[1] < nMinPointsPerQuadrant ||
anPerQuadrant[2] < nMinPointsPerQuadrant ||
anPerQuadrant[3] < nMinPointsPerQuadrant) )
{
*pdfValue = poOptions->dfNoDataValue;
}
else if( n < poOptions->nMinPoints || dfDenominator == 0.0 )
{
*pdfValue = poOptions->dfNoDataValue;
}
else
{
*pdfValue = dfNominator / dfDenominator;
}
return CE_None;
}
/************************************************************************/
/* GDALGridInverseDistanceToAPowerNoSearch() */
/************************************************************************/
/**
* Inverse distance to a power for whole data set.
*
* This is somewhat optimized version of the Inverse Distance to a Power
* method. It is used when the search ellips is not set. The algorithm and
* parameters are the same as in GDALGridInverseDistanceToAPower(), but this
* implementation works faster, because of no search.
*
* @see GDALGridInverseDistanceToAPower()
*/
CPLErr
GDALGridInverseDistanceToAPowerNoSearch(
const void *poOptionsIn, GUInt32 nPoints,
const double *padfX, const double *padfY, const double *padfZ,
double dfXPoint, double dfYPoint,
double *pdfValue,
void * /* hExtraParamsIn */)
{
const GDALGridInverseDistanceToAPowerOptions * const poOptions =
static_cast<const GDALGridInverseDistanceToAPowerOptions *>(
poOptionsIn);
const double dfPowerDiv2 = poOptions->dfPower / 2.0;
const double dfSmoothing = poOptions->dfSmoothing;
const double dfSmoothing2 = dfSmoothing * dfSmoothing;
double dfNominator = 0.0;
double dfDenominator = 0.0;
const bool bPower2 = dfPowerDiv2 == 1.0;
GUInt32 i = 0; // Used after if.
if( bPower2 )
{
if( dfSmoothing2 > 0 )
{
for( i = 0; i < nPoints; i++ )
{
const double dfRX = padfX[i] - dfXPoint;
const double dfRY = padfY[i] - dfYPoint;
const double dfR2 = dfRX * dfRX + dfRY * dfRY + dfSmoothing2;
const double dfInvR2 = 1.0 / dfR2;
dfNominator += dfInvR2 * padfZ[i];
dfDenominator += dfInvR2;
}
}
else
{
for( i = 0; i < nPoints; i++ )
{
const double dfRX = padfX[i] - dfXPoint;
const double dfRY = padfY[i] - dfYPoint;
const double dfR2 = dfRX * dfRX + dfRY * dfRY;
// If the test point is close to the grid node, use the point
// value directly as a node value to avoid singularity.
if( dfR2 < 0.0000000000001 )
{
break;
}
const double dfInvR2 = 1.0 / dfR2;
dfNominator += dfInvR2 * padfZ[i];
dfDenominator += dfInvR2;
}
}
}
else
{
for( i = 0; i < nPoints; i++ )
{
const double dfRX = padfX[i] - dfXPoint;
const double dfRY = padfY[i] - dfYPoint;
const double dfR2 = dfRX * dfRX + dfRY * dfRY + dfSmoothing2;
// If the test point is close to the grid node, use the point
// value directly as a node value to avoid singularity.
if( dfR2 < 0.0000000000001 )
{
break;
}
const double dfW = pow( dfR2, dfPowerDiv2 );
const double dfInvW = 1.0 / dfW;
dfNominator += dfInvW * padfZ[i];
dfDenominator += dfInvW;
}
}
if( i != nPoints )
{
*pdfValue = padfZ[i];
}
else
if( dfDenominator == 0.0 )
{
*pdfValue = poOptions->dfNoDataValue;
}
else
{
*pdfValue = dfNominator / dfDenominator;
}
return CE_None;
}
/************************************************************************/
/* GDALGridMovingAverage() */
/************************************************************************/
/**
* Moving average.
*
* The Moving Average is a simple data averaging algorithm. It uses a moving
* window of elliptic form to search values and averages all data points
* within the window. Search ellipse can be rotated by specified angle, the
* center of ellipse located at the grid node. Also the minimum number of data
* points to average can be set, if there are not enough points in window, the
* grid node considered empty and will be filled with specified NODATA value.
*
* Mathematically it can be expressed with the formula:
*
* \f[
* Z=\frac{\sum_{i=1}^n{Z_i}}{n}
* \f]
*
* where
* <ul>
* <li> \f$Z\f$ is a resulting value at the grid node,
* <li> \f$Z_i\f$ is a known value at point \f$i\f$,
* <li> \f$n\f$ is a total number of points in search ellipse.
* </ul>
*
* @param poOptionsIn Algorithm parameters. This should point to
* GDALGridMovingAverageOptions object.
* @param nPoints Number of elements in input arrays.
* @param padfX Input array of X coordinates.
* @param padfY Input array of Y coordinates.
* @param padfZ Input array of Z values.
* @param dfXPoint X coordinate of the point to compute.
* @param dfYPoint Y coordinate of the point to compute.
* @param pdfValue Pointer to variable where the computed grid node value
* will be returned.
* @param hExtraParamsIn extra parameters (unused)
*
* @return CE_None on success or CE_Failure if something goes wrong.
*/
CPLErr
GDALGridMovingAverage( const void *poOptionsIn, GUInt32 nPoints,
const double *padfX, const double *padfY,
const double *padfZ,
double dfXPoint, double dfYPoint, double *pdfValue,
CPL_UNUSED void * hExtraParamsIn )
{
// TODO: For optimization purposes pre-computed parameters should be moved
// out of this routine to the calling function.
const GDALGridMovingAverageOptions * const poOptions =
static_cast<const GDALGridMovingAverageOptions *>(poOptionsIn);
// Pre-compute search ellipse parameters.
const double dfRadius1Square = poOptions->dfRadius1 * poOptions->dfRadius1;
const double dfRadius2Square = poOptions->dfRadius2 * poOptions->dfRadius2;
const double dfSearchRadius = std::max(poOptions->dfRadius1, poOptions->dfRadius2);
const double dfR12Square = dfRadius1Square * dfRadius2Square;
GDALGridExtraParameters* psExtraParams = static_cast<GDALGridExtraParameters *>(hExtraParamsIn);
const CPLQuadTree* phQuadTree = psExtraParams->hQuadTree;
// Compute coefficients for coordinate system rotation.
const double dfAngle = TO_RADIANS * poOptions->dfAngle;
const bool bRotated = dfAngle != 0.0;
const double dfCoeff1 = bRotated ? cos(dfAngle) : 0.0;
const double dfCoeff2 = bRotated ? sin(dfAngle) : 0.0;
double dfAccumulator = 0.0;
GUInt32 n = 0; // Used after for.
if( phQuadTree != nullptr)
{
CPLRectObj sAoi;
sAoi.minx = dfXPoint - dfSearchRadius;
sAoi.miny = dfYPoint - dfSearchRadius;
sAoi.maxx = dfXPoint + dfSearchRadius;
sAoi.maxy = dfYPoint + dfSearchRadius;
int nFeatureCount = 0;
GDALGridPoint** papsPoints = reinterpret_cast<GDALGridPoint **>(
CPLQuadTreeSearch(phQuadTree, &sAoi, &nFeatureCount) );
if( nFeatureCount != 0 )
{
for( int k = 0; k < nFeatureCount; k++ )
{
const int i = papsPoints[k]->i;
const double dfRX = padfX[i] - dfXPoint;
const double dfRY = padfY[i] - dfYPoint;
if( dfRadius2Square * dfRX * dfRX + dfRadius1Square * dfRY * dfRY <= dfR12Square )
{
dfAccumulator += padfZ[i];
n++;
}
}
}
CPLFree(papsPoints);
}
else{
for( GUInt32 i = 0; i < nPoints; i++ )
{
double dfRX = padfX[i] - dfXPoint;
double dfRY = padfY[i] - dfYPoint;
if( bRotated )
{
const double dfRXRotated = dfRX * dfCoeff1 + dfRY * dfCoeff2;
const double dfRYRotated = dfRY * dfCoeff1 - dfRX * dfCoeff2;
dfRX = dfRXRotated;
dfRY = dfRYRotated;
}
// Is this point located inside the search ellipse?
if( dfRadius2Square * dfRX * dfRX + dfRadius1Square * dfRY * dfRY <= dfR12Square )
{
dfAccumulator += padfZ[i];
n++;
}
}
}
if( n < poOptions->nMinPoints || n == 0 )
{
*pdfValue = poOptions->dfNoDataValue;
}
else
{
*pdfValue = dfAccumulator / n;
}
return CE_None;
}
/************************************************************************/
/* GDALGridMovingAveragePerQuadrant() */
/************************************************************************/
/**
* Moving average, with a per-quadrant search logic.
*/
static CPLErr GDALGridMovingAveragePerQuadrant(
const void *poOptionsIn, GUInt32 /*nPoints*/,
const double *padfX, const double *padfY,
const double *padfZ,
double dfXPoint, double dfYPoint,
double *pdfValue,
void* hExtraParamsIn )
{
const GDALGridMovingAverageOptions * const poOptions =
static_cast<const GDALGridMovingAverageOptions *>(poOptionsIn);
const double dfRadius1Square = poOptions->dfRadius1 * poOptions->dfRadius1;
const double dfRadius2Square = poOptions->dfRadius2 * poOptions->dfRadius2;
const double dfR12Square = dfRadius1Square * dfRadius2Square;
const GUInt32 nMaxPoints = poOptions->nMaxPoints;
const GUInt32 nMinPointsPerQuadrant = poOptions->nMinPointsPerQuadrant;
const GUInt32 nMaxPointsPerQuadrant = poOptions->nMaxPointsPerQuadrant;
GDALGridExtraParameters* psExtraParams =
static_cast<GDALGridExtraParameters *>(hExtraParamsIn);
const CPLQuadTree* phQuadTree = psExtraParams->hQuadTree;
CPLAssert(phQuadTree);
std::multimap<double, double> oMapDistanceToZValuesPerQuadrant[4];
const double dfSearchRadius = std::max(poOptions->dfRadius1, poOptions->dfRadius2);
CPLRectObj sAoi;
sAoi.minx = dfXPoint - dfSearchRadius;
sAoi.miny = dfYPoint - dfSearchRadius;
sAoi.maxx = dfXPoint + dfSearchRadius;
sAoi.maxy = dfYPoint + dfSearchRadius;
int nFeatureCount = 0;
GDALGridPoint** papsPoints = reinterpret_cast<GDALGridPoint **>(
CPLQuadTreeSearch(phQuadTree, &sAoi, &nFeatureCount) );
if( nFeatureCount != 0 )
{
for( int k = 0; k < nFeatureCount; k++ )
{
const int i = papsPoints[k]->i;
const double dfRX = padfX[i] - dfXPoint;
const double dfRY = padfY[i] - dfYPoint;
const double dfRXSquare = dfRX * dfRX;
const double dfRYSquare = dfRY * dfRY;
if( dfRadius2Square * dfRXSquare + dfRadius1Square * dfRYSquare <= dfR12Square )
{
const int iQuadrant =
((dfRX >= 0) ? 1 : 0) |
(((dfRY >= 0) ? 1 : 0) << 1);
oMapDistanceToZValuesPerQuadrant[iQuadrant].insert(
std::make_pair(dfRXSquare + dfRYSquare, padfZ[i]) );
}
}
}
CPLFree(papsPoints);
std::multimap<double, double>::iterator aoIter[] = {
oMapDistanceToZValuesPerQuadrant[0].begin(),
oMapDistanceToZValuesPerQuadrant[1].begin(),
oMapDistanceToZValuesPerQuadrant[2].begin(),
oMapDistanceToZValuesPerQuadrant[3].begin(),
};
constexpr int ALL_QUADRANT_FLAGS = 1 + 2 + 4 + 8;
// Examine all "neighbors" within the radius (sorted by distance via the
// multimap), and use the closest n points based on distance until the max
// is reached.
// Do that by fetching the nearest point in quadrant 0, then the nearest
// point in quadrant 1, 2 and 3, and starting againg with the next nearest
// point in quarant 0, etc.
int nQuadrantIterFinishedFlag = 0;
GUInt32 anPerQuadrant[4] = {0};
double dfNominator = 0.0;
GUInt32 n = 0;
for( int iQuadrant = 0; /* true */ ; iQuadrant = (iQuadrant + 1) % 4 )
{
if( aoIter[iQuadrant] == oMapDistanceToZValuesPerQuadrant[iQuadrant].end() ||
(nMaxPointsPerQuadrant > 0 && anPerQuadrant[iQuadrant] >= nMaxPointsPerQuadrant) )
{
nQuadrantIterFinishedFlag |= 1 << iQuadrant;
if( nQuadrantIterFinishedFlag == ALL_QUADRANT_FLAGS )
break;
continue;
}
const double dfZ = aoIter[iQuadrant]->second;
++ aoIter[iQuadrant];
dfNominator += dfZ;
n++;
anPerQuadrant[iQuadrant] ++;
if( nMaxPoints > 0 && n >= nMaxPoints )
{
break;
}
}
if( nMinPointsPerQuadrant > 0 &&
(anPerQuadrant[0] < nMinPointsPerQuadrant ||
anPerQuadrant[1] < nMinPointsPerQuadrant ||
anPerQuadrant[2] < nMinPointsPerQuadrant ||
anPerQuadrant[3] < nMinPointsPerQuadrant) )
{
*pdfValue = poOptions->dfNoDataValue;
}
else if( n < poOptions->nMinPoints || n == 0 )
{
*pdfValue = poOptions->dfNoDataValue;
}
else
{
*pdfValue = dfNominator / n;
}
return CE_None;
}
/************************************************************************/
/* GDALGridNearestNeighbor() */
/************************************************************************/
/**
* Nearest neighbor.
*
* The Nearest Neighbor method doesn't perform any interpolation or smoothing,
* it just takes the value of nearest point found in grid node search ellipse
* and returns it as a result. If there are no points found, the specified
* NODATA value will be returned.
*
* @param poOptionsIn Algorithm parameters. This should point to
* GDALGridNearestNeighborOptions object.
* @param nPoints Number of elements in input arrays.
* @param padfX Input array of X coordinates.
* @param padfY Input array of Y coordinates.
* @param padfZ Input array of Z values.
* @param dfXPoint X coordinate of the point to compute.
* @param dfYPoint Y coordinate of the point to compute.
* @param pdfValue Pointer to variable where the computed grid node value
* will be returned.
* @param hExtraParamsIn extra parameters.
*
* @return CE_None on success or CE_Failure if something goes wrong.
*/
CPLErr
GDALGridNearestNeighbor( const void *poOptionsIn, GUInt32 nPoints,
const double *padfX, const double *padfY,
const double *padfZ,
double dfXPoint, double dfYPoint, double *pdfValue,
void* hExtraParamsIn)
{
// TODO: For optimization purposes pre-computed parameters should be moved
// out of this routine to the calling function.
const GDALGridNearestNeighborOptions * const poOptions =
static_cast<const GDALGridNearestNeighborOptions *>(poOptionsIn);
// Pre-compute search ellipse parameters.
const double dfRadius1Square = poOptions->dfRadius1 * poOptions->dfRadius1;
const double dfRadius2Square = poOptions->dfRadius2 * poOptions->dfRadius2;
const double dfR12Square = dfRadius1Square * dfRadius2Square;
GDALGridExtraParameters* psExtraParams =
static_cast<GDALGridExtraParameters *>(hExtraParamsIn);
CPLQuadTree* hQuadTree = psExtraParams->hQuadTree;
// Compute coefficients for coordinate system rotation.
const double dfAngle = TO_RADIANS * poOptions->dfAngle;
const bool bRotated = dfAngle != 0.0;
const double dfCoeff1 = bRotated ? cos(dfAngle) : 0.0;
const double dfCoeff2 = bRotated ? sin(dfAngle) : 0.0;
// If the nearest point will not be found, its value remains as NODATA.
double dfNearestValue = poOptions->dfNoDataValue;
GUInt32 i = 0;
double dfSearchRadius = psExtraParams->dfInitialSearchRadius;
if( hQuadTree != nullptr)
{
if( poOptions->dfRadius1 > 0 || poOptions->dfRadius2 > 0 )
dfSearchRadius = std::max(poOptions->dfRadius1, poOptions->dfRadius2);
CPLRectObj sAoi;
while( dfSearchRadius > 0 )
{
sAoi.minx = dfXPoint - dfSearchRadius;
sAoi.miny = dfYPoint - dfSearchRadius;
sAoi.maxx = dfXPoint + dfSearchRadius;
sAoi.maxy = dfYPoint + dfSearchRadius;
int nFeatureCount = 0;
GDALGridPoint** papsPoints = reinterpret_cast<GDALGridPoint **>(
CPLQuadTreeSearch(hQuadTree, &sAoi, &nFeatureCount) );
if( nFeatureCount != 0 )
{
// Nearest distance will be initialized with the distance to the first
// point in array.
double dfNearestRSquare = std::numeric_limits<double>::max();
for( int k = 0; k < nFeatureCount; k++)
{
const int idx = papsPoints[k]->i;
const double dfRX = padfX[idx] - dfXPoint;
const double dfRY = padfY[idx] - dfYPoint;
const double dfR2 = dfRX * dfRX + dfRY * dfRY;
if( dfR2 <= dfNearestRSquare )
{
dfNearestRSquare = dfR2;
dfNearestValue = padfZ[idx];
}
}
CPLFree(papsPoints);
break;
}
CPLFree(papsPoints);
if( poOptions->dfRadius1 > 0 || poOptions->dfRadius2 > 0 )
break;
dfSearchRadius *= 2;
#if DEBUG_VERBOSE
CPLDebug(
"GDAL_GRID", "Increasing search radius to %.16g",
dfSearchRadius);
#endif
}
}
else
{
double dfNearestRSquare = std::numeric_limits<double>::max();
while( i < nPoints )
{
double dfRX = padfX[i] - dfXPoint;
double dfRY = padfY[i] - dfYPoint;
if( bRotated )
{
const double dfRXRotated = dfRX * dfCoeff1 + dfRY * dfCoeff2;
const double dfRYRotated = dfRY * dfCoeff1 - dfRX * dfCoeff2;
dfRX = dfRXRotated;