I have a problem. I cut a map on tiles using gdal_csharp.dll library (working on gdal_retile.py script rewritten into C#). Everything works fine except one case. When I cut areas with nodata values (tiles on the borders) and save them as JPEG files, the nodata areas are black (and I need them white) - example tile included below. I tried to use tBand.SetNoDataValue(0); but sometimes black not-nodata values (e.g. city names, road names, etc.) contain white dots...
Example tiles: Tile no1 and Tile no2
I don't know how to set SetNoDataValue(/* what to write in here? */)... Maybe there's the other way to solve this problem?
Here's my method:
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();
}