QGIS API Documentation  2.99.0-Master (d55fa22)
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 #include "qgssettings.h"
24 
25 
27 {
28  QgsSettings settings;
29  return settings.value( "overpass_url", "http://overpass-api.de/api/interpreter" ).toString();
30 }
31 
32 
34 {
35  return QStringLiteral( "(node(%1,%2,%3,%4);<;);out;" ).arg( rect.yMinimum() ).arg( rect.xMinimum() )
36  .arg( rect.yMaximum() ).arg( rect.xMaximum() );
37 }
38 
39 
41  : mServiceUrl( defaultServiceUrl() )
42  , mReply( nullptr )
43 {
44 }
45 
47  : mServiceUrl( defaultServiceUrl() )
48  , mQuery( query )
49  , mReply( nullptr )
50 {
51 }
52 
54 {
55  if ( mReply )
56  {
57  mReply->abort();
58  mReply->deleteLater();
59  mReply = nullptr;
60  }
61 }
62 
63 
65 {
66  mError.clear();
67 
68  if ( mQuery.isEmpty() )
69  {
70  mError = tr( "No query has been specified." );
71  return false;
72  }
73 
74  if ( mReply )
75  {
76  mError = tr( "There is already a pending request for data." );
77  return false;
78  }
79 
80  if ( !mFile.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
81  {
82  mError = tr( "Cannot open output file: %1" ).arg( mFile.fileName() );
83  return false;
84  }
85 
87 
88  QUrl url( mServiceUrl );
89  url.addQueryItem( QStringLiteral( "data" ), mQuery );
90 
91  QNetworkRequest request( url );
92  request.setRawHeader( "User-Agent", "QGIS" );
93 
94  mReply = nwam->get( request );
95 
96  connect( mReply, &QIODevice::readyRead, this, &QgsOSMDownload::onReadyRead );
97  connect( mReply, static_cast < void ( QNetworkReply::* )( QNetworkReply::NetworkError ) >( &QNetworkReply::error ), this, &QgsOSMDownload::onError );
98  connect( mReply, &QNetworkReply::finished, this, &QgsOSMDownload::onFinished );
99  connect( mReply, &QNetworkReply::downloadProgress, this, &QgsOSMDownload::downloadProgress );
100 
101  return true;
102 }
103 
104 
106 {
107  if ( !mReply )
108  return false;
109 
110  mReply->abort();
111  return true;
112 }
113 
114 
115 void QgsOSMDownload::onReadyRead()
116 {
117  Q_ASSERT( mReply );
118 
119  QByteArray data = mReply->read( 1024 * 1024 );
120  mFile.write( data );
121 }
122 
123 
124 void QgsOSMDownload::onFinished()
125 {
126  qDebug( "finished" );
127  Q_ASSERT( mReply );
128 
129  mReply->deleteLater();
130  mReply = nullptr;
131 
132  mFile.close();
133 
134  emit finished();
135 }
136 
137 
138 void QgsOSMDownload::onError( QNetworkReply::NetworkError err )
139 {
140  qDebug( "error: %d", err );
141  Q_ASSERT( mReply );
142 
143  mError = mReply->errorString();
144 }
145 
146 
148 {
149  if ( !mReply )
150  return true;
151 
152  return mReply->isFinished();
153 }
A rectangle specified with double values.
Definition: qgsrectangle.h:38
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.
This class is a composition of two QSettings instances:
Definition: qgssettings.h:54
bool abort()
Aborts current pending request.
QString query() const
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
Returns the y minimum value (bottom side of rectangle).
Definition: qgsrectangle.h:106
double xMaximum() const
Returns the x maximum value (right side of rectangle).
Definition: qgsrectangle.h:91
static QgsNetworkAccessManager * instance()
returns a pointer to the single instance
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), const Section section=NoSection) const
Returns the value for setting key.
static QString defaultServiceUrl()
Return URL of the service that is used by default.
double xMinimum() const
Returns the x minimum value (left side of rectangle).
Definition: qgsrectangle.h:96
double yMaximum() const
Returns the y maximum value (top side of rectangle).
Definition: qgsrectangle.h:101
network access manager for QGIS