I am using C# bindings for gdal in order to get elevation data from GeoTIFF files. The API contains a Band.ReadRaster method. This method supports a lot of data types, but for reading unsigned integers, only contains a method compatible with byte data. However, GeoTIFF files also support data being in ushort or uint format.

How can I use the byte version of ReadRaster to read these formats?

My assumption is to create a byte array that is of length [img width * img height * 2] for ushort and [img width * img height * 4] for uint, call the ReadRaster method, and then convert every two bytes in the array to a ushort (or 4 bytes to a uint).

However, if this is the correct path, I am not sure if I need to worry about byte ordering (little endian vs big endian)?

What would be the method I need to use to convert the bytes to ushort or uint?

Will BitConverter.ToUInt16 and BitConverter.ToUInt32 work?

  • I had a look at the gdal_csharp.dll file using JustDecompile. The Band.ReadRaster methods simply invoke an external method passing in the data type of the buffer, so I just copy/pasted the method and changed it up for the ushort and uint types. I wonder if this is correct, as I don't see why these methods wouldn't be included by default (it makes me think there is some kind of issue with these types). The code is a little long, but the important bit is this: cPLErr = band.ReadRaster(arguments . . ., DataType.GDT_UInt16, more arguments); If it looks right I will post as an aswer – Kyle G Oct 18 at 21:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.