QGIS API Documentation  2.14.11-Essen
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( nullptr )
26 {
27 }
28 
30 {
31  if ( mReply )
32  {
33  mReply->abort();
34  mReply->deleteLater();
35  mReply = nullptr;
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 = nullptr;
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 }
A rectangle specified with double values.
Definition: qgsrectangle.h:35
void downloadProgress(qint64, qint64)
normally the total length is not known (until we reach end)
bool isFinished() const
Returns true if the request has already finished.
QString errorString() const
QString fileName() const
QString tr(const char *sourceText, const char *disambiguation, int n)
bool abort()
Aborts current pending request.
void clear()
bool isFinished() const
bool isEmpty() const
void finished()
emitted when the network reply has finished (with success or with an error)
qint64 read(char *data, qint64 maxSize)
bool start()
Starts network request for data.
void deleteLater()
static QString queryFromRect(const QgsRectangle &rect)
Create query (in Overpass Query Language) that fetches everything in given rectangle.
virtual bool open(QFlags< QIODevice::OpenModeFlag > mode)
virtual void abort()=0
double yMinimum() const
Get the y minimum value (bottom side of rectangle)
Definition: qgsrectangle.h:202
virtual void close()
double xMaximum() const
Get the x maximum value (right side of rectangle)
Definition: qgsrectangle.h:187
static QgsNetworkAccessManager * instance()
returns a pointer to the single instance
void setRawHeader(const QByteArray &headerName, const QByteArray &headerValue)
static QString defaultServiceUrl()
Return URL of the service that is used by default.
double xMinimum() const
Get the x minimum value (left side of rectangle)
Definition: qgsrectangle.h:192
qint64 write(const char *data, qint64 maxSize)
void addQueryItem(const QString &key, const QString &value)
double yMaximum() const
Get the y maximum value (top side of rectangle)
Definition: qgsrectangle.h:197
QNetworkReply * get(const QNetworkRequest &request)
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const