The Gdal library provides a very high number of algorithms, and at the same time, it provides the progress bar's parameters. For C + + calls, there should be no problem, but for C # calls, in the progress bar this piece needs to write an agent to pass. Start by writing a simple test code.
First define a trust function prototype that needs to be consistent with the C # progress bar interface in the Gdal Library, a simple prototype such as the following.
<summary>//// Progress Information callback function///</summary>// <param name= "Dfcomplete" > complete ratio, number between 0~1 </param>// <param name= "Pszmessage" > Progress bar Information </param>// <param name= "Pprogressarg" > progress bar User Data </param> //<returns></returns> public delegate int Progressfunc (double Dfcomplete, char[] pszmessage, IntPtr pprogressarg);
next in your own test code to write a progress bar function, for convenience, first write a console, directly from the Gdal library provided by the console progress bar changes, the progress bar function code such as the following. Detailed to the Gdal library in the console progress bar implementation code, almost identical, in addition to the printf function changed to Console.Write, nothing different.
class Gdalalgcstest {//Progress information callback function static int nlasttick =-1; public int termprogress (double dfcomplete, char[] strmessage, IntPtr Data) {int nthistick = (int) (dfcomple TE * 40.0); Nthistick = math.min (Math.max (0, Nthistick)); Have we started a new progress run? if (Nthistick < Nlasttick && Nlasttick >=) Nlasttick =-1; if (Nthistick <= Nlasttick) return 1; while (Nthistick > Nlasttick) {nlasttick++; if (nlasttick% 4 = = 0) console.write ("{0}", (NLASTTICK/4) * 10); Else Console.Write ("."); } if (Nthistick = =) Console.Write ("-done.\n"); else Console.Write (""); return 1; }}
The next step is to call it in the test code. The following is my Gdal library algorithm is encapsulated, just the interface of the progress bar and the Gdal Library algorithm progress bar interface exactly the same, I encapsulated a band merging algorithm, is to combine several single-band files into a multiband file, It is mainly used to merge data from the sub-band stored by Landsat satellites to form a multiband data. The interface declarations for functions are as follows:
//< Summary>///image band merge//</summary>//<param name= "astrsrcfilelist" > Input file list, all the input paths in the middle of Cut with * number </param>//<param name= "strdstfile" > Output file path </param>//<param name= "Idatatype" > Raster Data data type (reference gdaldatatype) </param>//<param name= "bunion" > Range inconsistent image processing, true to seek and false for intersection </ param>//<param name= "Strformat" > output file format, details gdal support data type </param>//<param name= "Pfun" ; progress bar callback function </param>//<param name= "Puserdata" > progress bar pointer </param>//<returns> return value, indicating that during the calculation Now various error messages </returns> [DllImport ("Gdalalg", EntryPoint = "Imagelayerstack")] public static extern int Ima Gelayerstack (char[] astrsrcfilelist, char[] strdstfile, int idatatype, bool bunion, string Strformat, PROGRESSF UNC Pfun, IntPtr puserdata);
The last two parameters are the progress bar, the second is the progress bar callback function (C # in the Trust function), the countdown to the first number of parameters required for the progress bar for the parameter information. For details please refer to my previous progress bar related blog. The above function implementation is no longer described here, nor is it the focus of this article. Here's a look at how to call this function and pass the progress bar in.
Using system;using gdalalg;using system.collections;using system.io;using system.text;namespace GDALAlgCS{class GDALA Lgcstest {//Progress information callback function static int nlasttick =-1; public int TermProgress1 (double dfcomplete, char[] strmessage, IntPtr Data) {int nthistick = (int) (Dfcompl ETE * 40.0); Nthistick = math.min (Math.max (0, Nthistick)); Have we started a new progress run? if (Nthistick < Nlasttick && Nlasttick >=) Nlasttick =-1; if (Nthistick <= Nlasttick) return 1; while (Nthistick > Nlasttick) {nlasttick++; if (nlasttick% 4 = = 0) console.write ("{0}", (NLASTTICK/4) * 10); Else Console.Write ("."); } if (Nthistick = =) Console.Write ("-done.\n"); else Console.Write (""); return 1; } static void Main (string[] args) {//Declaration progress Information callback function Progressfunc PD = new Progressfunc (new Gdalalgcstest (). TERMPROGRESS1); INTPTR p = new IntPtr (0); int ire = 0; String strLandsat1 = @ "F:\Data\LandSat\LT51230322011159IKR00\LT51230322011159IKR00_B1. TIF "; String strLandsat2 = @ "F:\Data\LandSat\LT51230322011159IKR00\LT51230322011159IKR00_B2. TIF "; String strLandsat3 = @ "F:\Data\LandSat\LT51230322011159IKR00\LT51230322011159IKR00_B3. TIF "; String strLandsat4 = @ "F:\Data\LandSat\LT51230322011159IKR00\LT51230322011159IKR00_B4. TIF "; String strLandsat5 = @ "F:\Data\LandSat\LT51230322011159IKR00\LT51230322011159IKR00_B5. TIF "; String strLandsat6 = @ "F:\Data\LandSat\LT51230322011159IKR00\LT51230322011159IKR00_B6. TIF "; String strLandsat7 = @ "F:\Data\LandSat\LT51230322011159IKR00\LT51230322011159IKR00_B7. TIF "; String strinfiles = StrLandsat1 + "*" + STRLANDSAT2 + "*" + STRLANDSAT3 + "*" + STRLANDSAT4 +"*" + STRLANDSAT5 + "*" + STRLANDSAT6 + "*" + strLandsat7; String strchineseout = @ "F:\Data\LandSat\LT51230322011159IKR00.tif"; IRE = Gdalalginterface.imagelayerstack (Strinfiles.tochararray (), Strchineseout.tochararray (), 0, False, "GTiff", PD, p ); Console.Write (Ire. ToString ()); } }}The above program executes in the progress effect for example as seen in.
The above is the console, then how to write in the graphical interface. Next, write a progress bar on the graphical interface. Declaring proxy functions are all the same, just that you need to write the corresponding progress function based on the progress bar control of the respective interface. This is the similar TermProgress1 function above. First do a simple interface, for example with what you see.
First look at the progress bar implementation function of this interface. Define a progress bar class in the form class, with detailed code such as the following:
public class Progress {public int progressbarinfo (double dfcomplete, char[] strmessage, IntPtr pData) { gdalform form = (gdalform) control.fromhandle (pData); int ivalue = (int) (+ * dfcomplete + 0.5); Form.progressBar.Value = Ivalue; String strMsg = new string (strmessage); Form.labelMessage.Text = STRMSG; return 1; } }
first of all, the function Progressbarinfo of the parameters, Dfcomplete is progress information, 0~1 between the decimal, strmessage is the progress of information, mainly some explanatory text, the last pdata is the user's own definition of the structure of information, The pdata here is the handle of the whole form. This makes it possible to convert from the form's handle to an object in the interface class, get the progress bar control from it, and then set the progress information to the progress bar control and set the message to a label on the interface.
Here's another look at the function call.
private void Buttonok_click (object sender, EventArgs e) {try {int irev = 0; string strinput = Textboxinput.text; string stroutput = Textboxoutput.text; String Strfield = "OBJECTID"; Progressfunc PD = new Progressfunc (new Progress (). Progressbarinfo); INTPTR pre = this. Handle; Irev = Gdalalginterface.shprasterize (Strinput.tochararray (), Stroutput.tochararray (), 1, 0, Strfield.tochararray ( ), "Gtiff", PD, pre); MessageBox.Show ("Process return code:" + irev.tostring (), "hint"); } catch (System.Exception ex) {MessageBox.Show (ex. ToString (), "hint"); } }
after clicking the "Calculate" button, first get the input and output file path, then declare a entrust, user-defined structure information to the current form of the handle. The entrusted function and handle are then passed into the algorithm function. The program executes, for example, as seen below. This algorithm is a wrapper function of vector rasterization.
The above two and interface design is somewhat inconsistent, mainly the vector rasterization of a few parameters can be set by the interface. These parameters are set to die in the code above. The rest is the same, for the progress bar this piece no matter what changes.
C # calls Gdal algorithm progress information passing