Warp or Reproject Raster Layers in GIS using C#

Reproject layer to WGS84 spatial reference on GeoTIFF samples

Aspose.GIS API lets you convert a raster layer from one spatial reference to another and apply new sizes as shown in the code snippet below.

// 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, "raster_float32.tif")))
using (var warped = layer.Warp(new WarpOptions(){Height = 40, Width = 40, TargetSpatialReferenceSystem = SpatialReferenceSystem.Wgs84}))
{
// read and print raster
var cellSize = warped.CellSize;
var extent = warped.GetExtent();
var spatialRefSys = warped.SpatialReferenceSystem;
var code = spatialRefSys == null ? "'no srs'" : spatialRefSys.EpsgCode.ToString();
var bounds = warped.Bounds;
var bandCount = warped.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 < warped.BandCount; i++)
{
var dataType = warped.GetBand(i).DataType;
var hasNoData = !warped.NoDataValues.IsNull();
var statistics = warped.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: {warped.NoDataValues[i]}");
}
}

Resize cell size in the raster layer on GeoTIFF samples

Aspose.GIS API lets you resize cells size in raster a layer as shown in the code snippet below.

// 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, "raster_float32.tif")))
{
Extent sourceExtent = layer.GetExtent();
var newExtent = new Extent(
sourceExtent.XMin,
sourceExtent.YMin,
sourceExtent.XMin + sourceExtent.Width * 0.5,
sourceExtent.YMax + sourceExtent.Height * 0.5,
layer.SpatialReferenceSystem);
using (var warped = layer.Warp(new WarpOptions() { CellWidth = 120, CellHeight = 120, TargetExtent = newExtent }))
{
// read and print raster
var cellSize = warped.CellSize;
var extent = warped.GetExtent();
var spatialRefSys = warped.SpatialReferenceSystem;
var code = spatialRefSys == null ? "'no srs'" : spatialRefSys.EpsgCode.ToString();
var bounds = warped.Bounds;
Console.WriteLine($"cellSize: {cellSize}");
Console.WriteLine($"source extent: {sourceExtent}");
Console.WriteLine($"target extent: {extent}");
Console.WriteLine($"spatialRefSys: {code}");
Console.WriteLine($"bounds: {bounds}");
}
}