26#include <QApplication>
31#include "moc_qgspointcloudlayerexporter.cpp"
35#include <pdal/StageFactory.hpp>
36#include <pdal/io/BufferReader.hpp>
37#include <pdal/Dimension.hpp>
45 return QStringLiteral(
"GPKG" );
47 return QStringLiteral(
"DXF" );
49 return QStringLiteral(
"ESRI Shapefile" );
51 return QStringLiteral(
"CSV" );
60 : mLayerAttributeCollection( layer->
attributes() )
61 , mIndex( layer->index() )
66 mPointRecordFormat = layer->
dataProvider()->originalMetadata().value( QStringLiteral(
"dataformat_id" ) ).toInt( &ok );
68 mPointRecordFormat = 3;
92 mFilterGeometryEngine = std::make_unique<QgsGeos>( geometry );
93 mFilterGeometryEngine->prepareGeometry();
102 QVector< QgsGeometry > allGeometries;
105 if ( selectedFeaturesOnly )
117 allGeometries.append( f.
geometry() );
127 QgsDebugError( QStringLiteral(
"Error transforming union of filter layer: %1" ).arg( cse.
what() ) );
128 QgsDebugError( QStringLiteral(
"FilterGeometry will be ignored." ) );
136 mRequestedAttributes.clear();
138 const QVector<QgsPointCloudAttribute> allAttributes = mLayerAttributeCollection.attributes();
142 if ( attribute.name().compare( QLatin1String(
"X" ), Qt::CaseInsensitive ) &&
143 attribute.name().compare( QLatin1String(
"Y" ), Qt::CaseInsensitive ) &&
144 attribute.name().compare( QLatin1String(
"Z" ), Qt::CaseInsensitive ) &&
145 attributeList.contains( attribute.name() ) &&
146 ! mRequestedAttributes.contains( attribute.name() ) )
148 mRequestedAttributes.append( attribute.name() );
155 QStringList allAttributeNames;
156 const QVector<QgsPointCloudAttribute> allAttributes = mLayerAttributeCollection.attributes();
159 allAttributeNames.append( attribute.name() );
166 const QVector<QgsPointCloudAttribute> allAttributes = mLayerAttributeCollection.
attributes();
171 if ( attribute.name().compare( QLatin1String(
"X" ), Qt::CaseInsensitive ) ||
172 attribute.name().compare( QLatin1String(
"Y" ), Qt::CaseInsensitive ) ||
173 attribute.name().compare( QLatin1String(
"Z" ), Qt::CaseInsensitive ) ||
174 mRequestedAttributes.contains( attribute.name(), Qt::CaseInsensitive ) )
176 requestAttributes.
push_back( attribute );
179 return requestAttributes;
182QgsFields QgsPointCloudLayerExporter::outputFields()
184 const QVector<QgsPointCloudAttribute>
attributes = mLayerAttributeCollection.attributes();
187 for (
const QgsPointCloudAttribute &attribute :
attributes )
189 if ( mRequestedAttributes.contains( attribute.name(), Qt::CaseInsensitive ) )
190 fields.
append( QgsField( attribute.name(), attribute.variantType(), attribute.displayType() ) );
198 mMemoryLayer =
nullptr;
203 if ( QApplication::instance()->thread() != QThread::currentThread() )
205 QgsDebugMsgLevel( QStringLiteral(
"prepareExport() should better be called from the main thread!" ), 2 );
216 if ( mExtent.isFinite() )
224 QgsDebugError( QStringLiteral(
"Error transforming extent: %1" ).arg( cse.
what() ) );
228 QStringList layerCreationOptions;
237 ExporterMemory exp(
this );
249 ExporterPdal exp(
this );
252 catch ( std::runtime_error &e )
254 setLastError( QString::fromLatin1( e.what() ) );
255 QgsDebugError( QStringLiteral(
"PDAL has thrown an exception: {}" ).arg( e.what() ) );
262 layerCreationOptions << QStringLiteral(
"GEOMETRY=AS_XYZ" )
263 << QStringLiteral(
"SEPARATOR=COMMA" );
280 ExporterVector exp(
this );
294 mMemoryLayer =
nullptr;
300 const QFileInfo fileInfo( mFilename );
301 return new QgsPointCloudLayer( mFilename, fileInfo.completeBaseName(), QStringLiteral(
"pdal" ) );
306 QString uri( mFilename );
307 uri +=
"|layername=" + mName;
315 const QFileInfo fileInfo( mFilename );
316 return new QgsVectorLayer( mFilename, fileInfo.completeBaseName(), QStringLiteral(
"ogr" ) );
326void QgsPointCloudLayerExporter::ExporterBase::run()
328 QgsRectangle geometryFilterRectangle( -std::numeric_limits<double>::infinity(),
329 -std::numeric_limits<double>::infinity(),
330 std::numeric_limits<double>::infinity(),
331 std::numeric_limits<double>::infinity(),
333 if ( mParent->mFilterGeometryEngine )
340 QVector<QgsPointCloudNodeId> nodes;
341 qint64 pointCount = 0;
342 QQueue<QgsPointCloudNodeId> queue;
343 queue.push_back( mParent->mIndex.root() );
344 while ( !queue.empty() )
346 QgsPointCloudNode node = mParent->mIndex.getNode( queue.front() );
348 const QgsBox3D nodeBounds = node.
bounds();
349 if ( mParent->mExtent.intersects( nodeBounds.
toRectangle() ) &&
350 mParent->mZRange.overlaps( { nodeBounds.zMinimum(), nodeBounds.zMaximum() } ) &&
351 geometryFilterRectangle.intersects( nodeBounds.
toRectangle() ) )
354 nodes.push_back( node.
id() );
356 for (
const QgsPointCloudNodeId &child : node.
children() )
358 queue.push_back( child );
362 const qint64 pointsToExport = mParent->mPointsLimit > 0 ? std::min( mParent->mPointsLimit, pointCount ) : pointCount;
363 QgsPointCloudRequest request;
364 request.
setAttributes( mParent->requestedAttributeCollection() );
365 std::unique_ptr<QgsPointCloudBlock> block =
nullptr;
366 qint64 pointsExported = 0;
367 for (
const QgsPointCloudNodeId &node : nodes )
369 block = mParent->mIndex.nodeData( node, request );
370 const QgsPointCloudAttributeCollection attributesCollection = block->attributes();
371 const char *ptr = block->data();
372 int count = block->pointCount();
374 const QgsVector3D scale = block->scale();
375 const QgsVector3D offset = block->offset();
376 int xOffset = 0, yOffset = 0, zOffset = 0;
380 for (
int i = 0; i < count; ++i )
383 if ( mParent->mFeedback &&
386 if ( pointsToExport > 0 )
388 mParent->mFeedback->setProgress( 100 *
static_cast< float >( pointsExported ) / pointsToExport );
390 if ( mParent->mFeedback->isCanceled() )
392 mParent->setLastError( QObject::tr(
"Canceled by user" ) );
397 if ( pointsExported >= pointsToExport )
407 if ( ! mParent->mZRange.contains( z ) ||
408 ! mParent->mExtent.contains( x, y ) ||
409 ( mParent->mFilterGeometryEngine && ! mParent->mFilterGeometryEngine->contains( x, y ) ) )
416 mParent->mTransform->transformInPlace( x, y, z );
418 handlePoint( x, y, z, attributeMap, pointsExported );
421 catch (
const QgsCsException &cse )
423 QgsDebugError( QStringLiteral(
"Error transforming point: %1" ).arg( cse.
what() ) );
440QgsPointCloudLayerExporter::ExporterMemory::~ExporterMemory()
442 mParent->mMemoryLayer->moveToThread( QApplication::instance()->thread() );
445void QgsPointCloudLayerExporter::ExporterMemory::handlePoint(
double x,
double y,
double z,
const QVariantMap &map,
const qint64 pointNumber )
447 Q_UNUSED( pointNumber )
450 feature.
setGeometry( QgsGeometry(
new QgsPoint( x, y, z ) ) );
451 QgsAttributes featureAttributes;
452 for (
const QString &attribute : std::as_const( mParent->mRequestedAttributes ) )
454 const double val = map[ attribute ].toDouble();
455 featureAttributes.append( val );
458 mFeatures.append( feature );
461void QgsPointCloudLayerExporter::ExporterMemory::handleNode()
463 QgsVectorLayer *vl = qgis::down_cast<QgsVectorLayer *>( mParent->mMemoryLayer );
474void QgsPointCloudLayerExporter::ExporterMemory::handleAll()
488QgsPointCloudLayerExporter::ExporterVector::~ExporterVector()
490 delete mParent->mVectorSink;
491 mParent->mVectorSink =
nullptr;
494void QgsPointCloudLayerExporter::ExporterVector::handlePoint(
double x,
double y,
double z,
const QVariantMap &map,
const qint64 pointNumber )
496 Q_UNUSED( pointNumber )
499 feature.
setGeometry( QgsGeometry(
new QgsPoint( x, y, z ) ) );
500 QgsAttributes featureAttributes;
501 for (
const QString &attribute : std::as_const( mParent->mRequestedAttributes ) )
503 const double val = map[ attribute ].toDouble();
504 featureAttributes.append( val );
507 mFeatures.append( feature );
510void QgsPointCloudLayerExporter::ExporterVector::handleNode()
512 if ( ! mParent->mVectorSink->addFeatures( mFeatures ) )
514 mParent->setLastError( mParent->mVectorSink->lastError() );
519void QgsPointCloudLayerExporter::ExporterVector::handleAll()
531 : mPointFormat( exp->mPointRecordFormat )
535 mOptions.add(
"filename", mParent->mFilename.toStdString() );
536 mOptions.add(
"a_srs", mParent->mTargetCrs.toWkt().toStdString() );
537 mOptions.add(
"minor_version", QStringLiteral(
"4" ).toStdString() );
538 mOptions.add(
"format", QString::number( mPointFormat ).toStdString() );
539 if ( mParent->mTransform->isShortCircuited() )
541 mOptions.add(
"offset_x", QString::number( mParent->mIndex.offset().x() ).toStdString() );
542 mOptions.add(
"offset_y", QString::number( mParent->mIndex.offset().y() ).toStdString() );
543 mOptions.add(
"offset_z", QString::number( mParent->mIndex.offset().z() ).toStdString() );
544 mOptions.add(
"scale_x", QString::number( mParent->mIndex.scale().x() ).toStdString() );
545 mOptions.add(
"scale_y", QString::number( mParent->mIndex.scale().y() ).toStdString() );
546 mOptions.add(
"scale_z", QString::number( mParent->mIndex.scale().z() ).toStdString() );
549 mTable.layout()->registerDim( pdal::Dimension::Id::X );
550 mTable.layout()->registerDim( pdal::Dimension::Id::Y );
551 mTable.layout()->registerDim( pdal::Dimension::Id::Z );
553 mTable.layout()->registerDim( pdal::Dimension::Id::Classification );
554 mTable.layout()->registerDim( pdal::Dimension::Id::Intensity );
555 mTable.layout()->registerDim( pdal::Dimension::Id::ReturnNumber );
556 mTable.layout()->registerDim( pdal::Dimension::Id::NumberOfReturns );
557 mTable.layout()->registerDim( pdal::Dimension::Id::ScanDirectionFlag );
558 mTable.layout()->registerDim( pdal::Dimension::Id::EdgeOfFlightLine );
559 mTable.layout()->registerDim( pdal::Dimension::Id::ScanAngleRank );
560 mTable.layout()->registerDim( pdal::Dimension::Id::UserData );
561 mTable.layout()->registerDim( pdal::Dimension::Id::PointSourceId );
563 if ( mPointFormat == 6 || mPointFormat == 7 || mPointFormat == 8 || mPointFormat == 9 || mPointFormat == 10 )
565 mTable.layout()->registerDim( pdal::Dimension::Id::ScanChannel );
566 mTable.layout()->registerDim( pdal::Dimension::Id::ClassFlags );
569 if ( mPointFormat != 0 && mPointFormat != 2 )
571 mTable.layout()->registerDim( pdal::Dimension::Id::GpsTime );
574 if ( mPointFormat == 2 || mPointFormat == 3 || mPointFormat == 5 || mPointFormat == 7 || mPointFormat == 8 || mPointFormat == 10 )
576 mTable.layout()->registerDim( pdal::Dimension::Id::Red );
577 mTable.layout()->registerDim( pdal::Dimension::Id::Green );
578 mTable.layout()->registerDim( pdal::Dimension::Id::Blue );
581 if ( mPointFormat == 8 || mPointFormat == 10 )
583 mTable.layout()->registerDim( pdal::Dimension::Id::Infrared );
586 mView = std::make_shared<pdal::PointView>( mTable );
589void QgsPointCloudLayerExporter::ExporterPdal::handlePoint(
double x,
double y,
double z,
const QVariantMap &map,
const qint64 pointNumber )
591 mView->setField( pdal::Dimension::Id::X, pointNumber, x );
592 mView->setField( pdal::Dimension::Id::Y, pointNumber, y );
593 mView->setField( pdal::Dimension::Id::Z, pointNumber, z );
596 mView->setField( pdal::Dimension::Id::Classification, pointNumber, map[ QStringLiteral(
"Classification" ) ].toInt() );
597 mView->setField( pdal::Dimension::Id::Intensity, pointNumber, map[ QStringLiteral(
"Intensity" ) ].toInt() );
598 mView->setField( pdal::Dimension::Id::ReturnNumber, pointNumber, map[ QStringLiteral(
"ReturnNumber" ) ].toInt() );
599 mView->setField( pdal::Dimension::Id::NumberOfReturns, pointNumber, map[ QStringLiteral(
"NumberOfReturns" ) ].toInt() );
600 mView->setField( pdal::Dimension::Id::ScanDirectionFlag, pointNumber, map[ QStringLiteral(
"ScanDirectionFlag" ) ].toInt() );
601 mView->setField( pdal::Dimension::Id::EdgeOfFlightLine, pointNumber, map[ QStringLiteral(
"EdgeOfFlightLine" ) ].toInt() );
602 mView->setField( pdal::Dimension::Id::ScanAngleRank, pointNumber, map[ QStringLiteral(
"ScanAngleRank" ) ].toFloat() );
603 mView->setField( pdal::Dimension::Id::UserData, pointNumber, map[ QStringLiteral(
"UserData" ) ].toInt() );
604 mView->setField( pdal::Dimension::Id::PointSourceId, pointNumber, map[ QStringLiteral(
"PointSourceId" ) ].toInt() );
606 if ( mPointFormat == 6 || mPointFormat == 7 || mPointFormat == 8 || mPointFormat == 9 || mPointFormat == 10 )
608 mView->setField( pdal::Dimension::Id::ScanChannel, pointNumber, map[ QStringLiteral(
"ScannerChannel" ) ].toInt() );
609 const int classificationFlags = ( map[ QStringLiteral(
"Synthetic" ) ].toInt() & 0x01 ) << 0 |
610 ( map[ QStringLiteral(
"KeyPoint" ) ].toInt() & 0x01 ) << 1 |
611 ( map[ QStringLiteral(
"Withheld" ) ].toInt() & 0x01 ) << 2 |
612 ( map[ QStringLiteral(
"Overlap" ) ].toInt() & 0x01 ) << 3;
613 mView->setField( pdal::Dimension::Id::ClassFlags, pointNumber, classificationFlags );
616 if ( mPointFormat != 0 && mPointFormat != 2 )
618 mView->setField( pdal::Dimension::Id::GpsTime, pointNumber, map[ QStringLiteral(
"GpsTime" ) ].toDouble() );
621 if ( mPointFormat == 2 || mPointFormat == 3 || mPointFormat == 5 || mPointFormat == 7 || mPointFormat == 8 || mPointFormat == 10 )
623 mView->setField( pdal::Dimension::Id::Red, pointNumber, map[ QStringLiteral(
"Red" ) ].toInt() );
624 mView->setField( pdal::Dimension::Id::Green, pointNumber, map[ QStringLiteral(
"Green" ) ].toInt() );
625 mView->setField( pdal::Dimension::Id::Blue, pointNumber, map[ QStringLiteral(
"Blue" ) ].toInt() );
628 if ( mPointFormat == 8 || mPointFormat == 10 )
630 mView->setField( pdal::Dimension::Id::Infrared, pointNumber, map[ QStringLiteral(
"Infrared" ) ].toInt() );
634void QgsPointCloudLayerExporter::ExporterPdal::handleNode()
639void QgsPointCloudLayerExporter::ExporterPdal::handleAll()
641 pdal::BufferReader reader;
642 reader.addView( mView );
644 pdal::StageFactory factory;
646 pdal::Stage *writer = factory.createStage(
"writers.las" );
648 writer->setInput( reader );
649 writer->setOptions( mOptions );
650 writer->prepare( mTable );
651 writer->execute( mTable );
668 mOwnedFeedback->cancel();
678 mExp->setFeedback( mOwnedFeedback.get() );
@ NoSymbology
Export only data.
@ Reverse
Reverse/inverse transform (from destination to source).
Abstract base class for all geometries.
virtual QgsRectangle boundingBox() const
Returns the minimal bounding box for the geometry.
QgsRectangle toRectangle() const
Converts the box to a 2D rectangle.
Represents a coordinate reference system (CRS).
Contains information about the context in which a coordinate transform is executed.
Custom exception class for Coordinate Reference System related exceptions.
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.
Wraps a request for features to a vector layer (or directly its vector data provider).
QgsFeatureRequest & setNoAttributes()
Set that no attributes will be fetched.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
bool hasGeometry() const
Returns true if the feature has an associated geometry.
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.
void progressChanged(double progress)
Emitted when the feedback object reports a progress change.
Container of fields for a vector layer.
bool append(const QgsField &field, Qgis::FieldOrigin origin=Qgis::FieldOrigin::Provider, int originIndex=-1)
Appends a field.
A geometry is the spatial representation of a feature.
Qgis::GeometryOperationResult transform(const QgsCoordinateTransform &ct, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward, bool transformZ=false)
Transforms this geometry as described by the coordinate transform ct.
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
static QgsGeometry unaryUnion(const QVector< QgsGeometry > &geometries, const QgsGeometryParameters ¶meters=QgsGeometryParameters())
Compute the unary union on a list of geometries.
Base class for all map layer types.
QgsCoordinateReferenceSystem crs
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.
A collection of point cloud attributes.
void push_back(const QgsPointCloudAttribute &attribute)
Adds extra attribute.
int pointRecordSize() const
Returns total size of record.
const QgsPointCloudAttribute * find(const QString &attributeName, int &offset) const
Finds the attribute with the name.
QVector< QgsPointCloudAttribute > attributes() const
Returns all attributes.
Attribute for point cloud data pair of name and size in bytes.
DataType
Systems of unit measurement.
static void getPointXYZ(const char *ptr, int i, std::size_t pointRecordSize, int xOffset, QgsPointCloudAttribute::DataType xType, int yOffset, QgsPointCloudAttribute::DataType yType, int zOffset, QgsPointCloudAttribute::DataType zType, const QgsVector3D &indexScale, const QgsVector3D &indexOffset, double &x, double &y, double &z)
Retrieves the x, y, z values for the point at index i.
static QVariantMap getAttributeMap(const char *data, std::size_t recordOffset, const QgsPointCloudAttributeCollection &attributeCollection)
Retrieves all the attributes of a point.
DataType type() const
Returns the data type.
void cancel() override
Notifies the task that it should terminate.
QgsPointCloudLayerExporterTask(QgsPointCloudLayerExporter *exporter)
Constructor for QgsPointCloudLayerExporterTask.
void exportComplete()
Emitted when exporting the layer is successfully completed.
void finished(bool result) override
If the task is managed by a QgsTaskManager, this will be called after the task has finished (whether ...
bool run() override
Performs the task's operation.
Handles exporting point cloud layers to memory layers, OGR supported files and PDAL supported files.
QgsMapLayer * takeExportedLayer()
Gets a pointer to the exported layer.
QgsCoordinateReferenceSystem crs() const
Gets the crs for the exported file.
ExportFormat format() const
Returns the format for the exported file or layer.
void setAttributes(const QStringList &attributes)
Sets the list of point cloud attributes that will be exported.
ExportFormat
Supported export formats for point clouds.
@ Csv
Comma separated values.
@ Las
LAS/LAZ point cloud.
~QgsPointCloudLayerExporter()
void setAllAttributes()
Sets that all attributes will be exported.
bool setFormat(const ExportFormat format)
Sets the format for the exported file.
static QString getOgrDriverName(ExportFormat format)
Gets the OGR driver name for the specified format.
QStringList attributes() const
Gets the list of point cloud attributes that will be exported.
QgsPointCloudLayerExporter(QgsPointCloudLayer *layer)
Constructor for QgsPointCloudLayerExporter, associated with the specified layer.
void prepareExport()
Creates the QgsVectorLayer for exporting to a memory layer, if necessary.
static QList< ExportFormat > supportedFormats()
Gets a list of the supported export formats.
void setFilterGeometry(const QgsAbstractGeometry *geometry)
Sets a spatial filter for points to be exported based on geom in the point cloud's CRS.
void doExport()
Performs the actual exporting operation.
Represents a map layer supporting display of point clouds.
QgsPointCloudDataProvider * dataProvider() override
Returns the layer's data provider, it may be nullptr.
QList< QgsPointCloudNodeId > children() const
Returns IDs of child nodes.
qint64 pointCount() const
Returns number of points contained in node data.
QgsPointCloudNodeId id() const
Returns node's ID (unique in index).
QgsBox3D bounds() const
Returns node's bounding cube in CRS coords.
void setAttributes(const QgsPointCloudAttributeCollection &attributes)
Set attributes filter in the request.
A rectangle specified with double values.
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.
void setProgress(double progress)
Sets the task's current progress.
QString lastError() const override
Returns the most recent error encountered by the sink, e.g.
bool addFeatures(QgsFeatureList &flist, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags()) override
Adds a list of features to the sink.
Options to pass to QgsVectorFileWriter::writeAsVectorFormat().
QString driverName
OGR driver to use.
QString layerName
Layer name. If let empty, it will be derived from the filename.
QStringList layerOptions
List of OGR layer creation options.
Qgis::FeatureSymbologyExport symbologyExport
Symbology to export.
QgsVectorFileWriter::ActionOnExistingFile actionOnExistingFile
Action on existing file.
QStringList datasourceOptions
List of OGR data source creation options.
QgsFeedback * feedback
Optional feedback object allowing cancellation of layer save.
static QStringList defaultLayerOptions(const QString &driverName)
Returns a list of the default layer options for a specified driver.
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.
static QStringList defaultDatasetOptions(const QString &driverName)
Returns a list of the default dataset options for a specified driver.
Represents a vector layer which manages a vector based dataset.
QgsFeatureIterator getSelectedFeatures(QgsFeatureRequest request=QgsFeatureRequest()) const
Returns an iterator of the selected features.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const final
Queries the layer for features specified in request.
QgsVectorDataProvider * dataProvider() final
Returns the layer's data provider, it may be nullptr.
#define BUILTIN_UNREACHABLE
#define QgsDebugMsgLevel(str, level)
#define QgsDebugError(str)