From d3e34297c2196190de4061ddc4cdbd3a0467819b Mon Sep 17 00:00:00 2001 From: Jiri Drbalek Date: Sun, 10 Jun 2018 17:36:50 +0000 Subject: [PATCH] gdal: Refactoring --- plugins/input/gdal/gdal_featureset.cpp | 99 ++++++++++++++------------ plugins/input/gdal/gdal_featureset.hpp | 6 ++ 2 files changed, 60 insertions(+), 45 deletions(-) diff --git a/plugins/input/gdal/gdal_featureset.cpp b/plugins/input/gdal/gdal_featureset.cpp index 440041481..5503a5a69 100644 --- a/plugins/input/gdal/gdal_featureset.cpp +++ b/plugins/input/gdal/gdal_featureset.cpp @@ -124,6 +124,33 @@ feature_ptr gdal_featureset::next() return feature_ptr(); } +void gdal_featureset::find_best_overview(int bandNumber, + int ideal_width, + int ideal_height, + int & current_width, + int & current_height) const +{ + GDALRasterBand * band = dataset_.GetRasterBand(bandNumber); + int band_overviews = band->GetOverviewCount(); + if (band_overviews > 0) + { + for (int b = 0; b < band_overviews; b++) + { + GDALRasterBand * overview = band->GetOverview(b); + int overview_width = overview->GetXSize(); + int overview_height = overview->GetYSize(); + if ((overview_width < current_width || + overview_height < current_height) && + ideal_width <= overview_width && + ideal_height <= overview_height) + { + current_width = overview_width; + current_height = overview_height; + } + } + } +} + feature_ptr gdal_featureset::get_feature(mapnik::query const& q) { feature_ptr feature = feature_factory::create(ctx_,1); @@ -208,61 +235,43 @@ feature_ptr gdal_featureset::get_feature(mapnik::query const& q) int im_width = width; double im_offset_x = x_off; double im_offset_y = y_off; - int current_width = (int)raster_width_; - int current_height = (int)raster_height_; + int current_width = static_cast(raster_width_); + int current_height = static_cast(raster_height_); + + // loop through overviews -- snap up in resolution to closest overview + // if necessary we find an image size that most resembles + // the resolution of our output image. + const double width_res = std::get<0>(q.resolution()); + const double height_res = std::get<1>(q.resolution()); + const int ideal_raster_width = static_cast( + std::floor(raster_extent_.width() * + width_res * filter_factor) + .5); + const int ideal_raster_height = static_cast( + std::floor(raster_extent_.height() * + height_res * filter_factor) + .5); - // loop through overviews -- snap up in resolution to closest overview if necessary - // we find an image size that most resembles the resolution of our output image. - double width_res = std::get<0>(q.resolution()); - double height_res = std::get<1>(q.resolution()); - int res_adjusted_raster_width = static_cast(std::floor(((double)raster_extent_.width() * width_res * filter_factor) + .5)); - int res_adjusted_raster_height = static_cast(std::floor(((double)raster_extent_.height() * height_res * filter_factor) + .5)); if (band_ > 0 && band_ < nbands_) { - GDALRasterBand * band = dataset_.GetRasterBand(band_); - int band_overviews = band->GetOverviewCount(); - if (band_overviews > 0) - { - for (int b = 0; b < band_overviews; b++) - { - GDALRasterBand * overview = band->GetOverview(b); - int overview_width = overview->GetXSize(); - int overview_height = overview->GetYSize(); - if ((overview_width < current_width || overview_height < current_height) && - res_adjusted_raster_width <= overview_width && - res_adjusted_raster_height <= overview_height) - { - current_width = overview_width; - current_height = overview_height; - } - } - } + find_best_overview(band_, + ideal_raster_width, + ideal_raster_height, + current_width, + current_height); } else { for (int i = 0; i < nbands_; ++i) { - GDALRasterBand * band = dataset_.GetRasterBand(i + 1); - int band_overviews = band->GetOverviewCount(); - if (band_overviews > 0) - { - for (int b = 0; b < band_overviews; b++) - { - GDALRasterBand * overview = band->GetOverview(b); - int overview_width = overview->GetXSize(); - int overview_height = overview->GetYSize(); - if ((overview_width < current_width || overview_height < current_height) && - res_adjusted_raster_width <= overview_width && - res_adjusted_raster_height <= overview_height) - { - current_width = overview_width; - current_height = overview_height; - } - } - } + find_best_overview(i + 1, + ideal_raster_width, + ideal_raster_height, + current_width, + current_height); } } - if (current_width != (int)raster_width_ || current_height != (int)raster_height_) + + if (current_width != (int)raster_width_ || + current_height != (int)raster_height_) { if (current_width != (int)raster_width_) { diff --git a/plugins/input/gdal/gdal_featureset.hpp b/plugins/input/gdal/gdal_featureset.hpp index 198d8a129..eb954e805 100644 --- a/plugins/input/gdal/gdal_featureset.hpp +++ b/plugins/input/gdal/gdal_featureset.hpp @@ -72,6 +72,12 @@ public: mapnik::feature_ptr next(); private: + void find_best_overview(int bandNumber, + int ideal_width, + int ideal_height, + int & current_width, + int & current_height) const; + mapnik::feature_ptr get_feature(mapnik::query const& q); mapnik::feature_ptr get_feature_at_point(mapnik::coord2d const& p); GDALDataset & dataset_;