FrmMainF.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #include <math.h>
  4. #include <stdio.h>
  5. #include <cmath>
  6. #include <inifiles.hpp>
  7. #pragma hdrstop
  8. #include "FrmMainF.h"
  9. #include "DownloaderF.h"
  10. #include "ThrDownloaderF.h"
  11. //---------------------------------------------------------------------------
  12. #pragma package(smart_init)
  13. #pragma link "IdBaseComponent"
  14. #pragma link "IdComponent"
  15. #pragma link "IdHTTP"
  16. #pragma link "IdTCPClient"
  17. #pragma link "IdTCPConnection"
  18. #pragma resource "*.dfm"
  19. TForm1 *Form1;
  20. extern long g_CompletedJobCnt;
  21. //---------------------------------------------------------------------------
  22. __fastcall TForm1::TForm1(TComponent* Owner)
  23. : TForm(Owner)
  24. {
  25. reMsg->Lines->Clear();
  26. Initialize();
  27. }
  28. //---------------------------------------------------------------------------
  29. void __fastcall TForm1::Initialize()
  30. {
  31. String sAppDir = ExtractFilePath(Application->ExeName);
  32. ChDir(sAppDir);
  33. TIniFile *pIniFile = NULL;
  34. String sIniFile = ChangeFileExt(ExtractFileName(Application->ExeName), ".ini");
  35. String sCfgFile = sAppDir + sIniFile;
  36. try
  37. {
  38. String sTmp;
  39. pIniFile = new TIniFile(sCfgFile);
  40. String sTitle = pIniFile->ReadString("APPLICATION", "TITLE", "�������� ���� �ٿ�ε�");
  41. Caption = sTitle;
  42. int nThreads = pIniFile->ReadInteger("MAP", "THREADS", 200);
  43. if (nThreads < 10) nThreads = 10;
  44. if (nThreads > 200) nThreads = 200;
  45. EdThreads->Text = String(nThreads);
  46. FDownloadUrl = pIniFile->ReadString("MAP", "URL", "http://xdworld.vworld.kr:8080/2d/Base/service/%d/%d/%d.png").Trim();
  47. FSaveDir = pIniFile->ReadString("MAP", "SAVE", "C:\MAPDATA\VWorld\Base").Trim();
  48. EdURL->Text = FDownloadUrl;
  49. EdDir->Text = FSaveDir + "\\Base";
  50. EdLat1->Text = pIniFile->ReadString("MAP", "TOP", "36.9784").Trim();
  51. EdLat2->Text = pIniFile->ReadString("MAP", "BOTTOM", "35.0188").Trim();
  52. EdLon1->Text = pIniFile->ReadString("MAP", "LEFT", "127.3535").Trim();
  53. EdLon2->Text = pIniFile->ReadString("MAP", "RIGHT", "131.6656").Trim();
  54. }
  55. __finally
  56. {
  57. if (pIniFile) delete pIniFile;
  58. pIniFile = NULL;
  59. }
  60. }
  61. //---------------------------------------------------------------------------
  62. void __fastcall TForm1::Button1Click(TObject *Sender)
  63. {
  64. double Lon = Edit1->Text.Trim().ToDouble();
  65. double Lat = Edit2->Text.Trim().ToDouble();
  66. int Zoom = Edit3->Text.Trim().ToIntDef(12);
  67. String sLog;
  68. sLog.printf(L"Lon: %.7f, Lat: %.7f, Zoom: %d", Lon, Lat, Zoom);
  69. reMsg->Lines->Add(sLog);
  70. sLog = getTileURL(Lon, Lat, Zoom);
  71. reMsg->Lines->Add(sLog);
  72. }
  73. //---------------------------------------------------------------------------
  74. double asinh(double x)
  75. {
  76. return log(x + sqrt(x*x + 1.0));
  77. }
  78. //---------------------------------------------------------------------------
  79. #include <string.h>
  80. char *strrstr( char *str, char *search )
  81. {
  82. int len1 = strlen(str);
  83. int len2 = strlen(search);
  84. int pos = len1 - len2;
  85. char *result = NULL;
  86. // ���� �ϳ��� ���� ���ų� ã�� ���ڿ����̰� �� ũ�� ������.
  87. if( len1 == 0 || len2 == 0 || len1 < len2 )
  88. return NULL;
  89. while( pos >= 0 )
  90. {
  91. if( strncmp( str + pos, search, len2 ) == 0 )
  92. {
  93. result = str + pos;
  94. break;
  95. }
  96. pos--;
  97. }
  98. return result;
  99. }
  100. void __fastcall TForm1::BtnDownloadClick(TObject *Sender)
  101. {
  102. double Lon1 = EdLon1->Text.Trim().ToDouble();
  103. double Lat1 = EdLat1->Text.Trim().ToDouble();
  104. double Lon2 = EdLon2->Text.Trim().ToDouble();
  105. double Lat2 = EdLat2->Text.Trim().ToDouble();
  106. int Zoom = CbZoom->Text.Trim().ToIntDef(12);
  107. String sUrl = EdURL->Text.Trim();
  108. String tmpUrl = sUrl.UpperCase();
  109. bool isDaum = tmpUrl.Pos("DAUM") ? true : false;
  110. if (isDaum) {
  111. switch(Zoom) {
  112. case 19: Zoom = 1; break;
  113. case 18: Zoom = 2; break;
  114. case 17: Zoom = 3; break;
  115. case 16: Zoom = 4; break;
  116. case 15: Zoom = 5; break;
  117. case 14: Zoom = 6; break;
  118. case 13: Zoom = 7; break;
  119. case 12: Zoom = 8; break;
  120. case 11: Zoom = 9; break;
  121. case 10: Zoom = 10; break;
  122. case 9: Zoom = 11; break;
  123. case 8: Zoom = 12; break;
  124. case 7: Zoom = 13; break;
  125. case 6: Zoom = 14; break;
  126. }
  127. }
  128. DownloadMap(Lon1, Lat1, Lon2, Lat2, Zoom);
  129. }
  130. void __fastcall TForm1::DownloadMap(double Lon1, double Lat1, double Lon2, double Lat2, int Zoom)
  131. {
  132. #if 0
  133. getXYZ: function(bounds) {
  134. var res = this.getServerResolution();
  135. var x = Math.round((bounds.left - this.maxExtent.left) /
  136. (res * this.tileSize.w));
  137. var y = Math.round((bounds.bottom - this.maxExtent.bottom) /
  138. (res * this.tileSize.h));
  139. var z = 14 - this.getServerZoom();
  140. if (this.wrapDateLine) {
  141. var limit = Math.pow(2, z);
  142. x = ((x % limit) + limit) % limit;
  143. }
  144. return {'x': x, 'y': y, 'z': z};
  145. },
  146. #endif
  147. #define min(x,y) (x)>(y)?(y):(x)
  148. #define max(x,y) (x)>(y)?(x):(y)
  149. String sUrl = EdURL->Text.Trim();
  150. String sPath;
  151. String sDir = EdDir->Text.Trim() + "\\";
  152. ForceDirectories(sDir.c_str());
  153. int xmin = (int)(min(Lon1, Lon2)*pow(2, Zoom)/360.0+pow(2.0, Zoom)/2.0);
  154. int xmax = (int)(max(Lon1, Lon2)*pow(2, Zoom)/360.0+pow(2.0, Zoom)/2.0)+1;
  155. #if 0
  156. //�̰� �̻��ϰ� �� ����
  157. double lat = asinh(tan(min(Lat1, Lat2)/360.0*2.0*M_PI));
  158. int ymax = (int)(pow(2.0, Zoom)/2.0-lat*pow(2.0, Zoom)/(2.0*M_PI))+1;
  159. lat = asinh(tan(max(Lat1, Lat2)/360.0*2.0*M_PI));
  160. int ymin = (int)(pow(2.0, Zoom)/2.0-lat*pow(2.0, Zoom)/(2.0*M_PI));
  161. #else
  162. double dymin = (1.0 - log(tan(Lat1 * M_PI/180.0) + 1.0 / cos(Lat1 * M_PI/180.0)) / M_PI) / 2.0 * pow(2.0, Zoom);
  163. double dymax = (1.0 - log(tan(Lat2 * M_PI/180.0) + 1.0 / cos(Lat2 * M_PI/180.0)) / M_PI) / 2.0 * pow(2.0, Zoom);
  164. int ymin = min(dymin, dymax);
  165. int ymax = max(dymin, dymax);
  166. ymax++;
  167. #endif
  168. reMsg->Lines->Clear();
  169. String sLog;
  170. //sLog.printf(L"xmin: %d, xmax: %d, ymin: %d, ymax: %d, latMin: %.7f, latMax: %.7f", xmin, xmax, (int)ymin, (int)ymax, latMin, latMax);
  171. sLog.printf(L"xmin: %d, xmax: %d, ymin: %d, ymax: %d", xmin, xmax, ymin, ymax);
  172. reMsg->Lines->Add(sLog);
  173. sPath.printf(L"%s%d\\", sDir.c_str(), Zoom);
  174. ForceDirectories(sPath.c_str());
  175. g_CompletedJobCnt = 0;
  176. FSuccessCnt = 0;
  177. FThreadCnt = (xmax-xmin)+1;
  178. FJobCnt = ((xmax-xmin)+1) * ((ymax-ymin)+1);
  179. FTotalJobCnt = FJobCnt;
  180. ProgressBar1->Visible = true;
  181. ProgressBar1->Max = FJobCnt;
  182. ProgressBar1->Min = 0;
  183. ProgressBar1->Position = 0;
  184. int MAX_THREADS = EdThreads->Text.Trim().ToIntDef(100);
  185. for (int ii = xmin; ii <= xmax; ii++)
  186. {
  187. sPath.printf(L"%s%d\\%d\\", sDir.c_str(), Zoom, ii);
  188. ForceDirectories(sPath.c_str());
  189. }
  190. char szTemp[512];
  191. memset(szTemp, 0x00, sizeof(szTemp));
  192. String sExt;
  193. char *pExt = strrstr(AnsiString(sUrl).c_str(), ".");
  194. pExt++;
  195. sExt.printf(L"%s", pExt);
  196. sExt = String(pExt);
  197. #if 1
  198. sPath.printf(L"First Thread: %d", FThreadCnt);
  199. reMsg->Lines->Add(sPath);
  200. int nMaxJob = 0;
  201. if (FThreadCnt > MAX_THREADS)
  202. {
  203. nMaxJob = FThreadCnt / (MAX_THREADS);
  204. }
  205. int nJob = 0;
  206. FThreadCnt = 0;
  207. for (int ii = xmin; ii <= xmax; )
  208. {
  209. FThreadCnt++;
  210. nJob = ii + nMaxJob;
  211. if (nJob > xmax) nJob = xmax;
  212. sPath.printf(L"%s%d\\", sDir.c_str(), Zoom);
  213. ForceDirectories(sPath.c_str());
  214. TThrDownloader *pDownloader = new TThrDownloader(true, ChkDelOldMap->Checked);
  215. if (pDownloader)
  216. {
  217. pDownloader->FZoom = Zoom;
  218. pDownloader->FXMin = ii;
  219. pDownloader->FXMax = nJob;
  220. pDownloader->FYMin = ymin;
  221. pDownloader->FYMax = ymax;
  222. pDownloader->FPath = sPath;
  223. pDownloader->FFileExt = sExt;
  224. pDownloader->FURL = sUrl;
  225. pDownloader->Resume();
  226. }
  227. ii += nMaxJob;
  228. ii++;
  229. }
  230. sPath.printf(L"Last Thread: %d", FThreadCnt);
  231. reMsg->Lines->Add(sPath);
  232. sLog.printf(L"xmin: %d, xmax: %d, ymin: %d, ymax: %d", xmin, xmax, ymin, ymax);
  233. reMsg->Lines->Add(sLog);
  234. #endif
  235. #if 0
  236. for (int ii = xmin; ii <= xmax; ii++)
  237. {
  238. sPath.printf(L"%s%d\\", sDir.c_str(), Zoom);
  239. ForceDirectories(sPath.c_str());
  240. TThrDownloader *pDownloader = new TThrDownloader(true);
  241. if (pDownloader)
  242. {
  243. pDownloader->FZoom = Zoom;
  244. pDownloader->FXMin = ii;
  245. pDownloader->FXMax = ii+MAX_THREADS;
  246. pDownloader->FYMin = ymin;
  247. pDownloader->FYMax = ymax;
  248. pDownloader->FPath = sPath;
  249. pDownloader->FFileExt = "png";
  250. pDownloader->FURL = sUrl;
  251. pDownloader->Resume();
  252. }
  253. }
  254. #endif
  255. BtnDownload->Enabled = false;
  256. Timer1->Enabled = true;
  257. Refresh();
  258. Application->ProcessMessages();
  259. }
  260. //---------------------------------------------------------------------------
  261. void __fastcall TForm1::ApplicationEvents2Message(tagMSG &Msg, bool &Handled)
  262. {
  263. int nThreadX = (int)Msg.wParam;
  264. int nJobCnt = (int)Msg.lParam;
  265. String sLog;
  266. if (Msg.message == WM_DOWNLOAD_COMPLETE)
  267. {
  268. sLog.printf(L"%d download completed. %d success.", nThreadX, nJobCnt);
  269. reMsg->Lines->Add(sLog);
  270. FSuccessCnt += nJobCnt;
  271. FThreadCnt--;
  272. if (FThreadCnt == 0)
  273. {
  274. reMsg->Lines->Add("Download Completed.................");
  275. BtnDownload->Enabled = true;
  276. ProgressBar1->Visible = false;
  277. }
  278. }
  279. else
  280. if (Msg.message == WM_DOWNLOAD_JOB)
  281. {
  282. //sLog.printf(L"%d download. %d EA.", nThreadX, nJobCnt);
  283. //reMsg->Lines->Add(sLog);
  284. FJobCnt -= nJobCnt;
  285. }
  286. else
  287. if (Msg.message == WM_DOWNLOAD_START)
  288. {
  289. //sLog.printf(L"%d download start.", nThreadX);
  290. //reMsg->Lines->Add(sLog);
  291. }
  292. if (Msg.message == WM_DOWNLOAD_COMPLETE ||
  293. Msg.message == WM_DOWNLOAD_JOB ||
  294. Msg.message == WM_DOWNLOAD_START)
  295. {
  296. ProgressBar1->Position = ProgressBar1->Position + 1;
  297. Application->ProcessMessages();
  298. }
  299. }
  300. //---------------------------------------------------------------------------
  301. void __fastcall TForm1::Timer1Timer(TObject *Sender)
  302. {
  303. Timer1->Enabled = false;
  304. ProgressBar1->Position = g_CompletedJobCnt;
  305. Application->ProcessMessages();
  306. if (BtnDownload->Enabled == false)
  307. {
  308. if (FThreadCnt == 0 || g_CompletedJobCnt >= FTotalJobCnt)
  309. {
  310. reMsg->Lines->Add("Download Completed.................");
  311. BtnDownload->Enabled = true;
  312. ProgressBar1->Visible = false;
  313. Application->ProcessMessages();
  314. Timer1->Enabled = false;
  315. }
  316. else
  317. {
  318. Timer1->Enabled = true;
  319. }
  320. }
  321. }
  322. //---------------------------------------------------------------------------
  323. void __fastcall TForm1::BtnDownloadAllClick(TObject *Sender)
  324. {
  325. if (BtnDownload->Enabled == false) return;
  326. double Lon1 = 114.52724705906037;
  327. double Lat1 = 44.66724476853472;
  328. double Lon2 = 142.41054557562398;
  329. double Lat2 = 30.95707087688254;
  330. int Zoom = CbZoom->Text.Trim().ToIntDef(12);
  331. String sUrl = EdURL->Text.Trim();
  332. String tmpUrl = sUrl.UpperCase();
  333. bool isDaum = tmpUrl.Pos("DAUM") ? true : false;
  334. if (isDaum) {
  335. switch(Zoom) {
  336. case 19: Zoom = 1; break;
  337. case 18: Zoom = 2; break;
  338. case 17: Zoom = 3; break;
  339. case 16: Zoom = 4; break;
  340. case 15: Zoom = 5; break;
  341. case 14: Zoom = 6; break;
  342. case 13: Zoom = 7; break;
  343. case 12: Zoom = 8; break;
  344. case 11: Zoom = 9; break;
  345. case 10: Zoom = 10; break;
  346. case 9: Zoom = 11; break;
  347. case 8: Zoom = 12; break;
  348. case 7: Zoom = 13; break;
  349. case 6: Zoom = 14; break;
  350. }
  351. if (Zoom < 7 || Zoom > 14) return;
  352. if (Zoom <= 9) {
  353. Lon1 = 121.6024417969118;
  354. Lat1 = 38.7011141762058;
  355. Lon2 = 131.62197223312378;
  356. Lat2 = 32.63770774488186;
  357. }
  358. }
  359. else {
  360. if (Zoom < 6 || Zoom > 13) return;
  361. if (Zoom >= 11) {
  362. Lon1 = 121.6024417969118;
  363. Lat1 = 38.7011141762058;
  364. Lon2 = 131.62197223312378;
  365. Lat2 = 32.63770774488186;
  366. }
  367. }
  368. DownloadMap(Lon1, Lat1, Lon2, Lat2, Zoom);
  369. }
  370. //---------------------------------------------------------------------------
  371. void __fastcall TForm1::CboTypeClick(TObject *Sender)
  372. {
  373. String type = CboType->Text.Trim();
  374. if (type == "Base") {
  375. EdURL->Text = "http://xdworld.vworld.kr:8080/2d/Base/service/%d/%d/%d.png";
  376. EdDir->Text = FSaveDir + "\\Base";
  377. }
  378. else if (type == "Hybrid") {
  379. EdURL->Text = "http://xdworld.vworld.kr:8080/2d/Hybrid/service/%d/%d/%d.png";
  380. EdDir->Text = FSaveDir + "\\Hybrid";
  381. }
  382. else if (type == "Satellite") {
  383. EdURL->Text = "http://xdworld.vworld.kr:8080/2d/Satellite/service/%d/%d/%d.jpeg";
  384. EdDir->Text = FSaveDir + "\\Satellite";
  385. }
  386. }
  387. //---------------------------------------------------------------------------