include/terraingen/raster/geo_transform.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | /// @file | ||
| 4 | /// @brief The affine pixel-to-world transform of a georeferenced raster. | ||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include "terraingen/geometry/geometry.h" | ||
| 8 | |||
| 9 | #include <array> | ||
| 10 | #include <cmath> | ||
| 11 | #include <stdexcept> | ||
| 12 | |||
| 13 | namespace terraingen { | ||
| 14 | |||
| 15 | /// Affine mapping from pixel to world coordinates, as GDAL's six-coefficient geotransform. | ||
| 16 | /// | ||
| 17 | /// Pixel (0, 0) is the outer corner of the first pixel, so centers sit at half-integer | ||
| 18 | /// coordinates. World coordinates are in the CRS's units (meters when projected). North-up rasters | ||
| 19 | /// have a negative `pixelHeight` and no rotation. The default maps pixels to world one to one, as | ||
| 20 | /// GDAL does for ungeoreferenced rasters. | ||
| 21 | 3 | struct GeoTransform { | |
| 22 | double originX = 0.0; ///< World x of pixel coordinate (0, 0). | ||
| 23 | double pixelWidth = 1.0; ///< World x change per column. | ||
| 24 | double rowRotation = 0.0; ///< World x change per row. | ||
| 25 | double originY = 0.0; ///< World y of pixel coordinate (0, 0). | ||
| 26 | double colRotation = 0.0; ///< World y change per column. | ||
| 27 | double pixelHeight = 1.0; ///< World y change per row; negative for north-up. | ||
| 28 | |||
| 29 | /// A north-up transform with square pixels. | ||
| 30 | /// @param originX World x of the raster's west edge. | ||
| 31 | /// @param originY World y of the raster's north edge. | ||
| 32 | /// @param pixelSize Pixel edge length in world units; positive. | ||
| 33 | 31640 | static GeoTransform NorthUp(double originX, double originY, double pixelSize) { | |
| 34 | 25175 | return GeoTransform{originX, pixelSize, 0.0, originY, 0.0, -pixelSize}; | |
| 35 | } | ||
| 36 | |||
| 37 | /// A transform from coefficients in the order `GDALDataset::GetGeoTransform` gives them. | ||
| 38 | 827 | static GeoTransform FromGdal(const std::array<double, 6>& c) { | |
| 39 | 828 | return GeoTransform{c[0], c[1], c[2], c[3], c[4], c[5]}; | |
| 40 | } | ||
| 41 | |||
| 42 | /// The coefficients in GDAL's order, as passed to `GDALDataset::SetGeoTransform`. | ||
| 43 | 15 | std::array<double, 6> ToGdal() const { | |
| 44 | 15 | return {originX, pixelWidth, rowRotation, originY, colRotation, pixelHeight}; | |
| 45 | } | ||
| 46 | |||
| 47 | /// Whether rows or columns are rotated against the world axes. | ||
| 48 | 783958 | bool IsRotated() const { | |
| 49 | 783958 | return rowRotation != 0.0 || colRotation != 0.0; | |
| 50 | } | ||
| 51 | |||
| 52 | /// The length in world units of a cell's side along its row, from one column to the next. | ||
| 53 | 68108 | double CellWidth() const { | |
| 54 | 68108 | return std::hypot(pixelWidth, colRotation); | |
| 55 | } | ||
| 56 | |||
| 57 | /// The length in world units of a cell's side along its column, from one row to the next. | ||
| 58 | 70040 | double CellHeight() const { | |
| 59 | 70040 | return std::hypot(rowRotation, pixelHeight); | |
| 60 | } | ||
| 61 | |||
| 62 | /// Maps pixel coordinates, 0 at the first pixel's outer edge, to world coordinates. | ||
| 63 | 36698047 | Vec2d PixelToWorld(double col, double row) const { | |
| 64 | 36698047 | return {originX + col * pixelWidth + row * rowRotation, | |
| 65 | 36698047 | originY + col * colRotation + row * pixelHeight}; | |
| 66 | } | ||
| 67 | |||
| 68 | /// Maps world coordinates to pixel coordinates (column, row). | ||
| 69 | /// @throws std::domain_error if the transform is not invertible. | ||
| 70 | 2892008 | Vec2d WorldToPixel(double x, double y) const { | |
| 71 | 2892008 | const double det = pixelWidth * pixelHeight - rowRotation * colRotation; | |
| 72 | 2892008 | if (det == 0.0) { | |
| 73 | 2 | throw std::domain_error("GeoTransform is not invertible"); | |
| 74 | } | ||
| 75 | 2892006 | const double dx = x - originX; | |
| 76 | 2892006 | const double dy = y - originY; | |
| 77 | 2892006 | return {(dx * pixelHeight - dy * rowRotation) / det, | |
| 78 | 2892006 | (dy * pixelWidth - dx * colRotation) / det}; | |
| 79 | } | ||
| 80 | |||
| 81 | /// Coefficientwise equality. | ||
| 82 | 938 | bool operator==(const GeoTransform& other) const { | |
| 83 | 898 | return originX == other.originX && pixelWidth == other.pixelWidth && | |
| 84 | 896 | rowRotation == other.rowRotation && originY == other.originY && | |
| 85 | 1832 | colRotation == other.colRotation && pixelHeight == other.pixelHeight; | |
| 86 | } | ||
| 87 | 6 | bool operator!=(const GeoTransform& other) const { | |
| 88 | 6 | return !(*this == other); | |
| 89 | } | ||
| 90 | }; | ||
| 91 | |||
| 92 | } // namespace terraingen | ||
| 93 |