Skip to content

Commit

Permalink
Get a pixel perfect match WMS image
Browse files Browse the repository at this point in the history
To avoid having little blurry image
  • Loading branch information
sbrunner committed May 9, 2023
1 parent 06708a0 commit 2049902
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
12 changes: 4 additions & 8 deletions src/ol/source/ImageWMS.js
Expand Up @@ -306,14 +306,10 @@ class ImageWMS extends ImageSource {
viewWidth,
viewHeight,
]);
const requestWidth = ceil(
(this.ratio_ * getWidth(extent)) / imageResolution,
DECIMALS
);
const requestHeight = ceil(
(this.ratio_ * getHeight(extent)) / imageResolution,
DECIMALS
);
const marginWidth = ceil(((this.ratio_ - 1) * viewWidth) / 2, DECIMALS);
const requestWidth = viewWidth + 2 * marginWidth;
const marginHeight = ceil(((this.ratio_ - 1) * viewHeight) / 2, DECIMALS);
const requestHeight = viewHeight + 2 * marginHeight;
const requestExtent = getForViewAndSize(center, imageResolution, 0, [
requestWidth,
requestHeight,
Expand Down
15 changes: 13 additions & 2 deletions test/browser/spec/ol/source/ImageWMS.test.js
Expand Up @@ -55,8 +55,19 @@ describe('ol/source/ImageWMS', function () {
const bbox = queryData.get('BBOX').split(',').map(Number);
const bboxAspectRatio = (bbox[3] - bbox[1]) / (bbox[2] - bbox[0]);
const imageAspectRatio = imageWidth / imageHeight;
expect(imageWidth).to.be(Math.ceil((viewWidth / resolution) * ratio));
expect(imageHeight).to.be(Math.ceil((viewHeight / resolution) * ratio));
const marginWidth = Math.ceil(
((ratio - 1) * viewWidth) / resolution / 2
);
const marginHeight = Math.ceil(
((ratio - 1) * viewHeight) / resolution / 2
);

expect(imageWidth).to.be(
Math.round(viewWidth / resolution) + 2 * marginWidth
);
expect(imageHeight).to.be(
Math.round(viewHeight / resolution) + 2 * marginHeight
);
expect(bboxAspectRatio).to.roughlyEqual(imageAspectRatio, 1e-12);
});
});
Expand Down

0 comments on commit 2049902

Please sign in to comment.