views:

280

answers:

1

hi i was using gdal api to read raster files... i found in some places that python version has readasarray i assume this takes the data of the raster file as two dimensional array, is there any similar option for c#, or at least can you show me how to do it? thanks a lot!

+1  A: 

There is no equivalent of ReadAsArray function available in C# bindings to GDAL. The ReadAsArray is available because GDAL Python bindings are supposed to be usable with array protocol defined by NumPy so this function exists for this specific purpose.

However, you can use ReadRaster method of Band class to read pixels into 1-dimension array and then iterate over such 1-dimension array as it was 2-dimension array.

Let's assume you read pixels of a band with width x height dimensions:

byte[] bits = new byte[width * height];
band.ReadRaster(0, 0, width, height, bits, width, height, 0, 0);

Now, you can calculate index of a pixel according to this formula: column + row * width

for (int col = 0; col < width; col++)
{
    for (int row = 0; row < height; row++)
    {
        // equivalent to bits[col][row] if bits is 2-dimension array

        byte pixel = bits[col + row * width];
    }
}
mloskot