QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
Loading...
Searching...
No Matches
qgsvectorwarper.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsgcptransformer.cpp
3 --------------------------------------
4 Date : February 2022
5 Copyright : (C) 2022 by Nyall Dawson
6 Email : nyall dot dawson 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
16#include "qgsvectorwarper.h"
17#include "moc_qgsvectorwarper.cpp"
18#include "qgsfeaturesink.h"
19#include "qgsfeedback.h"
21#include "qgsvectorlayer.h"
22#include "qgsvectorfilewriter.h"
23
24#include <QObject>
25#include <QFileInfo>
26
28 : mMethod( method )
29 , mPoints( points )
30 , mDestinationCrs( destinationCrs )
31{
32
33}
34
36{
37 if ( !sink )
38 return false;
39
40 QVector<QgsPointXY> sourcePoints;
41 sourcePoints.reserve( mPoints.size() );
42 QVector<QgsPointXY> destinationPoints;
43 destinationPoints.reserve( mPoints.size() );
44 for ( const QgsGcpPoint &gcpPoint : mPoints )
45 {
46 sourcePoints << gcpPoint.sourcePoint();
47 destinationPoints << gcpPoint.transformedDestinationPoint( mDestinationCrs, context );
48 }
49
50 if ( feedback && feedback->isCanceled() )
51 return false;
52
53 QgsGcpGeometryTransformer transformer( mMethod, sourcePoints, destinationPoints );
54
55 QgsFeature f;
56
57 long long i = 0;
58 while ( iterator.nextFeature( f ) )
59 {
60 if ( feedback )
61 {
62 if ( feedback->isCanceled() )
63 break;
64
65 feedback->setProcessedCount( i );
66 }
67 i++;
68
69 QgsFeature outputFeature = f;
70 bool ok = false;
71 const QgsGeometry transformed = transformer.transform( f.geometry(), ok, feedback );
72 if ( ok )
73 {
74 outputFeature.setGeometry( transformed );
75 if ( !sink->addFeature( outputFeature, QgsFeatureSink::FastInsert ) )
76 {
77 mError = sink->lastError();
78 return false;
79 }
80 }
81 else
82 {
83 mError = QObject::tr( "An error occurred while transforming a feature" );
84 return false;
85 }
86 }
87 return true;
88}
89
90
91
92//
93// QgsVectorWarperTask
94//
95
97 const QgsCoordinateReferenceSystem &destinationCrs,
98 QgsVectorLayer *layer, const QString &fileName )
99 : QgsTask( tr( "Warping %1" ).arg( fileName ), QgsTask::CanCancel )
100 , mMethod( method )
101 , mPoints( points )
102 , mDestinationCrs( destinationCrs )
103 , mDestFileName( fileName )
104{
105 if ( layer )
106 {
107 mTransformContext = layer->transformContext();
108 mSource.reset( new QgsVectorLayerFeatureSource( layer ) );
109 mFeatureCount = layer->featureCount();
110 mFields = layer->fields();
111 mWkbType = layer->wkbType();
112 }
113}
114
116{
117 if ( mFeedback )
118 mFeedback->cancel();
119
121}
122
124{
125 mFeedback = std::make_unique< QgsFeedback >();
126
128
129 const QString fileExtension = QFileInfo( mDestFileName ).completeSuffix();
130 saveOptions.driverName = QgsVectorFileWriter::driverForExtension( fileExtension );
131
132 std::unique_ptr< QgsVectorFileWriter > exporter( QgsVectorFileWriter::create( mDestFileName, mFields, mWkbType, mDestinationCrs, mTransformContext, saveOptions ) );
133 if ( exporter->hasError() )
134 {
135 mErrorMessage = exporter->errorMessage();
136 mResult = Result::Error;
137 return false;
138 }
139
140 QgsVectorWarper warper( mMethod, mPoints, mDestinationCrs );
141
142 connect( mFeedback.get(), &QgsFeedback::processedCountChanged, this, [ = ]( long long count )
143 {
144 const double newProgress = 100.0 * count / mFeatureCount;
145 // avoid flooding with too many events
146 if ( static_cast< int >( newProgress * 10 ) != static_cast< int >( mLastProgress * 10 ) )
147 {
148 mLastProgress = newProgress;
149 emit progressChanged( newProgress );
150 }
151 } );
152
153 QgsFeatureIterator iterator = mSource->getFeatures();
154 const bool res = warper.transformFeatures( iterator, exporter.get(), mTransformContext, mFeedback.get() );
155 if ( !res )
156 {
157 mErrorMessage = warper.error();
158 mResult = Result::Error;
159 }
160
161 mResult = mFeedback->isCanceled() ? Result::Canceled : Result::Success;
162 mFeedback.reset();
163 return mResult == Result::Success;
164}
165
This class represents a coordinate reference system (CRS).
Contains information about the context in which a coordinate transform is executed.
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
Fetch next feature and stores in f, returns true on success.
An interface for objects which accept features via addFeature(s) methods.
virtual bool addFeature(QgsFeature &feature, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags())
Adds a single feature to the sink.
@ FastInsert
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
virtual QString lastError() const
Returns the most recent error encountered by the sink, e.g.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
QgsGeometry geometry
Definition qgsfeature.h:69
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition qgsfeedback.h:44
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:53
void setProcessedCount(unsigned long long processedCount)
Sets the current processed objects count for the feedback object.
Definition qgsfeedback.h:96
void processedCountChanged(unsigned long long processedCount)
Emitted when the feedback object reports a change in the number of processed objects.
A geometry transformer which uses an underlying Ground Control Points (GCP) based transformation to m...
QgsGeometry transform(const QgsGeometry &geometry, bool &ok, QgsFeedback *feedback=nullptr)
Transforms the specified input geometry using the GCP based transform.
Contains properties of a ground control point (GCP).
Definition qgsgcppoint.h:31
TransformMethod
Available transformation methods.
A geometry is the spatial representation of a feature.
QgsCoordinateTransformContext transformContext() const
Returns the layer data provider coordinate transform context or a default transform context if the la...
Abstract base class for long running background tasks.
virtual void cancel()
Notifies the task that it should terminate.
Options to pass to writeAsVectorFormat()
static QString driverForExtension(const QString &extension)
Returns the OGR driver name for a specified file extension.
static QgsVectorFileWriter * create(const QString &fileName, const QgsFields &fields, Qgis::WkbType geometryType, const QgsCoordinateReferenceSystem &srs, const QgsCoordinateTransformContext &transformContext, const QgsVectorFileWriter::SaveVectorOptions &options, QgsFeatureSink::SinkFlags sinkFlags=QgsFeatureSink::SinkFlags(), QString *newFilename=nullptr, QString *newLayer=nullptr)
Create a new vector file writer.
Partial snapshot of vector layer's state (only the members necessary for access to features)
Represents a vector layer which manages a vector based data sets.
long long featureCount(const QString &legendKey) const
Number of features rendered with specified legend key.
Q_INVOKABLE Qgis::WkbType wkbType() const FINAL
Returns the WKBType or WKBUnknown in case of error.
bool run() override
Performs the task's operation.
@ Error
An error occurred while warping.
void cancel() override
Notifies the task that it should terminate.
QgsVectorWarperTask(QgsGcpTransformerInterface::TransformMethod method, const QList< QgsGcpPoint > &points, const QgsCoordinateReferenceSystem &destinationCrs, QgsVectorLayer *layer, const QString &fileName)
Constructor for QgsVectorWarperTask.
Vector layer warper which warps vector layers based on a list of source and destination GCPs.
bool transformFeatures(QgsFeatureIterator &iterator, QgsFeatureSink *sink, const QgsCoordinateTransformContext &context, QgsFeedback *feedback=nullptr) const
Transforms the features from iterator and adds the results to the specified sink.
QgsVectorWarper(QgsGcpTransformerInterface::TransformMethod method, const QList< QgsGcpPoint > &points, const QgsCoordinateReferenceSystem &destinationCrs)
Constructor for QgsVectorWarper.