/
OSMCtrlMapOperations.cpp
718 lines (606 loc) · 23.3 KB
/
OSMCtrlMapOperations.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
/*
Module : OSMCtrlMapOperations.cpp
Purpose: Implementation for the various COSMCtrl map operations classes
Created: PJN / 10-04-2011
History: PJN / None
Copyright (c) 2011 - 2014 by PJ Naughter (Web: www.naughter.com, Email: pjna@naughter.com)
All rights reserved.
Copyright / Usage Details:
You are allowed to include the source code in any product (commercial, shareware, freeware or otherwise)
when your product is released in binary form. You are allowed to modify the source code in any way you want
except you cannot modify the copyright details at the top of each module. If you want to distribute source
code with your application, then you are only allowed to distribute versions released by the author. This is
to maintain a single distribution point for the source code.
*/
///////////////////////////////// Includes //////////////////////////////////
#include "stdafx.h"
#include "OSMCtrlMapOperations.h"
#include "OSMCtrl.h"
///////////////////////////////// Implementation //////////////////////////////
UINT COSMCtrlMapOperationsDlg::sm_nMessageAddEvent = RegisterWindowMessage(_T("COSMCtrlMapOperations_AddEvent"));
BEGIN_MESSAGE_MAP(COSMCtrlMapOperationsDlg, CDialog)
ON_BN_CLICKED(ID_START_OPERATION, &COSMCtrlMapOperationsDlg::OnBnClickedStartOperation)
ON_BN_CLICKED(ID_STOP_OPERATION, &COSMCtrlMapOperationsDlg::OnBnClickedStopOperation)
ON_WM_HSCROLL()
ON_WM_DESTROY()
ON_REGISTERED_MESSAGE(sm_nMessageAddEvent, &COSMCtrlMapOperationsDlg::OnAddEvent)
END_MESSAGE_MAP()
COSMCtrlMapOperationsDlg::COSMCtrlMapOperationsDlg(CWnd* pParent): CDialog(COSMCtrlMapOperationsDlg::IDD, pParent),
m_nOperation(CacheTilesOnlyThoseNotDownloaded),
m_pWorkerThread(NULL)
{
}
COSMCtrlMapOperationsDlg::~COSMCtrlMapOperationsDlg()
{
}
void COSMCtrlMapOperationsDlg::DoDataExchange(CDataExchange* pDX)
{
//Let the parent class do its thing
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, ID_START_OPERATION, m_ctrlStartOperation);
DDX_Control(pDX, ID_STOP_OPERATION, m_ctrlStopOperation);
DDX_Control(pDX, IDC_OPERATION, m_ctrlOperation);
DDX_Control(pDX, IDC_ZOOM_LEVEL, m_ctrlZoomLevel);
DDX_CBIndex(pDX, IDC_OPERATION, m_nOperation);
DDX_Control(pDX, IDC_STATUS_TEXT, m_ctrlStatusText);
DDX_Control(pDX, IDC_PROGRESS, m_ctrlProgress);
}
void COSMCtrlMapOperationsDlg::OnBnClickedStartOperation()
{
if (UpdateData(TRUE))
{
//First thing to do is disable this button and enable the stop operation button
m_ctrlStartOperation.EnableWindow(FALSE);
m_ctrlStopOperation.EnableWindow(TRUE);
//Set focus to the Stop button
m_ctrlStopOperation.SetFocus();
//Reset the progress control
m_ctrlProgress.SetPos(0);
//Disable the zoom level control
m_ctrlZoomLevel.EnableWindow(FALSE);
//Empty out the array
m_PendingEvents.clear();
//Create the worker thread
CreateWorkerThread();
}
}
void COSMCtrlMapOperationsDlg::OnBnClickedStopOperation()
{
//First thing to do is disable this button and enable the start operation button
m_ctrlStopOperation.EnableWindow(FALSE);
m_ctrlStartOperation.EnableWindow(TRUE);
//Set focus back to the Start button
m_ctrlStartOperation.SetFocus();
//Shutdown the worker thread
DestroyWorkerThread();
}
BOOL COSMCtrlMapOperationsDlg::OnInitDialog()
{
//Let the base class do its thing
CDialog::OnInitDialog();
//Validate our parameters
ASSERT(m_pOSMCtrl != NULL);
//Setup the various controls
int nZoom = static_cast<int>(m_pOSMCtrl->GetZoom());
m_ctrlZoomLevel.SetRange(nZoom, min(COSMCtrl::OSMMaxZoom, nZoom+5), TRUE); //Only allow iteration up to 5 levels of zoom
m_ctrlZoomLevel.EnableWindow(nZoom != COSMCtrl::OSMMaxZoom);
m_ctrlStopOperation.EnableWindow(FALSE);
UpdateZoom(nZoom);
return TRUE;
}
void COSMCtrlMapOperationsDlg::UpdateZoom(int nZoomLevel)
{
//Validate our parameters
AFXASSUME(m_pOSMCtrl != NULL);
//Display a wait cursor while we do the maths to work out how many tiles we will be working on
CWaitCursor waitCursor;
//Reset the tiles array
m_Tiles.reserve(100000);
//Work out how many tiles this operation will affect
COSMCtrlPosition topLeft;
COSMCtrlPosition bottomRight;
int nZoom = static_cast<int>(m_pOSMCtrl->GetZoom());
if (m_pOSMCtrl->m_SelectionPolygon.GetBoundingRect(topLeft, bottomRight))
{
//Iterate across all the zoom levels we will be operating on
for (int i=nZoom; i<=nZoomLevel; i++)
{
//Work our the start and end X and Y values for the tiles at this zoom level
double fStartX = COSMCtrlHelper::Longitude2TileX(topLeft.m_fLongitude, i);
int nStartX = static_cast<int>(fStartX);
double fStartY = COSMCtrlHelper::Latitude2TileY(topLeft.m_fLatitude, i);
int nStartY = static_cast<int>(fStartY);
double fEndX = COSMCtrlHelper::Longitude2TileX(bottomRight.m_fLongitude, i);
int nEndX = static_cast<int>(fEndX);
double fEndY = COSMCtrlHelper::Latitude2TileY(bottomRight.m_fLatitude, i);
int nEndY = static_cast<int>(fEndY);
//Add all the tiles for this zoom level to the array
for (int j=nStartX; j<=nEndX; j++)
{
for (int k=nStartY; k<=nEndY; k++)
{
COSMCtrlMapOperationsDlgTile tile;
tile.m_nTileX = j;
tile.m_nTileY = k;
tile.m_nZoom = i;
m_Tiles.push_back(tile);
}
}
}
}
//Free up any unused memory in the array
m_Tiles.shrink_to_fit();
//Set the range on the progress control
int nTiles = static_cast<int>(m_Tiles.size());
m_ctrlProgress.SetPos(0);
m_ctrlProgress.SetRange32(0, nTiles);
//Finally update the text on the UI
CString sStatus;
CString sZoom1;
sZoom1.Format(_T("%d"), nZoom);
CString sTiles;
sTiles.Format(_T("%d"), nTiles);
if (nZoom == nZoomLevel)
AfxFormatString2(sStatus, IDS_OSMCTRL_MAP_OPERATIONS_UPDATE_ZOOM2, sZoom1, sTiles);
else
{
CString sZoom2;
sZoom2.Format(_T("%d"), nZoomLevel);
LPCTSTR szStrings[3];
szStrings[0] = sZoom1;
szStrings[1] = sZoom2;
szStrings[2] = sTiles;
AfxFormatStrings(sStatus, IDS_OSMCTRL_MAP_OPERATIONS_UPDATE_ZOOM3, szStrings, 3);
}
m_ctrlStatusText.SetWindowText(sStatus);
}
void COSMCtrlMapOperationsDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
//Validate our parameters
ASSERT(m_pOSMCtrl != NULL);
//Let the base class do its thing
CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
//Handle the notification if it is from our control
AFXASSUME(pScrollBar != NULL);
if (pScrollBar->GetSafeHwnd() == m_ctrlZoomLevel.GetSafeHwnd())
{
switch (nSBCode)
{
case TB_BOTTOM:
{
UpdateZoom(min(COSMCtrl::OSMMaxZoom, static_cast<int>(m_pOSMCtrl->GetZoom()) + 5));
break;
}
case TB_TOP:
{
UpdateZoom(static_cast<int>(m_pOSMCtrl->GetZoom()));
break;
}
case TB_LINEUP:
case TB_PAGEUP:
{
UpdateZoom(m_ctrlZoomLevel.GetPos());
break;
}
case TB_LINEDOWN:
case TB_PAGEDOWN:
{
UpdateZoom(m_ctrlZoomLevel.GetPos());
break;
}
case TB_THUMBTRACK: //deliberate fallthrough
case TB_THUMBPOSITION:
{
UpdateZoom(nPos);
break;
}
default:
{
//Nothing to do
break;
}
}
}
}
BOOL COSMCtrlMapOperationsDlg::CreateWorkerThread()
{
//What will be the return value from this function (assume the worst)
BOOL bSuccess = FALSE;
//Destroy the worker thread if currently running
DestroyWorkerThread();
//Spin off the background thread
ASSERT(m_pWorkerThread == NULL);
m_WorkerTerminateEvent.ResetEvent();
m_pWorkerThread = AfxBeginThread(_WorkerThread, this, THREAD_PRIORITY_IDLE, 0, CREATE_SUSPENDED);
if (m_pWorkerThread)
{
//We're in charge of deletion of the thread
m_pWorkerThread->m_bAutoDelete = FALSE;
//Resume the thread now that everything is ready to go
m_pWorkerThread->ResumeThread();
bSuccess = TRUE;
}
return bSuccess;
}
void COSMCtrlMapOperationsDlg::DestroyWorkerThread()
{
//Ensure the download thread is shutdown
if (m_pWorkerThread)
{
#ifdef _DEBUG
//If we are running a debug version of the code, display a wait cursor to visually indicate
//how long the destruction of the worker thread takes
CWaitCursor wait;
#endif
//Signal the worker thread to exit and wait for it to return
m_WorkerTerminateEvent.SetEvent();
//Close the session handle which is being used by the worker thread.
WinHttpCloseHandle(m_hSession);
//Wait for the thread to return
WaitForSingleObject(m_pWorkerThread->m_hThread, INFINITE);
delete m_pWorkerThread;
m_pWorkerThread = NULL;
}
}
void COSMCtrlMapOperationsDlg::OnDestroy()
{
//kill the worker thread if it is currently running
DestroyWorkerThread();
//Let the base class do its thing
CDialog::OnDestroy();
}
UINT COSMCtrlMapOperationsDlg::_WorkerThread(LPVOID pParam)
{
//Validate our parameters
ASSERT(pParam != NULL);
COSMCtrlMapOperationsDlg* pThis = static_cast<COSMCtrlMapOperationsDlg*>(pParam);
AFXASSUME(pThis != NULL);
//Convert from the SDK world to the C++ world
return pThis->WorkerThread();
}
BOOL COSMCtrlMapOperationsDlg::DeleteCachedTilesHelper()
{
//What will be the return value from this function (assume the best)
BOOL bSuccess = TRUE;
//Accumulate how many tiles we have deleted and not deleted
int nTilesDeleted = 0;
int nTilesNotDeleted = 0;
//Work thro all the tiles
CString sCacheDirectory(m_pOSMCtrl->GetCacheDirectory());
for (std::vector<COSMCtrlMapOperationsDlgTile>::size_type i=0; (i<m_Tiles.size()) && bSuccess; i++)
{
//Pull out the tile we are working on
const COSMCtrlMapOperationsDlgTile& tile = m_Tiles[i];
//Do the deletion of the tile
CString sTile(m_pOSMCtrl->GetTileCachePath(sCacheDirectory, tile.m_nZoom, tile.m_nTileX, tile.m_nTileY, FALSE));
COSMCtrlMapOperationsDlgEvent dlgEvent;
if (GetFileAttributes(sTile) != INVALID_FILE_ATTRIBUTES) //Don't bother doing anything if the tile does not already exist
{
if (DeleteFile(sTile))
{
//Update the stats
++nTilesDeleted;
dlgEvent.m_bSuccess = true;
}
else
{
//Update the stats
++nTilesNotDeleted;
dlgEvent.m_bSuccess = false;
}
}
//Update the UI
dlgEvent.m_Event = COSMCtrlMapOperationsDlgEvent::SimpleStringStatus;
dlgEvent.m_sString = sTile;
dlgEvent.m_nItemData = i + 1;
AddEvent(dlgEvent);
//Check if we have been cancelled before we loop around
bSuccess = (WaitForSingleObject(m_WorkerTerminateEvent, 0) == WAIT_TIMEOUT);
}
//Finally add a event about how many items have been deleted
COSMCtrlMapOperationsDlgEvent dlgEvent;
dlgEvent.m_Event = COSMCtrlMapOperationsDlgEvent::SimpleStringStatus;
CString sTilesDeleted;
sTilesDeleted.Format(_T("%d"), nTilesDeleted);
CString sTilesNotDeleted;
sTilesNotDeleted.Format(_T("%d"), nTilesNotDeleted);
AfxFormatString2(dlgEvent.m_sString, IDS_OSMCTRL_DELETE_FILES_STATS, sTilesDeleted, sTilesNotDeleted);
AddEvent(dlgEvent);
return bSuccess;
}
BOOL COSMCtrlMapOperationsDlg::DownloadTiles(BOOL bSkipIfTileAlreadyExists)
{
//Validate our parameters
AFXASSUME(m_pOSMCtrl != NULL);
//Pull out the tile provider we will be using
IOSMCtrlTileProvider* pTileProvider = m_pOSMCtrl->GetTileProvider();
AFXASSUME(pTileProvider != NULL);
//Next get the server to connect to
CStringW sServer(pTileProvider->GetDownloadServer());
//Accumulate how many tiles we have deleted and not deleted
int nTilesDownloaded = 0;
int nTilesNotDownloaded = 0;
//Next create the WinHTTP session object
CWinHTTPSession session;
HRESULT hr = m_pOSMCtrl->CreateSession(session, 0);
if (SUCCEEDED(hr))
{
//Now create the connection object from the session object
CWinHTTPConnection connection;
hr = connection.Initialize(session, sServer, pTileProvider->GetDownloadPort());
if (SUCCEEDED(hr))
{
//Iterate across the array of tiles to download
BOOL bSuccess = TRUE;
for (std::vector<COSMCtrlMapOperationsDlgTile>::size_type i=0; i<m_Tiles.size() && bSuccess; i++)
{
//Pull out the next tile to download
const COSMCtrlMapOperationsDlgTile& tile = m_Tiles[i];
//Create the sub directories if we can
CString sCacheDirectory(m_pOSMCtrl->GetCacheDirectory());
CString sSubDirectory;
sSubDirectory.Format(_T("%s\\%d"), sCacheDirectory.operator LPCTSTR(), tile.m_nZoom);
CreateDirectory(sSubDirectory, NULL);
sSubDirectory.Format(_T("%s\\%d\\%d"), sCacheDirectory.operator LPCTSTR(), tile.m_nZoom, tile.m_nTileX);
CreateDirectory(sSubDirectory, NULL);
//Form the path to the tile we will be downloading to and determine if we should do the download
CString sFile(COSMCtrl::GetTileCachePath(sCacheDirectory, tile.m_nZoom, tile.m_nTileX, tile.m_nTileY, FALSE));
BOOL bDownload = TRUE;
if (bSkipIfTileAlreadyExists)
bDownload = (GetFileAttributes(sFile) == INVALID_FILE_ATTRIBUTES);
//Now download the specific tile to the cache if required
COSMCtrlMapOperationsDlgEvent dlgEvent;
dlgEvent.m_bSuccess = false;
if (bDownload)
{
//We will accept any mime type
LPCWSTR pwszAcceptTypes[2];
pwszAcceptTypes[0] = L"*/*";
pwszAcceptTypes[1] = NULL;
CSyncWinHTTPDownloader winHttpRequest;
winHttpRequest.m_sFileToDownloadInto = sFile;
CString sObject(pTileProvider->GetDownloadObject(tile.m_nZoom, tile.m_nTileX, tile.m_nTileY));
hr = winHttpRequest.Initialize(connection, CStringW(sObject), NULL, NULL, NULL, pwszAcceptTypes, WINHTTP_FLAG_REFRESH);
if (FAILED(hr))
{
//report the error
TRACE(_T("COSMCtrlMapOperationsDlg::DownloadTiles, Failed to create request for tile \"%s\", Error:%08X\n"), sFile.operator LPCTSTR(), hr);
//Ensure any remants of a bad download file are nuked
DeleteFile(sFile);
//Update the stats
++nTilesNotDownloaded;
}
else
{
hr = winHttpRequest.SendRequestSync();
if (FAILED(hr))
{
//report the error
TRACE(_T("COSMCtrlMapOperationsDlg::DownloadTiles, Failed to send request for tile \"%s\", Error:%08X\n"), sFile.operator LPCTSTR(), hr);
//Ensure any remants of a bad download file are nuked
DeleteFile(sFile);
//Update the stats
++nTilesNotDownloaded;
}
else
{
//Update the stats
++nTilesDownloaded;
dlgEvent.m_bSuccess = true;
}
}
}
else
{
//Update the stats
++nTilesNotDownloaded;
}
//Update the UI
dlgEvent.m_Event = COSMCtrlMapOperationsDlgEvent::SimpleStringStatus;
dlgEvent.m_sString = sFile;
dlgEvent.m_nItemData = i + 1;
AddEvent(dlgEvent);
//Check if we have been cancelled before we loop around
bSuccess = (WaitForSingleObject(m_WorkerTerminateEvent, 0) == WAIT_TIMEOUT);
}
}
}
//Finally add a event about how many items have been downloaded
COSMCtrlMapOperationsDlgEvent dlgEvent;
dlgEvent.m_Event = COSMCtrlMapOperationsDlgEvent::SimpleStringStatus;
CString sTilesDownloaded;
sTilesDownloaded.Format(_T("%d"), nTilesDownloaded);
CString sTilesNotDownloaded;
sTilesNotDownloaded.Format(_T("%d"), nTilesNotDownloaded);
AfxFormatString2(dlgEvent.m_sString, IDS_OSMCTRL_DOWNLOAD_TILES_STATS, sTilesDownloaded, sTilesNotDownloaded);
AddEvent(dlgEvent);
return TRUE;
}
BOOL COSMCtrlMapOperationsDlg::ForceMapnikRerenderHelper()
{
//Validate our parameters
AFXASSUME(m_pOSMCtrl != NULL);
//Next get the server to connect to
COSMCtrlMapnikTileProvider MapnikTileProvider;
CStringW sServer(MapnikTileProvider.GetDownloadServer());
//Accumulate how many tiles we have request to rerender and which ones indicated a failure to rerender
int nTilesRerendered = 0;
int nTilesNotRerendered = 0;
//Next create the WinHTTP session object
CWinHTTPSession session;
HRESULT hr = m_pOSMCtrl->CreateSession(session, 0);
if (SUCCEEDED(hr))
{
//Now create the connection object from the session object
CWinHTTPConnection connection;
hr = connection.Initialize(session, sServer, MapnikTileProvider.GetDownloadPort());
if (SUCCEEDED(hr))
{
//Iterate across the array of tiles to download
BOOL bSuccess = TRUE;
for (std::vector<COSMCtrlMapOperationsDlgTile>::size_type i=0; i<m_Tiles.size() && bSuccess; i++)
{
//Pull out the next tile to download
const COSMCtrlMapOperationsDlgTile& tile = m_Tiles[i];
//Now issue the request to rerender
COSMCtrlMapOperationsDlgEvent dlgEvent;
dlgEvent.m_bSuccess = false;
LPCWSTR pwszAcceptTypes[2];
pwszAcceptTypes[0] = L"*/*";
pwszAcceptTypes[1] = NULL;
CSyncWinHTTPDownloader winHttpRequest;
//winHttpRequest.m_sFileToDownloadInto = sFile;
CString sObject(MapnikTileProvider.GetDownloadObject(tile.m_nZoom, tile.m_nTileX, tile.m_nTileY) + _T("/dirty"));
hr = winHttpRequest.Initialize(connection, CStringW(sObject), NULL, NULL, NULL, pwszAcceptTypes, WINHTTP_FLAG_REFRESH);
if (FAILED(hr))
{
//report the error
TRACE(_T("COSMCtrlMapOperationsDlg::ForceMapnikRerenderHelper, Failed to create request for object \"%s\", Error:%08X\n"), sObject.operator LPCTSTR(), hr);
//Update the stats
++nTilesNotRerendered;
}
else
{
hr = winHttpRequest.SendRequestSync();
if (FAILED(hr))
{
//report the error
TRACE(_T("COSMCtrlMapOperationsDlg::ForceMapnikRerenderHelper, Failed to send request for object \"%s\", Error:%08X\n"), sObject.operator LPCTSTR(), hr);
//Update the stats
++nTilesNotRerendered;
}
else
{
CStringA sResponse;
sResponse.Append(reinterpret_cast<LPCSTR>(winHttpRequest.m_Response.GetData()), static_cast<int>(winHttpRequest.m_Response.GetSize()));
if (sResponse.Find("Tile submitted for rendering") != -1)
{
//Update the stats
++nTilesRerendered;
dlgEvent.m_bSuccess = true;
}
else
{
//Update the stats
++nTilesNotRerendered;
}
}
}
//Update the UI
dlgEvent.m_Event = COSMCtrlMapOperationsDlgEvent::SimpleStringStatus;
dlgEvent.m_sString = sObject;
dlgEvent.m_nItemData = i + 1;
AddEvent(dlgEvent);
//Check if we have been cancelled before we loop around
bSuccess = (WaitForSingleObject(m_WorkerTerminateEvent, 0) == WAIT_TIMEOUT);
}
}
}
//Finally add a event about how many items have been downloaded
COSMCtrlMapOperationsDlgEvent dlgEvent;
dlgEvent.m_Event = COSMCtrlMapOperationsDlgEvent::SimpleStringStatus;
CString sTilesRerendered;
sTilesRerendered.Format(_T("%d"), nTilesRerendered);
CString sTilesNotRerendered;
sTilesNotRerendered.Format(_T("%d"), nTilesNotRerendered);
AfxFormatString2(dlgEvent.m_sString, IDS_OSMCTRL_RERENDER_TILES_STATS, sTilesRerendered, sTilesNotRerendered);
AddEvent(dlgEvent);
return TRUE;
}
UINT COSMCtrlMapOperationsDlg::WorkerThread()
{
//Validate our parameters
AFXASSUME(m_pOSMCtrl != NULL);
//What will be the return value from this function (assume the worst)
BOOL bSuccess = FALSE;
//Switch on the operation type we have been asked to work on
switch (m_nOperation)
{
case DeleteCachedTiles:
{
bSuccess = DeleteCachedTilesHelper();
break;
}
case CacheTilesIncludingThoseDownloaded:
{
bSuccess = DownloadTiles(FALSE);
break;
}
case CacheTilesOnlyThoseNotDownloaded:
{
bSuccess = DownloadTiles(TRUE);
break;
}
case ForceMapnikRerender:
{
bSuccess = ForceMapnikRerenderHelper();
break;
}
default:
{
ASSERT(FALSE);
break;
}
}
//Inform the UI that the work is completed
COSMCtrlMapOperationsDlgEvent dlgEvent;
dlgEvent.m_Event = COSMCtrlMapOperationsDlgEvent::ThreadCompleted;
dlgEvent.m_bSuccess = bSuccess ? true : false;
AddEvent(dlgEvent);
return 0;
}
void COSMCtrlMapOperationsDlg::AddEvent(COSMCtrlMapOperationsDlgEvent& dlgEvent)
{
//Serialize access to the array
CSingleLock sl(&m_csPendingEvents, TRUE);
m_PendingEvents.push_back(dlgEvent);
//Inform the GUI thread that there has been a new event added to the array
PostMessage(sm_nMessageAddEvent);
}
LRESULT COSMCtrlMapOperationsDlg::OnAddEvent(WPARAM /*wParam*/, LPARAM /*lParam*/)
{
//Validate our parameters
AFXASSUME(m_pOSMCtrl != NULL);
//Serialize access to the array
CSingleLock sl(&m_csPendingEvents, TRUE);
//Pull as many items of the queue as we can
while (m_PendingEvents.size())
{
COSMCtrlMapOperationsDlgEvent dlgEvent(m_PendingEvents[0]);
m_PendingEvents.erase(m_PendingEvents.cbegin());
switch (dlgEvent.m_Event)
{
case COSMCtrlMapOperationsDlgEvent::ThreadCompleted:
{
//Disable the stop button and reenable the start button
m_ctrlStopOperation.EnableWindow(FALSE);
m_ctrlStartOperation.EnableWindow(TRUE);
//Reset the progress control if necessary
if (!dlgEvent.m_bSuccess)
m_ctrlProgress.SetPos(0);
//Also reset the zoom level control
m_ctrlZoomLevel.EnableWindow(TRUE);
//Set focus back to the Start button
m_ctrlStartOperation.SetFocus();
//Force a refresh of map as some of the map operations may have invalidate the tiles
m_pOSMCtrl->Refresh();
break;
}
case COSMCtrlMapOperationsDlgEvent::SimpleStringStatus:
{
//Simple update the status static
m_ctrlStatusText.SetWindowText(dlgEvent.m_sString);
//Update the progress control if necessary
if (dlgEvent.m_nItemData)
m_ctrlProgress.SetPos(static_cast<int>(dlgEvent.m_nItemData));
break;
}
default:
{
ASSERT(FALSE);
break;
}
}
}
return 0L;
}