locationtech / geotrellis

GeoTrellis is a geographic data processing engine for high performance applications.
http://geotrellis.io
Other
1.34k stars 360 forks source link

File size larger than expected when using GeoTiff().writer to output Tif file #3527

Closed WKX-w closed 10 months ago

WKX-w commented 10 months ago

Describe the bug

I use the GeoTiff().writer function to output a Tiff image with dimensions of 259×259 and pixel values of Float64. Under normal circumstances, the size of the image should be 524KB, but the actual output is 2049KB. What could be the reason for this, and does Geotrellis output Tif files with a fixed size? image image image

pomadchin commented 10 months ago

Ha, that's a good catch. (Original response was about Float32, adjusted it to be in Float64 terms)

The reason for that is the TIFF tiled layout specified before write (256 by default) and the TIFF size 259.

As the result this TIFF has 4 segments 256 x 256 each that results in a 2mb result TIFF.

I don't really remember specifics, but I guess just a very unfortunate combination of the layout scheme + tile size for our writer.

Try the code below to see the expected tiff size:

val (cols, rows) = (259, 259)
val tile = ArrayTile(Array.fill(cols * rows)(0f), cols, rows)
val extent = Extent(0, 0, cols, rows)

val raster = Raster(tile, extent)
val tiff = SinglebandGeoTiff(tile, extent, LatLng, Tags.empty, GeoTiffOptions.DEFAULT.copy(Tiled(259, 259)))

tiff.write("/tmp/zeros.tiff", true)

GDAL on the other hand ueses a Striped layout scheme by default:

gdal_create -ot Float64 -outsize 259 259 -bands 1 -burn 0 out.tiff
# with the gdalinfo output
Band 1 Block=259x7 Type=Float64, ColorInterp=Gray

^ To achieve the same affect instead of a tiling scheme we could store tiff segments striped:

val tiff = SinglebandGeoTiff(tile, extent, LatLng, Tags.empty, GeoTiffOptions.DEFAULT.copy(Striped()))

We default into the Tiled 256x256 TIFFs to target clod optimized TIFFs. That leads to some edge cases like this one.

WKX-w commented 10 months ago

I see. Thank you very much!