在C#中通过cmd.exe调用GDAL工具实现特定功能

我们在C#中使用GDAL时,某些功能并不能像C++那样应有尽有,甚至还不如Python那么好用,因此要实现某些特定功能,可能就要迫不得已地通过cmd.exe调用GDAL工具,下以调用gdalwarp.exe为例,为正在C#中使用GDAL开发同行提供有益的参考。

System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.UseShellExecute = false;//是否使用操作系统shell启动
process.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
process.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
process.StartInfo.RedirectStandardError = true;//重定向标准错误输出
process.StartInfo.CreateNoWindow = true;
process.Start();//启动程序
 //获取gdalward.exe路径,我这里是与dll放在一起,方便其调用相关dll
var executingAssemblyFile = new Uri(Assembly.GetExecutingAssembly().GetName().CodeBase).LocalPath;
var executingDirectory = Path.GetDirectoryName(executingAssemblyFile);
var gdalPath = Path.Combine(executingDirectory, "gdal");
var nativePath = Path.Combine(gdalPath, IntPtr.Size == 4 ? "x86" : "x64");
//image registration:first order geometric polynomial
//路径、参考系(字符串)和文件名等均加了双引号,具体可以百度why
 string strCMD = "\"" + nativePath + "\\gdalwarp.exe" + "\"" + " -t_srs \"" + strTargetWkt +
                                 "\" -order 1 -r cubicspline -tr " + Convert.ToString(GeoTransform[1]) 
                                 + " " + Convert.ToString(GeoTransform[5]) + " \"" + strTempFile 
                                 +  "\" \"" + targetFileName + "\"";
process.StandardInput.WriteLine(strCMD+"&exit");
process.StandardInput.AutoFlush = true;
//string outPut = process.StandardOutput.ReadToEnd();//获取cmd窗口的输出信息
process.WaitForExit();//等待程序执行完退出进程
process.Close();
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章