Skip to content

Commit

Permalink
fix(tiler): Use nearest smoothing when down sizing (#1050)
Browse files Browse the repository at this point in the history
Use lanczos3 when up sizing. This is a fix to reduce artefacts when rendering the topo-50 nztm
layer.

Allow supplying source order when creating CogJob; needed when ordering is important or a source
directory contains more than one imagery set as in the case of topo-50.

Acked-by: Geoff Jacobsen <gjacobson@linz.govt.nz>
  • Loading branch information
Geoff Jacobsen committed Aug 12, 2020
1 parent 132cec1 commit 3a95844
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 8 deletions.
22 changes: 20 additions & 2 deletions packages/cli/src/cli/cogify/action.job.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Epsg, EpsgCode } from '@basemaps/geo';
import { FileConfig, FileOperator, ProjectionTileMatrixSet } from '@basemaps/shared';
import { FileConfig, FileOperator, ProjectionTileMatrixSet, FileConfigPath } from '@basemaps/shared';
import {
CommandLineAction,
CommandLineFlagParameter,
Expand Down Expand Up @@ -52,6 +52,7 @@ export class ActionJobCreate extends CommandLineAction {
private sourceProjection: CommandLineIntegerParameter;
private targetProjection: CommandLineIntegerParameter;
private oneCog: CommandLineFlagParameter;
private fileList: CommandLineStringParameter;

public constructor() {
super({
Expand Down Expand Up @@ -80,7 +81,7 @@ export class ActionJobCreate extends CommandLineAction {
}

async onExecute(): Promise<void> {
const source = this.fsConfig(this.source);
const source: FileConfig | FileConfigPath = this.fsConfig(this.source);
const output = this.fsConfig(this.output);

let cutline = undefined;
Expand All @@ -104,6 +105,16 @@ export class ActionJobCreate extends CommandLineAction {
};
}

const fileListPath = this.fileList?.value;
if (fileListPath != null) {
const fileData = await FileOperator.create(fileListPath).read(fileListPath);
(source as FileConfigPath).files = fileData
.toString()
.trim()
.split('\n')
.map((fn) => source.path + '/' + fn);
}

const ctx: JobCreationContext = {
source,
output,
Expand Down Expand Up @@ -197,5 +208,12 @@ export class ActionJobCreate extends CommandLineAction {
description: 'ignore target projection window and just produce one big COG.',
required: false,
});

this.fileList = this.defineStringParameter({
argumentName: 'FILE_LIST',
parameterLongName: '--filelist',
description: 'supply a list of files to use as source imagery',
required: false,
});
}
}
2 changes: 1 addition & 1 deletion packages/tiler-sharp/src/__test__/tile.creation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ o.spec('TileCreation', () => {

const topLeft = layer0.find((f) => f.source.x == 0 && f.source.y == 0);
o(topLeft?.tiff.source.name).equals(tiff.source.name);
o(topLeft?.resize).deepEquals({ width: 32, height: 32 });
o(topLeft?.resize).deepEquals({ width: 32, height: 32, downsize: false });
o(topLeft?.x).equals(64);
o(topLeft?.y).equals(64);
});
Expand Down
9 changes: 7 additions & 2 deletions packages/tiler-sharp/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ function notEmpty<T>(value: T | null | undefined): value is T {
}
export type SharpOverlay = { input: string | Buffer } & Sharp.OverlayOptions;

const SharpScaleOptions = { fit: Sharp.fit.cover };
const SharpScaleOptionsDownsize = { fit: Sharp.fit.cover, kernel: Sharp.kernel.nearest };
const SharpScaleOptionsUpsize = { fit: Sharp.fit.cover, kernel: Sharp.kernel.lanczos3 };

export class TileMakerSharp implements TileMaker {
static readonly MaxImageSize = 256 * 2 ** 15;
Expand Down Expand Up @@ -89,7 +90,11 @@ export class TileMakerSharp implements TileMaker {
}

if (composition.resize) {
sharp.resize(composition.resize.width, composition.resize.height, SharpScaleOptions);
sharp.resize(
composition.resize.width,
composition.resize.height,
composition.resize.downsize ? SharpScaleOptionsDownsize : SharpScaleOptionsUpsize,
);
}

if (composition.crop) {
Expand Down
2 changes: 1 addition & 1 deletion packages/tiler/src/__test__/tiler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ o.spec('tiler.test', () => {
y: 0,
x: 64,
extract: { width: 512, height: 387 },
resize: { width: 256, height: 194 },
resize: { width: 256, height: 194, downsize: true },
crop,
});

Expand Down
2 changes: 1 addition & 1 deletion packages/tiler/src/raster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export interface Composition {
/** Crop the initial bounds */
extract?: Size;
/** Resize the image */
resize?: Size;
resize?: Size & { downsize: boolean };
/** Crop after the resize */
crop?: Bounds;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/tiler/src/tiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export class Tiler {
// Often COG tiles do not align to the same size as XYZ Tiles
// This will scale the COG tile to the same size as a XYZ
if (source.width != target.width || source.height != target.height) {
composition.resize = { width: target.width, height: target.height };
composition.resize = { width: target.width, height: target.height, downsize: source.width > target.width };
}

// If the output XYZ tile needs a piece of a COG tile, extract the speicific
Expand Down

0 comments on commit 3a95844

Please sign in to comment.