Skip to content

Commit f15d770

Browse files
Merge pull request #361 from DanielJDufour/get-skewed-bounding-box
improved getBoundingBox support for skewed/rotated images
2 parents 7dc35ad + 89c00ca commit f15d770

File tree

3 files changed

+50
-13
lines changed

3 files changed

+50
-13
lines changed

src/geotiffimage.js

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -889,21 +889,51 @@ class GeoTIFFImage {
889889
* @returns {Array<number>} The bounding box
890890
*/
891891
getBoundingBox() {
892-
const origin = this.getOrigin();
893-
const resolution = this.getResolution();
892+
const height = this.getHeight();
893+
const width = this.getWidth();
894+
895+
if (this.fileDirectory.ModelTransformation) {
896+
// eslint-disable-next-line no-unused-vars
897+
const [a, b, c, d, e, f, g, h] = this.fileDirectory.ModelTransformation;
898+
899+
const corners = [
900+
[0, 0],
901+
[0, height],
902+
[width, 0],
903+
[width, height],
904+
];
905+
906+
const projected = corners.map(([I, J]) => [
907+
d + (a * I) + (b * J),
908+
h + (e * I) + (f * J),
909+
]);
910+
911+
const xs = projected.map((pt) => pt[0]);
912+
const ys = projected.map((pt) => pt[1]);
913+
914+
return [
915+
Math.min(...xs),
916+
Math.min(...ys),
917+
Math.max(...xs),
918+
Math.max(...ys),
919+
];
920+
} else {
921+
const origin = this.getOrigin();
922+
const resolution = this.getResolution();
894923

895-
const x1 = origin[0];
896-
const y1 = origin[1];
924+
const x1 = origin[0];
925+
const y1 = origin[1];
897926

898-
const x2 = x1 + (resolution[0] * this.getWidth());
899-
const y2 = y1 + (resolution[1] * this.getHeight());
927+
const x2 = x1 + (resolution[0] * this.getWidth());
928+
const y2 = y1 + (resolution[1] * this.getHeight());
900929

901-
return [
902-
Math.min(x1, x2),
903-
Math.min(y1, y2),
904-
Math.max(x1, x2),
905-
Math.max(y1, y2),
906-
];
930+
return [
931+
Math.min(x1, x2),
932+
Math.min(y1, y2),
933+
Math.max(x1, x2),
934+
Math.max(y1, y2),
935+
];
936+
}
907937
}
908938
}
909939

test/data/setup_data.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ gdal_translate -of GTiff -co NBITS=16 -ot Float32 -co TILED=YES initial.tiff flo
8181
gdal_translate -of GTiff -co NBITS=16 -ot Float32 -co INTERLEAVE=BAND initial.tiff float_n_bit_interleave_16.tiff || true
8282

8383
# GDAL_METADATA support
84-
wget https://github.com/GeoTIFF/test-data/archive/8ac198032d8b02160049ca161e8108e3d38176f3.zip -O geotiff-test-data.zip
84+
wget https://github.com/GeoTIFF/test-data/archive/6ec42abc044a6884037c148d67a87a5d28228ce5.zip -O geotiff-test-data.zip
8585
unzip -j -o geotiff-test-data.zip "test-data-*/files/*" -d .
8686
rm geotiff-test-data.zip
8787

test/geotiff.spec.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,13 @@ describe('Geo metadata tests', async () => {
538538
expect(image.getGeoKeys()).to.have.property('GeographicTypeGeoKey');
539539
expect(image.getGeoKeys().GeographicTypeGeoKey).to.equal(4326);
540540
});
541+
542+
it('should be able to get the bounding box of skewed images', async () => {
543+
const tiff = await GeoTIFF.fromSource(createSource('umbra_mount_yasur.tiff'));
544+
const image = await tiff.getImage();
545+
expect(image.getBoundingBox()).to.be.an('array');
546+
expect(image.getBoundingBox()).to.be.deep.equal([336494.9320674397, 7839364.913043569, 337934.4836350695, 7840804.464611199]);
547+
});
541548
});
542549

543550
describe('GDAL_METADATA tests', async () => {

0 commit comments

Comments
 (0)