MapWindow Developer Team : MapWindow Discussion Forum
I'm building a plug-in which will read a MS-Access database gets coordinates from a table and creates polygons of those coordinates and adds them to a new shapefile. The most time consuming part is the looping through the datatable, creating shapes and add them to the shapefile. This part I'
ICallBack and Backgroundworker
Posted by: pmeems ()
Date: February 24, 2009 02:54PM

I'm building a plug-in which will read a MS-Access database gets coordinates from a table and creates polygons of those coordinates and adds them to a new shapefile.
The most time consuming part is the looping through the datatable, creating shapes and add them to the shapefile. This part I've put in a backgroundworker.
On my form I use a label en progressbar for progress messages using the backgroundworker events (worker.ReportProgress()).
This works great. But after adding all shapes I need to save the shapefile, because it contains over 19.000 shapes this takes a while (5-6 minutes).
The sf.StopEditingShapes() methods can use the ICallBack interface I try the implement it. I haven't used it before and I don't get it to work.
The backgroundworker might be responsible for this, but when I put my saveShapefile method outside the backgroundworker thread nothing happens either.
First I want to know if I can use the ICallBack interface in a class method which is called by the Backgroundworker.
Secondly I would like to know if my implementation is ok.
Here's some code.

The form:
public partial class frmSettings : Form, MapWinGIS.ICallback
    {
  private void btnCreateShapefile_Click(object sender, EventArgs e)
  {
      this.clsPlugin = new pluginCode(_mapWin, this);
      if (this.clsPlugin.createShapefile(this))
      {
          this.lblProgress.Text = "Base shapefile is created.";
          this.progressBar1.Value = 1;
          this.lblProgress.Text = "Start computing ...";
          this.backgroundWorker1.RunWorkerAsync();
      }
  }
  private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
  {
      // Get the BackgroundWorker that raised this event.
            BackgroundWorker worker = sender as BackgroundWorker;
      worker.ReportProgress(1);
      // Loop through all records and fill the shapefile:            
            this.clsPlugin.executeQueryThread(worker,e);
  }

  private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
  {
      this.lblProgress.Text = String.Format("Calculating... {0}% completed.", e.ProgressPercentage);
      this.progressBar1.Value = e.ProgressPercentage;
  }

  private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  {
      if (!e.Cancelled) 
      {
          this.lblProgress.Text = "Finished creating shapefile. Start saving ...";
          this.backgroundWorker1.ReportProgress(this.progressBar1.Maximum);
          // Save the shapefile outside the backgroundworker thread:
          this.clsPlugin.saveShapefile();
      }
  }

  public void Error(string KeyOfSender, string ErrorMsg)
  {
      this.lblProgress.Text = KeyOfSender + ":" + ErrorMsg; 
  }

  public void Progress(string KeyOfSender, int Percent, string Message)
  {
      this.lblProgress.Text = String.Format("Adding shapes... {0}% completed. {1}", Percent, Message);
      this.progressBar1.Value = Percent;
  }
}

The class:
public class pluginCode
    {
  private MapWindow.Interfaces.IMapWin _mapWin;
  private MapWinGIS.Shapefile sf = null;
  private MapWinGIS.ICallback mwCallBack;

  /// <summary>
        /// Constructor
        /// </summary>
        public pluginCode(MapWindow.Interfaces.IMapWin MapWin, MapWinGIS.ICallback mwCallBack)
  {
      _mapWin = MapWin;
      this.mwCallBack = mwCallBack;
  }

  public void executeQueryThread(BackgroundWorker worker, DoWorkEventArgs e)
  {
    // Create shapes and add them to the shapefile

    worker.ReportProgress((int)((double)numRows / (double)maxRows * 100));
  }
  public void saveShapefile() 
  {
      // sf Save:
      if (!this.sf.StopEditingShapes(true, true, this.mwCallBack))
          throw new Exception("Error in saving shapefile: "
                    + this.sf.get_ErrorMsg(this.sf.LastErrorCode));

      this.mwCallBack.Progress("", 100, "Add Shapefile as layer.");
      this._mapWin.Layers.Add(ref this.sf, "Percelen", Convert.ToInt32(ColorTranslator.ToOle(Color.Aqua)), Convert.ToInt32(ColorTranslator.ToOle(Color.Black)));
  }
}

Any help is very much appreciated.

