Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(geo): Add an optional bias when rounding bounds #1033

Merged
merged 1 commit into from
Aug 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/geo/src/__tests__/bounds.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ o.spec('Bounds', () => {
o(new Bounds(1.1, 10.1, 12.2, 11.2).round().toJson()).deepEquals({ x: 1, y: 10, width: 12, height: 11 });
o(new Bounds(1.4, 10.6, 12.2, 11.4).round().toJson()).deepEquals({ x: 1, y: 11, width: 13, height: 11 });
o(new Bounds(0.4, 0.6, 2.4, 1.6).round().toJson()).deepEquals({ x: 0, y: 1, width: 3, height: 1 });

o(new Bounds(1.6, 10.6, 12.5, 11.5).round().toJson()).deepEquals({ x: 2, y: 11, width: 12, height: 11 });
o(new Bounds(1.6, 10.6, 12.5, 11.5).round(0.2).toJson()).deepEquals({ x: 1, y: 10, width: 13, height: 12 });
});

o('toBbox', () => {
Expand Down
11 changes: 7 additions & 4 deletions packages/geo/src/bounds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,14 @@ export class Bounds implements BoundingBox {

/**
* Round dimensions to integers keeping the error a low as possible

* @param bias influence the rounding in favour of a larger area (+ve bias) or smaller area (-ve
* bias). Example: bias = 0.2 would round left and top down when < 0.7 and round right and bottom up when >= 0.3
*/
public round(): Bounds {
const x = Math.round(this.x);
const y = Math.round(this.y);
return new Bounds(x, y, Math.round(this.right) - x, Math.round(this.bottom) - y);
public round(bias = 0): Bounds {
const x = Math.round(this.x - bias);
const y = Math.round(this.y - bias);
return new Bounds(x, y, Math.round(this.right + bias) - x, Math.round(this.bottom + bias) - y);
}

public add(bounds: Point): Bounds {
Expand Down
5 changes: 4 additions & 1 deletion packages/tiler/src/tiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ export interface RasterPixelBounds {
tiff: Bounds;
}

/** The amount to bias the Bounds.round function to cover a larger, rather than smaller, area. */
const ROUND_BIAS = 0.2;

export class Tiler {
/** Tile size for the tiler and sub objects */
public readonly tms: TileMatrixSet;
Expand Down Expand Up @@ -78,7 +81,7 @@ export class Tiler {
): Composition | null {
const source = Bounds.fromJson(img.getTileBounds(x, y));

const target = source.scale(scaleFactor, scaleFactor).add(raster.tiff).round();
const target = source.scale(scaleFactor, scaleFactor).add(raster.tiff).round(ROUND_BIAS);

// Validate that the requested COG tile actually intersects with the output raster
const tileIntersection = target.intersection(raster.tile);
Expand Down