-
Notifications
You must be signed in to change notification settings - Fork 3
/
gdal-2.4-resample-sum.patch
213 lines (202 loc) · 8.85 KB
/
gdal-2.4-resample-sum.patch
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
diff --git a/gdal/alg/gdalwarper.cpp b/gdal/alg/gdalwarper.cpp
index 4332582..b12799e 100644
--- a/gdal/alg/gdalwarper.cpp
+++ b/gdal/alg/gdalwarper.cpp
@@ -1584,6 +1584,8 @@ GDALSerializeWarpOptions( const GDALWarpOptions *psWO )
pszAlgName = "Quartile1";
else if( psWO->eResampleAlg == GRA_Q3 )
pszAlgName = "Quartile3";
+ else if( psWO->eResampleAlg == GRA_Sum )
+ pszAlgName = "Sum";
else
pszAlgName = "Unknown";
@@ -1833,6 +1835,8 @@ GDALWarpOptions * CPL_STDCALL GDALDeserializeWarpOptions( CPLXMLNode *psTree )
psWO->eResampleAlg = GRA_Q1;
else if( EQUAL(pszValue, "Quartile3") )
psWO->eResampleAlg = GRA_Q3;
+ else if( EQUAL(pszValue, "Sum") )
+ psWO->eResampleAlg = GRA_Sum;
else if( EQUAL(pszValue, "Default") )
/* leave as is */;
else
diff --git a/gdal/alg/gdalwarper.h b/gdal/alg/gdalwarper.h
index d25f61e..f1c5d2f 100644
--- a/gdal/alg/gdalwarper.h
+++ b/gdal/alg/gdalwarper.h
@@ -60,7 +60,8 @@ typedef enum {
/*! Min (selects minimum of all non-NODATA contributing pixels) */ GRA_Min=9,
/*! Med (selects median of all non-NODATA contributing pixels) */ GRA_Med=10,
/*! Q1 (selects first quartile of all non-NODATA contributing pixels) */ GRA_Q1=11,
- /*! Q3 (selects third quartile of all non-NODATA contributing pixels) */ GRA_Q3=12
+ /*! Q3 (selects third quartile of all non-NODATA contributing pixels) */ GRA_Q3=12,
+ /*! Sum (computes the sum of all non-NODATA contributing pixels) */ GRA_Sum=13
} GDALResampleAlg;
/*! GWKAverageOrMode Algorithm */
@@ -70,7 +71,8 @@ typedef enum {
/*! Mode of GDT_Byte, GDT_UInt16, or GDT_Int16 */ GWKAOM_Imode=3,
/*! Maximum */ GWKAOM_Max=4,
/*! Minimum */ GWKAOM_Min=5,
- /*! Quantile */ GWKAOM_Quant=6
+ /*! Quantile */ GWKAOM_Quant=6,
+ /*! Sum */ GWKAOM_Sum=7
} GWKAverageOrModeAlg;
/*! @cond Doxygen_Suppress */
diff --git a/gdal/alg/gdalwarpkernel.cpp b/gdal/alg/gdalwarpkernel.cpp
index fc59a2f..5fedfb4 100644
--- a/gdal/alg/gdalwarpkernel.cpp
+++ b/gdal/alg/gdalwarpkernel.cpp
@@ -93,6 +93,7 @@ static const int anGWKFilterRadius[] =
0, // Med
0, // Q1
0, // Q3
+ 0, // Sum
};
static double GWKBilinear(double dfX);
@@ -115,6 +116,7 @@ static const FilterFuncType apfGWKFilter[] =
nullptr, // Med
nullptr, // Q1
nullptr, // Q3
+ nullptr, // Sum
};
// TODO(schwehr): Can we make these functions have a const * const arg?
@@ -138,6 +140,7 @@ static const FilterFunc4ValuesType apfGWKFilter4Values[] =
nullptr, // Med
nullptr, // Q1
nullptr, // Q3
+ nullptr, // Sum
};
int GWKGetFilterRadius(GDALResampleAlg eResampleAlg)
@@ -622,7 +625,7 @@ static CPLErr GWKRun( GDALWarpKernel *poWK,
* Resampling algorithm.
*
* The resampling algorithm to use. One of GRA_NearestNeighbour, GRA_Bilinear,
- * GRA_Cubic, GRA_CubicSpline, GRA_Lanczos, GRA_Average, or GRA_Mode.
+ * GRA_Cubic, GRA_CubicSpline, GRA_Lanczos, GRA_Average, GRA_Mode or GRA_Sum.
*
* This field is required. GDT_NearestNeighbour may be used as a default
* value.
@@ -1279,6 +1282,9 @@ CPLErr GDALWarpKernel::PerformWarp()
if( eResample == GRA_Q3 )
return GWKAverageOrMode( this );
+ if( eResample == GRA_Sum )
+ return GWKAverageOrMode( this );
+
if (!GDALDataTypeIsComplex(eWorkingDataType))
{
return GWKRealCase( this );
@@ -5810,6 +5816,10 @@ static void GWKAverageOrModeThread( void* pData)
nAlgo = GWKAOM_Quant;
quant = 0.75;
}
+ else if( poWK->eResample == GRA_Sum )
+ {
+ nAlgo = GWKAOM_Sum;
+ }
else
{
// Other resample algorithms not permitted here.
@@ -6254,6 +6264,51 @@ static void GWKAverageOrModeThread( void* pData)
dfRealValuesTmp.clear();
}
} // Quantile.
+ else if( nAlgo == GWKAOM_Sum )
+ // poWK->eResample == GRA_Sum
+ {
+ // This code adapted from nAlgo 1 method, GRA_Average.
+ for( int iSrcY = iSrcYMin; iSrcY < iSrcYMax; iSrcY++ )
+ {
+ for( int iSrcX = iSrcXMin; iSrcX < iSrcXMax; iSrcX++ )
+ {
+ iSrcOffset = iSrcX + iSrcY * nSrcXSize;
+
+ if( poWK->panUnifiedSrcValid != nullptr
+ && !(poWK->panUnifiedSrcValid[iSrcOffset>>5]
+ & (0x01 << (iSrcOffset & 0x1f))) )
+ {
+ continue;
+ }
+
+ nCount2++;
+ if( GWKGetPixelValue(
+ poWK, iBand, iSrcOffset,
+ &dfBandDensity, &dfValueRealTmp,
+ &dfValueImagTmp ) &&
+ dfBandDensity > BAND_DENSITY_THRESHOLD )
+ {
+ nCount++;
+ dfTotalReal += dfValueRealTmp;
+ if (bIsComplex)
+ {
+ dfTotalImag += dfValueImagTmp;
+ }
+ }
+ }
+ }
+
+ if( nCount > 0 )
+ {
+ dfValueReal = dfTotalReal;
+ if (bIsComplex)
+ {
+ dfValueImag = dfTotalImag;
+ }
+ dfBandDensity = 1;
+ bHasFoundDensity = true;
+ }
+ } // GRA_Sum.
/* -------------------------------------------------------------------- */
/* We have a computed value from the source. Now apply it to */
diff --git a/gdal/alg/gdalwarpoperation.cpp b/gdal/alg/gdalwarpoperation.cpp
index 476d05c..bb123c7 100644
--- a/gdal/alg/gdalwarpoperation.cpp
+++ b/gdal/alg/gdalwarpoperation.cpp
@@ -267,7 +267,8 @@ int GDALWarpOperation::ValidateOptions()
&& psOptions->eResampleAlg != GRA_Min
&& psOptions->eResampleAlg != GRA_Med
&& psOptions->eResampleAlg != GRA_Q1
- && psOptions->eResampleAlg != GRA_Q3)
+ && psOptions->eResampleAlg != GRA_Q3
+ && psOptions->eResampleAlg != GRA_Sum)
{
CPLError( CE_Failure, CPLE_IllegalArg,
"GDALWarpOptions.Validate(): "
diff --git a/gdal/apps/gdalwarp_bin.cpp b/gdal/apps/gdalwarp_bin.cpp
index 8336143..f0fa9ed 100644
--- a/gdal/apps/gdalwarp_bin.cpp
+++ b/gdal/apps/gdalwarp_bin.cpp
@@ -167,6 +167,7 @@ algorithm, worst interpolation quality).</dd>
<dt><b>med</b></dt>: <dd>median resampling, selects the median value of all non-NODATA contributing pixels. (GDAL >= 2.0.0)</dd>
<dt><b>q1</b></dt>: <dd>first quartile resampling, selects the first quartile value of all non-NODATA contributing pixels. (GDAL >= 2.0.0)</dd>
<dt><b>q3</b></dt>: <dd>third quartile resampling, selects the third quartile value of all non-NODATA contributing pixels. (GDAL >= 2.0.0)</dd>
+<dt><b>sum</b></dt>: <dd>sum resampling, computes the sum of all non-NODATA contributing pixels. (GDAL >= 2.4.1)</dd>
</dl>
<dt> <b>-srcnodata</b> <em>value [value...]</em>:</dt><dd> Set nodata masking
values for input bands (different values can be supplied for each band). If
@@ -342,7 +343,7 @@ static void Usage(const char* pszErrorMsg = nullptr)
" srcfile* dstfile\n"
"\n"
"Available resampling methods:\n"
- " near (default), bilinear, cubic, cubicspline, lanczos, average, mode, max, min, med, Q1, Q3.\n" );
+ " near (default), bilinear, cubic, cubicspline, lanczos, average, mode, max, min, med, Q1, Q3, sum.\n" );
if( pszErrorMsg != nullptr )
fprintf(stderr, "\nFAILURE: %s\n", pszErrorMsg);
diff --git a/gdal/apps/gdalwarp_lib.cpp b/gdal/apps/gdalwarp_lib.cpp
index af69c91..dc6fa8f 100644
--- a/gdal/apps/gdalwarp_lib.cpp
+++ b/gdal/apps/gdalwarp_lib.cpp
@@ -150,7 +150,7 @@ struct GDALWarpAppOptions
/*! the resampling method. Available methods are: near, bilinear,
cubic, cubicspline, lanczos, average, mode, max, min, med,
- q1, q3 */
+ q1, q3, sum */
GDALResampleAlg eResampleAlg;
/*! nodata masking values for input bands (different values can be supplied
@@ -3605,6 +3605,8 @@ GDALWarpAppOptions *GDALWarpAppOptionsNew(char** papszArgv,
psOptions->eResampleAlg = GRA_Q1;
else if ( EQUAL(papszArgv[i], "q3") )
psOptions->eResampleAlg = GRA_Q3;
+ else if ( EQUAL(papszArgv[i], "sum") )
+ psOptions->eResampleAlg = GRA_Sum;
else
{
CPLError(CE_Failure, CPLE_IllegalArg, "Unknown resampling method: %s.", papszArgv[i]);