Skip to content

Commit beca218

Browse files
feat(Segmentation): address non matching levels (#174)
* Fall back to the fixed pixel spacing approach when no matches are found * Cleanup * Refactoring * Lint * Condense changes * Address gap issue * Revert unnecessary changes * Lint
1 parent 45a7b1b commit beca218

File tree

1 file changed

+40
-13
lines changed

1 file changed

+40
-13
lines changed

src/pyramid.js

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,7 @@ function _createTileLoadFunction ({
565565
}
566566

567567
function _fitImagePyramid (pyramid, refPyramid) {
568+
/** Get the matching levels between the two pyramids */
568569
const matchingLevelIndices = []
569570
for (let i = 0; i < refPyramid.metadata.length; i++) {
570571
for (let j = 0; j < pyramid.metadata.length; j++) {
@@ -582,14 +583,7 @@ function _fitImagePyramid (pyramid, refPyramid) {
582583
}
583584
}
584585

585-
if (matchingLevelIndices.length === 0) {
586-
console.error(pyramid, refPyramid)
587-
throw new Error(
588-
'Image pyramid cannot be fit to reference image pyramid.'
589-
)
590-
}
591-
592-
// Fit the pyramid levels to the reference image pyramid
586+
/** Create a new pyramid that fits the reference pyramid */
593587
const fittedPyramid = {
594588
extent: [...refPyramid.extent],
595589
origins: [],
@@ -600,18 +594,51 @@ function _fitImagePyramid (pyramid, refPyramid) {
600594
metadata: [],
601595
frameMappings: []
602596
}
603-
for (let i = 0; i < refPyramid.metadata.length; i++) {
604-
const index = matchingLevelIndices.find(element => element[0] === i)
605-
if (index) {
606-
const j = index[1]
597+
598+
if (matchingLevelIndices.length === 0) {
599+
console.warn('No matching pyramid levels found, handling fixed pixel spacing case...')
600+
601+
const refBaseLevel = refPyramid.metadata[refPyramid.metadata.length - 1]
602+
const refBaseTotalPixelMatrixColumns = refBaseLevel.TotalPixelMatrixColumns
603+
604+
for (let j = 0; j < pyramid.metadata.length; j++) {
605+
const imageLevel = pyramid.metadata[j]
606+
const totalPixelMatrixColumns = imageLevel.TotalPixelMatrixColumns
607+
608+
const resolution = refBaseTotalPixelMatrixColumns / totalPixelMatrixColumns
609+
const roundedResolution = Math.round(resolution)
610+
611+
/** Handle resolution conflicts similar to _computeImagePyramid */
612+
const finalResolution = fittedPyramid.resolutions.includes(roundedResolution)
613+
? parseFloat(resolution.toFixed(2))
614+
: roundedResolution
615+
607616
fittedPyramid.origins.push([...pyramid.origins[j]])
608617
fittedPyramid.gridSizes.push([...pyramid.gridSizes[j]])
609618
fittedPyramid.tileSizes.push([...pyramid.tileSizes[j]])
610-
fittedPyramid.resolutions.push(Number(refPyramid.resolutions[i]))
619+
fittedPyramid.resolutions.push(finalResolution)
611620
fittedPyramid.pixelSpacings.push([...pyramid.pixelSpacings[j]])
612621
fittedPyramid.metadata.push(pyramid.metadata[j])
613622
fittedPyramid.frameMappings.push(pyramid.frameMappings[j])
614623
}
624+
} else {
625+
/**
626+
* Fit the pyramid levels to the reference image pyramid.
627+
* Use the matching levels found in the matchingLevelIndices array.
628+
*/
629+
for (let i = 0; i < refPyramid.metadata.length; i++) {
630+
const index = matchingLevelIndices.find((element) => element[0] === i)
631+
if (index) {
632+
const j = index[1]
633+
fittedPyramid.origins.push([...pyramid.origins[j]])
634+
fittedPyramid.gridSizes.push([...pyramid.gridSizes[j]])
635+
fittedPyramid.tileSizes.push([...pyramid.tileSizes[j]])
636+
fittedPyramid.resolutions.push(refPyramid.resolutions[i])
637+
fittedPyramid.pixelSpacings.push([...pyramid.pixelSpacings[j]])
638+
fittedPyramid.metadata.push(pyramid.metadata[j])
639+
fittedPyramid.frameMappings.push(pyramid.frameMappings[j])
640+
}
641+
}
615642
}
616643

617644
let minZoom = 0

0 commit comments

Comments
 (0)