30#include "moc_qgsprofileexporter.cpp"
40 if ( std::unique_ptr< QgsAbstractProfileGenerator > generator{ source->createProfileGenerator( mRequest ) } )
41 mGenerators.emplace_back( std::move( generator ) );
50 if ( mGenerators.empty() )
57 mFeatures = renderer.
asFeatures( mType, feedback );
62 if ( mFeatures.empty() )
66 QHash< quint32, QVector< QgsAbstractProfileResults::Feature > > featuresByGeometryType;
69 featuresByGeometryType[
static_cast< quint32
>( feature.geometry.wkbType() )].append( feature );
73 QList< QgsVectorLayer * > res;
74 for (
auto wkbTypeIt = featuresByGeometryType.constBegin(); wkbTypeIt != featuresByGeometryType.constEnd(); ++wkbTypeIt )
78 outputFields.
append(
QgsField( QStringLiteral(
"layer" ), QMetaType::Type::QString ) );
82 for (
auto attributeIt = feature.attributes.constBegin(); attributeIt != feature.attributes.constEnd(); ++attributeIt )
84 const int existingFieldIndex = outputFields.
lookupField( attributeIt.key() );
85 if ( existingFieldIndex < 0 )
87 outputFields.
append(
QgsField( attributeIt.key(),
static_cast<QMetaType::Type
>( attributeIt.value().userType() ) ) );
91 if ( outputFields.
at( existingFieldIndex ).
type() != QMetaType::Type::QString && outputFields.
at( existingFieldIndex ).
type() != attributeIt.value().userType() )
94 outputFields[ existingFieldIndex ].setType( QMetaType::Type::QString );
102 QStringLiteral(
"profile" ),
108 QList< QgsFeature > featuresToAdd;
109 featuresToAdd.reserve( wkbTypeIt.value().size() );
115 for (
auto attributeIt = feature.attributes.constBegin(); attributeIt != feature.attributes.constEnd(); ++attributeIt )
117 const int outputFieldIndex = outputFields.
lookupField( attributeIt.key() );
118 const QgsField &targetField = outputFields.
at( outputFieldIndex );
119 QVariant value = attributeIt.value();
123 featuresToAdd << out;
127 res << outputLayer.release();
139 const QString &destination,
143 , mDestination( destination )
144 , mTransformContext( transformContext )
146 mExporter = std::make_unique< QgsProfileExporter >( sources, request, type );
151 mFeedback = std::make_unique< QgsFeedback >();
153 mExporter->run( mFeedback.get() );
155 mLayers = mExporter->toLayers();
157 if ( mFeedback->isCanceled() )
163 if ( !mDestination.isEmpty() && !mLayers.empty() )
165 const QFileInfo destinationFileInfo( mDestination );
166 const QString fileExtension = destinationFileInfo.completeSuffix();
169 if ( driverName == QLatin1String(
"DXF" ) )
173 QList< QgsDxfExport::DxfLayer > dxfLayers;
177 dxfLayers.append( dxfLayer );
178 if ( layer->crs().isValid() )
182 QFile dxfFile( mDestination );
183 switch ( dxf.
writeToFile( &dxfFile, QStringLiteral(
"UTF-8" ) ) )
187 mCreatedFiles.append( mDestination );
208 QString thisLayerFilename;
210 if ( outputFormatIsMultiLayer )
212 thisLayerFilename = mDestination;
215 if ( mLayers.size() > 1 )
216 options.
layerName = QStringLiteral(
"profile_%1" ).arg( layerCount );
221 if ( mLayers.size() > 1 )
223 thisLayerFilename = QStringLiteral(
"%1/%2_%3.%4" ).arg( destinationFileInfo.path(), destinationFileInfo.baseName() ).arg( layerCount ).arg( fileExtension );
227 thisLayerFilename = mDestination;
246 if ( !mCreatedFiles.contains( newFileName ) )
247 mCreatedFiles.append( newFileName );
277 else if ( mLayers.empty() )
284 layer->moveToThread(
nullptr );
301 QList<QgsVectorLayer *> res;
302 res.reserve( mLayers.size() );
305 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.
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...
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.
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...