Skip to content

Commit 98c1139

Browse files
Merge pull request #256 from DanielJDufour/updated-writing
updated writing
2 parents 473995c + 2b4cd95 commit 98c1139

File tree

3 files changed

+31
-15
lines changed

3 files changed

+31
-15
lines changed

src/geotiffwriter.js

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ const _writeIFD = (bin, data, _offset, ifd) => {
123123

124124
let val = ifd[key];
125125

126-
if (typeof val === 'undefined') {
126+
if (val === undefined) {
127127
throw new Error(`failed to get value for key ${key}`);
128128
}
129129

@@ -290,16 +290,7 @@ const toArray = (input) => {
290290
const metadataDefaults = [
291291
['Compression', 1], // no compression
292292
['PlanarConfiguration', 1],
293-
['XPosition', 0],
294-
['YPosition', 0],
295-
['ResolutionUnit', 1], // Code 1 for actual pixel count or 2 for pixels per inch.
296-
['ExtraSamples', 0], // should this be an array??
297-
['GeoAsciiParams', 'WGS 84\u0000'],
298-
['ModelTiepoint', [0, 0, 0, -180, 90, 0]], // raster fits whole globe
299-
['GTModelTypeGeoKey', 2],
300-
['GTRasterTypeGeoKey', 1],
301-
['GeographicTypeGeoKey', 4326],
302-
['GeogCitationGeoKey', 'WGS 84'],
293+
['ExtraSamples', 0],
303294
];
304295

305296
export function writeGeotiff(data, metadata) {
@@ -373,10 +364,32 @@ export function writeGeotiff(data, metadata) {
373364
metadata.SampleFormat = times(numBands, () => 1);
374365
}
375366

367+
// if didn't pass in projection information, assume the popular 4326 "geographic projection"
368+
if (!metadata.hasOwnProperty('GeographicTypeGeoKey') && !metadata.hasOwnProperty('ProjectedCSTypeGeoKey')) {
369+
metadata.GeographicTypeGeoKey = 4326;
370+
metadata.ModelTiepoint = [0, 0, 0, -180, 90, 0]; // raster fits whole globe
371+
metadata.GeogCitationGeoKey = 'WGS 84';
372+
metadata.GTModelTypeGeoKey = 2;
373+
}
374+
376375
const geoKeys = Object.keys(metadata)
377376
.filter((key) => endsWith(key, 'GeoKey'))
378377
.sort((a, b) => name2code[a] - name2code[b]);
379378

379+
if (!metadata.GeoAsciiParams) {
380+
let geoAsciiParams = '';
381+
geoKeys.forEach((name) => {
382+
const code = Number(name2code[name]);
383+
const tagType = fieldTagTypes[code];
384+
if (tagType === 'ASCII') {
385+
geoAsciiParams += `${metadata[name].toString()}\u0000`;
386+
}
387+
});
388+
if (geoAsciiParams.length > 0) {
389+
metadata.GeoAsciiParams = geoAsciiParams;
390+
}
391+
}
392+
380393
if (!metadata.GeoKeyDirectory) {
381394
const NumberOfKeys = geoKeys.length;
382395

@@ -421,7 +434,9 @@ export function writeGeotiff(data, metadata) {
421434
'GTRasterTypeGeoKey',
422435
'ImageLength', // synonym of ImageHeight
423436
'ImageWidth',
437+
'Orientation',
424438
'PhotometricInterpretation',
439+
'ProjectedCSTypeGeoKey',
425440
'PlanarConfiguration',
426441
'ResolutionUnit',
427442
'SamplesPerPixel',

src/globals.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ export const fieldTagTypes = {
145145
286: 'SHORT',
146146
287: 'RATIONAL',
147147
296: 'SHORT',
148+
297: 'SHORT',
148149
305: 'ASCII',
149150
306: 'ASCII',
150151
338: 'SHORT',
@@ -155,6 +156,8 @@ export const fieldTagTypes = {
155156
1025: 'SHORT',
156157
2048: 'SHORT',
157158
2049: 'ASCII',
159+
3072: 'SHORT',
160+
3073: 'ASCII',
158161
33550: 'DOUBLE',
159162
33922: 'DOUBLE',
160163
34665: 'LONG',

test/geotiff.spec.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,7 @@ describe('fillValue', async () => {
656656
expect(band).to.have.lengthOf(1);
657657
expect(band).to.deep.equal(new Uint16Array([42]));
658658
}
659-
});
659+
}).timeout(10000);
660660

661661
it('should fill areas in overview tiles outside the image extent (below)', async () => {
662662
const tiff = await GeoTIFF.fromSource(createSource('cog.tiff'));
@@ -667,7 +667,7 @@ describe('fillValue', async () => {
667667
expect(band).to.have.lengthOf(1);
668668
expect(band).to.deep.equal(new Uint16Array([42]));
669669
}
670-
});
670+
}).timeout(10000);
671671
});
672672

673673
describe('64 bit tests', () => {
@@ -972,7 +972,6 @@ describe('writeTests', () => {
972972
const geoKeys = image.getGeoKeys();
973973
expect(geoKeys).to.be.an('object');
974974
expect(geoKeys.GTModelTypeGeoKey).to.equal(2);
975-
expect(geoKeys.GTRasterTypeGeoKey).to.equal(1);
976975
expect(geoKeys.GeographicTypeGeoKey).to.equal(4326);
977976
expect(geoKeys.GeogCitationGeoKey).to.equal('WGS 84');
978977

@@ -1029,7 +1028,6 @@ describe('writeTests', () => {
10291028
const geoKeys = image.getGeoKeys();
10301029
expect(geoKeys).to.be.an('object');
10311030
expect(geoKeys.GTModelTypeGeoKey).to.equal(2);
1032-
expect(geoKeys.GTRasterTypeGeoKey).to.equal(1);
10331031
expect(geoKeys.GeographicTypeGeoKey).to.equal(4326);
10341032
expect(geoKeys.GeogCitationGeoKey).to.equal('WGS 84');
10351033

0 commit comments

Comments
 (0)