From 4f570413ba238220af09e909f4f1c524fd841b2b Mon Sep 17 00:00:00 2001 From: Jiri Drbalek Date: Tue, 22 May 2018 13:18:26 +0000 Subject: [PATCH 1/5] gdal: Fix finding closest overview --- plugins/input/gdal/gdal_featureset.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/input/gdal/gdal_featureset.cpp b/plugins/input/gdal/gdal_featureset.cpp index 7ec336f55..ac8d30206 100644 --- a/plugins/input/gdal/gdal_featureset.cpp +++ b/plugins/input/gdal/gdal_featureset.cpp @@ -213,8 +213,8 @@ feature_ptr gdal_featureset::get_feature(mapnik::query const& q) // 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_width_ * width_res) + .5)); - int res_adjusted_raster_height = static_cast(std::floor(((double)raster_height_ * height_res) + .5)); + int res_adjusted_raster_width = static_cast(std::floor(((double)raster_extent_.width() * width_res) + .5)); + int res_adjusted_raster_height = static_cast(std::floor(((double)raster_extent_.height() * height_res) + .5)); if (band_ > 0 && band_ < nbands_) { GDALRasterBand * band = dataset_.GetRasterBand(band_); From 10aaf85f5414434ddc59c97125aa75e2b17e4ef2 Mon Sep 17 00:00:00 2001 From: Jiri Drbalek Date: Thu, 24 May 2018 11:50:45 +0000 Subject: [PATCH 2/5] gdal: Count in filter_factor --- plugins/input/gdal/gdal_featureset.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/input/gdal/gdal_featureset.cpp b/plugins/input/gdal/gdal_featureset.cpp index ac8d30206..c8e1cf006 100644 --- a/plugins/input/gdal/gdal_featureset.cpp +++ b/plugins/input/gdal/gdal_featureset.cpp @@ -213,8 +213,8 @@ feature_ptr gdal_featureset::get_feature(mapnik::query const& q) // 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) + .5)); - int res_adjusted_raster_height = static_cast(std::floor(((double)raster_extent_.height() * height_res) + .5)); + 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_); From 25f1b8cb18eb267f55795d517e7fc6c9782e53c0 Mon Sep 17 00:00:00 2001 From: Jiri Drbalek Date: Thu, 24 May 2018 13:23:40 +0000 Subject: [PATCH 3/5] gdal: Do not shrink query extent --- plugins/input/gdal/gdal_featureset.cpp | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/plugins/input/gdal/gdal_featureset.cpp b/plugins/input/gdal/gdal_featureset.cpp index c8e1cf006..39dc08904 100644 --- a/plugins/input/gdal/gdal_featureset.cpp +++ b/plugins/input/gdal/gdal_featureset.cpp @@ -265,18 +265,14 @@ feature_ptr gdal_featureset::get_feature(mapnik::query const& q) if (current_width != (int)raster_width_) { double ratio = (double)current_width / (double)raster_width_; - int adjusted_width = static_cast(std::floor((ratio * im_width) + 0.5)); - double adjusted_ratio = (double)adjusted_width / (double)im_width; - im_offset_x = adjusted_ratio * im_offset_x; - im_width = adjusted_width; + im_offset_x = std::floor(ratio * im_offset_x); + im_width = static_cast(std::ceil(ratio * im_width)); } if (current_height != (int)raster_height_) { double ratio = (double)current_height / (double)raster_height_; - int adjusted_height = static_cast(std::floor((ratio * im_height) + 0.5)); - double adjusted_ratio = (double)adjusted_height / (double)im_height; - im_offset_y = adjusted_ratio * im_offset_y; - im_height = adjusted_height; + im_offset_y = std::floor(ratio * im_offset_y); + im_height = static_cast(std::ceil(ratio * im_height)); } } From 4753aeb73aa75542f10be3de4bfadd888738eaa4 Mon Sep 17 00:00:00 2001 From: Jiri Drbalek Date: Sun, 10 Jun 2018 17:36:50 +0000 Subject: [PATCH 4/5] 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 39dc08904..ef14f4bd7 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); @@ -206,61 +233,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 325c6a999..8ca1bae56 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_; From 712cce82139b636b4f895b0b161e6b00c1263f19 Mon Sep 17 00:00:00 2001 From: Jiri Drbalek Date: Mon, 11 Jun 2018 12:09:45 +0000 Subject: [PATCH 5/5] Update test data --- test/data | 2 +- test/data-visual | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/data b/test/data index 0c278955e..049658fa1 160000 --- a/test/data +++ b/test/data @@ -1 +1 @@ -Subproject commit 0c278955e867f9b88abb6bcd5a2aa3ee76e870c1 +Subproject commit 049658fa104f368f78c3e8d9bddd50719492bcd5 diff --git a/test/data-visual b/test/data-visual index 23034ae27..574ad9bf8 160000 --- a/test/data-visual +++ b/test/data-visual @@ -1 +1 @@ -Subproject commit 23034ae27fb0b00d202688865268a80d05065fcc +Subproject commit 574ad9bf84450708a7f8befa63dddbc0a377cfa5