GdalUtils Class Reference

Implementation of the GDAL v2 librified functions. Not all functions are implemented yet. More...

Collaboration diagram for GdalUtils:
Collaboration graph

Public Member Functions

bool ClipVectorWithVector (string subjectFilename, string overlayFilename, string destinationFilename, bool useSharedConnection=true)
 Clips the vector with another vector. More...
 
string ErrorMsg (int errorCode)
 Retrieves the error message associated with the specified error code. More...
 
bool GdalBuildOverviews (string sourceFilename, tkGDALResamplingMethod resamplingMethod, int[] overviewList, int[] bandList, string[] configOptions)
 Build raster overview(s) Implementing the librified function of GDAL's gdaladdo.exe tool More...
 
bool GdalRasterTranslate (string sourceFilename, string destinationFilename, string[] options)
 Converts raster data between different formats. Implementing the librified function of GDAL's gdal_translate.exe tool More...
 
bool GdalRasterWarp (string sourceFilename, string destinationFilename, string[] options)
 Image reprojection and warping utility. Implementing the librified function of GDAL's gdalwarp.exe tool More...
 
bool GdalVectorReproject (string sourceFilename, string destinationFilename, int sourceEpsgCode, int destinationEpsgCode, bool useSharedConnection=false)
 Helper file to simply reproject a file from an EPSG code to another EPSG code. Uses GdalVectorTranslate() internally. If you need more options use GdalVectorTranslate() instead. More...
 
bool GdalVectorTranslate (string sourceFilename, string destinationFilename, string[] options, bool useSharedConnection=false)
 Converts simple features data between file formats. Implementing the librified function of GDAL's ogr2ogr.exe tool More...
 

Properties

string DetailedErrorMsg [get]
 Gets the detailed error message. More...
 
ICallback GlobalCallback [get, set]
 The global callback is the interface used by MapWinGIS to pass progress and error events to interested applications. More...
 
string Key [get, set]
 The key may be used by the programmer to store any string data associated with the object. More...
 
int LastErrorCode [get]
 Retrieves the last error generated in the object. More...
 

Detailed Description

Implementation of the GDAL v2 librified functions. Not all functions are implemented yet.

New API 4.9.5:
Added in version 4.9.5

Member Function Documentation

◆ ClipVectorWithVector()

bool GdalUtils.ClipVectorWithVector ( string  subjectFilename,
string  overlayFilename,
string  destinationFilename,
bool  useSharedConnection = true 
)

Clips the vector with another vector.

Parameters
subjectFilenameThe subject filename.
overlayFilenameThe overlay filename.
destinationFilenameThe destination filename.
useSharedConnectionIf set to true improves performance but also might make it instable.

Uses GdalUtils.GdalVectorTranslate under the hood.

New API 4.9.5:
Added in version 4.9.5
// Clipping large shapefile with border file
var outputFilename = Path.Combine(tempFolder, "GdalVectorTranslate.shp");
var gdalUtils = new GdalUtils();
if (!gdalUtils.ClipVectorWithVector("LargeFile.shp", "Border.shp", outputFilename))
{
Debug.WriteLine("GdalVectorTranslate failed: " + gdalUtils.ErrorMsg[gdalUtils.LastErrorCode] + " Detailed error: " + gdalUtils.DetailedErrorMsg);
}
Implementation of the GDAL v2 librified functions. Not all functions are implemented yet.
Definition: GdalUtils.cs:26

◆ ErrorMsg()

string GdalUtils.ErrorMsg ( int  errorCode)

Retrieves the error message associated with the specified error code.

Parameters
errorCodeThe error code for which the error message is required.
Returns
The error message description for the specified error code.
New API 4.9.5:
Added in version 4.9.5
// Get the error message when the method returns false
var gdalUtils = new GdalUtils();
Debug.WriteLine(gdalUtils.ErrorMsg[gdalUtils.LastErrorCode]);

◆ GdalBuildOverviews()

bool GdalUtils.GdalBuildOverviews ( string  sourceFilename,
tkGDALResamplingMethod  resamplingMethod,
int[]  overviewList,
int[]  bandList,
string[]  configOptions 
)

Build raster overview(s) Implementing the librified function of GDAL's gdaladdo.exe tool

