Skip to content

Commit 8bad35d

Browse files
Merge pull request #245 from DanielJDufour/241-xml-utils
replaced txml with xml-utils
2 parents e00372e + 98921d3 commit 8bad35d

File tree

7 files changed

+118
-79
lines changed

7 files changed

+118
-79
lines changed

package-lock.json

Lines changed: 39 additions & 59 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"pako": "^2.0.4",
2929
"parse-headers": "^2.0.2",
3030
"threads": "^1.7.0",
31-
"txml": "^5.0.0"
31+
"xml-utils": "^1.0.2"
3232
},
3333
"devDependencies": {
3434
"@babel/core": "^7.8.7",
@@ -76,8 +76,7 @@
7676
"fs": false,
7777
"http": false,
7878
"https": false,
79-
"url": false,
80-
"./src/txml": "./src/browser/txml"
79+
"url": false
8180
},
8281
"sideEffects": false,
8382
"contributors": [

src/browser/txml.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/geotiffimage.js

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/* eslint max-len: ["error", { "code": 120 }] */
22

33
import { getFloat16 } from '@petamoriken/float16';
4-
import { parse } from './txml';
4+
import getAttribute from "xml-utils/get-attribute";
5+
import findTagsByName from 'xml-utils/find-tags-by-name';
56

67
import { photometricInterpretations, ExtraSamplesValues } from './globals';
78
import { fromWhiteIsZero, fromBlackIsZero, fromPalette, fromCMYK, fromYCbCr, fromCIELab } from './rgb';
@@ -777,27 +778,16 @@ class GeoTIFFImage {
777778
return null;
778779
}
779780
const string = this.fileDirectory.GDAL_METADATA;
780-
const xmlDom = parse(string.substring(0, string.length - 1));
781781

782-
if (!xmlDom[0].tagName) {
783-
throw new Error('Failed to parse GDAL metadata XML.');
784-
}
785-
786-
const root = xmlDom[0];
787-
if (root.tagName !== 'GDALMetadata') {
788-
throw new Error('Unexpected GDAL metadata XML tag.');
789-
}
790-
791-
let items = root.children
792-
.filter((child) => child.tagName === 'Item');
782+
let items = findTagsByName(string, 'Item');
793783

794784
if (sample !== null) {
795-
items = items.filter((item) => Number(item.attributes.sample) === sample);
785+
items = items.filter((item) => Number(getAttribute(item, "sample")) === sample);
796786
}
797787

798788
for (let i = 0; i < items.length; ++i) {
799789
const item = items[i];
800-
metadata[item.attributes.name] = item.children[0];
790+
metadata[getAttribute(item, "name")] = item.inner;
801791
}
802792
return metadata;
803793
}

src/txml.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/data/setup_data.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,8 @@ done
7979
gdal_translate -of GTiff -co NBITS=16 -ot Float32 initial.tiff float_n_bit_16.tiff || true
8080
gdal_translate -of GTiff -co NBITS=16 -ot Float32 -co TILED=YES initial.tiff float_n_bit_tiled_16.tiff || true
8181
gdal_translate -of GTiff -co NBITS=16 -ot Float32 -co INTERLEAVE=BAND initial.tiff float_n_bit_interleave_16.tiff || true
82+
83+
# GDAL_METADATA support
84+
wget https://github.com/GeoTIFF/test-data/archive/8ac198032d8b02160049ca161e8108e3d38176f3.zip -O geotiff-test-data.zip
85+
unzip -j -o geotiff-test-data.zip "test-data-*/files/*" -d .
86+
rm geotiff-test-data.zip

test/geotiff.spec.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,73 @@ describe('Geo metadata tests', async () => {
529529
});
530530
});
531531

532+
describe("GDAL_METADATA tests", async () => {
533+
it('should parse stats for specific sample', async () => {
534+
const tiff = await GeoTIFF.fromSource(createSource('abetow-ERD2018-EBIRD_SCIENCE-20191109-a5cf4cb2_hr_2018_abundance_median.tiff'));
535+
const image = await tiff.getImage();
536+
const metadata = await image.getGDALMetadata(10);
537+
expect(metadata).to.deep.equal({
538+
STATISTICS_MAXIMUM: '7.2544522285461',
539+
STATISTICS_MEAN: 'nan',
540+
STATISTICS_MINIMUM: '0',
541+
STATISTICS_STDDEV: 'nan'
542+
});
543+
});
544+
545+
it('should parse stats for single-band GeoTIFF', async () => {
546+
const tiff = await GeoTIFF.fromSource(createSource('nt_20201024_f18_nrt_s.tif'));
547+
const image = await tiff.getImage();
548+
const metadata = await image.getGDALMetadata();
549+
expect(metadata).to.deep.equal({
550+
STATISTICS_MAXIMUM: '100',
551+
STATISTICS_MEAN: '28.560288669249',
552+
STATISTICS_MINIMUM: '0',
553+
STATISTICS_STDDEV: '39.349526064368'
554+
});
555+
});
556+
557+
it('should parse layer type', async () => {
558+
const tiff = await GeoTIFF.fromSource(createSource('eu_pasture.tiff'));
559+
const image = await tiff.getImage();
560+
const metadata = await image.getGDALMetadata();
561+
expect(metadata).to.deep.equal({
562+
LAYER_TYPE: 'athematic'
563+
});
564+
});
565+
566+
it('should parse color interpretation', async () => {
567+
const tiff = await GeoTIFF.fromSource(createSource('utm.tif'));
568+
const image = await tiff.getImage();
569+
const metadata = await image.getGDALMetadata();
570+
expect(metadata).to.deep.equal({
571+
COLORINTERP: 'Palette'
572+
});
573+
});
574+
575+
it('should parse stats for another single-band GeoTIFF', async () => {
576+
const tiff = await GeoTIFF.fromSource(createSource('vestfold.tif'));
577+
const image = await tiff.getImage();
578+
const metadata = await image.getGDALMetadata();
579+
expect(metadata).to.deep.equal({
580+
STATISTICS_MAXIMUM: '332.6073328654',
581+
STATISTICS_MEAN: '83.638959236148',
582+
STATISTICS_MINIMUM: '18.103807449341',
583+
STATISTICS_STDDEV: '69.590554367352'
584+
});
585+
});
586+
587+
it('should parse creation times', async () => {
588+
const tiff = await GeoTIFF.fromSource(createSource('wind_direction.tif'));
589+
const image = await tiff.getImage();
590+
const metadata = await image.getGDALMetadata();
591+
expect(metadata).to.deep.equal({
592+
creationTime: '1497289465',
593+
creationTimeString: '2017-06-12T17:44:25.466257Z',
594+
name: 'Wind_Dir_SFC'
595+
});
596+
});
597+
});
598+
532599
describe('COG tests', async () => {
533600
it('should parse the header ghost area when present', async () => {
534601
const tiff = await GeoTIFF.fromSource(createSource('cog.tiff'));

0 commit comments

Comments
 (0)