如何使用 libtiff.net 获取 GeoTiff 世界定位元数据
How to get GeoTiff world-positioning metadata using libtiff.net
我正在尝试从 ALOS 数据集 (JAXA) 获取 GeoTiff 文件的边界框:
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);
}
modelPointTags
中有 2 个值:System.Int32
和 System.Byte[]
。如何继续读取世界定位元数据?
我能够使用 https://build-failed.blogspot.com/2014/12/processing-geotiff-files-in-net-without.html
中的说明读取边界框
如何使用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]);
}
}
}
}
}
我正在尝试从 ALOS 数据集 (JAXA) 获取 GeoTiff 文件的边界框:
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);
}
modelPointTags
中有 2 个值:System.Int32
和 System.Byte[]
。如何继续读取世界定位元数据?
我能够使用 https://build-failed.blogspot.com/2014/12/processing-geotiff-files-in-net-without.html
中的说明读取边界框如何使用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]);
}
}
}
}
}