GDAL

gdal_geotransform.h
1/******************************************************************************
2 *
3 * Name: gdal_geotransform.h
4 * Project: GDAL Core
5 * Purpose: Declaration of GDALGeoTransform class
6 * Author: Even Rouault, <even.rouault@spatialys.com>
7 *
8 ******************************************************************************
9 * Copyright (c) 2025, Even Rouault, <even.rouault@spatialys.com>
10 *
11 * SPDX-License-Identifier: MIT
12 ****************************************************************************/
13
14#ifndef GDALGEOTRANSFORM_H_INCLUDED
15#define GDALGEOTRANSFORM_H_INCLUDED
16
17#include "gdal.h"
18
19#include <utility>
20
21 class GDALRasterWindow;
22
23/* ******************************************************************** */
24/* GDALGeoTransform */
25/* ******************************************************************** */
26
41 class CPL_DLL GDALGeoTransform
42{
43 public:
44 // NOTE to GDAL developers: do not reorder those coefficients!
45
47 double xorig = 0;
48
50 double xscale = 1;
51
53 double xrot = 0;
54
56 double yorig = 0;
57
59 double yrot = 0;
60
62 double yscale = 1;
63
65 inline GDALGeoTransform() = default;
66
68 inline explicit GDALGeoTransform(const double coeffs[6])
69 {
70 static_assert(sizeof(GDALGeoTransform) == 6 * sizeof(double),
71 "Wrong size for GDALGeoTransform");
72 xorig = coeffs[0];
73 xscale = coeffs[1];
74 xrot = coeffs[2];
75 yorig = coeffs[3];
76 yrot = coeffs[4];
77 yscale = coeffs[5];
78 }
79
81 inline GDALGeoTransform(double xorigIn, double xscaleIn, double xrotIn,
82 double yorigIn, double yrotIn, double yscaleIn)
83 {
84 xorig = xorigIn;
85 xscale = xscaleIn;
86 xrot = xrotIn;
87 yorig = yorigIn;
88 yrot = yrotIn;
89 yscale = yscaleIn;
90 }
91
93 template <typename T> inline double operator[](T idx) const
94 {
95 return *(&xorig + idx);
96 }
97
99 template <typename T> inline double &operator[](T idx)
100 {
101 return *(&xorig + idx);
102 }
103
105 inline bool operator==(const GDALGeoTransform &other) const
106 {
107 return xorig == other.xorig && xscale == other.xscale &&
108 xrot == other.xrot && yorig == other.yorig &&
109 yrot == other.yrot && yscale == other.yscale;
110 }
111
113 inline bool operator!=(const GDALGeoTransform &other) const
114 {
115 return !(operator==(other));
116 }
117
119 inline const double *data() const
120 {
121 return &xorig;
122 }
123
125 inline double *data()
126 {
127 return &xorig;
128 }
129
150 inline void Apply(double dfPixel, double dfLine, double *pdfGeoX,
151 double *pdfGeoY) const
152 {
153 GDALApplyGeoTransform(data(), dfPixel, dfLine, pdfGeoX, pdfGeoY);
154 }
155
162 bool Apply(const OGREnvelope &env, GDALRasterWindow &window) const;
163
170 bool Apply(const GDALRasterWindow &window, OGREnvelope &env) const;
171
189 inline std::pair<double, double> Apply(double dfPixel, double dfLine) const
190 {
191 double dfOutX, dfOutY;
192 GDALApplyGeoTransform(data(), dfPixel, dfLine, &dfOutX, &dfOutY);
193 return {dfOutX, dfOutY};
194 }
195
206 inline bool GetInverse(GDALGeoTransform &inverse) const
207 {
208 return GDALInvGeoTransform(data(), inverse.data()) == TRUE;
209 }
210
218 inline void Rescale(double dfXRatio, double dfYRatio)
219 {
220 xscale *= dfXRatio;
221 xrot *= dfYRatio;
222 yrot *= dfXRatio;
223 yscale *= dfYRatio;
224 }
225
228 inline bool IsAxisAligned() const
229 {
230 return xrot == 0 && yrot == 0;
231 }
232};
233
234#endif
GDALGeoTransform
Class that encapsulates a geotransform matrix.
Definition gdal_geotransform.h:42
GDALGeoTransform::GDALGeoTransform
GDALGeoTransform(const double coeffs[6])
Constructor from a array of 6 double.
Definition gdal_geotransform.h:68
GDALGeoTransform::xorig
double xorig
X value of the origin of the raster.
Definition gdal_geotransform.h:47
GDALGeoTransform::operator[]
double operator[](T idx) const
Element accessor.
Definition gdal_geotransform.h:93
GDALGeoTransform::data
double * data()
Cast to double*.
Definition gdal_geotransform.h:125
GDALGeoTransform::IsAxisAligned
bool IsAxisAligned() const
Check whether the geotransform has a rotation component.
Definition gdal_geotransform.h:228
GDALGeoTransform::GDALGeoTransform
GDALGeoTransform(double xorigIn, double xscaleIn, double xrotIn, double yorigIn, double yrotIn, double yscaleIn)
Constructor from 6 double values.
Definition gdal_geotransform.h:81
GDALGeoTransform::yrot
double yrot
Y rotation factor.
Definition gdal_geotransform.h:59
GDALGeoTransform::operator!=
bool operator!=(const GDALGeoTransform &other) const
Inequality test operator.
Definition gdal_geotransform.h:113
GDALGeoTransform::GDALGeoTransform
GDALGeoTransform()=default
Default constructor for an identity geotransformation matrix.
GDALGeoTransform::operator[]
double & operator[](T idx)
Element accessor.
Definition gdal_geotransform.h:99
GDALGeoTransform::data
const double * data() const
Cast to const double*.
Definition gdal_geotransform.h:119
GDALGeoTransform::Apply
std::pair< double, double > Apply(double dfPixel, double dfLine) const
Apply GeoTransform to x/y coordinate.
Definition gdal_geotransform.h:189
GDALGeoTransform::operator==
bool operator==(const GDALGeoTransform &other) const
Equality test operator.
Definition gdal_geotransform.h:105
GDALGeoTransform::yorig
double yorig
Y value of the origin of the raster.
Definition gdal_geotransform.h:56
GDALGeoTransform::Rescale
void Rescale(double dfXRatio, double dfYRatio)
Rescale a geotransform by multiplying its scale and rotation terms by the provided ratios.
Definition gdal_geotransform.h:218
GDALGeoTransform::xscale
double xscale
X scale factor.
Definition gdal_geotransform.h:50
GDALGeoTransform::GetInverse
bool GetInverse(GDALGeoTransform &inverse) const
Invert Geotransform.
Definition gdal_geotransform.h:206
GDALGeoTransform::Apply
void Apply(double dfPixel, double dfLine, double *pdfGeoX, double *pdfGeoY) const
Apply GeoTransform to x/y coordinate.
Definition gdal_geotransform.h:150
GDALGeoTransform::xrot
double xrot
X rotation factor.
Definition gdal_geotransform.h:53
GDALGeoTransform::yscale
double yscale
Y scale factor.
Definition gdal_geotransform.h:62
GDALRasterWindow
A rectangular subset of pixels within a raster.
Definition gdal_rasterband.h:87
OGREnvelope
Simple container for a bounding region (rectangle)
Definition ogr_core.h:44
gdal.h
Public (C callable) GDAL entry points.
GDALInvGeoTransform
int GDALInvGeoTransform(const double *padfGeoTransformIn, double *padfInvGeoTransformOut)
Invert Geotransform.
Definition gdaltransformer.cpp:4562
GDALApplyGeoTransform
void GDALApplyGeoTransform(const double *, double, double, double *, double *)
Apply GeoTransform to x/y coordinate.
Definition gdaltransformer.cpp:4536