Skip to content

Commit

Permalink
Merge pull request #2063 from lossyrob/fix/small-geotiff-write
Browse files Browse the repository at this point in the history
LazySegmentBytes fix: always read bytes at the offset
  • Loading branch information
lossyrob authored Mar 14, 2017
2 parents f148592 + 4ddbbc2 commit e21f88e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,15 @@ class GeoTiffReaderSpec extends FunSpec

gt3.tags.headTags.get(Tags.TIFFTAG_DATETIME) should be (Some("1988:02:18 13:59:59"))
}

it("must read tiny tiles") {
val extent = Extent(0,0,100,100)
val expected = IntArrayTile(Array(145), 1, 1)
GeoTiff(expected, extent, LatLng).write(path)
addToPurge(path)
val actual = SinglebandGeoTiff(path).tile
assertEqual(actual, expected)
}
}

describe("handling special CRS cases") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class LazySegmentBytes(
val chunkEndOffset = segments.maxBy(_.endOffset).endOffset
byteReader.position(chunkStartOffset)
logger.debug(s"Fetching segments ${segments.map(_.id).mkString(", ")} at [$chunkStartOffset, $chunkEndOffset]")
val chunkBytes = byteReader.getSignedByteArray(chunkStartOffset, chunkEndOffset - chunkStartOffset + 1)
val chunkBytes = getBytes(chunkStartOffset, chunkEndOffset - chunkStartOffset + 1)
for { segment <- segments } yield {
val segmentStart = (segment.startOffset - chunkStartOffset).toInt
val segmentEnd = (segment.endOffset - chunkStartOffset).toInt
Expand All @@ -88,7 +88,7 @@ class LazySegmentBytes(
val startOffset = segmentOffsets(i)
val endOffset = segmentOffsets(i) + segmentByteCounts(i) - 1
logger.debug(s"Fetching segment $i at [$startOffset, $endOffset]")
byteReader.getSignedByteArray(startOffset, segmentByteCounts(i))
getBytes(startOffset, segmentByteCounts(i))
}

def getSegments(indices: Traversable[Int]): Iterator[(Int, Array[Byte])] = {
Expand All @@ -97,6 +97,11 @@ class LazySegmentBytes(
.flatMap( chunk => readChunk(chunk))
}

private def getBytes(offset: Long, length: Long): Array[Byte] = {
byteReader.position(offset)
byteReader.getBytes(length.toInt)
}

// Must prevent inherited `Seq.toString` from calling `foreach` method
override def toString(): String = s"StreamingSegmentBytes($byteReader, $tiffTags, $maxChunkSize)"
}
Expand All @@ -108,4 +113,4 @@ object LazySegmentBytes {
case class Segment(id: Int, startOffset: Long, endOffset: Long) {
def size: Long = endOffset - startOffset + 1
}
}
}

0 comments on commit e21f88e

Please sign in to comment.