jblindsay / whitebox-tools

An advanced geospatial data analysis platform
https://www.whiteboxgeo.com/
MIT License
953 stars 161 forks source link

Invalid result when processing GeoTIFF created with gdal and PREDICTOR=2 #137

Open zdila opened 3 years ago

zdila commented 3 years ago

Used whitebox-tools version: db8423fd1e9b884ab9c6cd11eff7309c1e892a0a

Command:

whitebox_tools -r=FeaturePreservingSmoothing --wd="." -v --dem=scaled_128.tif -o=scaled_128_smooth.tif --filter=10 --norm_diff=20.0 --num_iter=4

Input: https://drive.google.com/file/d/1LUw0YHwMV5RMFSOoy8trauhyVXBLK-mP/view?usp=sharing

Output: https://drive.google.com/file/d/1kXEpenEZB52myoyAYAT63AQ_sib75mTB/view?usp=sharing

zdila commented 3 years ago

Just found out that tool ConvertNodataToZero also produces invalid result for the same input. So other tools may be affected as well.

zdila commented 3 years ago

I localized the problem to be -co PREDICTOR=2:

Whitebox Tools produces bad result for GeoTIFF created like this:

gdalwarp -tr 64 64 -tap -r average -of GTiff -co TILED=YES -co BIGTIFF=YES -co COMPRESS=LZW -co PREDICTOR=2 -co NUM_THREADS=ALL_CPUS -wo NUM_THREADS=ALL_CPUS -multi dmr5.vrt scaled_64.tif

...but is OK with

gdalwarp  -tr 64 64 -tap -r average -of GTiff -co TILED=YES -co BIGTIFF=YES -co COMPRESS=LZW -co NUM_THREADS=ALL_CPUS -wo NUM_THREADS=ALL_CPUS -multi dmr5.vrt scaled_64.tif
jfbourdon commented 3 years ago

In #97 it was said by @jblindsay that the issue with PREDICTOR=2 should be fixed in release 1.3.1 ... but I can also confirm that neither 1.3.1 or 1.5.0 produces valid raster when the input uses PREDICTOR=2 (both DEFLATE or LWZ). However, using as input a raster with PREDICTOR=3 does show the error message The GeoTIFF reader does not currently support floating-point predictors (PREDICTOR=3).

jfbourdon commented 3 years ago

I looked into this a bit more and it seems that the values inside data are not stored properly even before entering the decoding predictor code part. The first value of each row (first column) is always the right one, but not the following as they are way too small (like 2.05e-38 instead of 5 for exemple...) and the last value of the last row is always -3.24e38. That is for a 32-bit float raster. I haven't yet tested other datatypes.

https://github.com/jblindsay/whitebox-tools/blob/b4359fdd7a2f1f7e299e2541ca8eee301fde697d/whitebox-raster/src/geotiff/mod.rs#L1607-L1617

The math behind the predictor seems good when comparing with this random reference.

It boggles me that the same raster, with DELFATE compression but without predictor is read correctly...