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.

263 lines
7.8 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. * Copyright (C) 2014 Mattes Jaehne
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Lesser General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public License
  20. * along with QMapControl. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * original software by Kai Winter
  23. * Contact e-mail: kaiwinter@gmx.de
  24. * Program URL : http://qmapcontrol.sourceforge.net/
  25. *
  26. * modified to use Google Static Maps API V2 by
  27. * Mattes Jaehne <mattes@dev.uavp.ch>
  28. * for
  29. * NGOS - The Next Generation multicopter OS
  30. * http://ng.uavp.ch
  31. *
  32. */
  33. #include "googleapimapadapter.h"
  34. #include <QtCore/QCryptographicHash>
  35. #define _USE_MATH_DEFINES
  36. //#include <math.h>
  37. googleApiMapadapter::googleApiMapadapter(layerType qMapType, apiType qApiType, QString qApiKey, QString qApiClientID, QString qServerAddress)
  38. : TileMapAdapter(qServerAddress, "/maps/api/staticmap?", 256, 1, 22),
  39. mApiKey(qApiKey),
  40. mApiClientID(qApiClientID),
  41. mApiType( qApiType )
  42. {
  43. mMapLayerType = typeToString(qMapType);
  44. mNumberOfTiles = pow(2., mCurrent_zoom + 0.0);
  45. mCoord_per_x_tile = 360. / mNumberOfTiles;
  46. mCoord_per_y_tile = 180. / mNumberOfTiles;
  47. bool usingBusinessAPI = (mApiType == GoogleMapsForBusinessesAPI);
  48. if ( !mApiKey.isEmpty() && !usingBusinessAPI )
  49. {
  50. mApiKey.prepend("&key=");
  51. }
  52. if ( !mApiClientID.isEmpty() && usingBusinessAPI )
  53. {
  54. mApiClientID.prepend("&client=");
  55. }
  56. if ( !mMapLayerType.isEmpty() )
  57. {
  58. mMapLayerType.prepend("&maptype=");
  59. }
  60. }
  61. googleApiMapadapter::~googleApiMapadapter()
  62. {
  63. }
  64. QString googleApiMapadapter::getHost() const
  65. {
  66. return QString("maps.googleapis.com");
  67. }
  68. QPoint googleApiMapadapter::coordinateToDisplay(const QPointF& coordinate) const
  69. {
  70. qreal x = (coordinate.x() + 180.) * (mNumberOfTiles * mTileSize) / 360.; // coord to pixel!
  71. qreal y = (1. - log(tan(coordinate.y() * PI / 180.) + 1. / cos(coordinate.y() * PI / 180.)) / PI) / 2. * (mNumberOfTiles*mTileSize);
  72. x += mTileSize / 2;
  73. y += mTileSize / 2;
  74. return QPoint(int(x), int(y));
  75. }
  76. QPointF googleApiMapadapter::displayToCoordinate(const QPoint& point) const
  77. {
  78. qreal lon = (point.x() - mTileSize / 2) / (mNumberOfTiles * mTileSize) * 360. - 180.;
  79. qreal lat = PI - 2. * PI * (point.y() - mTileSize / 2) / (mNumberOfTiles * mTileSize);
  80. lat = 180. / PI * atan(0.5 * (exp(lat) - exp(-lat)));
  81. return QPointF(lon, lat);
  82. }
  83. qreal googleApiMapadapter::getMercatorLatitude(qreal YCoord) const
  84. {
  85. if (YCoord > PI) return 9999.;
  86. if (YCoord < -PI) return -9999.;
  87. qreal t = atan(exp(YCoord));
  88. qreal res = (2. * t) - (PI / 2.);
  89. return res;
  90. }
  91. qreal googleApiMapadapter::getMercatorYCoord(qreal lati) const
  92. {
  93. qreal phi = PI * lati / 180.;
  94. qreal res = 0.5 * log((1. + sin(phi)) / (1. - sin(phi)));
  95. return res;
  96. }
  97. void googleApiMapadapter::zoom_in()
  98. {
  99. if (mCurrent_zoom >= maxZoom())
  100. return;
  101. mCurrent_zoom += 1;
  102. mNumberOfTiles = pow(2, mCurrent_zoom + 0.0);
  103. mCoord_per_x_tile = 360. / mNumberOfTiles;
  104. mCoord_per_y_tile = 180. / mNumberOfTiles;
  105. }
  106. void googleApiMapadapter::zoom_out()
  107. {
  108. if (mCurrent_zoom <= minZoom())
  109. return;
  110. mCurrent_zoom -= 1;
  111. mNumberOfTiles = pow(2, mCurrent_zoom + 0.0);
  112. mCoord_per_x_tile = 360. / mNumberOfTiles;
  113. mCoord_per_y_tile = 180. / mNumberOfTiles;
  114. }
  115. bool googleApiMapadapter::isValid(int x, int y, int z) const
  116. {
  117. if ((x >= 0 && x < mNumberOfTiles) && (y >= 0 && y < mNumberOfTiles) && z >= 0)
  118. return true;
  119. return false;
  120. }
  121. QString googleApiMapadapter::typeToString(layerType qLayerType)
  122. {
  123. switch (qLayerType)
  124. {
  125. case layerType_SATELLITE: return "satellite";
  126. case layerType_HYBRID: return "hybrid";
  127. case layerType_TERRAIN: return "terrain";
  128. case layerType_ROADMAP:
  129. default:
  130. return "roadmap";
  131. }
  132. }
  133. QString googleApiMapadapter::query(int i, int j, int z) const
  134. {
  135. qreal longi = ((i * mTileSize) - (mTileSize * pow(2.0, z - 1))) / ((mTileSize * pow(2.0, z)) / 360.);
  136. qreal latit = PI - 2. * PI * j / pow(2., z);
  137. latit = 180. / PI * atan(0.5 * (exp(latit) - exp(-latit)));
  138. return getQ(longi, latit, z);
  139. }
  140. QString googleApiMapadapter::getQ(qreal longitude, qreal latitude, int zoom) const
  141. {
  142. QString location = "/maps/api/staticmap?&sensor=false&center=";
  143. location.append(QVariant(latitude).toString());
  144. location.append(",");
  145. location.append(QVariant(longitude).toString());
  146. location.append("&zoom=");
  147. location.append(QString::number(zoom));
  148. location.append("&size=" + QString::number(mTileSize) + "x" + QString::number(mTileSize) + "&scale=1");
  149. if (! mMapLayerType.isEmpty())
  150. {
  151. location.append(mMapLayerType);
  152. }
  153. if ( mApiType == GoogleMapsAPI )
  154. {
  155. if (mApiClientID.isEmpty())
  156. {
  157. fprintf(stderr, "You are using Google Maps API without a (valid) key. This is against \"Terms of use\" of Google Maps\r\n");
  158. }
  159. else
  160. {
  161. location.append(mApiClientID);
  162. }
  163. }
  164. else if ( mApiType == GoogleMapsForBusinessesAPI )
  165. {
  166. if (mApiClientID.isEmpty())
  167. {
  168. fprintf(stderr, "You are using Google Maps API without a (valid) key. This is against \"Terms of use\" of Google Maps\r\n");
  169. }
  170. else
  171. {
  172. // Google maps for business requires we sign every URL request against our apiKey
  173. //Example as taken from https://developers.google.com/maps/documentation/business/webservices/auth#digital_signatures
  174. //
  175. // URL: https://maps.googleapis.com/maps/api/geocode/json?address=New+York&client=clientID
  176. // Private Key: vNIXE0xscrmjlyV-12Nj_BvUPaw=
  177. // URL Portion to Sign: /maps/api/geocode/json?address=New+York&client=clientID
  178. // Signature: chaRF2hTJKOScPr-RQCEhZbSzIE=
  179. QString urlSignature = "&signature=";
  180. QString computedHash = signURL( location, mApiKey );
  181. urlSignature.append( computedHash );
  182. location.append( urlSignature );
  183. }
  184. }
  185. return location;
  186. }
  187. QString googleApiMapadapter::signURL(const QString &qURL, const QString &qCryptoKey) const
  188. {
  189. // Convert the key from 'web safe' base 64 to binary
  190. QString usablePrivateKey = qCryptoKey;
  191. usablePrivateKey.replace("-", "+");
  192. usablePrivateKey.replace("_", "/");
  193. QByteArray privateKeyBytes;
  194. privateKeyBytes.append(qPrintable(usablePrivateKey));
  195. QCryptographicHash hash(QCryptographicHash::Sha1);
  196. hash.addData(qPrintable(qURL));
  197. QString rawSignature = hash.hash(privateKeyBytes, QCryptographicHash::Sha1);
  198. // convert the bytes to string and make url-safe by replacing '+' and '/' characters
  199. QByteArray base64signature;
  200. base64signature.append(rawSignature);
  201. // base 64 encode the binary signature
  202. QString result = base64signature.toBase64();
  203. // convert the signature to 'web safe' base 64
  204. result.replace("+", "-");
  205. result.replace("/", "_");
  206. return result;
  207. }
  208. void googleApiMapadapter::setKey(QString qApiKey)
  209. {
  210. if (qApiKey.isEmpty())
  211. return;
  212. mApiKey.clear();
  213. mApiKey.append("&key=");
  214. mApiKey.append(qApiKey);
  215. }
  216. void googleApiMapadapter::setMapLayerType(layerType qMapType)
  217. {
  218. mMapLayerType.clear();
  219. mMapLayerType.append("&maptype=");
  220. mMapLayerType.append(typeToString(qMapType));
  221. }