Skip to content
Prev 6383 / 29559 Next

Rgdal and unsigned integers

On Thu, 20 Aug 2009, Robert J. Hijmans wrote:

            
The next release of rgdal will contain some tools to permit the handling 
of signed/unsigned integer conversion (for 8 and 16 bit data so far), 
where GDAL drivers are unhelpful (submitted to CRAN, available for source 
check-out from the sourceforge rgdal repository.

In this case, the LAN/GIS driver assumes 16 bit signed integers when the 
data are 16 bit, and there are no "authorities" to say that signed or 
unsigned are correct. Here, the new helper function toUnSigned(x, 16) (for 
x as the band of interest) will do the conversion.

GDALinfo() now reports band-wise the GDAL Data Type (GDT), and the band 
minimum and maximum values known to the driver (either based on metadata, 
which varies with driver - maybe in an auxiliary file, or on the 
characteristics of the GDT, so just the range of the data representation, 
not the data in the band) It will not read the band to find the values.

The toSigned() helper function may be used for example when the data are 
read as 8 bit unsigned (the only option for GDT Byte), but ought to be 
signed.

Does anyone need conversion for 32 bit integers? This will overflow if 
not coerced to numeric 8-byte representation with storage.mode "double", 
as R integers are signed.

In summary, if the data look de-ranged, use GDALinfo() to see what GDT the 
driver assigned, and if appropriate, convert signed/unsigned to suit.

Hope this helps,

Roger

PS. Don't be thrown if the summary() of the data shows rounded maximum or 
minimum values - summary() takes a digits= argument with a tight default, 
because the output is usually read for its most significant (left) digits, 
not the rightmost digits. If in doubt, use range().
rows        839
columns     1758
bands       1
origin.x        33773.26
origin.y        861578
res.x       100
res.y       100
oblique.x   0
oblique.y   0
driver      LAN
projection  NA
file        stand_bm.gis
apparent band summary:
   GDType   Bmin  Bmax
1  Int16 -32768 32767
stand_bm.gis has GDAL driver LAN
and has 839 rows and 1758 columns
Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
  -32140       0       0    8316   19180   29620
Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
       0       0   14250   12830   22230   48500
Min.  1st Qu.   Median     Mean  3rd Qu.     Max.
     0.00     0.00 14247.00 12833.83 22226.00 48499.00
[1]     0 48499