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

fix(tiler): Use nearest smoothing when down sizing #1050

Merged
merged 1 commit into from
Aug 12, 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
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