You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

187 lines
5.0 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. *
  3. * This file is part of QMapControl,
  4. * an open-source cross-platform map widget
  5. *
  6. * Copyright (C) 2007 - 2008 Kai Winter
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Lesser General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public License
  19. * along with QMapControl. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. * Contact e-mail: kaiwinter@gmx.de
  22. * Program URL : http://qmapcontrol.sourceforge.net/
  23. *
  24. */
  25. #include "tilemapadapter.h"
  26. TileMapAdapter::TileMapAdapter(const QString& host, const QString& serverPath, int tilesize, int minZoom, int maxZoom)
  27. :MapAdapter(host, serverPath, tilesize, minZoom, maxZoom)
  28. {
  29. PI = acos(-1.0);
  30. /*
  31. Initialize the "substring replace engine". First the string replacement
  32. in getQuery was made by QString().arg() but this was very slow. So this
  33. splits the servers path into substrings and when calling getQuery the
  34. substrings get merged with the parameters of the URL.
  35. Pretty complicated, but fast.
  36. */
  37. param1 = serverPath.indexOf("%1");
  38. param2 = serverPath.indexOf("%2");
  39. param3 = serverPath.indexOf("%3");
  40. int min = param1 < param2 ? param1 : param2;
  41. min = param3 < min ? param3 : min;
  42. int max = param1 > param2 ? param1 : param2;
  43. max = param3 > max ? param3 : max;
  44. int middle = param1+param2+param3-min-max;
  45. order[0][0] = min;
  46. if (min == param1)
  47. order[0][1] = 0;
  48. else if (min == param2)
  49. order[0][1] = 1;
  50. else
  51. order[0][1] = 2;
  52. order[1][0] = middle;
  53. if (middle == param1)
  54. order[1][1] = 0;
  55. else if (middle == param2)
  56. order[1][1] = 1;
  57. else
  58. order[1][1] = 2;
  59. order[2][0] = max;
  60. if (max == param1)
  61. order[2][1] = 0;
  62. else if(max == param2)
  63. order[2][1] = 1;
  64. else
  65. order[2][1] = 2;
  66. int zoom = mMax_zoom < mMin_zoom ? mMin_zoom - mCurrent_zoom : mCurrent_zoom;
  67. mNumberOfTiles = tilesonzoomlevel(zoom);
  68. loc.setNumberOptions(QLocale::OmitGroupSeparator);
  69. }
  70. TileMapAdapter::~TileMapAdapter()
  71. {
  72. }
  73. //TODO: pull out
  74. void TileMapAdapter::zoom_in()
  75. {
  76. if (mMin_zoom > mMax_zoom)
  77. {
  78. //mCurrent_zoom = mCurrent_zoom-1;
  79. mCurrent_zoom = mCurrent_zoom > mMax_zoom ? mCurrent_zoom-1 : mMax_zoom;
  80. }
  81. else if (mMin_zoom < mMax_zoom)
  82. {
  83. //mCurrent_zoom = mCurrent_zoom+1;
  84. mCurrent_zoom = mCurrent_zoom < mMax_zoom ? mCurrent_zoom+1 : mMax_zoom;
  85. }
  86. int zoom = mMax_zoom < mMin_zoom ? mMin_zoom - mCurrent_zoom : mCurrent_zoom;
  87. mNumberOfTiles = tilesonzoomlevel(zoom);
  88. }
  89. void TileMapAdapter::zoom_out()
  90. {
  91. if (mMin_zoom > mMax_zoom)
  92. {
  93. //mCurrent_zoom = mCurrent_zoom+1;
  94. mCurrent_zoom = mCurrent_zoom < mMin_zoom ? mCurrent_zoom+1 : mMin_zoom;
  95. }
  96. else if (mMin_zoom < mMax_zoom)
  97. {
  98. //mCurrent_zoom = mCurrent_zoom-1;
  99. mCurrent_zoom = mCurrent_zoom > mMin_zoom ? mCurrent_zoom-1 : mMin_zoom;
  100. }
  101. int zoom = mMax_zoom < mMin_zoom ? mMin_zoom - mCurrent_zoom : mCurrent_zoom;
  102. mNumberOfTiles = tilesonzoomlevel(zoom);
  103. }
  104. qreal TileMapAdapter::deg_rad(qreal x) const
  105. {
  106. return x * (PI/180.0);
  107. }
  108. qreal TileMapAdapter::rad_deg(qreal x) const
  109. {
  110. return x * (180/PI);
  111. }
  112. QString TileMapAdapter::query(int x, int y, int z) const
  113. {
  114. x = xoffset(x);
  115. y = yoffset(y);
  116. int a[3] = {z, x, y};
  117. return QString(serverPath().replace(order[2][0],2, loc.toString(a[order[2][1]]))
  118. .replace(order[1][0],2, loc.toString(a[order[1][1]]))
  119. .replace(order[0][0],2, loc.toString(a[order[0][1]])));
  120. }
  121. QPoint TileMapAdapter::coordinateToDisplay(const QPointF& coordinate) const
  122. {
  123. qreal x = (coordinate.x()+180) * (mNumberOfTiles*mTileSize)/360.; // coord to pixel!
  124. qreal y = (1-(log(tan(PI/4+deg_rad(coordinate.y())/2)) /PI)) /2 * (mNumberOfTiles*mTileSize);
  125. return QPoint(int(x), int(y));
  126. }
  127. QPointF TileMapAdapter::displayToCoordinate(const QPoint& point) const
  128. {
  129. qreal longitude = (point.x()*(360/(mNumberOfTiles*mTileSize)))-180;
  130. qreal latitude = rad_deg(atan(sinh((1-point.y()*(2/(mNumberOfTiles*mTileSize)))*PI)));
  131. return QPointF(longitude, latitude);
  132. }
  133. bool TileMapAdapter::isTileValid(int x, int y, int z) const
  134. {
  135. if (mMax_zoom < mMin_zoom)
  136. {
  137. z= mMin_zoom - z;
  138. }
  139. if (x<0 || x > (1 << z)-1 ||
  140. y<0 || y > (1 << z)-1)
  141. {
  142. return false;
  143. }
  144. return true;
  145. }
  146. int TileMapAdapter::tilesonzoomlevel(int zoomlevel) const
  147. {
  148. return int(pow(2.0, zoomlevel));
  149. }
  150. int TileMapAdapter::xoffset(int x) const
  151. {
  152. return x;
  153. }
  154. int TileMapAdapter::yoffset(int y) const
  155. {
  156. return y;
  157. }