Opened 14 years ago

Last modified 13 years ago

#1071 closed defect (fixed)

ASCII Grids round minimum and maximum if first cell value is integer

Reported by: cmichaelis@… Owned by: Mateusz Łoskot
Priority: high Milestone:
Component: GDAL_Raster Version: unspecified
Severity: normal Keywords: VERIFIED
Cc: Markus Neteler

Description

Hi there,

With ASCII grids, if the first cell value is an integer or a nodata value, then
the minimum and maximum will be rounded.

I put together a very simple Windows C++ example to demonstrate this bug, which
may be downloaded here:

Full project: http://dl.happysquirrel.com/temporary/ASCMinMaxExample.zip
Binaries only: http://dl.happysquirrel.com/temporary/ASCMinMaxBug-BinariesOnly.zip

In the zip file are two ascii grids of interest - "good.asc" and "bad.asc". They
are identical except that the first cell value in "good.asc" has been replaced
with 4.6 to illustrate that the first cell value is the determining factor.

Example output:
C:\dev\MinMaxExample\Debug>MinMaxExample.exe bad.asc
Min: 4.000000
Max: 4.000000

C:\dev\MinMaxExample\Debug>MinMaxExample.exe good.asc
Min: 4.600000
Max: 4.899000

Thank you!
PS... I believe this is with GDAL 1.3.1 if memory serves.

Change History (4)

comment:1 Changed 14 years ago by Mateusz Łoskot

(In reply to comment #0)
> With ASCII grids, if the first cell value is an integer or a nodata value, then
> the minimum and maximum will be rounded.

Hmm, AFAIK it's not a bug.
According to the AAIGrid format description
(http://www.gdal.org/frmt_various.html#AAIGrid) it's correct behaviour:

"Grids are treated as signed 16bit integer unless the first scanline contains
decimal values in which case the image is treated as 32bit floating point."

NOTE: I'm testing your grid files and sample program on Linux, with current GDAL
1.3.2.

Here I include last line of gdalinfo output on your files.
Please, note the Type= values for good.asc and bad.asc:

mloskot@dog~$ gdalinfo good.asc
....
Band 1 Block=117x1 Type=Float32, ColorInterp=Undefined
  NoData Value=-99

mloskot@dog~$ gdalinfo bad.asc
....
Band 1 Block=117x1 Type=Int16, ColorInterp=Undefined
  NoData Value=-99


So, according to the AAIGrid format spec:

1) the good.asc is handled as 32bit floating point and values printed by your
test application are not rounded:

Min: 4.600000
Max: 4.899000

2) the bad.asc is handled as 16bit signed integer and your app outputs rounded
values:

Min: 4.000000
Max: 4.000000

Please, verify my explanation if I'm correct.

comment:2 Changed 14 years ago by warmerdam

Mateusz,

This has been a long standing problem with the ascii grid reader.  On 
the one hand, I don't want to have to scan the whole grid file on open just
to find if there are some floating point values "way down" in the file.  
This would be quite destructive to performance for some applications.   On
the other hand, the current logic is constantly causing problems for people.
I get this sort of bug report at least once per month.

Most of the time, the problem is that there are large chunks of "nodata"
at the beginning, and that hides the real floating point data.  So I think one
improvement would be to scan through the data till at least the first value
that isn't no-data is encountered.  

Another approach would be to give up on my hesitancy to scan the whole file,
but to try and do the pre-scan as efficiently as possible.  Load the data in
substantial chunks (100K at a time perhaps) and use strchr() to look for a
period.  Ensure we stop scanning when we encounter a period of course. 

On reflection, I think we had better use the second method (scan the whole
file efficiently) and be done with the issue. 

comment:3 Changed 14 years ago by Mateusz Łoskot

This bug has been fixed already.
Appropriate changes are available in CVS.

Chris,
Could you test if your MinMaxExample works with those changes?

comment:4 Changed 14 years ago by cmichaelis@…

Works perfectly now -- thanks guys!

--Chris
Note: See TracTickets for help on using tickets.