GDAL

gdalgeoloc.h
1 /******************************************************************************
2 *
3 * Project: GDAL
4 * Purpose: Implements Geolocation array based transformer.
5 * Author: Even Rouault, <even.rouault at spatialys.com>
6 *
7 ******************************************************************************
8 * Copyright (c) 2022, Planet Labs
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a
11 * copy of this software and associated documentation files (the "Software"),
12 * to deal in the Software without restriction, including without limitation
13 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 * and/or sell copies of the Software, and to permit persons to whom the
15 * Software is furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included
18 * in all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 * DEALINGS IN THE SOFTWARE.
27 ****************************************************************************/
28
29 #ifndef GDALGEOLOC_H
30 #define GDALGEOLOC_H
31
32 #include "gdal_alg_priv.h"
33
34 /************************************************************************/
35 /* GDALGeoLoc */
36 /************************************************************************/
37
40 template<class Accessors> struct GDALGeoLoc
41 {
42 static bool LoadGeolocFinish( GDALGeoLocTransformInfo *psTransform );
43
44 static bool GenerateBackMap( GDALGeoLocTransformInfo *psTransform );
45
46 static bool PixelLineToXY(const GDALGeoLocTransformInfo *psTransform,
47 const int nGeoLocPixel,
48 const int nGeoLocLine,
49 double& dfX,
50 double& dfY);
51
52 static bool PixelLineToXY(const GDALGeoLocTransformInfo *psTransform,
53 const double dfGeoLocPixel,
54 const double dfGeoLocLine,
55 double& dfX,
56 double& dfY);
57
58 static bool ExtractSquare(const GDALGeoLocTransformInfo *psTransform,
59 int nX, int nY,
60 double& dfX_0_0, double& dfY_0_0,
61 double& dfX_1_0, double& dfY_1_0,
62 double& dfX_0_1, double& dfY_0_1,
63 double& dfX_1_1, double& dfY_1_1);
64
65 static int Transform( void *pTransformArg,
66 int bDstToSrc,
67 int nPointCount,
68 double *padfX, double *padfY,
69 double * /* padfZ */,
70 int *panSuccess );
71 };
75 bool GDALGeoLocExtractSquare(const GDALGeoLocTransformInfo *psTransform,
76 int nX, int nY,
77 double& dfX_0_0, double& dfY_0_0,
78 double& dfX_1_0, double& dfY_1_0,
79 double& dfX_0_1, double& dfY_0_1,
80 double& dfX_1_1, double& dfY_1_1);
81
82 void GDALInverseBilinearInterpolation(const double x,
83 const double y,
84 const double x0,
85 const double y0,
86 const double x1,
87 const double y1,
88 const double x2,
89 const double y2,
90 const double x3,
91 const double y3,
92 double& i,
93 double& j);
94
95 /************************************************************************/
96 /* ShiftGeoX() */
97 /************************************************************************/
98
99 // Avoid discontinuity at anti-meridian when interpolating longitude
100 // dfXRef is a "reference" longitude, typically the one of 4 points to
101 // interpolate), towards which we apply a potential +/- 360 deg shift.
102 // This may result in a value slightly outside [-180,180]
103 static double ShiftGeoX(const GDALGeoLocTransformInfo *psTransform,
104 double dfXRef,
105 double dfX)
106 {
107 if( !psTransform->bGeographicSRSWithMinus180Plus180LongRange )
108 return dfX;
109 // The threshold at 170 deg is a bit arbitrary. A smarter approach
110 // would try to guess the "average" longitude step between 2 grid values
111 // and use 180 - average_step * some_factor as the threshold.
112 if( dfXRef < -170 && dfX > 170 )
113 return dfX - 360;
114 if( dfXRef > 170 && dfX < -170 )
115 return dfX + 360;
116 return dfX;
117 }
118
119 #endif