LCOV - code coverage report
Current view: top level - gcore - gdal_geotransform.h (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 49 49 100.0 %
Date: 2026-12-03 10:33:05 Functions: 15 15 100.0 %

          Line data    Source code
       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             : 
      27             : /** Class that encapsulates a geotransform matrix.
      28             :  *
      29             :  * It contains 6 coefficients expressing an affine transformation from
      30             :  * (column, line) raster space to (X, Y) georeferenced space, such that
      31             :  *
      32             :  * \code{.c}
      33             :  *  X = xorig + column * xscale + line * xrot;
      34             :  *  Y = yorig + column * yrot   + line * yscale;
      35             :  * \endcode
      36             :  *
      37             :  * The default value is the identity transformation.
      38             :  *
      39             :  * @since 3.12
      40             :  */
      41             : class CPL_DLL GDALGeoTransform
      42             : {
      43             :   public:
      44             :     // NOTE to GDAL developers: do not reorder those coefficients!
      45             : 
      46             :     /** X value of the origin of the raster */
      47             :     double xorig = 0;
      48             : 
      49             :     /** X scale factor */
      50             :     double xscale = 1;
      51             : 
      52             :     /** X rotation factor */
      53             :     double xrot = 0;
      54             : 
      55             :     /** Y value of the origin of the raster */
      56             :     double yorig = 0;
      57             : 
      58             :     /** Y rotation factor */
      59             :     double yrot = 0;
      60             : 
      61             :     /** Y scale factor */
      62             :     double yscale = 1;
      63             : 
      64             :     /** Default constructor for an identity geotransformation matrix. */
      65      222692 :     inline GDALGeoTransform() = default;
      66             : 
      67             :     /** Constructor from an array of 6 doubles */
      68          45 :     inline explicit GDALGeoTransform(const double coeffs[6])
      69          45 :     {
      70             :         static_assert(sizeof(GDALGeoTransform) == 6 * sizeof(double),
      71             :                       "Wrong size for GDALGeoTransform");
      72          45 :         xorig = coeffs[0];
      73          45 :         xscale = coeffs[1];
      74          45 :         xrot = coeffs[2];
      75          45 :         yorig = coeffs[3];
      76          45 :         yrot = coeffs[4];
      77          45 :         yscale = coeffs[5];
      78          45 :     }
      79             : 
      80             :     /** Constructor from 6 double values */
      81        7580 :     inline GDALGeoTransform(double xorigIn, double xscaleIn, double xrotIn,
      82             :                             double yorigIn, double yrotIn, double yscaleIn)
      83        7580 :     {
      84        7580 :         xorig = xorigIn;
      85        7580 :         xscale = xscaleIn;
      86        7580 :         xrot = xrotIn;
      87        7580 :         yorig = yorigIn;
      88        7580 :         yrot = yrotIn;
      89        7580 :         yscale = yscaleIn;
      90        7580 :     }
      91             : 
      92             :     /** Element accessor. idx must be in [0,5] range */
      93        2344 :     template <typename T> inline double operator[](T idx) const
      94             :     {
      95             :         static constexpr double GDALGeoTransform::*apMembers[] = {
      96             :             &GDALGeoTransform::xorig, &GDALGeoTransform::xscale,
      97             :             &GDALGeoTransform::xrot,  &GDALGeoTransform::yorig,
      98             :             &GDALGeoTransform::yrot,  &GDALGeoTransform::yscale};
      99        2344 :         return this->*apMembers[idx];
     100             :     }
     101             : 
     102             :     /** Element accessor. idx must be in [0,5] range */
     103     6182469 :     template <typename T> inline double &operator[](T idx)
     104             :     {
     105             :         static constexpr double GDALGeoTransform::*apMembers[] = {
     106             :             &GDALGeoTransform::xorig, &GDALGeoTransform::xscale,
     107             :             &GDALGeoTransform::xrot,  &GDALGeoTransform::yorig,
     108             :             &GDALGeoTransform::yrot,  &GDALGeoTransform::yscale};
     109     6182469 :         return this->*apMembers[idx];
     110             :     }
     111             : 
     112             :     /** Equality test operator */
     113        5550 :     inline bool operator==(const GDALGeoTransform &other) const
     114             :     {
     115        3798 :         return xorig == other.xorig && xscale == other.xscale &&
     116        3768 :                xrot == other.xrot && yorig == other.yorig &&
     117        9348 :                yrot == other.yrot && yscale == other.yscale;
     118             :     }
     119             : 
     120             :     /** Inequality test operator */
     121        5487 :     inline bool operator!=(const GDALGeoTransform &other) const
     122             :     {
     123        5487 :         return !(operator==(other));
     124             :     }
     125             : 
     126             :     /** Cast to const double* */
     127      111941 :     inline const double *data() const
     128             :     {
     129      111941 :         return &xorig;
     130             :     }
     131             : 
     132             :     /** Cast to double* */
     133       25328 :     inline double *data()
     134             :     {
     135       25328 :         return &xorig;
     136             :     }
     137             : 
     138             :     /**
     139             :      * Apply GeoTransform to x/y coordinate.
     140             :      *
     141             :      * Applies the following computation, converting a (pixel, line) coordinate
     142             :      * into a georeferenced (geo_x, geo_y) location.
     143             :      * \code{.c}
     144             :      *  *pdfGeoX = padfGeoTransform[0] + dfPixel * padfGeoTransform[1]
     145             :      *                                 + dfLine  * padfGeoTransform[2];
     146             :      *  *pdfGeoY = padfGeoTransform[3] + dfPixel * padfGeoTransform[4]
     147             :      *                                 + dfLine  * padfGeoTransform[5];
     148             :      * \endcode
     149             :      *
     150             :      * @param dfPixel Input pixel position.
     151             :      * @param dfLine Input line position.
     152             :      * @param pdfGeoX output location where geo_x (easting/longitude)
     153             :      * location is placed.
     154             :      * @param pdfGeoY output location where geo_y (northing/latitude)
     155             :      * location is placed.
     156             :      */
     157             : 
     158       12298 :     inline void Apply(double dfPixel, double dfLine, double *pdfGeoX,
     159             :                       double *pdfGeoY) const
     160             :     {
     161       12298 :         GDALApplyGeoTransform(data(), dfPixel, dfLine, pdfGeoX, pdfGeoY);
     162       12298 :     }
     163             : 
     164             :     /** Apply a (inverse) geotransform to an OGREnvelope in georeferenced coordinates.
     165             :      *
     166             :      * @param env An envelope in georeferenced coordinates
     167             :      * @param[out] window A window in pixel/line coordinates
     168             :      * @return true if the geotransform was successfully applied
     169             :      */
     170             :     bool Apply(const OGREnvelope &env, GDALRasterWindow &window) const;
     171             : 
     172             :     /** Apply a geotransform to a GDALRasterWindow in pixel/line coordinates.
     173             :      *
     174             :      * @param window A window in pixel/line coordinates
     175             :      * @param[out] env An envelope in georeferenced coordinates
     176             :      * @return true if the geotransform was successfully applied
     177             :      */
     178             :     bool Apply(const GDALRasterWindow &window, OGREnvelope &env) const;
     179             : 
     180             :     /**
     181             :      * Apply GeoTransform to x/y coordinate.
     182             :      *
     183             :      * Applies the following computation, converting a (pixel, line) coordinate
     184             :      * into a georeferenced (geo_x, geo_y) location.
     185             :      * \code{.c}
     186             :      *  out.first = padfGeoTransform[0] + dfPixel * padfGeoTransform[1]
     187             :      *                                   + dfLine  * padfGeoTransform[2];
     188             :      *  out.second = padfGeoTransform[3] + dfPixel * padfGeoTransform[4]
     189             :      *                                   + dfLine  * padfGeoTransform[5];
     190             :      * \endcode
     191             :      *
     192             :      * @param dfPixel Input pixel position.
     193             :      * @param dfLine Input line position.
     194             :      * @return output location as a (geo_x, geo_y) pair
     195             :      */
     196             : 
     197       99248 :     inline std::pair<double, double> Apply(double dfPixel, double dfLine) const
     198             :     {
     199             :         double dfOutX, dfOutY;
     200       99248 :         GDALApplyGeoTransform(data(), dfPixel, dfLine, &dfOutX, &dfOutY);
     201       99248 :         return {dfOutX, dfOutY};
     202             :     }
     203             : 
     204             :     /**
     205             :      * Invert Geotransform.
     206             :      *
     207             :      * This function will invert a standard 3x2 set of GeoTransform coefficients.
     208             :      * This converts the equation from being pixel to geo to being geo to pixel.
     209             :      *
     210             :      * @param[out] inverse Output geotransform
     211             :      *
     212             :      * @return true on success or false if the equation is uninvertable.
     213             :      */
     214         395 :     inline bool GetInverse(GDALGeoTransform &inverse) const
     215             :     {
     216         395 :         return GDALInvGeoTransform(data(), inverse.data()) == TRUE;
     217             :     }
     218             : 
     219             :     /** Rescale a geotransform by multiplying its scale and rotation terms by
     220             :      * the provided ratios.
     221             :      *
     222             :      * This is typically used to compute the geotransform matrix of an overview
     223             :      * dataset from the full resolution dataset, where the ratios are the size
     224             :      * of the full resolution dataset divided by the size of the overview.
     225             :      */
     226        2124 :     inline void Rescale(double dfXRatio, double dfYRatio)
     227             :     {
     228        2124 :         xscale *= dfXRatio;
     229        2124 :         xrot *= dfYRatio;
     230        2124 :         yrot *= dfXRatio;
     231        2124 :         yscale *= dfYRatio;
     232        2124 :     }
     233             : 
     234             :     /** Check whether the geotransform has a rotation component.
     235             :      */
     236        1522 :     inline bool IsAxisAligned() const
     237             :     {
     238        1522 :         return xrot == 0 && yrot == 0;
     239             :     }
     240             : 
     241             :     /** Initialize a geotransform from a string.
     242             :      *
     243             :      * @param pszGT the string to parse
     244             :      * @param pszSep string separating the elements of the geotransform
     245             :      * @return true if the initialization was successful
     246             :      * @since 3.13
     247             :      */
     248             :     bool Init(const char *pszGT, const char *pszSep = ",");
     249             : 
     250             :     /** Return a string containing the six geotransform values
     251             :      * separated by commas.
     252             :      *
     253             :      * @param pszSep characters to place between elements of the geotransform
     254             :      * @since 3.13
     255             :      */
     256             :     std::string ToString(const char *pszSep = ", ") const;
     257             : };
     258             : 
     259             : #endif

Generated by: LCOV version 1.14