|
47 | 47 | #endif
|
48 | 48 |
|
49 | 49 | #include <Eigen/Dense>
|
| 50 | +#include <array> |
| 51 | +#include <cassert> |
50 | 52 |
|
51 | 53 | enum class ESPG { ECEF = 4978, WGS84 = 4326, WGS84_32N = 32632, CH1903_LV03 = 21781 };
|
52 | 54 |
|
| 55 | +struct Location { |
| 56 | + ESPG espg{ESPG::WGS84}; |
| 57 | + // <east (lat), north (lng), up (alt)> |
| 58 | + //! @todo Switch to geographic_msgs/GeoPoint to make x-y not confusing? |
| 59 | + Eigen::Vector3d position{Eigen::Vector3d::Zero()}; |
| 60 | +}; |
| 61 | + |
53 | 62 | /**
|
54 | 63 | * @brief Helper function for transforming using gdal
|
55 | 64 | *
|
@@ -100,4 +109,81 @@ inline Eigen::Vector3d transformCoordinates(ESPG src_coord, const std::string wk
|
100 | 109 | return target_coordinates;
|
101 | 110 | }
|
102 | 111 |
|
| 112 | +struct Corners { |
| 113 | + ESPG espg{ESPG::WGS84}; |
| 114 | + Eigen::Vector2d top_left{Eigen::Vector2d::Zero()}; |
| 115 | + Eigen::Vector2d top_right{Eigen::Vector2d::Zero()}; |
| 116 | + Eigen::Vector2d bottom_left{Eigen::Vector2d::Zero()}; |
| 117 | + Eigen::Vector2d bottom_right{Eigen::Vector2d::Zero()}; |
| 118 | +}; |
| 119 | + |
| 120 | +/** |
| 121 | + * @brief Helper function converting from image to geo coordinates |
| 122 | + * |
| 123 | + * @ref |
| 124 | + https://gdal.org/tutorials/geotransforms_tut.html#transformation-from-image-coordinate-space-to-georeferenced-coordinate-space |
| 125 | + * @see GDALApplyGeoTransform |
| 126 | + * |
| 127 | + * @param geoTransform The 6-element Geo transform |
| 128 | + * @param imageCoords The image-coordinates <row, column>, also called <pixel, line> |
| 129 | +
|
| 130 | + * @return The geo-coordinates in <x, y> |
| 131 | + */ |
| 132 | +inline Eigen::Vector2d imageToGeo(const std::array<double, 6> geoTransform, const Eigen::Vector2i imageCoords) { |
| 133 | + const auto x_pixel = imageCoords.x(); |
| 134 | + const auto y_line = imageCoords.y(); |
| 135 | + |
| 136 | + return {geoTransform.at(0) + x_pixel * geoTransform.at(1) + y_line * geoTransform.at(2), |
| 137 | + geoTransform.at(3) + x_pixel * geoTransform.at(4) + y_line * geoTransform.at(5)}; |
| 138 | +} |
| 139 | + |
| 140 | +/** |
| 141 | + * @brief Helper function converting from geo to image coordinates. Assumes no rotation. |
| 142 | + * Uses the assumption that GT2 and GT4 are zero |
| 143 | + * |
| 144 | + * @ref |
| 145 | + * https://gis.stackexchange.com/questions/384221/calculating-inverse-polynomial-transforms-for-pixel-sampling-when-map-georeferen |
| 146 | + * @see GDALApplyGeoTransform |
| 147 | + * |
| 148 | + * @param geoTransform The 6-element forward Geo transform |
| 149 | + * @param geoCoords The geo-coordinates in <x, y> |
| 150 | + * |
| 151 | + * @return The image-coordinates in <row, column>, also called <pixel, line> |
| 152 | + */ |
| 153 | +inline Eigen::Vector2d geoToImageNoRot(const std::array<double, 6>& geoTransform, const Eigen::Vector2d geoCoords) { |
| 154 | + assert(geoTransform.at(2) == 0); // assume no rotation |
| 155 | + assert(geoTransform.at(4) == 0); // assume no rotation |
| 156 | + |
| 157 | + return {(geoCoords.x() - geoTransform.at(0)) / geoTransform.at(1), |
| 158 | + (geoCoords.y() - geoTransform.at(3)) / geoTransform.at(5)}; |
| 159 | +} |
| 160 | + |
| 161 | +/** |
| 162 | + * @brief Helper function for getting dataset corners |
| 163 | + * Inspired by gdalinfo_lib.cpp::GDALInfoReportCorner() |
| 164 | + * |
| 165 | + * @param datasetPtr The pointer to the dataset |
| 166 | + * @param corners The returned corners in the geographic coordinates |
| 167 | + * @return |
| 168 | + */ |
| 169 | +inline bool getGeoCorners(const GDALDatasetUniquePtr& datasetPtr, Corners& corners) { |
| 170 | + double originX, originY, pixelSizeX, pixelSizeY; |
| 171 | + std::array<double, 6> geoTransform; |
| 172 | + |
| 173 | + // https://gdal.org/tutorials/geotransforms_tut.html#introduction-to-geotransforms |
| 174 | + if (!datasetPtr->GetGeoTransform(geoTransform.data()) == CE_None) { |
| 175 | + return false; |
| 176 | + } |
| 177 | + |
| 178 | + const auto raster_width = datasetPtr->GetRasterXSize(); |
| 179 | + const auto raster_height = datasetPtr->GetRasterYSize(); |
| 180 | + |
| 181 | + corners.top_left = imageToGeo(geoTransform, {0, 0}); |
| 182 | + corners.top_right = imageToGeo(geoTransform, {raster_width, 0}); |
| 183 | + corners.bottom_left = imageToGeo(geoTransform, {0, raster_height}); |
| 184 | + corners.bottom_right = imageToGeo(geoTransform, {raster_width, raster_height}); |
| 185 | + |
| 186 | + return true; |
| 187 | +} |
| 188 | + |
103 | 189 | #endif
|
0 commit comments