Skip to content

Commit

Permalink
fix: read nodata from tiff
Browse files Browse the repository at this point in the history
  • Loading branch information
Joseph Ramsay committed Mar 15, 2020
1 parent 6922bd8 commit 64d3e9c
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 5 deletions.
21 changes: 21 additions & 0 deletions packages/cog/src/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ export interface CogBuilderBounds {

/** EPSG projection number */
projection: number;

/** GDAL_NODATA value */
nodata: number;
}
export const InvalidProjectionCode = 32767;
export const CacheFolder = './.cache';
Expand Down Expand Up @@ -51,6 +54,7 @@ export class CogBuilder {
let resolution = -1;
let bandCount = -1;
let projection = -1;
let nodata = -1;
let count = 0;
const coordinates = sources.map(source => {
return this.q(async () => {
Expand Down Expand Up @@ -83,13 +87,22 @@ export class CogBuilder {
projection = imageProjection;
}

const noData = this.findNoData(tiff);
if (noData != null && noData != projection) {
if (nodata != -1) {
throw new Error('Multiple No Data values');
}
nodata = noData;
}

return output;
});
});

const polygons = await Promise.all(coordinates);
return {
projection,
nodata,
bands: bandCount,
bounds: GeoJson.toFeatureCollection(polygons),
resolution,
Expand Down Expand Up @@ -135,6 +148,14 @@ export class CogBuilder {
throw new Error('Failed to find projection');
}

/**
* Get the nodata value stored in the source tiff
* @param tiff
*/
findNoData(tiff: CogTiff): number {
return tiff.getImage(0).value(TiffTag.GDAL_NODATA) ?? 255;
}

/**
* Generate the bounding boxes for a GeoTiff converting to WGS84
* @param tiff
Expand Down
1 change: 0 additions & 1 deletion packages/cog/src/cli/actions/action.cog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export class ActionCogCreate extends CommandLineAction {
private quadKey?: CommandLineStringParameter;
private commit?: CommandLineFlagParameter;
private quadKeyIndex?: CommandLineIntegerParameter;
private resampleMethod?: CommandLineStringParameter;

public constructor() {
super({
Expand Down
11 changes: 9 additions & 2 deletions packages/cog/src/cli/actions/action.job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import { FileConfig, FileOperator, FileOperatorS3, LogConfig } from '@basemaps/l
import { CogSource } from '@cogeotiff/core';
import { CogSourceAwsS3 } from '@cogeotiff/source-aws';
import { CogSourceFile } from '@cogeotiff/source-file';
import { CommandLineAction, CommandLineFlagParameter, CommandLineIntegerParameter, CommandLineStringParameter } from '@microsoft/ts-command-line';
import {
CommandLineAction,
CommandLineFlagParameter,
CommandLineIntegerParameter,
CommandLineStringParameter,
} from '@microsoft/ts-command-line';
import { createReadStream, promises as fs } from 'fs';
import { basename } from 'path';
import * as ulid from 'ulid';
Expand Down Expand Up @@ -131,6 +136,7 @@ export class ActionJobCreate extends CommandLineAction {
const metadata = await builder.build(tiffSource, logger);

const logObj = { ...metadata };
const nodata = metadata.nodata;
delete logObj.bounds; // Don't log bounds as it is huge
logger.info(logObj, 'CoveringGenerated');

Expand All @@ -148,7 +154,7 @@ export class ActionJobCreate extends CommandLineAction {
);
}

const vrtOptions: VrtOptions = { addAlpha: true, forceEpsg3857: true, forceNoData255: true };
const vrtOptions: VrtOptions = { addAlpha: true, forceEpsg3857: true, forceNoData: true };
// -addalpha to vrt adds extra alpha layers even if one already exist
if (metadata.bands > 3) {
logger.warn({ bandCount: metadata.bands }, 'Vrt:DetectedAlpha, Disabling -addalpha');
Expand All @@ -167,6 +173,7 @@ export class ActionJobCreate extends CommandLineAction {
output: {
...outputConfig,
resample: getResample(this.resample?.value),
nodata: nodata,
vrt: {
options: vrtOptions,
},
Expand Down
1 change: 1 addition & 0 deletions packages/cog/src/cog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export interface CogJob {
/** Folder/S3 bucket to store the output */
output: {
resample: GdalCogBuilderOptionsResampling;
nodata: number;
vrt: {
options: VrtOptions;
};
Expand Down
4 changes: 2 additions & 2 deletions packages/cog/src/cog.vrt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ export async function buildWarpedVrt(
gdalCommand.parser.on('progress', onProgress({ target: `vrt.${EPSG.Google}` }, logger));

const warpOpts = ['-of', 'VRT', '-t_srs', Projection.toEpsgString(EPSG.Google), vrtPath, vrtWarpedPath];
if (options.forceNoData255) {
warpOpts.push('-srcnodata', '255', '-dstnodata', '255');
if (options.forceNoData) {
warpOpts.push('-srcnodata', String(job.output.nodata), '-dstnodata', String(job.output.nodata));
}
if (job.output.resample) {
warpOpts.push('-r', job.output.resample);
Expand Down

0 comments on commit 64d3e9c

Please sign in to comment.