geotifflibtifflibtiff.net

How to get GeoTiff world-positioning metadata using libtiff.net


I am trying to get the bounding-box of a GeoTiff file from the ALOS dataset (JAXA):

Tiff terrainTiff = Tiff.Open(@"Assets/Project/Heightmaps/" + "N046E007" + "/ALPSMLC30_" + "N046E007" + "_DSM.tif", "r");
                FieldValue[] modelPointTags = terrainTiff.GetField(TiffTag.GEOTIFF_MODELTIEPOINTTAG);
                foreach (FieldValue modelPointTag in modelPointTags)
                {
                    System.Type valueType = modelPointTag.Value.GetType();
                    Debug.Log(modelPointTag.Value.ToString());
                    Debug.Log(valueType);
                }

There are 2 values in modelPointTags: System.Int32 and System.Byte[]. How to I proceed reading world-positioning metadata?


Solution

  • how about using libtiff

     using (Tiff tiff = Tiff.Open(fileName, "r"))
            {
                //Image size
                int nWidth = tiff.GetField(TiffTag.IMAGEWIDTH)[0].ToInt();
                int nHeight = tiff.GetField(TiffTag.IMAGELENGTH)[0].ToInt();
                float[,] heightMap = new float[nWidth, nHeight];
                FieldValue[] modelPixelScaleTag = tiff.GetField(TiffTag.GEOTIFF_MODELPIXELSCALETAG);
                FieldValue[] modelTiePointTag = tiff.GetField(TiffTag.GEOTIFF_MODELTIEPOINTTAG);
    
                byte[] modelPixelScale = modelPixelScaleTag[1].GetBytes();
                double dW = BitConverter.ToDouble(modelPixelScale, 0);
                double dH = BitConverter.ToDouble(modelPixelScale, 8) * -1;
    
                byte[] modelTransformation = modelTiePointTag[1].GetBytes();
                double originLon = BitConverter.ToDouble(modelTransformation, 24);
                double originLat = BitConverter.ToDouble(modelTransformation, 32);
    
                double startW = originLon + dW / 2.0;
                double startH = originLat + dH / 2.0;
    
                FieldValue[] tileByteCountsTag = tiff.GetField(TiffTag.TILEBYTECOUNTS);
                long[] tileByteCounts = tileByteCountsTag[0].TolongArray();
    
                FieldValue[] bitsPerSampleTag = tiff.GetField(TiffTag.BITSPERSAMPLE);
                int bytesPerSample = bitsPerSampleTag[0].ToInt() / 8;
    
                FieldValue[] tilewtag = tiff.GetField(TiffTag.TILEWIDTH);
                FieldValue[] tilehtag = tiff.GetField(TiffTag.TILELENGTH);
                int tilew = tilewtag[0].ToInt();
                int tileh = tilehtag[0].ToInt();
    
                int tileWidthCount = nWidth / tilew;
                int remainingWidth = nWidth - tileWidthCount * tilew;
                if (remainingWidth > 0)
                {
                    tileWidthCount++;
                }
    
                int tileHeightCount = nHeight / tileh;
                int remainingHeight = nHeight - tileHeightCount * tileh;
                if (remainingHeight > 0)
                {
                    tileHeightCount++;
                }
    
                int tileSize = tiff.TileSize();
                for (int iw = 0; iw < nWidth; iw += tilew)
                {
                    for (int ih = 0; ih < nHeight; ih += tileh)
                    {
                        byte[] buffer = new byte[tileSize];
                        tiff.ReadTile(buffer, 0, iw, ih, 0, 0);
                        for (int itw = 0; itw < tilew; itw++)
                        {
                            int iwhm = ih + itw;
                            if (iwhm > nWidth - 1)
                            {
                                break;
                            }
                            for (int ith = 0; ith < tileh; ith++)
                            {
                                int iyhm = iw + ith;
                                if (iyhm > nHeight - 1)
                                {
                                    break;
                                }
                                heightMap[iwhm, iyhm] =
                                  BitConverter.ToSingle(buffer, (itw * tileh + ith) * 4);
    
                                Console.WriteLine(heightMap[itw, ith]);
                            }
                        }
                    }
                }
            }