2

I am using libtiff.net, and my geotiff file has exactly this format after being converted to ASCII Gridded XYZ ( via gdal_translate -of XYZ <input.tif> <output.xyz> function).

<LONGITUDE>     <LATITUDE>      <VALUE1> 

So far I am following the tutorial here, and although I can get the Longitude and Latitude by following the code sample in the blog post:

using (Tiff tiff = Tiff.Open(filePath, "r"))
{
    int height  = tiff.GetField(TiffTag.IMAGELENGTH)[0].ToInt();
    FieldValue[] modelPixelScaleTag = tiff.GetField((TiffTag)33550);
    FieldValue[] modelTiepointTag = tiff.GetField((TiffTag)33922);

    byte[] modelPixelScale = modelPixelScaleTag[1].GetBytes();
    double pixelSizeX = BitConverter.ToDouble(modelPixelScale, 0);
    double pixelSizeY = BitConverter.ToDouble(modelPixelScale, 8)*-1;

    byte[] modelTransformation = modelTiepointTag[1].GetBytes();
    double originLon = BitConverter.ToDouble(modelTransformation, 24);
    double originLat = BitConverter.ToDouble(modelTransformation, 32);

    double startLat = originLat + (pixelSizeY/2.0);
    double startLon = originLon + (pixelSizeX / 2.0);

    var scanline = new byte[tiff.ScanlineSize()]; 

    //TODO: Check if band is stored in 1 byte or 2 bytes. 
    //If 2, the following code would be required
    //var scanline16Bit = new ushort[tiff.ScanlineSize() / 2];
    //Buffer.BlockCopy(scanline, 0, scanline16Bit, 0, scanline.Length);

    double currentLat = startLat;
    double currentLon = startLon;

     int count=0;
    for (int i = 0; i < height; i++)
    {
        tiff.ReadScanline(scanline, i); //Loading ith Line            

        var latitude = currentLat + (pixelSizeY * i);
        for (var j = 0; j < scanline.Length; j++)
        {
            var longitude = currentLon + (pixelSizeX*j);

            object value = scanline[j];
           count++;

        }
    }
}

Maybe there is some manipulations I need to do with scanline[j] in order to get VALUE1 column, but I'm unsure how is this possible, any idea how can this be done?

Also, in my <output.xyz> file I have 1 million ++ data points, but it seems that my count is only a quarter of that. I am not sure also why this is so.

Anyone has any success in obtaining all of the column values from geotiff raster file, using libtiff.net?

Graviton
  • 81,782
  • 146
  • 424
  • 602

0 Answers0