31#include "moc_qgsprofileexporter.cpp"
33using namespace Qt::StringLiterals;
43 if ( std::unique_ptr< QgsAbstractProfileGenerator > generator { source->createProfileGenerator( mRequest ) } )
44 mGenerators.emplace_back( std::move( generator ) );
53 if ( mGenerators.empty() )
60 mFeatures = renderer.
asFeatures( mType, feedback );
65 if ( mFeatures.empty() )
69 QHash< quint32, QVector< QgsAbstractProfileResults::Feature > > featuresByGeometryType;
72 featuresByGeometryType[
static_cast< quint32
>( feature.geometry.wkbType() )].append( feature );
76 QList< QgsVectorLayer * > res;
77 for (
auto wkbTypeIt = featuresByGeometryType.constBegin(); wkbTypeIt != featuresByGeometryType.constEnd(); ++wkbTypeIt )
81 outputFields.
append(
QgsField( u
"layer"_s, QMetaType::Type::QString ) );
85 for (
auto attributeIt = feature.attributes.constBegin(); attributeIt != feature.attributes.constEnd(); ++attributeIt )
87 const int existingFieldIndex = outputFields.
lookupField( attributeIt.key() );
88 if ( existingFieldIndex < 0 )
90 outputFields.
append(
QgsField( attributeIt.key(),
static_cast<QMetaType::Type
>( attributeIt.value().userType() ) ) );
94 if ( outputFields.
at( existingFieldIndex ).
type() != QMetaType::Type::QString && outputFields.
at( existingFieldIndex ).
type() != attributeIt.value().userType() )
97 outputFields[existingFieldIndex].setType( QMetaType::Type::QString );
104 std::unique_ptr< QgsVectorLayer > outputLayer(
109 QList< QgsFeature > featuresToAdd;
110 featuresToAdd.reserve( wkbTypeIt.value().size() );
116 for (
auto attributeIt = feature.attributes.constBegin(); attributeIt != feature.attributes.constEnd(); ++attributeIt )
118 const int outputFieldIndex = outputFields.
lookupField( attributeIt.key() );
119 const QgsField &targetField = outputFields.
at( outputFieldIndex );
120 QVariant value = attributeIt.value();
124 featuresToAdd << out;
129 QgsDebugError( u
"Error exporting feature: %1"_s.arg( outputLayer->dataProvider()->lastError() ) );
131 res << outputLayer.release();
144 , mDestination( destination )
145 , mTransformContext( transformContext )
147 mExporter = std::make_unique< QgsProfileExporter >( sources, request, type );
152 mFeedback = std::make_unique< QgsFeedback >();
154 mExporter->run( mFeedback.get() );
156 mLayers = mExporter->toLayers();
158 if ( mFeedback->isCanceled() )
164 if ( !mDestination.isEmpty() && !mLayers.empty() )
166 const QFileInfo destinationFileInfo( mDestination );
167 const QString fileExtension = destinationFileInfo.completeSuffix();
170 if ( driverName ==
"DXF"_L1 )
174 QList< QgsDxfExport::DxfLayer > dxfLayers;
178 dxfLayers.append( dxfLayer );
179 if ( layer->crs().isValid() )
183 QFile dxfFile( mDestination );
188 mCreatedFiles.append( mDestination );
209 QString thisLayerFilename;
211 if ( outputFormatIsMultiLayer )
213 thisLayerFilename = mDestination;
215 if ( mLayers.size() > 1 )
216 options.
layerName = u
"profile_%1"_s.arg( layerCount );
221 if ( mLayers.size() > 1 )
223 thisLayerFilename = u
"%1/%2_%3.%4"_s.arg( destinationFileInfo.path(), destinationFileInfo.baseName() ).arg( layerCount ).arg( fileExtension );
227 thisLayerFilename = mDestination;
239 if ( !mCreatedFiles.contains( newFileName ) )
240 mCreatedFiles.append( newFileName );
270 else if ( mLayers.empty() )
277 layer->moveToThread(
nullptr );
294 QList<QgsVectorLayer *> res;
295 res.reserve( mLayers.size() );
298 layer->moveToThread( QThread::currentThread() );
ProfileExportType
Types of export for elevation profiles.
@ Profile2D
Export profiles as 2D profile lines, with elevation stored in exported geometry Y dimension and dista...
WkbType
The WKB type describes the number of dimensions a geometry has.
Interface for classes which can generate elevation profiles.
Represents a coordinate reference system (CRS).
Contains information about the context in which a coordinate transform is executed.
Exports QGIS layers to the DXF format.
@ DeviceNotWritableError
Device not writable error.
@ Success
Successful export.
@ EmptyExtentError
Empty extent, no extent given and no extent could be derived from layers.
@ InvalidDeviceError
Invalid device error.
ExportResult writeToFile(QIODevice *d, const QString &codec)
Export to a dxf file in the given encoding.
void setDestinationCrs(const QgsCoordinateReferenceSystem &crs)
Set destination CRS.
void addLayers(const QList< QgsDxfExport::DxfLayer > &layers)
Add layers to export.
@ FastInsert
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Q_INVOKABLE bool setAttribute(int field, const QVariant &attr)
Sets an attribute's value by field index.
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.
Encapsulate a field in an attribute table or data source.
bool convertCompatible(QVariant &v, QString *errorMessage=nullptr) const
Converts the provided variant to a compatible format.
Container of fields for a vector layer.
bool append(const QgsField &field, Qgis::FieldOrigin origin=Qgis::FieldOrigin::Provider, int originIndex=-1)
Appends a field.
QgsField at(int i) const
Returns the field at particular index (must be in range 0..N-1).
Q_INVOKABLE int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
Utility functions for use with the memory vector data provider.
ExportResult
Results of exporting the profile.
@ Canceled
Export was canceled.
@ Success
Successful export.
@ LayerExportFailed
Generic error when outputting to files.
@ DxfExportFailed
Generic error when outputting to DXF.
@ DeviceError
Could not open output file device.
@ Empty
Results were empty.
QgsProfileExporterTask(const QList< QgsAbstractProfileSource * > &sources, const QgsProfileRequest &request, Qgis::ProfileExportType type, const QString &destination, const QgsCoordinateTransformContext &transformContext)
Constructor for QgsProfileExporterTask, saving results to the specified destination file.
bool run() override
Performs the task's operation.
QgsProfileExporterTask::ExportResult result() const
Returns the result of the export operation.
void cancel() override
Notifies the task that it should terminate.
QList< QgsVectorLayer * > takeLayers()
Returns a list of vector layer containing the exported profile results.
void run(QgsFeedback *feedback=nullptr)
Runs the profile generation.
QgsProfileExporter(const QList< QgsAbstractProfileSource * > &sources, const QgsProfileRequest &request, Qgis::ProfileExportType type)
Constructor for QgsProfileExporter, using the provided list of profile sources to generate the result...
QList< QgsVectorLayer * > toLayers()
Returns a list of vector layer containing the exported profile results.
Generates and renders elevation profile plots.
QVector< QgsAbstractProfileResults::Feature > asFeatures(Qgis::ProfileExportType type, QgsFeedback *feedback=nullptr)
Exports the profile results as a set of features.
void startGeneration()
Start the generation job and immediately return.
void waitForFinished()
Block until the current job has finished.
Encapsulates properties and constraints relating to fetching elevation profiles from different source...
virtual void cancel()
Notifies the task that it should terminate.
QgsTask(const QString &description=QString(), QgsTask::Flags flags=AllFlags)
Constructor for QgsTask.
@ CanCancel
Task can be canceled.
Options to pass to QgsVectorFileWriter::writeAsVectorFormat().
QString fileEncoding
Encoding to use.
QString driverName
OGR driver to use.
QString layerName
Layer name. If let empty, it will be derived from the filename.
QgsVectorFileWriter::ActionOnExistingFile actionOnExistingFile
Action on existing file.
QgsFeedback * feedback
Optional feedback object allowing cancellation of layer save.
static QgsVectorFileWriter::WriterError writeAsVectorFormatV3(QgsVectorLayer *layer, const QString &fileName, const QgsCoordinateTransformContext &transformContext, const QgsVectorFileWriter::SaveVectorOptions &options, QString *errorMessage=nullptr, QString *newFilename=nullptr, QString *newLayer=nullptr)
Writes a layer out to a vector file.
@ ErrAttributeTypeUnsupported
@ Canceled
Writing was interrupted by manual cancellation.
@ ErrAttributeCreationFailed
@ ErrSavingMetadata
Metadata saving failed.
static QString driverForExtension(const QString &extension)
Returns the OGR driver name for a specified file extension.
@ SupportsMultipleLayers
Filter to only formats which support multiple layers.
static QStringList supportedFormatExtensions(VectorFormatOptions options=SortRecommended)
Returns a list of file extensions for supported formats, e.g "shp", "gpkg".
@ CreateOrOverwriteLayer
Create or overwrite layer.
@ CreateOrOverwriteFile
Create or overwrite file.
Represents a vector layer which manages a vector based dataset.
#define QgsDebugError(str)
Encapsulates information about a feature exported from the profile results.
Encapsulates the properties of a vector layer containing features that will be exported to the DXF fi...