Skip to content

Commit

Permalink
feat(cogify): force a background color for transparent pixels
Browse files Browse the repository at this point in the history
  • Loading branch information
tawera-manaena committed Dec 12, 2024
1 parent c3c795b commit 229021d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
17 changes: 14 additions & 3 deletions packages/cogify/src/cogify/cli/cli.cog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { CutlineOptimizer } from '../../cutline.js';
import { SourceDownloader } from '../../download.js';
import { HashTransform } from '../../hash.stream.js';
import { getLogger, logArguments } from '../../log.js';
import { gdalBuildCog, gdalBuildVrt, gdalBuildVrtWarp } from '../gdal.command.js';
import { gdalBuildCog, gdalBuildVrt, gdalBuildVrtWarp, gdalCreate } from '../gdal.command.js';
import { GdalRunner } from '../gdal.runner.js';
import { Url, UrlArrayJsonFile } from '../parsers.js';
import { CogifyCreationOptions, CogifyStacItem, getCutline, getSources } from '../stac.js';
Expand Down Expand Up @@ -332,10 +332,21 @@ async function createCog(ctx: CogCreationContext): Promise<URL> {
);
await new GdalRunner(vrtWarpCommand).run(logger);

logger?.debug({ tileId }, 'Cog:Create:Tiff');
// Create a tiff file covering the whole world in sea blue
const gdalCreateCommand = gdalCreate(new URL(`${tileId}-bg.tiff`, ctx.tempFolder), options);
await new GdalRunner(gdalCreateCommand).run(logger);

// Create a vrt layering with the sea blue behind the warp VRT
const vrtMergeCommand = gdalBuildVrt(new URL(`${tileId}-merged.vrt`, ctx.tempFolder), [
gdalCreateCommand.output,
vrtWarpCommand.output,
]);
await new GdalRunner(vrtMergeCommand).run(logger);

// Create the COG from the warped vrt
const cogCreateCommand = gdalBuildCog(new URL(`${tileId}.tiff`, ctx.tempFolder), vrtWarpCommand.output, options);
const cogCreateCommand = gdalBuildCog(new URL(`${tileId}.tiff`, ctx.tempFolder), vrtMergeCommand.output, options);
await new GdalRunner(cogCreateCommand).run(logger);

return cogCreateCommand.output;
}

Expand Down
26 changes: 26 additions & 0 deletions packages/cogify/src/cogify/gdal.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,29 @@ export function gdalBuildCog(targetTiff: URL, sourceVrt: URL, opt: CogifyCreatio
.map(String),
};
}

export function gdalCreate(targetTiff: URL, opt: CogifyCreationOptions): GdalCommand {
const cfg = { ...Presets[opt.preset], ...opt };
const tileMatrix = TileMatrixSets.find(cfg.tileMatrix);
if (tileMatrix == null) throw new Error('Unable to find tileMatrix: ' + cfg.tileMatrix);

const bounds = tileMatrix.tileToSourceBounds({ x: 0, y: 0, z: 0 });

return {
command: 'gdal_create',
output: targetTiff,
args: [
['-of', 'GTiff'],
['-outsize', 20, 20],
['-bands', '4'],
['-burn', '208 231 244 255'], // this is the color
['-a_srs', tileMatrix.projection.toEpsgString()],
['-a_ullr', bounds.x, bounds.bottom, bounds.right, bounds.y],
['-co', 'COMPRESS=LZW'],
urlToString(targetTiff),
]
.filter((f) => f != null)
.flat()
.map(String),
};
}
2 changes: 1 addition & 1 deletion packages/cogify/src/cogify/gdal.runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export interface GdalCommand {
/** Output file location */
output: URL;
/** GDAL command to use */
command: 'gdalwarp' | 'gdalbuildvrt' | 'gdal_translate';
command: 'gdal_create' | 'gdalwarp' | 'gdalbuildvrt' | 'gdal_translate';
/** GDAL arguments to use */
args: string[];
}
Expand Down

0 comments on commit 229021d

Please sign in to comment.