26#include <QApplication>
32#include "moc_qgspointcloudlayerexporter.cpp"
34using namespace Qt::StringLiterals;
38#include <pdal/StageFactory.hpp>
39#include <pdal/io/BufferReader.hpp>
40#include <pdal/Dimension.hpp>
52 return u
"ESRI Shapefile"_s;
63 : mLayerAttributeCollection( layer->
attributes() )
64 , mIndex( layer->index() )
69 mPointRecordFormat = layer->
dataProvider()->originalMetadata().value( u
"dataformat_id"_s ).toInt( &ok );
71 mPointRecordFormat = 3;
95 mFilterGeometryEngine = std::make_unique<QgsGeos>( geometry );
96 mFilterGeometryEngine->prepareGeometry();
105 QVector< QgsGeometry > allGeometries;
108 if ( selectedFeaturesOnly )
120 allGeometries.append( f.
geometry() );
130 QgsDebugError( u
"Error transforming union of filter layer: %1"_s.arg( cse.
what() ) );
139 mRequestedAttributes.clear();
141 const QVector<QgsPointCloudAttribute> allAttributes = mLayerAttributeCollection.attributes();
145 if ( attribute.name().compare(
'X'_L1, Qt::CaseInsensitive )
146 && attribute.name().compare(
'Y'_L1, Qt::CaseInsensitive )
147 && attribute.name().compare(
'Z'_L1, Qt::CaseInsensitive )
148 && attributeList.contains( attribute.name() )
149 && !mRequestedAttributes.contains( attribute.name() ) )
151 mRequestedAttributes.append( attribute.name() );
158 QStringList allAttributeNames;
159 const QVector<QgsPointCloudAttribute> allAttributes = mLayerAttributeCollection.attributes();
162 allAttributeNames.append( attribute.name() );
169 const QVector<QgsPointCloudAttribute> allAttributes = mLayerAttributeCollection.
attributes();
174 if ( attribute.name().compare(
'X'_L1, Qt::CaseInsensitive )
175 || attribute.name().compare(
'Y'_L1, Qt::CaseInsensitive )
176 || attribute.name().compare(
'Z'_L1, Qt::CaseInsensitive )
177 || mRequestedAttributes.contains( attribute.name(), Qt::CaseInsensitive ) )
179 requestAttributes.
push_back( attribute );
182 return requestAttributes;
185QgsFields QgsPointCloudLayerExporter::outputFields()
187 const QVector<QgsPointCloudAttribute>
attributes = mLayerAttributeCollection.attributes();
190 for (
const QgsPointCloudAttribute &attribute :
attributes )
192 if ( mRequestedAttributes.contains( attribute.name(), Qt::CaseInsensitive ) )
193 fields.
append( QgsField( attribute.name(), attribute.variantType(), attribute.displayType() ) );
201 mMemoryLayer =
nullptr;
206 if ( QApplication::instance()->thread() != QThread::currentThread() )
208 QgsDebugMsgLevel( u
"prepareExport() should better be called from the main thread!"_s, 2 );
219 if ( mExtent.isFinite() )
231 QStringList layerCreationOptions;
240 ExporterMemory exp(
this );
252 ExporterPdal exp(
this );
255 catch ( std::runtime_error &e )
257 setLastError( QString::fromLatin1( e.what() ) );
258 QgsDebugError( u
"PDAL has thrown an exception: {}"_s.arg( e.what() ) );
265 layerCreationOptions << u
"GEOMETRY=AS_XYZ"_s << u
"SEPARATOR=COMMA"_s;
282 ExporterVector exp(
this );
296 mMemoryLayer =
nullptr;
302 const QFileInfo fileInfo( mFilename );
308 QString uri( mFilename );
309 uri +=
"|layername=" + mName;
317 const QFileInfo fileInfo( mFilename );
318 return new QgsVectorLayer( mFilename, fileInfo.completeBaseName(), u
"ogr"_s );
328void QgsPointCloudLayerExporter::ExporterBase::run()
331 geometryFilterRectangle( -std::numeric_limits<double>::infinity(), -std::numeric_limits<double>::infinity(), std::numeric_limits<double>::infinity(), std::numeric_limits<double>::infinity(),
false );
332 if ( mParent->mFilterGeometryEngine )
339 QVector<QgsPointCloudNodeId> nodes;
340 qint64 pointCount = 0;
341 QQueue<QgsPointCloudNodeId> queue;
342 queue.push_back( mParent->mIndex.root() );
343 while ( !queue.empty() )
345 QgsPointCloudNode node = mParent->mIndex.getNode( queue.front() );
347 const QgsBox3D nodeBounds = node.
bounds();
348 if ( mParent->mExtent.intersects( nodeBounds.
toRectangle() )
349 && mParent->mZRange.overlaps( { nodeBounds.zMinimum(), nodeBounds.zMaximum() } )
350 && geometryFilterRectangle.intersects( nodeBounds.
toRectangle() ) )
353 nodes.push_back( node.
id() );
355 for (
const QgsPointCloudNodeId &child : node.
children() )
357 queue.push_back( child );
361 const qint64 pointsToExport = mParent->mPointsLimit > 0 ? std::min( mParent->mPointsLimit, pointCount ) : pointCount;
362 QgsPointCloudRequest request;
363 request.
setAttributes( mParent->requestedAttributeCollection() );
364 std::unique_ptr<QgsPointCloudBlock> block =
nullptr;
365 qint64 pointsExported = 0;
366 for (
const QgsPointCloudNodeId &node : nodes )
368 block = mParent->mIndex.nodeData( node, request );
369 const QgsPointCloudAttributeCollection attributesCollection = block->attributes();
370 const char *ptr = block->data();
371 int count = block->pointCount();
373 const QgsVector3D scale = block->scale();
374 const QgsVector3D offset = block->offset();
375 int xOffset = 0, yOffset = 0, zOffset = 0;
379 for (
int i = 0; i < count; ++i )
381 if ( mParent->mFeedback && i % 1000 == 0 )
383 if ( pointsToExport > 0 )
385 mParent->mFeedback->setProgress( 100 *
static_cast< float >( pointsExported ) / pointsToExport );
387 if ( mParent->mFeedback->isCanceled() )
389 mParent->setLastError( QObject::tr(
"Canceled by user" ) );
394 if ( pointsExported >= pointsToExport )
398 QgsPointCloudAttribute::getPointXYZ( ptr, i, recordSize, xOffset, xType, yOffset, yType, zOffset, zType, scale, offset, x, y, z );
399 if ( !mParent->mZRange.contains( z ) || !mParent->mExtent.contains( x, y ) || ( mParent->mFilterGeometryEngine && !mParent->mFilterGeometryEngine->contains( x, y ) ) )
406 mParent->mTransform->transformInPlace( x, y, z );
408 handlePoint( x, y, z, attributeMap, pointsExported );
411 catch (
const QgsCsException &cse )
430QgsPointCloudLayerExporter::ExporterMemory::~ExporterMemory()
432 mParent->mMemoryLayer->moveToThread( QApplication::instance()->thread() );
435void QgsPointCloudLayerExporter::ExporterMemory::handlePoint(
double x,
double y,
double z,
const QVariantMap &map,
const qint64 pointNumber )
437 Q_UNUSED( pointNumber )
440 feature.
setGeometry( QgsGeometry(
new QgsPoint( x, y, z ) ) );
441 QgsAttributes featureAttributes;
442 for (
const QString &attribute : std::as_const( mParent->mRequestedAttributes ) )
444 const double val = map[attribute].toDouble();
445 featureAttributes.append( val );
448 mFeatures.append( feature );
451void QgsPointCloudLayerExporter::ExporterMemory::handleNode()
453 QgsVectorLayer *vl = qgis::down_cast<QgsVectorLayer *>( mParent->mMemoryLayer );
464void QgsPointCloudLayerExporter::ExporterMemory::handleAll()
476QgsPointCloudLayerExporter::ExporterVector::~ExporterVector()
478 delete mParent->mVectorSink;
479 mParent->mVectorSink =
nullptr;
482void QgsPointCloudLayerExporter::ExporterVector::handlePoint(
double x,
double y,
double z,
const QVariantMap &map,
const qint64 pointNumber )
484 Q_UNUSED( pointNumber )
487 feature.
setGeometry( QgsGeometry(
new QgsPoint( x, y, z ) ) );
488 QgsAttributes featureAttributes;
489 for (
const QString &attribute : std::as_const( mParent->mRequestedAttributes ) )
491 const double val = map[attribute].toDouble();
492 featureAttributes.append( val );
495 mFeatures.append( feature );
498void QgsPointCloudLayerExporter::ExporterVector::handleNode()
500 if ( !mParent->mVectorSink->addFeatures( mFeatures ) )
502 mParent->setLastError( mParent->mVectorSink->lastError() );
507void QgsPointCloudLayerExporter::ExporterVector::handleAll()
517 : mPointFormat( exp->mPointRecordFormat )
521 mOptions.add(
"filename", mParent->mFilename.toStdString() );
522 mOptions.add(
"a_srs", mParent->mTargetCrs.toWkt().toStdString() );
523 mOptions.add(
"minor_version", u
"4"_s.toStdString() );
524 mOptions.add(
"format", QString::number( mPointFormat ).toStdString() );
525 if ( mParent->mTransform->isShortCircuited() )
527 mOptions.add(
"offset_x", QString::number( mParent->mIndex.offset().x() ).toStdString() );
528 mOptions.add(
"offset_y", QString::number( mParent->mIndex.offset().y() ).toStdString() );
529 mOptions.add(
"offset_z", QString::number( mParent->mIndex.offset().z() ).toStdString() );
530 mOptions.add(
"scale_x", QString::number( mParent->mIndex.scale().x() ).toStdString() );
531 mOptions.add(
"scale_y", QString::number( mParent->mIndex.scale().y() ).toStdString() );
532 mOptions.add(
"scale_z", QString::number( mParent->mIndex.scale().z() ).toStdString() );
535 mTable.layout()->registerDim( pdal::Dimension::Id::X );
536 mTable.layout()->registerDim( pdal::Dimension::Id::Y );
537 mTable.layout()->registerDim( pdal::Dimension::Id::Z );
539 mTable.layout()->registerDim( pdal::Dimension::Id::Classification );
540 mTable.layout()->registerDim( pdal::Dimension::Id::Intensity );
541 mTable.layout()->registerDim( pdal::Dimension::Id::ReturnNumber );
542 mTable.layout()->registerDim( pdal::Dimension::Id::NumberOfReturns );
543 mTable.layout()->registerDim( pdal::Dimension::Id::ScanDirectionFlag );
544 mTable.layout()->registerDim( pdal::Dimension::Id::EdgeOfFlightLine );
545 mTable.layout()->registerDim( pdal::Dimension::Id::ScanAngleRank );
546 mTable.layout()->registerDim( pdal::Dimension::Id::UserData );
547 mTable.layout()->registerDim( pdal::Dimension::Id::PointSourceId );
549 if ( mPointFormat == 6 || mPointFormat == 7 || mPointFormat == 8 || mPointFormat == 9 || mPointFormat == 10 )
551 mTable.layout()->registerDim( pdal::Dimension::Id::ScanChannel );
552 mTable.layout()->registerDim( pdal::Dimension::Id::ClassFlags );
555 if ( mPointFormat != 0 && mPointFormat != 2 )
557 mTable.layout()->registerDim( pdal::Dimension::Id::GpsTime );
560 if ( mPointFormat == 2 || mPointFormat == 3 || mPointFormat == 5 || mPointFormat == 7 || mPointFormat == 8 || mPointFormat == 10 )
562 mTable.layout()->registerDim( pdal::Dimension::Id::Red );
563 mTable.layout()->registerDim( pdal::Dimension::Id::Green );
564 mTable.layout()->registerDim( pdal::Dimension::Id::Blue );
567 if ( mPointFormat == 8 || mPointFormat == 10 )
569 mTable.layout()->registerDim( pdal::Dimension::Id::Infrared );
572 mView = std::make_shared<pdal::PointView>( mTable );
575void QgsPointCloudLayerExporter::ExporterPdal::handlePoint(
double x,
double y,
double z,
const QVariantMap &map,
const qint64 pointNumber )
577 mView->setField( pdal::Dimension::Id::X, pointNumber, x );
578 mView->setField( pdal::Dimension::Id::Y, pointNumber, y );
579 mView->setField( pdal::Dimension::Id::Z, pointNumber, z );
582 mView->setField( pdal::Dimension::Id::Classification, pointNumber, map[u
"Classification"_s].toInt() );
583 mView->setField( pdal::Dimension::Id::Intensity, pointNumber, map[u
"Intensity"_s].toInt() );
584 mView->setField( pdal::Dimension::Id::ReturnNumber, pointNumber, map[u
"ReturnNumber"_s].toInt() );
585 mView->setField( pdal::Dimension::Id::NumberOfReturns, pointNumber, map[u
"NumberOfReturns"_s].toInt() );
586 mView->setField( pdal::Dimension::Id::ScanDirectionFlag, pointNumber, map[u
"ScanDirectionFlag"_s].toInt() );
587 mView->setField( pdal::Dimension::Id::EdgeOfFlightLine, pointNumber, map[u
"EdgeOfFlightLine"_s].toInt() );
588 mView->setField( pdal::Dimension::Id::ScanAngleRank, pointNumber, map[u
"ScanAngleRank"_s].toFloat() );
589 mView->setField( pdal::Dimension::Id::UserData, pointNumber, map[u
"UserData"_s].toInt() );
590 mView->setField( pdal::Dimension::Id::PointSourceId, pointNumber, map[u
"PointSourceId"_s].toInt() );
592 if ( mPointFormat == 6 || mPointFormat == 7 || mPointFormat == 8 || mPointFormat == 9 || mPointFormat == 10 )
594 mView->setField( pdal::Dimension::Id::ScanChannel, pointNumber, map[u
"ScannerChannel"_s].toInt() );
595 const int classificationFlags = ( map[u
"Synthetic"_s].toInt() & 0x01 ) << 0
596 | ( map[u
"KeyPoint"_s].toInt() & 0x01 ) << 1
597 | ( map[u
"Withheld"_s].toInt() & 0x01 ) << 2
598 | ( map[u
"Overlap"_s].toInt() & 0x01 ) << 3;
599 mView->setField( pdal::Dimension::Id::ClassFlags, pointNumber, classificationFlags );
602 if ( mPointFormat != 0 && mPointFormat != 2 )
604 mView->setField( pdal::Dimension::Id::GpsTime, pointNumber, map[u
"GpsTime"_s].toDouble() );
607 if ( mPointFormat == 2 || mPointFormat == 3 || mPointFormat == 5 || mPointFormat == 7 || mPointFormat == 8 || mPointFormat == 10 )
609 mView->setField( pdal::Dimension::Id::Red, pointNumber, map[u
"Red"_s].toInt() );
610 mView->setField( pdal::Dimension::Id::Green, pointNumber, map[u
"Green"_s].toInt() );
611 mView->setField( pdal::Dimension::Id::Blue, pointNumber, map[u
"Blue"_s].toInt() );
614 if ( mPointFormat == 8 || mPointFormat == 10 )
616 mView->setField( pdal::Dimension::Id::Infrared, pointNumber, map[u
"Infrared"_s].toInt() );
620void QgsPointCloudLayerExporter::ExporterPdal::handleNode()
623void QgsPointCloudLayerExporter::ExporterPdal::handleAll()
625 pdal::BufferReader reader;
626 reader.addView( mView );
628 pdal::StageFactory factory;
630 pdal::Stage *writer = factory.createStage(
"writers.las" );
632 writer->setInput( reader );
633 writer->setOptions( mOptions );
634 writer->prepare( mTable );
635 writer->execute( mTable );
651 mOwnedFeedback->cancel();
661 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)