Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
316 additions
and
17 deletions.
There are no files selected for viewing
117 changes: 117 additions & 0 deletions
117
python/core/auto_generated/processing/qgsprocessingparametervectortilewriterlayers.sip.in
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/************************************************************************ | ||
* This file has been generated automatically from * | ||
* * | ||
* src/core/processing/qgsprocessingparametervectortilewriterlayers.h * | ||
* * | ||
* Do not edit manually ! Edit header and run scripts/sipify.pl again * | ||
************************************************************************/ | ||
|
||
|
||
|
||
|
||
class QgsProcessingParameterVectorTileWriterLayers : QgsProcessingParameterDefinition | ||
{ | ||
%Docstring | ||
A parameter for processing algorithms that need a list of input vector layers for writing | ||
of vector tiles - this parameter provides processing framework's adapter for QList<QgsVectorTileWriter.Layer>. | ||
|
||
A valid value for this parameter is a list (QVariantList), where each item is a map (QVariantMap) in this form: | ||
{ | ||
'layer': <string> or <QgsMapLayer>, | ||
// key-value pairs below are optional | ||
'layerName': <string>, | ||
'filterExpression': <string>, | ||
'minZoom': <int>, | ||
'maxZoom': <int> | ||
} | ||
|
||
Static functions parametersAsLayers(), variantMapAsLayer(), layerAsVariantMap() provide conversion between | ||
QgsVectorTileWriter.Layer representation and QVariant representation. | ||
|
||
.. note:: | ||
|
||
This class is not a part of public API. | ||
|
||
.. versionadded:: 3.14 | ||
%End | ||
|
||
%TypeHeaderCode | ||
#include "qgsprocessingparametervectortilewriterlayers.h" | ||
%End | ||
public: | ||
QgsProcessingParameterVectorTileWriterLayers( const QString &name, const QString &description = QString() ); | ||
%Docstring | ||
Constructor for QgsProcessingParameterVectorTileWriterLayers. | ||
%End | ||
|
||
virtual QgsProcessingParameterDefinition *clone() const; | ||
|
||
virtual QString type() const; | ||
|
||
virtual bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context ) const; | ||
|
||
virtual QString valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const; | ||
|
||
virtual QString asPythonString( QgsProcessing::PythonOutputType outputType = QgsProcessing::PythonQgsProcessingAlgorithmSubclass ) const; | ||
|
||
|
||
static QString typeName(); | ||
%Docstring | ||
Returns the type name for the parameter class. | ||
%End | ||
|
||
static QList<QgsVectorTileWriter::Layer> parameterAsLayers( const QVariant &layersVariant, QgsProcessingContext &context ); | ||
%Docstring | ||
Converts a QVariant value (a QVariantList) to a list of input layers | ||
%End | ||
static QgsVectorTileWriter::Layer variantMapAsLayer( const QVariantMap &layerVariantMap, QgsProcessingContext &context ); | ||
%Docstring | ||
Converts a QVariant value (a QVariantMap) to a single input layer | ||
%End | ||
static QVariantMap layerAsVariantMap( const QgsVectorTileWriter::Layer &layer ); | ||
%Docstring | ||
Converts a single input layer to QVariant representation (a QVariantMap) | ||
%End | ||
|
||
}; | ||
|
||
|
||
class QgsProcessingParameterTypeVectorTileWriterLayers : QgsProcessingParameterType | ||
{ | ||
%Docstring | ||
Parameter type definition for QgsProcessingParameterVectorTileWriterLayers. | ||
|
||
.. note:: | ||
|
||
This class is not a part of public API. | ||
|
||
.. versionadded:: 3.14 | ||
%End | ||
|
||
%TypeHeaderCode | ||
#include "qgsprocessingparametervectortilewriterlayers.h" | ||
%End | ||
public: | ||
virtual QgsProcessingParameterDefinition *create( const QString &name ) const /Factory/; | ||
|
||
virtual QString description() const; | ||
|
||
virtual QString name() const; | ||
|
||
virtual QString id() const; | ||
|
||
virtual QString pythonImportString() const; | ||
|
||
virtual QString className() const; | ||
|
||
virtual QStringList acceptedPythonTypes() const; | ||
}; | ||
|
||
|
||
/************************************************************************ | ||
* This file has been generated automatically from * | ||
* * | ||
* src/core/processing/qgsprocessingparametervectortilewriterlayers.h * | ||
* * | ||
* Do not edit manually ! Edit header and run scripts/sipify.pl again * | ||
************************************************************************/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
|
||
#include "qgsnetworkaccessmanager.h" | ||
|
||
class TileReply : public QObject | ||
{ | ||
QOBJECT | ||
public: | ||
TileReply( QString url ): mUrl( url ) {} | ||
~TileReply() | ||
{ | ||
// TODO: notify manager | ||
//if ( active ) | ||
// DownloadManager::cancelRequest( this ); | ||
} | ||
QString url() const { return mUrl; } | ||
|
||
//void setParentReply( QNetworkReply *networkReply ) { mNetworkReply = networkReply; } | ||
//QNetworkReply *parentReply() const { return mNetworkReply; } | ||
|
||
void setData( const QByteArray &data ) { mData = data; } | ||
QByteArray data() const { return mData; } | ||
|
||
signals: | ||
void finished(); | ||
|
||
private: | ||
QString mUrl; | ||
//QNetworkReply *mNetworkReply = nullptr; | ||
QByteArray mData; | ||
}; | ||
|
||
class DownloadManager; | ||
|
||
class Worker : public QObject | ||
{ | ||
QOBJECT | ||
|
||
public slots: | ||
|
||
void request( QString url ) | ||
{ | ||
// this is only called if such request does not exist already | ||
|
||
QNetworkRequest request( url ); | ||
request.setAttribute( ( QNetworkRequest::Attribute )( QNetworkRequest::User + 1 ), url ); | ||
QNetworkReply *networkReply = QgsNetworkAccessManager::instance()->get( request ); | ||
connect( networkReply, &QNetworkReply::finished, this, &Worker::tileReplyFinished ); | ||
} | ||
|
||
void cancelRequest( QString url ) | ||
{ | ||
// this is only called if such request has been requested here | ||
|
||
QNetworkReply *r = mNetworkReplies.take( url ); | ||
r->abort(); | ||
} | ||
|
||
private slots: | ||
void tileReplyFinished() | ||
{ | ||
QNetworkReply *reply = qobject_cast<QNetworkReply *>( sender() ); | ||
|
||
QString url = reply->request().attribute( ( QNetworkRequest::Attribute )( QNetworkRequest::User + 1 ) ).toString(); | ||
QByteArray data = reply->readAll(); | ||
|
||
mNetworkReplies.remove( url ); | ||
reply->deleteLater(); | ||
|
||
// there may be multiple replies waiting for the same network request | ||
QMutexLocker locker( &DownloadManager::mutex ); | ||
const QList<TileReply *> tileReplies = DownloadManager::replies[url]; | ||
for ( TileReply *rr : tileReplies ) | ||
{ | ||
rr->setData( data ); | ||
QMetaObject::invokeMethod( rr, "finished" ); | ||
} | ||
} | ||
|
||
private: | ||
QMap<QString, QNetworkReply *> mNetworkReplies; | ||
|
||
}; | ||
|
||
// | ||
// TODO: | ||
// - DownloadManager - singleton? | ||
// - auto-kill thread after some time? | ||
// - auto-start thread when needed | ||
// - in-memory caching | ||
// | ||
|
||
/* | ||
* singleton that lives in the main thread. | ||
*/ | ||
class DownloadManager | ||
{ | ||
public: | ||
|
||
static void startThread() | ||
{ | ||
worker = new Worker; | ||
worker->moveToThread( &workerThread ); | ||
QObject::connect( &workerThread, &QThread::finished, worker, &QObject::deleteLater ); | ||
workerThread.start(); | ||
} | ||
|
||
static void stopThread() | ||
{ | ||
workerThread.quit(); | ||
workerThread.wait(); | ||
} | ||
|
||
//! thread-safe | ||
static TileReply *requestTile( const QString &url ) | ||
{ | ||
QMutexLocker locker( &mutex ); | ||
|
||
TileReply *reply = new TileReply( url ); // lives in the same thread as the caller | ||
|
||
if ( replies.contains( url ) ) | ||
{ | ||
// no extra work to do - previous reply is not finished yet | ||
replies[url].append( reply ); | ||
} | ||
else | ||
{ | ||
replies[url] = QList<TileReply *>() << reply; | ||
// asynchronously request network request | ||
QMetaObject::invokeMethod( worker, "request", Qt::QueuedConnection, Q_ARG( QString, url ) ); | ||
} | ||
|
||
return reply; | ||
} | ||
|
||
//! thread-safe | ||
static void cancelTileRequest( TileReply *reply ) | ||
{ | ||
QMutexLocker locker( &mutex ); | ||
|
||
QString url = reply->url(); | ||
|
||
replies[url].removeOne( reply ); | ||
|
||
if ( replies[url].isEmpty() ) | ||
{ | ||
replies.remove( url ); | ||
// also make sure the underlying request is cancelled | ||
QMetaObject::invokeMethod( worker, "cancelRequest", Qt::QueuedConnection, Q_ARG( QString, url ) ); | ||
} | ||
} | ||
|
||
private: | ||
static QThread workerThread; // will run its event loop | ||
static Worker *worker; | ||
static QMutex mutex; // protecting data structures with replies | ||
static QMap<QString, QList<TileReply *> > replies; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.