Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8644961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:25:40+00:00 2026-06-12T12:25:40+00:00

I have a problem. I cut a map on tiles using gdal_csharp.dll library (working

  • 0

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();
}

[EDIT]
There’s still the same problem… I changed the loop:

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]);
}

Now I think that my problem can appear by my conversion method. The data I get, is in 8-bit format – after reprojecting the tiles are 90% filled by black color. So first I convert the source data into 24-bit using pct2rgb.py script (rewritten to C#).

Here’s the code:

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;
}

Whole classes: GdalRetile.cs and TiffConverter.cs

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T12:25:41+00:00Added an answer on June 12, 2026 at 12:25 pm

    Ok, I found a solution for this! I have simply filled my TEMP image on white, that is a background for a result tile.

    Changes made in GetDataSet() method:

    Dataset resultDS = _tempDriver.Create("TEMP", resultSizeX, resultSizeY, _bands, _bandType, new string[] { });

    Line above changed to:

    Dataset resultDS = _tempDriver.Create("TEMP", resultSizeX, resultSizeY, _bands, _bandType, new string[] { });
    for (int i = 1; i < _bands + 1; i++)
    {
        Band band = resultDS.GetRasterBand(i);
        band.Fill(255, 0);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got strange problem with cut I wrote script, there I have row: ...
I have some buttons in an UIView. My problem is, that they get cut
I have problem with my query on C, I’m using the oci8 driver. This
I have problem while loading data into html select when users press or click
i have problem with cut some text from UITextView , i idon't understand how
I have problem with variable printing inside javascript. variable htmlString printing not working: document.write(htmlString)
I have cut my problem down to simple lines of Scala + JavaFX 2's
I'm working on a program to trigger cut and pastes Pastes I have no
I have a problem with the cut function. I have this situation: codice 1
I am doing some image scaling using GDI+ (C#), and have noticed a problem

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.