Thanks,

Paul

--
Don't forget to read the new documentation: www.mapwindow.org/documentation/mapwingis4.8
Join us Google+: MapWindow GIS Google+ Community
Join the MapWindow Group on LinkedIn! LinkedIn - MapWindow Group

Download the latest beta installer at:
tinyurl.com/mwMonthly 32-Bit
tinyurl.com/mwMonthlyx64 64-Bit
Follow me on Twitter MapWindow_nl to read when a new installer is published.

---
Paul Meems
The Netherlands
[www.bontepaarden.nl]
Release manager, configuration manager and
forum moderator of MapWindow GIS

Owner of MapWindow.nl - Support for
Dutch speaking users: www.mapwindow.nl

*******
Everything I say or write is my personal opinion and
not the opinion of the company I work for.
*******
View my profile on LinkedIn

Options: ReplyQuote
Re: ICallBack and Backgroundworker
Posted by: Phil ()
Date: February 24, 2009 03:11PM

Where are you calling StartEditingShapes(True)?

Thanks.

-Phil

Options: ReplyQuote
Re: ICallBack and Backgroundworker
Posted by: pmeems ()
Date: February 24, 2009 03:31PM

Before calling the backgroundworker. Might that be the problem?
I could move everything inside the backgroundworker. I only use a MessageBox if the shapefile already exists. Don't know if that is going to be a problem.
Tomorrow (it's almost midnight here) I'll try moving everything inside the backgroundworker.

Phil, You have used a backgroundworker and the ICallBack before?

Thanks,

Paul

--
Don't forget to read the new documentation: www.mapwindow.org/documentation/mapwingis4.8
Join us Google+: MapWindow GIS Google+ Community
Join the MapWindow Group on LinkedIn! LinkedIn - MapWindow Group

Download the latest beta installer at:
tinyurl.com/mwMonthly 32-Bit
tinyurl.com/mwMonthlyx64 64-Bit
Follow me on Twitter MapWindow_nl to read when a new installer is published.

---
Paul Meems
The Netherlands
[www.bontepaarden.nl]
Release manager, configuration manager and
forum moderator of MapWindow GIS

Owner of MapWindow.nl - Support for
Dutch speaking users: www.mapwindow.nl

*******
Everything I say or write is my personal opinion and
not the opinion of the company I work for.
*******
View my profile on LinkedIn

Options: ReplyQuote
Re: ICallBack and Backgroundworker
Posted by: Phil ()
Date: February 24, 2009 03:42PM

No, I haven't used either. I just didn't see where you were calling that.

Not sure what you mean by "saving" the shapefile. In my code where I create shapefiles, it's always just something like this:

ShpF.StartEditingShapes(True)
loop, calling ShpF.EditInsertShape and ShpF.CellValue for each shape to add
ShpF.StopEditingShapes(True, True)


Thanks.

-Phil

Options: ReplyQuote
Re: ICallBack and Backgroundworker
Posted by: pmeems ()
Date: February 25, 2009 04:27AM

Phil,

By saving the shapefile I call the method saveShapefile().
As seen in my start post it calls
sf.StopEditingShapes
and ads the shapefile as a layer to MapWindow.

Too bad you haven't used a backgroundworker and the ICallBack ;)

Does anybody else have any experience with it?

Thanks,

Paul

--
Don't forget to read the new documentation: www.mapwindow.org/documentation/mapwingis4.8
Join us Google+: MapWindow GIS Google+ Community
Join the MapWindow Group on LinkedIn! LinkedIn - MapWindow Group

Download the latest beta installer at:
tinyurl.com/mwMonthly 32-Bit
tinyurl.com/mwMonthlyx64 64-Bit
Follow me on Twitter MapWindow_nl to read when a new installer is published.

---
Paul Meems
The Netherlands
[www.bontepaarden.nl]
Release manager, configuration manager and
forum moderator of MapWindow GIS

Owner of MapWindow.nl - Support for
Dutch speaking users: www.mapwindow.nl

*******
Everything I say or write is my personal opinion and
not the opinion of the company I work for.
*******
View my profile on LinkedIn

Options: ReplyQuote


Sorry, only registered users may post in this forum.





Banner Exchange




GISCP.com




Send us your banner logo (160x120) for the space above, and add this MapWindow banner ad to your site:

Just paste this text in your page: