|
| 1 | +#include "relocateRaster.h" |
| 2 | + |
| 3 | +#include <string> |
| 4 | +#include <isce3/core/Projections.h> |
| 5 | +#include <isce3/io/Raster.h> |
| 6 | +#include <isce3/geometry/geometry.h> |
| 7 | +#include <isce3/geometry/DEMInterpolator.h> |
| 8 | +#include <isce3/geometry/loadDem.h> |
| 9 | +#include <isce3/product/GeoGridParameters.h> |
| 10 | + |
| 11 | +namespace isce3 { |
| 12 | +namespace geogrid { |
| 13 | + |
| 14 | + |
| 15 | +template<class T> |
| 16 | +static isce3::core::Matrix<T> |
| 17 | +getNanArray(const isce3::product::GeoGridParameters& geogrid) |
| 18 | +{ |
| 19 | + isce3::core::Matrix<T> data_array(geogrid.length(), geogrid.width()); |
| 20 | + data_array.fill(std::numeric_limits<T>::quiet_NaN()); |
| 21 | + return data_array; |
| 22 | +} |
| 23 | + |
| 24 | +template<class T> |
| 25 | +static void writeArray(isce3::io::Raster& raster, |
| 26 | + isce3::core::Matrix<T>& data_array, int band_index) |
| 27 | +{ |
| 28 | + raster.setBlock(data_array.data(), 0, 0, data_array.width(), |
| 29 | + data_array.length(), band_index + 1); |
| 30 | +} |
| 31 | + |
| 32 | +void _validateRasters(isce3::io::Raster& input_raster, |
| 33 | + const isce3::product::GeoGridParameters& geogrid, |
| 34 | + isce3::io::Raster& output_raster) { |
| 35 | + |
| 36 | + if (input_raster.getEPSG() < 0) { |
| 37 | + std::string error_message = |
| 38 | + "ERROR invalid input raster EPSG code: " + |
| 39 | + std::to_string(input_raster.getEPSG()); |
| 40 | + throw isce3::except::RuntimeError(ISCE_SRCINFO(), error_message); |
| 41 | + } |
| 42 | + |
| 43 | + if (geogrid.epsg() < 0) { |
| 44 | + std::string error_message = |
| 45 | + "ERROR invalid geogrid EPSG code: " + |
| 46 | + std::to_string(geogrid.epsg()); |
| 47 | + throw isce3::except::RuntimeError(ISCE_SRCINFO(), error_message); |
| 48 | + } |
| 49 | + |
| 50 | + if (geogrid.length() != output_raster.length() or |
| 51 | + geogrid.width() != output_raster.width()) { |
| 52 | + std::string error_message = |
| 53 | + "ERROR geogrid and output rasters have different dimensions"; |
| 54 | + throw isce3::except::RuntimeError(ISCE_SRCINFO(), error_message); |
| 55 | + } |
| 56 | + |
| 57 | + if (input_raster.numBands() != output_raster.numBands()) { |
| 58 | + std::string error_message = |
| 59 | + "ERROR input and output rasters have different number of bands: " + |
| 60 | + std::to_string(input_raster.numBands()) + " (input raster) vs " + |
| 61 | + std::to_string(input_raster.numBands()) + " (output raster)"; |
| 62 | + throw isce3::except::RuntimeError(ISCE_SRCINFO(), error_message); |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +void relocateRaster(isce3::io::Raster& input_raster, |
| 67 | + const isce3::product::GeoGridParameters& geogrid, |
| 68 | + isce3::io::Raster& output_raster, |
| 69 | + isce3::core::dataInterpMethod interp_method) { |
| 70 | + |
| 71 | + pyre::journal::info_t info("isce.geogrid.relocateRaster"); |
| 72 | + |
| 73 | + _validateRasters(input_raster, geogrid, output_raster); |
| 74 | + |
| 75 | + geogrid.print(); |
| 76 | + |
| 77 | + auto proj = isce3::core::makeProjection(geogrid.epsg()); |
| 78 | + |
| 79 | + double refheight = 0; |
| 80 | + isce3::geometry::DEMInterpolator dem_interp(refheight, interp_method); |
| 81 | + const double minX = geogrid.startX(); |
| 82 | + const double maxX = geogrid.startX() + (geogrid.spacingX() * |
| 83 | + geogrid.width()); |
| 84 | + double minY = geogrid.startY(); |
| 85 | + double maxY = geogrid.startY() + geogrid.spacingY() * geogrid.length(); |
| 86 | + |
| 87 | + const int dem_margin_in_pixels = 100; |
| 88 | + |
| 89 | + for (int band_index = 0; band_index < input_raster.numBands(); |
| 90 | + ++band_index) { |
| 91 | + |
| 92 | + auto error_code = loadDemFromProj(input_raster, minX, maxX, minY, |
| 93 | + maxY, &dem_interp, proj.get(), |
| 94 | + dem_margin_in_pixels, |
| 95 | + dem_margin_in_pixels, |
| 96 | + band_index + 1); |
| 97 | + |
| 98 | + if (error_code != isce3::error::ErrorCode::Success) { |
| 99 | + std::string error_message = |
| 100 | + "ERROR loading raster for given area"; |
| 101 | + throw isce3::except::RuntimeError(ISCE_SRCINFO(), error_message); |
| 102 | + } |
| 103 | + |
| 104 | + /* |
| 105 | + Get DEM interpolator and function getDemCoords to convert DEM coordinates |
| 106 | + to the geogrid EPSG coordinates: |
| 107 | + */ |
| 108 | + std::function<isce3::core::Vec3(double, double, |
| 109 | + const isce3::geometry::DEMInterpolator&, |
| 110 | + isce3::core::ProjectionBase*)> getDemCoords; |
| 111 | + |
| 112 | + if (geogrid.epsg() == input_raster.getEPSG()) { |
| 113 | + getDemCoords = isce3::geometry::getDemCoordsSameEpsg; |
| 114 | + |
| 115 | + } else { |
| 116 | + getDemCoords = isce3::geometry::getDemCoordsDiffEpsg; |
| 117 | + } |
| 118 | + |
| 119 | + auto interpolated_dem_array = getNanArray<float>(geogrid); |
| 120 | + |
| 121 | + #pragma omp parallel for |
| 122 | + for (int i = 0; i < geogrid.length(); ++i) { |
| 123 | + double pos_y = geogrid.startY() + (0.5 + i) * geogrid.spacingY(); |
| 124 | + for (int j = 0; j < geogrid.width(); ++j) { |
| 125 | + double pos_x = |
| 126 | + geogrid.startX() + (0.5 + j) * geogrid.spacingX(); |
| 127 | + |
| 128 | + const isce3::core::Vec3 input_dem = |
| 129 | + getDemCoords(pos_x, pos_y, dem_interp, proj.get()); |
| 130 | + |
| 131 | + interpolated_dem_array(i, j) = input_dem[2]; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + writeArray(output_raster, interpolated_dem_array, band_index); |
| 136 | + |
| 137 | + } |
| 138 | + |
| 139 | + double geotransform[] = { |
| 140 | + geogrid.startX(), geogrid.spacingX(), 0, geogrid.startY(), 0, |
| 141 | + geogrid.spacingY()}; |
| 142 | + |
| 143 | + output_raster.setGeoTransform(geotransform); |
| 144 | + output_raster.setEPSG(geogrid.epsg()); |
| 145 | + |
| 146 | +} |
| 147 | + |
| 148 | +}} |
0 commit comments