QGIS API Documentation  2.15.0-Master (af20121)
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() )
26  , mReply( nullptr )
27 {
28 }
29 
31 {
32  if ( mReply )
33  {
34  mReply->abort();
35  mReply->deleteLater();
36  mReply = nullptr;
37  }
38 }
39 
40 
42 {
43  mError.clear();
44 
45  if ( mQuery.isEmpty() )
46  {
47  mError = tr( "No query has been specified." );
48  return false;
49  }
50 
51  if ( mReply )
52  {
53  mError = tr( "There is already a pending request for data." );
54  return false;
55  }
56 
57  if ( !mFile.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
58  {
59  mError = tr( "Cannot open output file: %1" ).arg( mFile.fileName() );
60  return false;
61  }
62 
64 
65  QUrl url( mServiceUrl );
66  url.addQueryItem( "data", mQuery );
67 
68  QNetworkRequest request( url );
69  request.setRawHeader( "User-Agent", "QGIS" );
70 
71  mReply = nwam->get( request );
72 
73  connect( mReply, SIGNAL( readyRead() ), this, SLOT( onReadyRead() ) );
74  connect( mReply, SIGNAL( error( QNetworkReply::NetworkError ) ), this, SLOT( onError( QNetworkReply::NetworkError ) ) );
75  connect( mReply, SIGNAL( finished() ), this, SLOT( onFinished() ) );
76  connect( mReply, SIGNAL( downloadProgress( qint64, qint64 ) ), this, SIGNAL( downloadProgress( qint64, qint64 ) ) );
77 
78  return true;
79 }
80 
81 
83 {
84  if ( !mReply )
85  return false;
86 
87  mReply->abort();
88  return true;
89 }
90 
91 
92 void QgsOSMDownload::onReadyRead()
93 {
94  Q_ASSERT( mReply );
95 
96  QByteArray data = mReply->read( 1024 * 1024 );
97  mFile.write( data );
98 }
99 
100 
101 void QgsOSMDownload::onFinished()
102 {
103  qDebug( "finished" );
104  Q_ASSERT( mReply );
105 
106  mReply->deleteLater();
107  mReply = nullptr;
108 
109  mFile.close();
110 
111  emit finished();
112 }
113 
114 
115 void QgsOSMDownload::onError( QNetworkReply::NetworkError err )
116 {
117  qDebug( "error: %d", err );
118  Q_ASSERT( mReply );
119 
120  mError = mReply->errorString();
121 }
122 
123 
125 {
126  if ( !mReply )
127  return true;
128 
129  return mReply->isFinished();
130 }
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)
QString errorString() const
double yMaximum() const
Get the y maximum value (top side of rectangle)
Definition: qgsrectangle.h:197
QString fileName() const
QString tr(const char *sourceText, const char *disambiguation, int n)
bool abort()
Aborts current pending request.
bool isFinished() const
Returns true if the request has already finished.
void clear()
double yMinimum() const
Get the y minimum value (bottom side of rectangle)
Definition: qgsrectangle.h:202
double xMaximum() const
Get the x maximum value (right side of rectangle)
Definition: qgsrectangle.h:187
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
virtual void close()
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.
qint64 write(const char *data, qint64 maxSize)
void addQueryItem(const QString &key, const QString &value)
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
double xMinimum() const
Get the x minimum value (left side of rectangle)
Definition: qgsrectangle.h:192