Skip to content

Commit

Permalink
Fix AutoHigherResolution ovr strategy and cover it with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pomadchin authored and echeipesh committed Sep 12, 2018
1 parent 9825fd2 commit 197040d
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,15 @@ trait GeoTiff[T <: CellGrid] extends GeoTiffData {
strategy match {
case AutoHigherResolution =>
(this :: list) // overviews can have erased extent information
.map { v => (v.cellSize.resolution - cellSize.resolution) -> v }
.map { v => (cellSize.resolution - v.cellSize.resolution) -> v }
.filter(_._1 >= 0)
.sortBy(_._1)
.map(_._2)
.headOption
.getOrElse(this)
case Auto(n) =>
list
.sortBy(v => math.abs(v.cellSize.resolution - cellSize.resolution))
(this :: list) // overviews can have erased extent information
.sortBy(v => math.abs(cellSize.resolution - v.cellSize.resolution))
.lift(n)
.getOrElse(this) // n can be out of bounds,
// makes only overview lookup as overview position is important
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,53 @@ class MultibandGeoTiffReaderSpec extends FunSpec
}
}

it("should pick up the tiff overview correct (AutoHigherResolution test)") {
def cellSizesSequence(cellSize: CellSize): List[CellSize] = {
val CellSize(w, h) = cellSize

val seq = for {
wp <- (w / 2 + w / 4) until (w - w / 100) by 100
hp <- (h / 2 + h / 4) until (h - h / 100) by 100
} yield CellSize(wp, hp)

seq.toList
}

val tiff = MultibandGeoTiff(geoTiffPath("overviews/multiband.tif"))

val overviews = tiff :: tiff.overviews

// cell sizes of overviews, starting with the base ifd
val cellSizes = ({
// an extra check for the base level overview
val CellSize(w, h) = overviews.head.cellSize
CellSize(w / 2, h / 2) -> -1
} :: overviews.map(_.cellSize).zipWithIndex) :+ {
// an extra check for the last overview
val CellSize(w, h) = overviews.last.cellSize
CellSize(w * 2, h * 2) -> overviews.length
}

// check all overviews
cellSizes.foreach { case (cz, i) =>
cellSizesSequence(cz).foreach { scz =>
if(i == -1) {
val closestOvr = tiff.getClosestOverview(scz, AutoHigherResolution)
val ovr = overviews(0)
closestOvr.raster.cellSize should be(ovr.raster.cellSize)
} else if(i == 0) {
val closestOvr = tiff.getClosestOverview(scz, AutoHigherResolution)
val ovr = overviews(i)
closestOvr.raster.cellSize should be(ovr.raster.cellSize)
} else {
val closestOvr = tiff.getClosestOverview(scz, AutoHigherResolution)
val ovr = overviews(i - 1)
closestOvr.raster.cellSize should be(ovr.raster.cellSize)
}
}
}
}

it("should read tiff with external overviews correct") {
// sizes of overviews, starting with the base ifd
val sizes = List(1056 -> 1052, 528 -> 526, 264 -> 263, 132 -> 132, 66 -> 66, 33 -> 33)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,53 @@ class SinglebandGeoTiffReaderSpec extends FunSpec
}
}

it("should pick up the tiff overview correct (AutoHigherResolution test)") {
def cellSizesSequence(cellSize: CellSize): List[CellSize] = {
val CellSize(w, h) = cellSize

val seq = for {
wp <- (w / 2 + w / 4) until (w - w / 100) by 100
hp <- (h / 2 + h / 4) until (h - h / 100) by 100
} yield CellSize(wp, hp)

seq.toList
}

val tiff = SinglebandGeoTiff(geoTiffPath("overviews/singleband.tif"))

val overviews = tiff :: tiff.overviews

// cell sizes of overviews, starting with the base ifd
val cellSizes = ({
// an extra check for the base level overview
val CellSize(w, h) = overviews.head.cellSize
CellSize(w / 2, h / 2) -> -1
} :: overviews.map(_.cellSize).zipWithIndex) :+ {
// an extra check for the last overview
val CellSize(w, h) = overviews.last.cellSize
CellSize(w * 2, h * 2) -> overviews.length
}

// check all overviews
cellSizes.foreach { case (cz, i) =>
cellSizesSequence(cz).foreach { scz =>
if(i == -1) {
val closestOvr = tiff.getClosestOverview(scz, AutoHigherResolution)
val ovr = overviews(0)
closestOvr.raster.cellSize should be(ovr.raster.cellSize)
} else if(i == 0) {
val closestOvr = tiff.getClosestOverview(scz, AutoHigherResolution)
val ovr = overviews(i)
closestOvr.raster.cellSize should be(ovr.raster.cellSize)
} else {
val closestOvr = tiff.getClosestOverview(scz, AutoHigherResolution)
val ovr = overviews(i - 1)
closestOvr.raster.cellSize should be(ovr.raster.cellSize)
}
}
}
}

it("should read tiff with external overviews correct") {
// sizes of overviews, starting with the base ifd
val sizes = List(1056 -> 1052, 528 -> 526, 264 -> 263, 132 -> 132, 66 -> 66, 33 -> 33)
Expand Down

0 comments on commit 197040d

Please sign in to comment.