Work with Raster Layers using GIS C# Library

Working with multi-band raster on GeoTIFF samples

GeoTIFF is one of the most widely used raster formats among GIS Software. Aspose.GIS lets you work with the GeoTIFF raster format.

Read general data in multi-band raster

// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET
string filesPath = RunExamples.GetDataDir();
using (var layer = Drivers.GeoTiff.OpenLayer(Path.Combine(filesPath, "raster50x50.tif")))
{
// read and print raster
var cellSize = layer.CellSize;
var extent = layer.GetExtent();
var spatialRefSys = layer.SpatialReferenceSystem;
var code = spatialRefSys == null ? "'no srs'" : spatialRefSys.EpsgCode.ToString();
var bounds = layer.Bounds;
var bandCount = layer.BandCount;
Console.WriteLine($"cellSize: {cellSize}");
Console.WriteLine($"extent: {extent}");
Console.WriteLine($"spatialRefSys: {code}");
Console.WriteLine($"bounds: {bounds}");
Console.WriteLine($"bandCount: {bandCount}");
// read and print bands
for (int i = 0; i < layer.BandCount; i++)
{
var dataType = layer.GetBand(i).DataType;
var hasNoData = !layer.NoDataValues.IsNull();
var statistics = layer.GetStatistics(i);
Console.WriteLine();
Console.WriteLine($"Band: {i}");
Console.WriteLine($"dataType: {dataType}");
Console.WriteLine($"statistics: {statistics}");
Console.WriteLine($"hasNoData: {hasNoData}");
if (hasNoData)
Console.WriteLine($"noData: {layer.NoDataValues[i]}");
}
}

Read values line by line

string filesPath = RunExamples.GetDataDir();
using (var layer = Drivers.GeoTiff.OpenLayer(Path.Combine(filesPath, "raster_float32.tif")))
{
var count = 0;
for (int i = 0; i < layer.Height; i++)
{
var lineRect = new RasterRect(0, i, layer.Width, 1);
var lineDump = layer.GetValuesDump(lineRect);
count += lineDump.Length;
}
Console.WriteLine($"total read values: {count}");
}

Read value of specified type

string filesPath = RunExamples.GetDataDir();
using (var layer = Drivers.GeoTiff.OpenLayer(Path.Combine(filesPath, "raster_float32.tif")))
{
// get band values in corner cell.
var bandValues = layer.GetValues(0, 0);
// we read data from the same strip with type float32, but all values are integers less than 255
// so the next auto-cast is correct.
Console.WriteLine($"byte: {bandValues.AsByte()}");
Console.WriteLine($"integer: {bandValues.AsInteger()}");
Console.WriteLine($"float: {bandValues.AsFloat()}");
// the next two lines are equivalent
Console.WriteLine($"double: {bandValues.AsDouble()}");
Console.WriteLine($"double with []: {bandValues[0]}");
}

Analyze rasters values use LINQ or Expression methods

string filesPath = RunExamples.GetDataDir();
using (var layer = Drivers.GeoTiff.OpenLayer(Path.Combine(filesPath, "raster_float32.tif")))
{
var count = 0;
for (int i = 0; i < layer.Height; i++)
{
var lineRect = new RasterRect(0, i, layer.Width, 1);
var lineDump = layer.GetValuesDump(lineRect);
count += lineDump.Length;
}
Console.WriteLine($"total read values: {count}");
}

Read raw bits in GoeTIFF

string filesPath = RunExamples.GetDataDir();
using (var layer = Drivers.GeoTiff.OpenLayer(Path.Combine(filesPath, "raster_int10.tif")))
{
var dump = layer.GetValuesDump(layer.Bounds);
foreach (var values in dump)
{
var bits = values.AsRawBits();
}
Console.WriteLine("raw bits is read");
}

Working with single-band raster format on Esri ASCII samples

The EsriAscii format always has only one band.

Read general data in single-band raster

string filesPath = RunExamples.GetDataDir();
using (var layer = Drivers.EsriAscii.OpenLayer(Path.Combine(filesPath, "raster_single.asc")))
{
// The EsriAscii format always has only one band.
var cellSize = layer.CellSize;
var extent = layer.GetExtent();
var spatialRefSys = layer.SpatialReferenceSystem;
var code = spatialRefSys == null ? "'no srs'" : spatialRefSys.EpsgCode.ToString();
var bounds = layer.Bounds;
var bandCount = layer.BandCount;
var nodata = layer.NoDataValues[0];
var dataType = layer.GetBand().DataType;
var statistics = layer.GetStatistics();
Console.WriteLine($"cellSize: {cellSize}");
Console.WriteLine($"extent: {extent}");
Console.WriteLine($"spatialRefSys: {code}");
Console.WriteLine($"bounds: {bounds}");
Console.WriteLine($"bandCount: {bandCount}");
Console.WriteLine($"nodata: {nodata}");
Console.WriteLine($"dataType: {dataType}");
Console.WriteLine($"statistics: {statistics}");
layer.GetValuesOnExpression(layer.Bounds, (context, values) =>
{
Console.WriteLine($"x: {context.CellX}; y: {context.CellY}; v: {values[0]}; e: {values.EqualsNoData()}");
});
}