本文介绍了如何读取GeoTIFF文件从C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经获得了一些地区的数字高程地图(地球的高度图)。我的目标是创建现实地形。



地形生成没有问题。我实践了使用VC#& XNA框架。



问题是那些高度地图文件是GeoTIFF格式,我不知道如何阅读。我也没有以前的经验,阅读任何图像文件,以便我可以使用一些小秘诀,在互联网上提供的关于读取GeoTIFF文件的实验。




  • geoTIFF文件是3601 x 3601个文件。

  • 每个文件有两个版本,一个十进制&

  • 每个文件都有每秒钟经度和数据的数据。纬度
    Geo-Coords以及高度图,即Lon,Lat,海拔高度



如何阅读这些文件:)



我有来自ASTER G-DEM版本2的文件根据他们GeoTIFF是非常标准,这是因为一些GeoTIFF可视化我下载显示正确的数据。



我要使用C#。






EDIT



好吧,我得到了 libtiff ,这是我做的,

  using(Tiff tiff = Tiff.Open(@Test\N41E071_dem.tif,r))
{
int width = tiff.GetField(TiffTag.IMAGEWIDTH)[0] .ToInt();
int height = tiff.GetField(TiffTag.IMAGELENGTH)[0] .ToInt();
double dpiX = tiff.GetField(TiffTag.XRESOLUTION)[0] .ToDouble ;
double dpiY = tiff.GetField(TiffTag.YRESOLUTION)[0] .ToDouble();

byte [] scanline = new byte [tiff.ScanlineSize()];
ushort [] scanline16Bit = new ushort [tiff.ScanlineSize()/ 2];

for(int i = 0; i {
tiff .ReadScanline(scanline,i); //加载第i行
MultiplyScanLineAs16BitSamples(scanline,scanline16Bit,16,i);
}
}

private static void MultiplyScanLineAs16BitSamples (byte [] scanline,ushort [] temp,ushort factor,int row)
{
if(scanline.Length%2!= 0)
{
// each two字节定义一个样本,所以应该有偶数个字节
throw new ArgumentException();
}

Buffer.BlockCopy(scanline,0,temp,0,scanline.Length);

for(int i = 0; i {
temp [i] * = factor;
MessageBox.Show(Row:+ row.ToString()+Column:+(i / 2).ToString()+Value:+ temp [i] .ToString());
}
}

在显示消息框时,相应的值,我做的正确,我问这个铜像这是我的第一个图像的经验& 8 \16位问题。我认为不同于官方教程libtiff我应该使用而不是 ushort ,因为我使用的图像是GeoTIFF,有符号的16位

解决方案

有一些SDK可以从C#读取GeoTIFF文件:




  • (commercial)

  • (免费)

  • - 对我来说似乎GeoTIFF可以包含不同的子类型信息,而这些信息又需要进行相应的解释...


    I have acquired Digital Elevation Maps(Height Map of Earth) of some area. My aim was to create Realistic Terrains.

    Terrain Generation is no problem. I have practiced that using VC# & XNA framework.

    The problem is that those Height Map Files are in GeoTIFF format which i don't know how to read. Nor do i have previous experience with reading any image files so that i could experiment something using little tips-bits available on internet about reading GeoTIFF files. So far i have been unsuccessful.

    • The geoTIFF files I have are 3601 x 3601 files.
    • Each file has two version, a decimal & num valued files.
    • Each file has data of every second of longitude & latitude ofGeo-Coords along with Height Map i.e Lon, Lat, height from sea level

    How to read these file :)

    The files I have are from ASTER G-DEM Version-2 LINK TO OFFICIAL DESCRIPTION according to them GeoTIFF is pretty standard which is because some GeoTIFF Visualizers I dwonloaded are showing me the correct data.

    I am gonna be using C#. I would appreciate if we talk in relation to this language.


    E D I T

    okay i got the libtiff and this what i have done,

    using (Tiff tiff = Tiff.Open(@"Test\N41E071_dem.tif", r"))
    {
      int width   = tiff.GetField(TiffTag.IMAGEWIDTH)[0].ToInt();
      int height  = tiff.GetField(TiffTag.IMAGELENGTH)[0].ToInt();
      double dpiX = tiff.GetField(TiffTag.XRESOLUTION)[0].ToDouble();
      double dpiY = tiff.GetField(TiffTag.YRESOLUTION)[0].ToDouble(); 
    
      byte[] scanline        = new byte[tiff.ScanlineSize()]; 
      ushort[] scanline16Bit = new ushort[tiff.ScanlineSize() / 2];
    
      for (int i = 0; i < height; i++)
      {
        tiff.ReadScanline(scanline, i); //Loading ith Line                        
        MultiplyScanLineAs16BitSamples(scanline, scanline16Bit, 16,i);
      }
    }
    
    private static void MultiplyScanLineAs16BitSamples(byte[] scanline, ushort[] temp, ushort factor,int row)
    {
      if (scanline.Length % 2 != 0)
      {
        // each two bytes define one sample so there should be even number of bytes
        throw new ArgumentException();
      }
    
      Buffer.BlockCopy(scanline, 0,   temp, 0, scanline.Length);
    
      for (int i = 0; i < temp.Length; i++)
      {                
        temp[i] *= factor;
        MessageBox.Show("Row:"+row.ToString()+"Column:"+(i/2).ToString()+"Value:"+temp[i].ToString());
      }
    }
    

    where i am displaying the message box, i am displaying the corresponding values, Am i doing it Right, i am asking this cuz this is my maiden experience with images & 8\16 bit problem. I think unlike the official tutorials of libtiff i should be using short instead of ushort because the images i am using are "GeoTIFF, signed 16 bits"

    解决方案

    There are some SDKs out there usable from C# to read GeoTIFF files:

    UPDATE:

    The spec for GeoTIFF can befounf here - to me it seems that GeoTIFFs can contain different "subtypes" of information which in turn need to be interpreted appropriately...

    这篇关于如何读取GeoTIFF文件从C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 06:59