Parameters
sourceFilenameThe source filename
resamplingMethodControlling the downsampling method applied, defaults to grmNearest
overviewListA list of integral overview levels to build, as an integer array
bandListList of band numbers, band numbering starts from 1, as an integer array
configOptionsThe configuration options, as a string array (--config)

See GDAL's documentation here: https://gdal.org/programs/gdaladdo.html A good guide about compressions of GeoTiff: https://kokoalberti.com/articles/geotiff-compression-optimization-guide/

New API 5.1:
Added in version 5.1.0
// Example of creating overviews for all bands with autogenerated levels:
var configOptions = new[] { "COMPRESS_OVERVIEW DEFLATE", "INTERLEAVE_OVERVIEW PIXEL" };
var gdalUtils = new GdalUtils();
if (!gdalUtils.GdalBuildOverviews("test.tif, tkGDALResamplingMethod.grmCubic, null, null, configOptions);)
{
Debug.WriteLine("GdalBuildOverviews failed: " + gdalUtils.ErrorMsg[gdalUtils.LastErrorCode] + " Detailed error: " + gdalUtils.DetailedErrorMsg);
}
bool GdalBuildOverviews(string sourceFilename, tkGDALResamplingMethod resamplingMethod, int[] overviewList, int[] bandList, string[] configOptions)
Build raster overview(s) Implementing the librified function of GDAL's gdaladdo.exe tool
Definition: GdalUtils.cs:153
// Example of creating overviews for only the first 3 bands and predefined levels:
// // To produce the smallest possible JPEG-In-TIFF overviews, you should use:
var configOptions = new[] { "COMPRESS_OVERVIEW JPEG", "PHOTOMETRIC_OVERVIEW YCBCR", "INTERLEAVE_OVERVIEW PIXEL" };
var overviewList = new[] { 2, 4, 8, 16, 32, 64, 128 };
var bandList = new[] { 1, 2, 3 };
var gdalUtils = new GdalUtils();
if (!gdalUtils.GdalBuildOverviews("test.tif", tkGDALResamplingMethod.grmAverage, overviewList, bandList, configOptions);)
{
Debug.WriteLine("GdalBuildOverviews failed: " + gdalUtils.ErrorMsg[gdalUtils.LastErrorCode] + " Detailed error: " + gdalUtils.DetailedErrorMsg);
}
tkGDALResamplingMethod
The resampling mode which is used for building GDAL overviews, controlling the downsampling method ap...
Definition: Enumerations.cs:549

◆ GdalRasterTranslate()

bool GdalUtils.GdalRasterTranslate ( string  sourceFilename,
string  destinationFilename,
string[]  options 
)

Converts raster data between different formats. Implementing the librified function of GDAL's gdal_translate.exe tool

Parameters
sourceFilenameThe source filename.
destinationFilenameThe destination filename.
optionsThe options, as a string array

See GDAL's documentation here: https://gdal.org/programs/gdal_translate.html

New API 5.1:
Added in version 5.1.0
// Example of changing the resolution of a TIFF file:
var output = Path.GetTempPath() + "ChangedResolution.tif";
var options = new[]
{
"-ot", "Float32",
"-tr", "0.2", "0.2",
"-r", "average",
"-projwin", "-180", "90", "180", "-90"
};
var gdalUtils = new GdalUtils();
if (!gdalUtils.GdalRasterTranslate("test.tif", output, options))
{
Debug.WriteLine("GdalRasterTranslate failed: " + gdalUtils.ErrorMsg[gdalUtils.LastErrorCode] + " Detailed error: " + gdalUtils.DetailedErrorMsg);
}

◆ GdalRasterWarp()

bool GdalUtils.GdalRasterWarp ( string  sourceFilename,
string  destinationFilename,
string[]  options 
)

Image reprojection and warping utility. Implementing the librified function of GDAL's gdalwarp.exe tool

Parameters
sourceFilenameThe source filename.
destinationFilenameThe destination filename.
optionsThe options, as a string array

See GDAL's documentation here: https://gdal.org/programs/gdalwarp.html

New API 4.9.5:
Added in version 4.9.5
New API 5.1:
Renamed from GdalWarp in version 5.1.0
// Example of creating VRT file from TIFF file. More options are possible:
var output = Path.GetTempPath() + "GdalWarp.vrt";
var options = new[]
{
"-of", "vrt",
"-overwrite"
};
var gdalUtils = new GdalUtils();
if (!gdalUtils.GdalRasterWarp("test.tif", output, options))
{
Debug.WriteLine("GdalRasterWarp failed: " + gdalUtils.ErrorMsg[gdalUtils.LastErrorCode] + " Detailed error: " + gdalUtils.DetailedErrorMsg);
}
// Example of cutting a TIFF file with a border file:
var output = Path.GetTempPath() + "GdalWarpCutline.vrt";
const string border = @"test.shp";
var options = new[]
{
"-of", "vrt",
"-overwrite",
"-crop_to_cutline",
"-cutline", border
};
var gdalUtils = new GdalUtils();
if (!gdalUtils.GdalRasterWarp("test.tif", output, options))
{
Debug.WriteLine("GdalRasterWarp failed: " + gdalUtils.ErrorMsg[gdalUtils.LastErrorCode] + " Detailed error: " + gdalUtils.DetailedErrorMsg);
}

◆ GdalVectorReproject()

bool GdalUtils.GdalVectorReproject ( string  sourceFilename,
string  destinationFilename,
int  sourceEpsgCode,
int  destinationEpsgCode,
bool  useSharedConnection = false 
)

Helper file to simply reproject a file from an EPSG code to another EPSG code. Uses GdalVectorTranslate() internally. If you need more options use GdalVectorTranslate() instead.

Parameters
sourceFilenameThe source filename.
destinationFilenameThe destination filename.
sourceEpsgCodeThe source EPSG code.
destinationEpsgCodeThe destination EPSG code.
useSharedConnectionIf set to true improves performance but also might make it instable.
New API 5.4:
Added in version 5.4
// Reprojecting the source file to another EPSG code
var gdalUtils = new GdalUtils();
var outputFilename = Path.Combine(Path.GetTempPath(), "translated.shp");
if (!gdalUtils.GdalVectorReproject(sourceFilename, outputFilename, 4326, 28992))
{
Debug.WriteLine("GdalVectorReproject failed: " + gdalUtils.ErrorMsg[gdalUtils.LastErrorCode] + " Detailed error: " + gdalUtils.DetailedErrorMsg);
}

◆ GdalVectorTranslate()

bool GdalUtils.GdalVectorTranslate ( string  sourceFilename,
string  destinationFilename,
string[]  options,
bool  useSharedConnection = false 
)

Converts simple features data between file formats. Implementing the librified function of GDAL's ogr2ogr.exe tool

Parameters
sourceFilenameThe source filename.
destinationFilenameThe destination filename.
optionsThe options, as a string array
useSharedConnectionIf set to true improves performance but also might make it instable.

See GDAL's documentation here: https://gdal.org/programs/ogr2ogr.html

New API 4.9.5:
Added in version 4.9.5
// Converting shapefile to gml:
var outputFilename = Path.Combine(Path.GetTempPath(), "translated.gml");
var options = new[]
{
"-f", "GML"
};
var gdalUtils = new GdalUtils();
if (!gdalUtils.GdalVectorTranslate(inputFilename, outputFilename, options, true))
{
Debug.WriteLine("GdalVectorTranslate failed: " + gdalUtils.ErrorMsg[gdalUtils.LastErrorCode] + " Detailed error: " + gdalUtils.DetailedErrorMsg);
}

Property Documentation

◆ DetailedErrorMsg

string GdalUtils.DetailedErrorMsg
get

Gets the detailed error message.

New API 4.9.5:
Added in version 4.9.5
// Get the last detailed error message when the method returns false
var gdalUtils = new GdalUtils();
Debug.WriteLine(gdalUtils.DetailedErrorMsg);

◆ GlobalCallback

ICallback GdalUtils.GlobalCallback
getset

The global callback is the interface used by MapWinGIS to pass progress and error events to interested applications.

New API 4.9.5:
Added in version 4.9.5

◆ Key

string GdalUtils.Key
getset

The key may be used by the programmer to store any string data associated with the object.

New API 4.9.5:
Added in version 4.9.5

◆ LastErrorCode

int GdalUtils.LastErrorCode
get

Retrieves the last error generated in the object.

New API 4.9.5:
Added in version 4.9.5
// Get the last error code when the method returns false
var gdalUtils = new GdalUtils();
Debug.WriteLine(gdalUtils.ErrorMsg[gdalUtils.LastErrorCode]);