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.

209 lines
5.9 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 "wmsmapadapter.h"
  26. #include <QtCore/QStringList>
  27. WMSMapAdapter::WMSMapAdapter(QString host, QString serverPath, int tilesize)
  28. : MapAdapter(host, serverPath, tilesize, 0, 17)
  29. {
  30. mNumberOfTiles = pow(2.0, mCurrent_zoom);
  31. coord_per_x_tile = 360. / mNumberOfTiles;
  32. coord_per_y_tile = 180. / mNumberOfTiles;
  33. changeHostAddress( host, serverPath );
  34. loc = QLocale(QLocale::English);
  35. loc.setNumberOptions( QLocale::OmitGroupSeparator );
  36. setBoundingBox( -180, -90, 180, 90 );
  37. qreal res = 0.703125;
  38. for( int z = 0; z < 17; z++ )
  39. {
  40. mResolutions[z] = res;
  41. res = res / 2;
  42. }
  43. }
  44. WMSMapAdapter::~WMSMapAdapter()
  45. {
  46. }
  47. void WMSMapAdapter::changeHostAddress( const QString qHost, const QString qServerPath )
  48. {
  49. mServerOptions.clear();
  50. QString serverPathPrefix;
  51. if ( qServerPath.contains("?") &&
  52. qServerPath.split("?").size() > 1 )
  53. {
  54. serverPathPrefix = qServerPath.split("?").at( 0 );
  55. foreach( QString opt, qServerPath.split("?").at(1).split("&") )
  56. {
  57. if (opt.contains( "="))
  58. {
  59. mServerOptions[ opt.split("=").at(0).toUpper() ] = opt.split("=").at(1);
  60. }
  61. }
  62. }
  63. else
  64. {
  65. serverPathPrefix = "/wms/";
  66. }
  67. MapAdapter::changeHostAddress(qHost,serverPathPrefix);
  68. //force expected parameters
  69. if (!mServerOptions.contains("VERSION"))
  70. {
  71. mServerOptions["VERSION"] = "1.1.1";
  72. }
  73. if (!mServerOptions.contains("TRANSPARENT"))
  74. {
  75. mServerOptions["TRANSPARENT"]= "TRUE";
  76. }
  77. //if (!mServerOptions.contains("LAYERS"))
  78. //{
  79. // mServerOptions["LAYERS"] = TBD;
  80. //}
  81. if (!mServerOptions.contains("SRS") &&
  82. !mServerOptions.contains("CRS"))
  83. {
  84. mServerOptions["SRS"] = "EPSG:4326";
  85. //mServerOptions["SRS"] = "EPSG:900913"; //google mercator
  86. //mServerOptions["SRS"] = "EPSG:3857"; //EPSG:3857 is a Spherical Mercator projection
  87. }
  88. if (!mServerOptions.contains("STYLES"))
  89. {
  90. mServerOptions["STYLES"]= QString();
  91. }
  92. if (!mServerOptions.contains("FORMAT"))
  93. {
  94. mServerOptions["FORMAT"] = "IMAGE/PNG";
  95. }
  96. mServerOptions["SRS"] = "EPSG:4326"; //mercator
  97. mServerOptions["SERVICE"]= "WMS";
  98. mServerOptions["TILED"]= "TRUE";
  99. mServerOptions["REQUEST"]= "GetMap";
  100. mServerOptions["WIDTH"]= loc.toString(tilesize());
  101. mServerOptions["HEIGHT"]= loc.toString(tilesize());
  102. mServerOptions.remove("BBOX"); //added at time of query string
  103. }
  104. QString WMSMapAdapter::serverPath() const
  105. {
  106. QString urlPath;
  107. foreach( QString key, mServerOptions.keys() )
  108. {
  109. if (!urlPath.isEmpty())
  110. {
  111. urlPath.append("&");
  112. }
  113. urlPath.append( QString("%1=%2").arg( key ).arg( mServerOptions[key] ) );
  114. }
  115. return QString("%1?%2").arg( MapAdapter::serverPath() ).arg( urlPath );
  116. }
  117. QPoint WMSMapAdapter::coordinateToDisplay(const QPointF& coordinate) const
  118. {
  119. qreal x = (coordinate.x()+180) * (mNumberOfTiles*mTileSize)/360.; // coord to pixel!
  120. qreal y = -1*(coordinate.y()-90) * (mNumberOfTiles*mTileSize)/180.; // coord to pixel!
  121. return QPoint(int(x), int(y));
  122. }
  123. QPointF WMSMapAdapter::displayToCoordinate(const QPoint& point) const
  124. {
  125. qreal lon = (point.x()*(360./(mNumberOfTiles*mTileSize)))-180;
  126. qreal lat = -(point.y()*(180./(mNumberOfTiles*mTileSize)))+90;
  127. return QPointF(lon, lat);
  128. }
  129. void WMSMapAdapter::zoom_in()
  130. {
  131. mCurrent_zoom+=1;
  132. mNumberOfTiles = pow(2.0, mCurrent_zoom);
  133. coord_per_x_tile = 360. / mNumberOfTiles;
  134. coord_per_y_tile = 180. / mNumberOfTiles;
  135. }
  136. void WMSMapAdapter::zoom_out()
  137. {
  138. mCurrent_zoom-=1;
  139. mNumberOfTiles = pow(2.0, mCurrent_zoom);
  140. coord_per_x_tile = 360. / mNumberOfTiles;
  141. coord_per_y_tile = 180. / mNumberOfTiles;
  142. }
  143. bool WMSMapAdapter::isTileValid(int /*x*/, int /*y*/, int /*z*/) const
  144. {
  145. // if (x>0 && y>0 && z>0)
  146. {
  147. return true;
  148. }
  149. // return false;
  150. }
  151. QString WMSMapAdapter::query(int i, int j, int /*z*/) const
  152. {
  153. return getQ(-180+i*coord_per_x_tile,
  154. 90-(j+1)*coord_per_y_tile,
  155. -180+i*coord_per_x_tile+coord_per_x_tile,
  156. 90-(j+1)*coord_per_y_tile+coord_per_y_tile);
  157. }
  158. QString WMSMapAdapter::getQ(qreal ux, qreal uy, qreal ox, qreal oy) const
  159. {
  160. qreal x1 = ux;
  161. qreal y1 = uy;
  162. qreal x2 = ox;
  163. qreal y2 = oy;
  164. //if ( mServerOptions["SRS"].toUpper() == "EPSG:4326" )
  165. //{
  166. // if ( x1 < 0 )
  167. // {
  168. // x1 += 180;
  169. // }
  170. // if ( x1 > 360 )
  171. // {
  172. // x1 -= 180;
  173. // }
  174. // if ( x2 < 0 )
  175. // {
  176. // x2 += 180;
  177. // }
  178. // if ( x2 > 360 )
  179. // {
  180. // x2 -= 180;
  181. // }
  182. //}
  183. return QString("%1&BBOX=%2,%3,%4,%5")
  184. .arg(serverPath())
  185. .arg(QString::number(x1,'f',6))
  186. .arg(QString::number(y1,'f',6))
  187. .arg(QString::number(x2,'f',6))
  188. .arg(QString::number(y2,'f',6));
  189. }