首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >GeoTIFF libtiff.net在c#中获取高程数据

GeoTIFF libtiff.net在c#中获取高程数据
EN

Stack Overflow用户
提问于 2017-05-09 08:14:10
回答 4查看 3.4K关注 0票数 2

我试图使用libtiff.net从GeoTIFF文件中读取高程数据。到目前为止,我在libtiff.net的网页上使用示例读取了文件中的元数据。

但如何阅读海拔数据我不明白..。我试着用Tiff.ReadScanline()进行第一次阅读,就像这里描述的那样,但是我所存储的文件似乎是不同的(如果我正确理解的话,可能会用瓷砖存储)。

以下是元数据(据我所能读到的)( tiff文件来自丹麦地形海拔数据集):

c:\Users***\DTM_1km_6170_500.tif,第0页设置了以下标记: System.Int32 : 2500 System.Int32 : 2500 BITSPERSAMPLE System.Int16 : 32 压缩BitMiracle.LibTiff.Classic.Compression : ADOBE_DEFLATE 光度法BitMiracle.LibTiff.Classic.Photometric : MINISBLACK System.UInt64[]:System.UInt64[] SAMPLESPERPIXEL System.Int16: System.UInt64[]:System.UInt64[] BitMiracle.LibTiff.Classic.PlanarConfig : CONTIG 预测器BitMiracle.LibTiff.Classic.Predictor :流点 System.Int32 : 256 System.Int32 : 256 System.UInt64[]:System.UInt64[] System.UInt64[]:System.UInt64[] BitMiracle.LibTiff.Classic.SampleFormat : IEEEFP 数据类型System.Int16 :3 GEOTIFF_MODELPIXELSCALETAG System.Int32 :3 GEOTIFF_MODELPIXELSCALETAG System.Byte[]:GEOTIFF_MODELTIEPOINTTAG System.Int32 :6 GEOTIFF_MODELTIEPOINTTAG System.Byte[]:A ^WA 34735 System.Int32 : 36 34735 System.Byte[]:±±°èd )# 34736 System.Int32 :3 34736 System.Byte[]:34737 System.Int32 : 30 34737 System.Byte[]:ETRS89 / UTM专区32N\ETRS89 89 42113 System.Int32 :6 42113 System.Byte[]:-9999

到目前为止,我编写的代码如下:

代码语言:javascript
运行
复制
namespace GeoTIFFReader
{
  public class GeoTIFF
  {
    private double[,] heightmap;
    private double dx;
    private double dy;
    private double startx;
    private double starty;


    public GeoTIFF(string fn)
    {
      using (Tiff tiff = Tiff.Open(fn, "r"))
      {
        if (tiff == null)
        {
          // Error - could not open
          return;
        }



        int width = tiff.GetField(TiffTag.IMAGEWIDTH)[0].ToInt();
        int height = tiff.GetField(TiffTag.IMAGELENGTH)[0].ToInt();
        heightmap = new double[width, height];
        FieldValue[] modelPixelScaleTag = tiff.GetField(TiffTag.GEOTIFF_MODELPIXELSCALETAG);
        FieldValue[] modelTiePointTag = tiff.GetField(TiffTag.GEOTIFF_MODELTIEPOINTTAG);

        byte[] modelPixelScale = modelPixelScaleTag[1].GetBytes();
        dx = BitConverter.ToDouble(modelPixelScale, 0);
        dy = BitConverter.ToDouble(modelPixelScale, 8) * -1;

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

        startx = originLon + dx / 2.0;
        starty = originLat + dy / 2.0;

        double curx = startx;
        double cury = starty;

        FieldValue[] bitsPerSampleTag = tiff.GetField(TiffTag.BITSPERSAMPLE);

        FieldValue[] tilewtag = tiff.GetField(TiffTag.TILEWIDTH);
        FieldValue[] tilehtag = tiff.GetField(TiffTag.TILELENGTH);
        int tilew = tilewtag[0].ToInt();
        int tileh = tilehtag[0].ToInt();

        var tile = new byte[tilew*tileh];

        //var scanline = new byte[tiff.ScanlineSize()]; Does not work... wrong format
        for (int il = 0; il < height; il++)
        {
          //tiff.ReadScanline(scanline, il); // Load il'th line of data 
          for (int ir = 0; ir < width; ir++)
          {

            // Here I would like to read each pixel data that contains elevation in gray-scale in f32 as far I as I understand from metadata

            //object value = scanline[ir];
            //heightmap[ir, il] = double.Parse(value.ToString());
          }
        }

        Console.WriteLine(heightmap.ToString());
      }

    }
  }
}

因此,如果有人知道如何提取这些数据,那将是非常感谢的。

EN

Stack Overflow用户

发布于 2021-06-15 20:43:11

它有一个库(在c#中)是LibTiff的补充,它使给定纬度/经度的高程查询在GeoTiffCOG上可用

代码语言:javascript
运行
复制
  GeoTiff geoTiff = new GeoTiff(file_tiff);
  double value = geoTiff.GetElevationAtLatLon(latitude, longitude);

它还通过添加URI作为参数来支持云优化的GeoTiff (COG)。

票数 1
EN
查看全部 4 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43864543

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档