QGIS API Documentation  2.99.0-Master (e077efd)
qgsosmdownload.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsosmdownload.cpp
3  ---------------------
4  begin : February 2013
5  copyright : (C) 2013 by Martin Dobias
6  email : wonder dot sk at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 #include "qgsosmdownload.h"
16 
17 #include <QNetworkAccessManager>
18 #include <QNetworkRequest>
19 #include <QNetworkReply>
20 
22 #include "qgsrectangle.h"
23 
24 
26 {
27  return QStringLiteral( "http://overpass-api.de/api/interpreter" );
28 }
29 
30 
32 {
33  return QStringLiteral( "(node(%1,%2,%3,%4);<;);out;" ).arg( rect.yMinimum() ).arg( rect.xMinimum() )
34  .arg( rect.yMaximum() ).arg( rect.xMaximum() );
35 }
36 
37 
39  : mServiceUrl( defaultServiceUrl() )
40  , mReply( nullptr )
41 {
42 }
43 
45 {
46  if ( mReply )
47  {
48  mReply->abort();
49  mReply->deleteLater();
50  mReply = nullptr;
51  }
52 }
53 
54 
56 {
57  mError.clear();
58 
59  if ( mQuery.isEmpty() )
60  {
61  mError = tr( "No query has been specified." );
62  return false;
63  }
64 
65  if ( mReply )
66  {
67  mError = tr( "There is already a pending request for data." );
68  return false;
69  }
70 
71  if ( !mFile.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
72  {
73  mError = tr( "Cannot open output file: %1" ).arg( mFile.fileName() );
74  return false;
75  }
76 
78 
79  QUrl url( mServiceUrl );
80  url.addQueryItem( QStringLiteral( "data" ), mQuery );
81 
82  QNetworkRequest request( url );
83  request.setRawHeader( "User-Agent", "QGIS" );
84 
85  mReply = nwam->get( request );
86 
87  connect( mReply, SIGNAL( readyRead() ), this, SLOT( onReadyRead() ) );
88  connect( mReply, SIGNAL( error( QNetworkReply::NetworkError ) ), this, SLOT( onError( QNetworkReply::NetworkError ) ) );
89  connect( mReply, SIGNAL( finished() ), this, SLOT( onFinished() ) );
90  connect( mReply, SIGNAL( downloadProgress( qint64, qint64 ) ), this, SIGNAL( downloadProgress( qint64, qint64 ) ) );
91 
92  return true;
93 }
94 
95 
97 {
98  if ( !mReply )
99  return false;
100 
101  mReply->abort();
102  return true;
103 }
104 
105 
106 void QgsOSMDownload::onReadyRead()
107 {
108  Q_ASSERT( mReply );
109 
110  QByteArray data = mReply->read( 1024 * 1024 );
111  mFile.write( data );
112 }
113 
114 
115 void QgsOSMDownload::onFinished()
116 {
117  qDebug( "finished" );
118  Q_ASSERT( mReply );
119 
120  mReply->deleteLater();
121  mReply = nullptr;
122 
123  mFile.close();
124 
125  emit finished();
126 }
127 
128 
129 void QgsOSMDownload::onError( QNetworkReply::NetworkError err )
130 {
131  qDebug( "error: %d", err );
132  Q_ASSERT( mReply );
133 
134  mError = mReply->errorString();
135 }
136 
137 
139 {
140  if ( !mReply )
141  return true;
142 
143  return mReply->isFinished();
144 }
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.
bool abort()
Aborts current pending request.
void finished()
Emitted when the network reply has finished (with success or with an error)
bool start()
Starts network request for data.
static QString queryFromRect(const QgsRectangle &rect)
Create query (in Overpass Query Language) that fetches everything in given rectangle.
double yMinimum() const
Get the y minimum value (bottom side of rectangle)
Definition: qgsrectangle.h:206
double xMaximum() const
Get the x maximum value (right side of rectangle)
Definition: qgsrectangle.h:191
static QgsNetworkAccessManager * instance()
returns a pointer to the single instance
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:196
double yMaximum() const
Get the y maximum value (top side of rectangle)
Definition: qgsrectangle.h:201
network access manager for QGIS