MapDB Export/Import Progress

Oct 21, 2010 at 5:47 PM

Hi Radioman,

if you export / import the map db to/from file, application looks like freeze if the db are big. add/replace this code in SQLitePureImageCache:

 

public static bool ExportMapDataToDB(string sourceFile, string

destFile)

{

 

bool ret = true

;

 

try

{

 

if(!File

.Exists(destFile))

{

ret = CreateEmptyDB(destFile);

}

 

if

(ret)

{

 

using(SQLiteConnection cn1 = new SQLiteConnection

())

{

#if

 

!MONO

cn1.ConnectionString =

string.Format("Data Source=\"{0}\";Page Size=32768;Pooling=True"

, sourceFile);

#else

cn1.ConnectionString = string.Format("Version=3,URI=file://{0},FailIfMissing=True,Page Size=32768,Pooling=True", sourceFile);

#endif

cn1.Open();

 

if(cn1.State == System.Data.ConnectionState

.Open)

{

 

using(SQLiteConnection cn2 = new SQLiteConnection

())

{

#if

 

!MONO

cn2.ConnectionString =

string.Format("Data Source=\"{0}\";Page Size=32768;Pooling=True"

, destFile);

#else

cn2.ConnectionString = string.Format("Version=3,URI=file://{0},FailIfMissing=True,Page Size=32768,Pooling=True", destFile);

#endif

cn2.Open();

 

if(cn2.State == System.Data.ConnectionState

.Open)

{

 

using(SQLiteCommand cmd = new SQLiteCommand(string.Format("ATTACH DATABASE \"{0}\" AS Source"

, sourceFile), cn2))

{

cmd.ExecuteNonQuery();

}

#if

 

!MONO

 

using(SQLiteTransaction

tr = cn2.BeginTransaction())

#else

using(DbTransaction tr = cn2.BeginTransaction())

#endif

{

 

try

{

 

List<long> add = new List<long

>();

 

using(SQLiteCommand cmd = new SQLiteCommand("SELECT id, X, Y, Zoom, Type FROM Tiles;"

, cn1))

{

 

using(SQLiteDataReader

rd = cmd.ExecuteReader())

{

 

while

(rd.Read())

{

 

long

id = rd.GetInt64(0);

 

using(SQLiteCommand cmd2 = new SQLiteCommand(string.Format("SELECT id FROM Tiles WHERE X={0} AND Y={1} AND Zoom={2} AND Type={3};"

, rd.GetInt32(1), rd.GetInt32(2), rd.GetInt32(3), rd.GetInt32(4)), cn2))

{

 

using(SQLiteDataReader

rd2 = cmd2.ExecuteReader())

{

 

if

(!rd2.Read())

{

add.Add(id);

}

}

}

}

}

}

System.Windows.Forms.

Form NWind = new System.Windows.Forms.Form

();

NWind.Width = 400;

NWind.Height = 60;

NWind.FormBorderStyle = System.Windows.Forms.

FormBorderStyle

.FixedToolWindow;

System.Windows.Forms.

ProgressBar nProgressBar = new System.Windows.Forms.ProgressBar

();

nProgressBar.Name =

"Progress"

;

nProgressBar.Minimum = 0;

nProgressBar.Maximum = add.Count;

nProgressBar.Dock = System.Windows.Forms.

DockStyle

.Fill;

NWind.Controls.Add(nProgressBar);

NWind.StartPosition = System.Windows.Forms.

FormStartPosition

.CenterScreen;

NWind.TopMost =

true

;

NWind.Show();

 

 

 

 

 

foreach(long id in

add)

{

 

using(SQLiteCommand cmd = new SQLiteCommand(string.Format("INSERT INTO Tiles(X, Y, Zoom, Type) SELECT X, Y, Zoom, Type FROM Source.Tiles WHERE id={0}; INSERT INTO TilesData(id, Tile) Values((SELECT last_insert_rowid()), (SELECT Tile FROM Source.TilesData WHERE id={0}));"

, id), cn2))

{

cmd.Transaction = tr;

cmd.ExecuteNonQuery();

nProgressBar.Value = nProgressBar.Value + 1;

 

}

}

add.Clear();

NWind.Close();

tr.Commit();

}

 

catch

{

tr.Rollback();

ret =

false

;

}

}

}

}

}

}

}

}

 

catch(Exception

ex)

{

 

Debug.WriteLine("ExportMapDataToDB: "

+ ex.ToString());

ret =

false

;

}

 

return

ret;

}

Nov 2, 2010 at 7:32 AM

You can call the export/import methods on a worker thread and just display a marquee progress bar in the UI until the process is finished (you can detect that via events wired in the worker).

Nov 2, 2010 at 9:49 AM

Hi marceln,

yes thats true, but i think marquee style is a placeholder for "i am busy" and not a real progress.  With the code from my first post i have a real progress in a little Winforms window.