我遇到問題。我使用gdal_csharp.dll
庫(在gdal_retile.py
腳本中重寫爲C#
)在瓷磚上切割了一張地圖。除了一種情況,一切正常。當我用nodata
值(邊界上的圖塊)剪切區域並將它們保存爲JPEG
文件時,nodata
區域爲黑色(我需要它們爲白色) - 包含下面的示例圖塊。我試圖用tBand.SetNoDataValue(0);
但有時黑not-nodata
值(例如城市名,路名等)含有白點......白色無數值而不是黑色
我不知道如何設置SetNoDataValue(/* what to write in here? */)
......也許有另一種方法可以解決這個問題?
這裏是我的方法:
private void CreatePyramidTile(MosaicInfo levelMosaicInfo, int offsetX, int offsetY, int width, int height, string tileName, DataSource ogrds)
{
double sx = levelMosaicInfo.ScaleX * _scaleFactor;
double sy = levelMosaicInfo.ScaleY * _scaleFactor;
AffineTransformDecorator dec =
new AffineTransformDecorator(new[]
{
levelMosaicInfo.Ulx + offsetX*sx, sx, 0,
levelMosaicInfo.Uly + offsetY*sy, 0, sy
});
Dataset sFh = levelMosaicInfo.GetDataSet((int) Math.Round(dec.Ulx),
(int) Math.Round((dec.Uly + height*dec.ScaleY)),
(int) Math.Round((dec.Ulx + width*dec.ScaleX)),
(int) Math.Round(dec.Uly));
if (sFh == null)
{
return;
}
if (ogrds != null)
{
var points = dec.PointsFor(width, height);
AddFeature(ogrds, tileName, points);
}
DataType bt;
if (_bandType == DataType.GDT_Unknown)
{
bt = levelMosaicInfo.BandType;
}
else
{
bt = _bandType;
}
double[] geotransform = { dec.Ulx, dec.ScaleX, 0, dec.Uly, 0, dec.ScaleY };
int bands = levelMosaicInfo.Bands;
Dataset tFh;
if (_memDriver == null)
{
tFh = _driver.Create(tileName, width, height, bands, bt, _createOptions.ToArray());
}
else
{
tFh = _memDriver.Create(tileName, width, height, bands, bt, _createOptions.ToArray());
}
if (tFh == null)
{
throw new Exception("Creation failed, terminating gdal_tile.");
}
tFh.SetGeoTransform(geotransform);
tFh.SetProjection(levelMosaicInfo.Projection);
// I think, the problem occurs in this loop; I tried to set nodata values
for (int band = 1; band < bands + 1; band++)
{
Band tBand = tFh.GetRasterBand(band);
if (levelMosaicInfo.Ct != null)
{
tBand.SetRasterColorTable(levelMosaicInfo.Ct);
}
tBand.SetRasterColorInterpretation(levelMosaicInfo.Ci[band - 1]);
// tBand.SetNoDataValue(0);
}
var res = Gdal.ReprojectImage(sFh, tFh, null, null, _resamplingMethod, 0, 0, null, null);
if (res != 0)
{
throw new Exception("Reprojection failed for " + tileName + ", error " + res + ".");
}
levelMosaicInfo.CloseDataSet(ref sFh);
if (_memDriver != null)
{
Dataset ttFh = _driver.CreateCopy(tileName, tFh, 0, _createOptions.ToArray(), null, null);
ttFh.Dispose();
}
tFh.Dispose();
}
[編輯]
總還是有同樣的問題...我改變了循環:
for (int band = 1; band < bands + 1; band++)
{
Band tBand = tFh.GetRasterBand(band);
tBand.Fill(255, 0);
tBand.SetNoDataValue(255);
if (levelMosaicInfo.Ct != null)
{
tBand.SetRasterColorTable(levelMosaicInfo.Ct);
}
tBand.SetRasterColorInterpretation(levelMosaicInfo.Ci[band - 1]);
}
現在我認爲我的問題可能出現通過我的轉換方法。我得到的數據是8-bit
格式 - 重新投影后,黑色填充90%。因此,我首先使用pct2rgb.py
腳本(將其重寫爲C#
)將源數據轉換爲24-bit
。
下面的代碼:
public Dataset Convert8To24Bit()
{
Gdal.AllRegister();
string[] argv = Gdal.GeneralCmdLineProcessor(_args, 0);
int i = 1;
while (i < argv.Count())
{
string arg = argv.ElementAt(i);
switch (arg)
{
case "-of":
i++;
_format = argv[i];
break;
case "-b":
i++;
_bandNumber = int.Parse(argv[i]);
break;
case "-rgba":
_outBands = 4;
break;
default:
if (string.IsNullOrEmpty(_srcFileName))
{
_srcFileName = argv[i];
}
else
{
Usage();
}
break;
}
i++;
}
string tmpFileName = _srcFileName + ".bak";
// open source file
Dataset srcDS = Gdal.Open(_srcFileName, Access.GA_ReadOnly);
if (srcDS == null)
{
throw new Exception("Unable to open " + _srcFileName + ".");
}
Band srcBand = srcDS.GetRasterBand(_bandNumber);
// ensure we recognise the driver
Driver dstDriver = Gdal.GetDriverByName(_format);
if (dstDriver == null)
{
throw new Exception("\"" + _format + "\" not registered.");
}
// build color table
int[][] lookup = new int[4][];
lookup[0] = Enumerable.Range(0, 256).ToArray();
lookup[1] = Enumerable.Range(0, 256).ToArray();
lookup[2] = Enumerable.Range(0, 256).ToArray();
lookup[3] = new int[256];
for (i = 0; i < 256; i++)
{
lookup[3][i] = 255;
}
ColorTable ct = srcBand.GetRasterColorTable();
if (ct != null)
{
for (i = 0; i < Math.Min(256, ct.GetCount()); i++)
{
ColorEntry entry = ct.GetColorEntry(i);
for (int j = 0; j < 4; j++)
{
switch (j)
{
case 0:
lookup[j][i] = entry.c1;
break;
case 1:
lookup[j][i] = entry.c2;
break;
case 2:
lookup[j][i] = entry.c3;
break;
case 3:
lookup[j][i] = entry.c4;
break;
}
}
}
}
else
{
return srcDS;
}
// create the working file
string tifFileName = string.Empty;
if (_format.Equals("GTiff", StringComparison.OrdinalIgnoreCase))
{
tifFileName = tmpFileName;
}
else
{
tifFileName = Path.Combine(Directory.GetParent(tmpFileName).Name, "temp.gif");
}
// Driver gTiffDriver = Gdal.GetDriverByName("GTiff");
Driver gTiffDriver = Gdal.GetDriverByName("MEM");
Dataset tifDS = gTiffDriver.Create(tifFileName, srcDS.RasterXSize, srcDS.RasterYSize, _outBands, DataType.GDT_Byte,
new string[] {});
// we should copy projection information and so forth at this point
tifDS.SetProjection(srcDS.GetProjection());
double[] geotransform = new double[6];
srcDS.GetGeoTransform(geotransform);
tifDS.SetGeoTransform(geotransform);
if (srcDS.GetGCPCount() > 0)
{
tifDS.SetGCPs(srcDS.GetGCPs(), srcDS.GetGCPProjection());
}
// do the processing one scanline at a time
for (int iY = 0; iY < srcDS.RasterYSize; iY++)
{
byte[] srcData = new byte[srcDS.RasterXSize*1];
srcBand.ReadRaster(0, iY, srcDS.RasterXSize, 1, srcData, srcDS.RasterXSize, 1, 0, 0);
for (int iBand = 0; iBand < _outBands; iBand++)
{
int[] bandLookup = lookup[iBand];
int[] dstData = new int[srcData.Count()];
for (int index = 0; index < srcData.Count(); index++)
{
byte b = srcData[index];
dstData[index] = bandLookup[b];
}
tifDS.GetRasterBand(iBand + 1).WriteRaster(0, iY, srcDS.RasterXSize, 1, dstData,
srcDS.RasterXSize, 1, 0, 0);
}
}
// if (tifFileName != _srcFileName)
// {
// tifDS = Gdal.Open(tifFileName, Access.GA_ReadOnly);
// dstDriver.CreateCopy(_srcFileName, tifDS, 0, new string[] { }, null, null);;
// }
srcDS.Dispose();
return tifDS;
}
全類:GdalRetile.cs和TiffConverter.cs
它沒有爲我工作,但檢查我的編輯,請。也許這是問題...... :( – Nickon