37 if ( std::unique_ptr< QgsAbstractProfileGenerator > generator{ source->createProfileGenerator( mRequest ) } )
38 mGenerators.emplace_back( std::move( generator ) );
47 if ( mGenerators.empty() )
54 mFeatures = renderer.
asFeatures( mType, feedback );
59 if ( mFeatures.empty() )
63 QHash< quint32, QVector< QgsAbstractProfileResults::Feature > > featuresByGeometryType;
66 featuresByGeometryType[
static_cast< quint32
>( feature.geometry.wkbType() )].append( feature );
70 QList< QgsVectorLayer * > res;
71 for (
auto wkbTypeIt = featuresByGeometryType.constBegin(); wkbTypeIt != featuresByGeometryType.constEnd(); ++wkbTypeIt )
75 outputFields.
append(
QgsField( QStringLiteral(
"layer" ), QMetaType::Type::QString ) );
79 for (
auto attributeIt = feature.attributes.constBegin(); attributeIt != feature.attributes.constEnd(); ++attributeIt )
81 const int existingFieldIndex = outputFields.
lookupField( attributeIt.key() );
82 if ( existingFieldIndex < 0 )
84 outputFields.
append(
QgsField( attributeIt.key(),
static_cast<QMetaType::Type
>( attributeIt.value().userType() ) ) );
88 if ( outputFields.
at( existingFieldIndex ).
type() != QMetaType::Type::QString && outputFields.
at( existingFieldIndex ).
type() != attributeIt.value().userType() )
91 outputFields[ existingFieldIndex ].setType( QMetaType::Type::QString );
99 QStringLiteral(
"profile" ),
105 QList< QgsFeature > featuresToAdd;
106 featuresToAdd.reserve( wkbTypeIt.value().size() );
112 for (
auto attributeIt = feature.attributes.constBegin(); attributeIt != feature.attributes.constEnd(); ++attributeIt )
114 const int outputFieldIndex = outputFields.
lookupField( attributeIt.key() );
115 const QgsField &targetField = outputFields.
at( outputFieldIndex );
116 QVariant value = attributeIt.value();
120 featuresToAdd << out;
124 res << outputLayer.release();
136 const QString &destination,
139 :
QgsTask( tr(
"Exporting elevation profile" ),
QgsTask::CanCancel )
140 , mDestination( destination )
141 , mTransformContext( transformContext )
143 mExporter = std::make_unique< QgsProfileExporter >( sources, request, type );
148 mFeedback = std::make_unique< QgsFeedback >();
150 mExporter->run( mFeedback.get() );
152 mLayers = mExporter->toLayers();
154 if ( mFeedback->isCanceled() )
160 if ( !mDestination.isEmpty() && !mLayers.empty() )
162 const QFileInfo destinationFileInfo( mDestination );
163 const QString fileExtension = destinationFileInfo.completeSuffix();
166 if ( driverName == QLatin1String(
"DXF" ) )
170 QList< QgsDxfExport::DxfLayer > dxfLayers;
174 dxfLayers.append( dxfLayer );
175 if ( layer->crs().isValid() )
179 QFile dxfFile( mDestination );
180 switch ( dxf.
writeToFile( &dxfFile, QStringLiteral(
"UTF-8" ) ) )
184 mCreatedFiles.append( mDestination );
205 QString thisLayerFilename;
207 if ( outputFormatIsMultiLayer )
209 thisLayerFilename = mDestination;
212 if ( mLayers.size() > 1 )
213 options.
layerName = QStringLiteral(
"profile_%1" ).arg( layerCount );
218 if ( mLayers.size() > 1 )
220 thisLayerFilename = QStringLiteral(
"%1/%2_%3.%4" ).arg( destinationFileInfo.path(), destinationFileInfo.baseName() ).arg( layerCount ).arg( fileExtension );
224 thisLayerFilename = mDestination;
243 if ( !mCreatedFiles.contains( newFileName ) )
244 mCreatedFiles.append( newFileName );
274 else if ( mLayers.empty() )
281 layer->moveToThread(
nullptr );
298 QList<QgsVectorLayer *> res;
299 res.reserve( mLayers.size() );
302 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.
This class 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.
static QgsVectorLayer * createMemoryLayer(const QString &name, const QgsFields &fields, Qgis::WkbType geometryType=Qgis::WkbType::NoGeometry, const QgsCoordinateReferenceSystem &crs=QgsCoordinateReferenceSystem(), bool loadDefaultStyle=true) SIP_FACTORY
Creates a new memory layer using the specified parameters.
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...
QgsCoordinateReferenceSystem crs() const
Returns the desired Coordinate Reference System for the profile.
Abstract base class for long running background tasks.
virtual void cancel()
Notifies the task that it should terminate.
Options to pass to 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 (since QGIS 3.32)
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 data sets.
Encapsulates information about a feature exported from the profile results.
Layers and optional attribute index to split into multiple layers using attribute value as layer name...