GdalConfiguration.cs
* 以下の関連記事を参照。.NET で GDAL を使うには... ~ Hello World編 ~
https://blogs.yahoo.co.jp/dk521123/38100268.html
Form1.cs
using OSGeo.GDAL; using System; using System.Runtime.InteropServices; using System.Windows.Forms; namespace SampleForm { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { GdalConfiguration.ConfigureGdal(); GdalWarp( "C:/Temp/IMG-HH-ALOS2014410740-140829-UBSL1.5GUA", "C:/Temp/Output2.tiff", new string[] { "-of", "GTiff", "-overwrite" }, new Gdal.GDALProgressFuncDelegate(OnProgress)); } /// <summary> /// 処理中の更新状態通知(プログレス)の際にコールバック関数 /// </summary> /// <param name="completeRate">処理率</param> /// <param name="message">処理メッセージ</param> /// <param name="data">データ</param> /// <returns></returns> /// <see cref="">https://svn.osgeo.org/gdal/trunk/gdal/swig/csharp/apps/GDALGetHistogram.cs"/> private static int OnProgress(double completeRate, IntPtr message, IntPtr data) { Console.Write("Processing ... " + completeRate * 100 + "% Completed."); if (message != IntPtr.Zero) { Console.Write(" Message:" + Marshal.PtrToStringAnsi(message)); } if (data != IntPtr.Zero) { Console.Write(" Data:" + Marshal.PtrToStringAnsi(data)); } Console.WriteLine(""); return 1; } /// <summary> /// gdalwarpコマンド相当の処理 /// </summary> /// <param name="inputFilePath">入力画像パス</param> /// <param name="outputFilePath">出力画像パス</param> /// <param name="gdalOptions">コマンド・オプション</param> /// <param name="callback">コールバック関数</param> /// <returns>true:処理成功、false:処理失敗</returns> public static bool GdalWarp( string inputFilePath, string outputFilePath, string[] gdalOptions, Gdal.GDALProgressFuncDelegate callback) { using (Dataset inputDataset = Gdal.Open(inputFilePath, Access.GA_ReadOnly)) { IntPtr[] pointer = { Dataset.getCPtr(inputDataset).Handle }; GCHandle gcHandle = GCHandle.Alloc(pointer, GCHandleType.Pinned); try { SWIGTYPE_p_p_GDALDatasetShadow datasetShadow = new SWIGTYPE_p_p_GDALDatasetShadow(gcHandle.AddrOfPinnedObject(), false, null); using (Dataset result = Gdal.wrapper_GDALWarpDestName( outputFilePath, 1, datasetShadow, new GDALWarpAppOptions(gdalOptions), callback, null)) { if (result == null) { Console.WriteLine("GdalWarp failed: " + Gdal.GetLastErrorMsg()); return false; } } } catch (Exception ex) { Console.WriteLine("Error : " + ex.Message); return false; } finally { if (gcHandle.IsAllocated) { gcHandle.Free(); } } } return true; } } }
出力結果
Processing ... 0% Completed. Message:Processing C:/Temp/IMG-HH-ALOS2014410740-140829-UBSL1.5GUA [1/1] Processing ... 0% Completed. Message:Processing C:/Temp/IMG-HH-ALOS2014410740-140829-UBSL1.5GUA [1/1] ・・・略・・・ Processing ... 99.9995820435372% Completed. Message:Processing C:/Temp/IMG-HH-ALOS2014410740-140829-UBSL1.5GUA [1/1] Processing ... 100% Completed. Message:Processing C:/Temp/IMG-HH-ALOS2014410740-140829-UBSL1.5GUA [1/1] Processing ... 100.001% Completed. Message:Processing C:/Temp/IMG-HH-ALOS2014410740-140829-UBSL1.5GUA [1/1]
参考文献
* 以下のサンプルを使って実装したhttps://gis.stackexchange.com/questions/297831/using-gdalwarp-with-c-bindings/300182
https://lists.osgeo.org/pipermail/gdal-dev/2017-February/046046.html
https://svn.osgeo.org/gdal/trunk/gdal/swig/csharp/apps/GDALGetHistogram.cs
関連記事
C#
.NET で GDAL を使うには... ~ 環境設定編 ~https://blogs.yahoo.co.jp/dk521123/38092205.html
.NET で GDAL を使うには... ~ Hello World編 ~
https://blogs.yahoo.co.jp/dk521123/38100268.html
.NET で GDAL・gdalwarpを実行するには... [1]
https://blogs.yahoo.co.jp/dk521123/38098235.html
GISツール
GISツール ~ GDAL / OGR ~https://blogs.yahoo.co.jp/dk521123/38092840.html
GISツール ~ GDALコマンド ~
https://blogs.yahoo.co.jp/dk521123/38093845.html