QGIS API Documentation  2.8.2-Wien
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgsosmdownload.cpp
Go to the documentation of this file.
1 #include "qgsosmdownload.h"
2 
3 #include <QNetworkAccessManager>
4 #include <QNetworkRequest>
5 #include <QNetworkReply>
6 
8 #include "qgsrectangle.h"
9 
10 
12 {
13  return "http://overpass-api.de/api/interpreter";
14 }
15 
16 
18 {
19  return QString( "(node(%1,%2,%3,%4);<;);out;" ).arg( rect.yMinimum() ).arg( rect.xMinimum() )
20  .arg( rect.yMaximum() ).arg( rect.xMaximum() );
21 }
22 
23 
25  : mServiceUrl( defaultServiceUrl() ), mReply( 0 )
26 {
27 }
28 
30 {
31  if ( mReply )
32  {
33  mReply->abort();
34  mReply->deleteLater();
35  mReply = 0;
36  }
37 }
38 
39 
41 {
42  mError.clear();
43 
44  if ( mQuery.isEmpty() )
45  {
46  mError = tr( "No query has been specified." );
47  return false;
48  }
49 
50  if ( mReply )
51  {
52  mError = tr( "There is already a pending request for data." );
53  return false;
54  }
55 
56  if ( !mFile.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
57  {
58  mError = tr( "Cannot open output file: %1" ).arg( mFile.fileName() );
59  return false;
60  }
61 
63 
64  QUrl url( mServiceUrl );
65  url.addQueryItem( "data", mQuery );
66 
67  QNetworkRequest request( url );
68  request.setRawHeader( "User-Agent", "QGIS" );
69 
70  mReply = nwam->get( request );
71 
72  connect( mReply, SIGNAL( readyRead() ), this, SLOT( onReadyRead() ) );
73  connect( mReply, SIGNAL( error( QNetworkReply::NetworkError ) ), this, SLOT( onError( QNetworkReply::NetworkError ) ) );
74  connect( mReply, SIGNAL( finished() ), this, SLOT( onFinished() ) );
75  connect( mReply, SIGNAL( downloadProgress( qint64, qint64 ) ), this, SIGNAL( downloadProgress( qint64, qint64 ) ) );
76 
77  return true;
78 }
79 
80 
82 {
83  if ( !mReply )
84  return false;
85 
86  mReply->abort();
87  return true;
88 }
89 
90 
91 void QgsOSMDownload::onReadyRead()
92 {
93  Q_ASSERT( mReply );
94 
95  QByteArray data = mReply->read( 1024 * 1024 );
96  mFile.write( data );
97 }
98 
99 
100 void QgsOSMDownload::onFinished()
101 {
102  qDebug( "finished" );
103  Q_ASSERT( mReply );
104 
105  mReply->deleteLater();
106  mReply = 0;
107 
108  mFile.close();
109 
110  emit finished();
111 }
112 
113 
114 void QgsOSMDownload::onError( QNetworkReply::NetworkError err )
115 {
116  qDebug( "error: %d", err );
117  Q_ASSERT( mReply );
118 
119  mError = mReply->errorString();
120 }
121 
122 
124 {
125  if ( !mReply )
126  return true;
127 
128  return mReply->isFinished();
129 }