65 : ParameterAcceptor(subsection)
67 filename_ =
"ryujin.tif";
68 this->add_parameter(
"filename", filename_,
"GeoTIFF: image file to load");
70 transformation_ = {0., 0.01, 0., 0., 0., 0.01};
74 "Array \"t[]\" describing an affine transformation between image "
75 "space (indices i and j from bottom left) and real coordinates (x "
76 "and y): x = t[0] + t[1] * i + t[2] * j, and y = t[3] + t[4] * i + "
77 "t[5] * j. (This transformation sets the origin of the image space "
78 "into the bottom left corner with index i to the right and index j "
81 transformation_allow_out_of_bounds_queries_ =
false;
83 "transformation allow out of bounds queries",
84 transformation_allow_out_of_bounds_queries_,
85 "GeoTIFF: allow out-of-bounds queries. When set to true, the reader "
86 "returns constant extended values for coordinates that are outside "
87 "of the image range.");
89 transformation_use_geotiff_ =
true;
90 this->add_parameter(
"transformation use geotiff",
91 transformation_use_geotiff_,
92 "GeoTIFF: read in transformation from GeoTIFF for "
93 "constructing the affine transformation. If set to "
94 "false the manually specified transformation "
95 "parameters will be used instead.");
97 transformation_use_geotiff_origin_ =
false;
99 "transformation use geotiff origin",
100 transformation_use_geotiff_origin_,
101 "GeoTIFF: read in affine shift (i.e., position of "
102 "lower left corner) from GeoTIFF for constructing "
103 "the affine transformation. If set to false the origin specified "
104 "in the transformation parameter will be used instead.");
107 this->add_parameter(
"height normalization",
108 height_normalization_,
109 "GeoTIFF: choose base point for height normalization "
110 "that is set to 0.: none, minimum, average, maximum");
112 height_scaling_ = 1.0;
113 this->add_parameter(
"height scaling",
115 "GeoTIFF: choose base point for height normalization "
116 "that is set to 0.: none, minimum, average, maximum");
118 const auto set_up = [&] {
123 driver_projection_ =
"";
124 affine_transformation_ = {0, 0, 0, 0, 0, 0};
125 inverse_affine_transformation_ = {0, 0, 0, 0, 0, 0};
126 raster_offset_ = {0, 0};
127 raster_size_ = {0, 0};
133 this->parse_parameters_call_back.connect(set_up);
147 DEAL_II_ALWAYS_INLINE
inline double
156 if constexpr (dim >= 1)
160 if constexpr (dim >= 2)
163 const auto &[di, dj] = apply_inverse_transformation(x, y);
167 const bool in_bounds =
168 di > -1.0 && di < static_cast<double>(raster_size_[0]) + 1.0 &&
169 dj > -1.0 && dj < static_cast<double>(raster_size_[1]) + 1.0;
173 std::cout << std::setprecision(16);
174 std::cout <<
"Queried point out of bounds." << std::endl;
175 std::cout <<
"Point: " << point << std::endl;
176 std::cout <<
"Transformed coordinates: (" << di <<
"," << dj <<
")"
182 transformation_allow_out_of_bounds_queries_ || in_bounds,
183 dealii::ExcMessage(
"Raster error: The requested point is outside "
184 "the image boundary of the geotiff file"));
191 const auto i_left = std::min(
192 std::max(
static_cast<int>(std::floor(di)), 0), raster_size_[0]);
193 const auto i_right = std::min(
194 std::max(
static_cast<int>(std::ceil(di)), 0), raster_size_[0]);
195 const auto j_left = std::min(
196 std::max(
static_cast<int>(std::floor(dj)), 0), raster_size_[1]);
197 const auto j_right = std::min(
198 std::max(
static_cast<int>(std::ceil(dj)), 0), raster_size_[1]);
202 std::cout <<
"index bounding box: (" << i_left <<
"," << j_left
203 <<
") and (" << i_right <<
"," << j_right <<
")" << std::endl;
207 const double i_ratio = std::fmod(di, 1.);
208 const double j_ratio = std::fmod(dj, 1.);
210 const auto v_iljl = raster_[i_left + j_left * raster_size_[0]];
211 const auto v_irjl = raster_[i_right + j_left * raster_size_[0]];
213 const auto v_iljr = raster_[i_left + j_right * raster_size_[0]];
214 const auto v_irjr = raster_[i_right + j_right * raster_size_[0]];
216 const auto v_jl = v_iljl * (1. - i_ratio) + v_irjl * i_ratio;
217 const auto v_jr = v_iljr * (1. - i_ratio) + v_irjr * i_ratio;
219 return height_scaling_ * (v_jl * (1. - j_ratio) + v_jr * j_ratio);
233 void read_in_raster()
const
236 auto dataset_handle = GDALOpen(filename_.c_str(), GA_ReadOnly);
237 AssertThrow(dataset_handle,
238 dealii::ExcMessage(
"GDAL error: file not found"));
240 auto dataset = GDALDataset::FromHandle(dataset_handle);
241 Assert(dataset, dealii::ExcInternalError());
243 const auto driver = dataset->GetDriver();
245 driver_name_ = driver->GetMetadataItem(GDAL_DMD_LONGNAME);
246 if (dataset->GetProjectionRef() !=
nullptr)
247 driver_projection_ = dataset->GetProjectionRef();
252 dataset->GetRasterCount() == 1,
254 "GDAL driver error: currently we only support one raster"));
256 const auto raster_band = dataset->GetRasterBand(1);
258 AssertThrow(dataset->GetRasterXSize() == raster_band->GetXSize() &&
259 dataset->GetRasterYSize() == raster_band->GetYSize(),
261 "GDAL driver error: the raster band has a different "
262 "dimension than the (global) raster dimension of the "
263 "geotiff image. This is not supported."));
273 raster_offset_ = {0, 0};
274 raster_size_ = {dataset->GetRasterXSize(), dataset->GetRasterYSize()};
276 raster_.resize(raster_size_[0] * raster_size_[1]);
277 const auto error_code = raster_band->RasterIO(
290 AssertThrow(error_code == 0,
292 "GDAL driver error: error reading in geotiff file"));
304 if (transformation_use_geotiff_) {
306 dataset->GetGeoTransform(affine_transformation_.data()) == CE_None;
308 dealii::ExcMessage(
"GDAL driver error: no geo transform "
309 "present in geotiff file"));
311 affine_transformation_ = transformation_;
313 affine_transformation_[2] *= -1.;
314 affine_transformation_[5] *= -1.;
321 if (transformation_use_geotiff_ ==
false ||
322 transformation_use_geotiff_origin_ ==
false) {
323 const auto j_max = raster_size_[1] - 1;
324 affine_transformation_[0] =
325 transformation_[0] - j_max * affine_transformation_[2];
326 affine_transformation_[3] =
327 transformation_[3] - j_max * affine_transformation_[5];
340 inverse_affine_transformation_[0] = affine_transformation_[0];
341 inverse_affine_transformation_[3] = affine_transformation_[3];
343 const auto determinant =
344 affine_transformation_[1] * affine_transformation_[5] -
345 affine_transformation_[2] * affine_transformation_[4];
346 const auto inv = 1. / determinant;
347 inverse_affine_transformation_[1] = inv * affine_transformation_[5];
348 inverse_affine_transformation_[2] = inv * (-affine_transformation_[2]);
349 inverse_affine_transformation_[4] = inv * (-affine_transformation_[4]);
350 inverse_affine_transformation_[5] = inv * affine_transformation_[1];
352 GDALClose(dataset_handle);
355 std::cout << std::setprecision(16);
356 std::cout <<
"GDAL: driver name = " << driver_name_;
357 std::cout <<
"\nGDAL: projection = " << driver_projection_;
358 std::cout <<
"\nGDAL: transformation =";
359 for (
const auto &it : affine_transformation_)
360 std::cout <<
" " << it;
361 std::cout <<
"\nGDAL: inverse trafo =";
362 for (
const auto &it : inverse_affine_transformation_)
363 std::cout <<
" " << it;
364 std::cout <<
"\nGDAL: raster offset =";
365 for (
const auto &it : raster_offset_)
366 std::cout <<
" " << it;
367 std::cout <<
"\nGDAL: raster size =";
368 for (
const auto &it : raster_size_)
369 std::cout <<
" " << it;
370 std::cout << std::endl;
377 shift = *std::min_element(std::begin(raster_), std::end(raster_));
379 shift = *std::max_element(std::begin(raster_), std::end(raster_));
382 dealii::ExcInternalError());
383 const auto sum = std::reduce(std::begin(raster_), std::end(raster_));
384 shift = sum / raster_.size();
387 std::for_each(std::begin(raster_),
389 [&](
auto &element) { element -= shift; });
393 static constexpr auto message =
394 "ryujin has to be configured with GDAL support in order to read in "
396 AssertThrow(
false, dealii::ExcMessage(message));
402 DEAL_II_ALWAYS_INLINE
inline std::array<double, 2>
403 apply_transformation(
const double i,
const double j)
const
405 const auto &at = affine_transformation_;
406 const double x = at[0] + at[1] * i + at[2] * j;
407 const double y = at[3] + at[4] * i + at[5] * j;
412 DEAL_II_ALWAYS_INLINE
inline std::array<double, 2>
413 apply_inverse_transformation(
const double x,
const double y)
const
415 const auto &iat = inverse_affine_transformation_;
416 const double i = iat[1] * (x - iat[0]) + iat[2] * (y - iat[3]);
417 const double j = iat[4] * (x - iat[0]) + iat[5] * (y - iat[3]);
423 std::string filename_;
425 std::array<double, 6> transformation_;
426 bool transformation_allow_out_of_bounds_queries_;
427 bool transformation_use_geotiff_;
428 bool transformation_use_geotiff_origin_;
430 double height_scaling_;
438 Lazy<bool> geotiff_guard_;
439 mutable std::string driver_name_;
440 mutable std::string driver_projection_;
441 mutable std::array<double, 6> affine_transformation_;
442 mutable std::array<double, 6> inverse_affine_transformation_;
443 mutable std::array<int, 2> raster_offset_;
444 mutable std::array<int, 2> raster_size_;
445 mutable std::vector<float> raster_;