64 : ParameterAcceptor(subsection)
66 filename_ =
"ryujin.tif";
67 this->add_parameter(
"filename", filename_,
"GeoTIFF: image file to load");
69 transformation_ = {0., 0.01, 0., 0., 0., 0.01};
73 "Array \"t[]\" describing an affine transformation between image "
74 "space (indices i and j from bottom left) and real coordinates (x "
75 "and y): x = t[0] + t[1] * i + t[2] * j, and y = t[3] + t[4] * i + "
76 "t[5] * j. (This transformation sets the origin of the image space "
77 "into the bottom left corner with index i to the right and index j "
80 transformation_allow_out_of_bounds_queries_ =
false;
82 "transformation allow out of bounds queries",
83 transformation_allow_out_of_bounds_queries_,
84 "GeoTIFF: allow out-of-bounds queries. When set to true, the reader "
85 "returns constant extended values for coordinates that are outside "
86 "of the image range.");
88 transformation_use_geotiff_ =
true;
89 this->add_parameter(
"transformation use geotiff",
90 transformation_use_geotiff_,
91 "GeoTIFF: read in transformation from GeoTIFF for "
92 "constructing the affine transformation. If set to "
93 "false the manually specified transformation "
94 "parameters will be used instead.");
96 transformation_use_geotiff_origin_ =
false;
98 "transformation use geotiff origin",
99 transformation_use_geotiff_origin_,
100 "GeoTIFF: read in affine shift (i.e., position of "
101 "lower left corner) from GeoTIFF for constructing "
102 "the affine transformation. If set to false the origin specified "
103 "in the transformation parameter will be used instead.");
106 this->add_parameter(
"height normalization",
107 height_normalization_,
108 "GeoTIFF: choose base point for height normalization "
109 "that is set to 0.: none, minimum, average, maximum");
111 height_scaling_ = 1.0;
112 this->add_parameter(
"height scaling",
114 "GeoTIFF: choose base point for height normalization "
115 "that is set to 0.: none, minimum, average, maximum");
117 const auto set_up = [&] {
122 driver_projection_ =
"";
123 affine_transformation_ = {0, 0, 0, 0, 0, 0};
124 inverse_affine_transformation_ = {0, 0, 0, 0, 0, 0};
125 raster_offset_ = {0, 0};
126 raster_size_ = {0, 0};
132 this->parse_parameters_call_back.connect(set_up);
146 DEAL_II_ALWAYS_INLINE
inline double
155 if constexpr (dim >= 1)
159 if constexpr (dim >= 2)
162 const auto &[di, dj] = apply_inverse_transformation(x, y);
166 const bool in_bounds =
167 di > -1.0 && di < static_cast<double>(raster_size_[0]) + 1.0 &&
168 dj > -1.0 && dj < static_cast<double>(raster_size_[1]) + 1.0;
172 std::cout << std::setprecision(16);
173 std::cout <<
"Queried point out of bounds." << std::endl;
174 std::cout <<
"Point: " << point << std::endl;
175 std::cout <<
"Transformed coordinates: (" << di <<
"," << dj <<
")"
181 transformation_allow_out_of_bounds_queries_ || in_bounds,
182 dealii::ExcMessage(
"Raster error: The requested point is outside "
183 "the image boundary of the geotiff file"));
190 const auto i_left = std::min(
191 std::max(
static_cast<int>(std::floor(di)), 0), raster_size_[0]);
192 const auto i_right = std::min(
193 std::max(
static_cast<int>(std::ceil(di)), 0), raster_size_[0]);
194 const auto j_left = std::min(
195 std::max(
static_cast<int>(std::floor(dj)), 0), raster_size_[1]);
196 const auto j_right = std::min(
197 std::max(
static_cast<int>(std::ceil(dj)), 0), raster_size_[1]);
201 std::cout <<
"index bounding box: (" << i_left <<
"," << j_left
202 <<
") and (" << i_right <<
"," << j_right <<
")" << std::endl;
206 const double i_ratio = std::fmod(di, 1.);
207 const double j_ratio = std::fmod(dj, 1.);
209 const auto v_iljl = raster_[i_left + j_left * raster_size_[0]];
210 const auto v_irjl = raster_[i_right + j_left * raster_size_[0]];
212 const auto v_iljr = raster_[i_left + j_right * raster_size_[0]];
213 const auto v_irjr = raster_[i_right + j_right * raster_size_[0]];
215 const auto v_jl = v_iljl * (1. - i_ratio) + v_irjl * i_ratio;
216 const auto v_jr = v_iljr * (1. - i_ratio) + v_irjr * i_ratio;
218 return height_scaling_ * (v_jl * (1. - j_ratio) + v_jr * j_ratio);
232 void read_in_raster()
const
235 auto dataset_handle = GDALOpen(filename_.c_str(), GA_ReadOnly);
236 AssertThrow(dataset_handle,
237 dealii::ExcMessage(
"GDAL error: file not found"));
239 auto dataset = GDALDataset::FromHandle(dataset_handle);
240 Assert(dataset, dealii::ExcInternalError());
242 const auto driver = dataset->GetDriver();
244 driver_name_ = driver->GetMetadataItem(GDAL_DMD_LONGNAME);
245 if (dataset->GetProjectionRef() !=
nullptr)
246 driver_projection_ = dataset->GetProjectionRef();
251 dataset->GetRasterCount() == 1,
253 "GDAL driver error: currently we only support one raster"));
255 const auto raster_band = dataset->GetRasterBand(1);
257 AssertThrow(dataset->GetRasterXSize() == raster_band->GetXSize() &&
258 dataset->GetRasterYSize() == raster_band->GetYSize(),
260 "GDAL driver error: the raster band has a different "
261 "dimension than the (global) raster dimension of the "
262 "geotiff image. This is not supported."));
272 raster_offset_ = {0, 0};
273 raster_size_ = {dataset->GetRasterXSize(), dataset->GetRasterYSize()};
275 raster_.resize(raster_size_[0] * raster_size_[1]);
276 const auto error_code = raster_band->RasterIO(
289 AssertThrow(error_code == 0,
291 "GDAL driver error: error reading in geotiff file"));
303 if (transformation_use_geotiff_) {
305 dataset->GetGeoTransform(affine_transformation_.data()) == CE_None;
307 dealii::ExcMessage(
"GDAL driver error: no geo transform "
308 "present in geotiff file"));
310 affine_transformation_ = transformation_;
312 affine_transformation_[2] *= -1.;
313 affine_transformation_[5] *= -1.;
320 if (transformation_use_geotiff_ ==
false ||
321 transformation_use_geotiff_origin_ ==
false) {
322 const auto j_max = raster_size_[1] - 1;
323 affine_transformation_[0] =
324 transformation_[0] - j_max * affine_transformation_[2];
325 affine_transformation_[3] =
326 transformation_[3] - j_max * affine_transformation_[5];
339 inverse_affine_transformation_[0] = affine_transformation_[0];
340 inverse_affine_transformation_[3] = affine_transformation_[3];
342 const auto determinant =
343 affine_transformation_[1] * affine_transformation_[5] -
344 affine_transformation_[2] * affine_transformation_[4];
345 const auto inv = 1. / determinant;
346 inverse_affine_transformation_[1] = inv * affine_transformation_[5];
347 inverse_affine_transformation_[2] = inv * (-affine_transformation_[2]);
348 inverse_affine_transformation_[4] = inv * (-affine_transformation_[4]);
349 inverse_affine_transformation_[5] = inv * affine_transformation_[1];
351 GDALClose(dataset_handle);
354 std::cout << std::setprecision(16);
355 std::cout <<
"GDAL: driver name = " << driver_name_;
356 std::cout <<
"\nGDAL: projection = " << driver_projection_;
357 std::cout <<
"\nGDAL: transformation =";
358 for (
const auto &it : affine_transformation_)
359 std::cout <<
" " << it;
360 std::cout <<
"\nGDAL: inverse trafo =";
361 for (
const auto &it : inverse_affine_transformation_)
362 std::cout <<
" " << it;
363 std::cout <<
"\nGDAL: raster offset =";
364 for (
const auto &it : raster_offset_)
365 std::cout <<
" " << it;
366 std::cout <<
"\nGDAL: raster size =";
367 for (
const auto &it : raster_size_)
368 std::cout <<
" " << it;
369 std::cout << std::endl;
376 shift = *std::min_element(std::begin(raster_), std::end(raster_));
378 shift = *std::max_element(std::begin(raster_), std::end(raster_));
381 dealii::ExcInternalError());
382 const auto sum = std::reduce(std::begin(raster_), std::end(raster_));
383 shift = sum / raster_.size();
386 std::for_each(std::begin(raster_),
388 [&](
auto &element) { element -= shift; });
392 static constexpr auto message =
393 "ryujin has to be configured with GDAL support in order to read in "
395 AssertThrow(
false, dealii::ExcMessage(message));
401 DEAL_II_ALWAYS_INLINE
inline std::array<double, 2>
402 apply_transformation(
const double i,
const double j)
const
404 const auto &at = affine_transformation_;
405 const double x = at[0] + at[1] * i + at[2] * j;
406 const double y = at[3] + at[4] * i + at[5] * j;
411 DEAL_II_ALWAYS_INLINE
inline std::array<double, 2>
412 apply_inverse_transformation(
const double x,
const double y)
const
414 const auto &iat = inverse_affine_transformation_;
415 const double i = iat[1] * (x - iat[0]) + iat[2] * (y - iat[3]);
416 const double j = iat[4] * (x - iat[0]) + iat[5] * (y - iat[3]);
422 std::string filename_;
424 std::array<double, 6> transformation_;
425 bool transformation_allow_out_of_bounds_queries_;
426 bool transformation_use_geotiff_;
427 bool transformation_use_geotiff_origin_;
429 double height_scaling_;
437 Lazy<bool> geotiff_guard_;
438 mutable std::string driver_name_;
439 mutable std::string driver_projection_;
440 mutable std::array<double, 6> affine_transformation_;
441 mutable std::array<double, 6> inverse_affine_transformation_;
442 mutable std::array<int, 2> raster_offset_;
443 mutable std::array<int, 2> raster_size_;
444 mutable std::vector<float> raster_;