QGIS API Documentation 3.28.0-Firenze (ed3ad0430f)
qgspointcloudlayerexporter.h
Go to the documentation of this file.
1/***************************************************************************
2 qgspointcloudlayerexporter.h
3 ---------------------
4 begin : July 2022
5 copyright : (C) 2022 by Stefanos Natsis
6 email : uclaros at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18#ifndef QGSPOINTCLOUDLAYEREXPORTER_H
19#define QGSPOINTCLOUDLAYEREXPORTER_H
20
21#include "qgis_core.h"
22#include "qgsvectorlayer.h"
23#include "qgspointcloudlayer.h"
24#include "qgstaskmanager.h"
25#include "qgsvectorfilewriter.h"
26
27#ifdef HAVE_PDAL_QGIS
28#include <pdal/PointView.hpp>
29#include <pdal/PointTable.hpp>
30#include <pdal/Options.hpp>
31#endif
32
34class QgsGeos;
35
45{
46 public:
47
51 enum class ExportFormat : int
52 {
53 Memory = 0,
54 Las = 1,
55 Gpkg = 2,
56 Shp = 3,
57 Dxf = 4,
58 Csv = 5,
59 };
60
66 static QList< ExportFormat > supportedFormats() SIP_SKIP
67 {
68 QList< ExportFormat > formats;
69 formats << ExportFormat::Memory
70#ifdef HAVE_PDAL_QGIS
71 << ExportFormat::Las
72#endif
73 << ExportFormat::Gpkg
74 << ExportFormat::Shp
75 << ExportFormat::Dxf
76 << ExportFormat::Csv;
77 return formats;
78 }
79
84 static QString getOgrDriverName( ExportFormat format ) SIP_SKIP;
85
92
94
98 void setFileName( const QString &filename ) { mFilename = filename; }
99
103 QString fileName() const { return mFilename; }
104
108 void setLayerName( const QString &name ) { mName = name; }
109
113 QString layerName() const { return mName; }
114
120 void setFilterExtent( const QgsRectangle extent ) { mExtent = extent; }
121
125 QgsRectangle filterExtent() const { return mExtent; }
126
131 void setFilterGeometry( const QgsAbstractGeometry *geometry );
132
137 void setFilterGeometry( QgsMapLayer *layer, bool selectedFeaturesOnly = false );
138
143 void setZRange( const QgsDoubleRange zRange ) { mZRange = zRange; }
144
148 QgsDoubleRange zRange() const { return mZRange; }
149
155 void setFeedback( QgsFeedback *feedback ) { mFeedback = feedback; }
156
160 QgsFeedback *feedback() const { return mFeedback; }
161
167 void setAttributes( const QStringList &attributes );
168
173 void setNoAttributes() { mRequestedAttributes.clear(); }
174
178 void setAllAttributes();
179
183 QStringList attributes() const { return mRequestedAttributes; }
184
189 void setCrs( const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &context = QgsCoordinateTransformContext() ) { mTargetCrs = crs; mTransformContext = context; }
190
194 QgsCoordinateReferenceSystem crs() const { return mTargetCrs; }
195
201 bool setFormat( const ExportFormat format );
202
206 ExportFormat format() const { return mFormat; }
207
212 void setPointsLimit( qint64 limit ) { mPointsLimit = std::max< qint64 >( limit, 0 ); }
213
217 qint64 pointsLimit() { return mPointsLimit; }
218
223 void setActionOnExistingFile( const QgsVectorFileWriter::ActionOnExistingFile action ) { mActionOnExistingFile = action; }
224
230 void prepareExport();
231
235 void doExport();
236
241 QgsMapLayer *takeExportedLayer() SIP_FACTORY;
242
247 QString lastError() const { return mLastError; }
248
249 private:
250
251 void exportToSink( QgsFeatureSink * );
252
253 QgsFields outputFields();
254 const QgsPointCloudAttributeCollection requestedAttributeCollection();
255 void setLastError( QString error ) { mLastError = error; }
256
257 const QgsPointCloudAttributeCollection mLayerAttributeCollection;
258
259 QgsPointCloudIndex *mIndex = nullptr;
260 QString mName = QObject::tr( "Exported" );
261 ExportFormat mFormat = ExportFormat::Memory;
262 QString mFilename;
263 QString mLastError;
264 QgsRectangle mExtent = QgsRectangle( -std::numeric_limits<double>::infinity(),
265 -std::numeric_limits<double>::infinity(),
266 std::numeric_limits<double>::infinity(),
267 std::numeric_limits<double>::infinity(),
268 false );
269 std::unique_ptr< QgsGeos > mFilterGeometryEngine;
270 QgsDoubleRange mZRange;
271 QgsFeedback *mFeedback = nullptr;
272 qint64 mPointsLimit = 0;
273 QStringList mRequestedAttributes;
276 QgsCoordinateTransformContext mTransformContext;
277 int mPointRecordFormat;
278 QgsVectorFileWriter::ActionOnExistingFile mActionOnExistingFile = QgsVectorFileWriter::ActionOnExistingFile::CreateOrOverwriteFile;
279
280 QgsMapLayer *mMemoryLayer = nullptr;
281 QgsFeatureSink *mVectorSink = nullptr;
282 QgsCoordinateTransform *mTransform = nullptr;
283
284
285 class ExporterBase
286 {
287 public:
288 ExporterBase() = default;
289 virtual ~ExporterBase() = default;
290 void run();
291 protected:
292 virtual void handlePoint( double x, double y, double z, const QVariantMap &map, const qint64 pointNumber ) = 0;
293 virtual void handleNode() = 0;
294 virtual void handleAll() = 0;
295 QgsPointCloudLayerExporter *mParent = nullptr;
296 };
297
298 class ExporterMemory : public ExporterBase
299 {
300 public:
301 ExporterMemory( QgsPointCloudLayerExporter *exp );
302 ~ExporterMemory() override;
303
304 private:
305 void handlePoint( double x, double y, double z, const QVariantMap &map, const qint64 pointNumber ) override;
306 void handleNode() override;
307 void handleAll() override;
308 QgsFeatureList mFeatures;
309 };
310
311 class ExporterVector : public ExporterBase
312 {
313 public:
314 ExporterVector( QgsPointCloudLayerExporter *exp );
315 ~ExporterVector() override;
316
317 private:
318 void handlePoint( double x, double y, double z, const QVariantMap &map, const qint64 pointNumber ) override;
319 void handleNode() override;
320 void handleAll() override;
321 QgsFeatureList mFeatures;
322 };
323
324#ifdef HAVE_PDAL_QGIS
325 class ExporterPdal : public ExporterBase
326 {
327 public:
328 ExporterPdal( QgsPointCloudLayerExporter *exp );
329
330 private:
331 void handlePoint( double x, double y, double z, const QVariantMap &map, const qint64 pointNumber ) override;
332 void handleNode() override;
333 void handleAll() override;
334 const int mPointFormat;
335 pdal::Options mOptions;
336 pdal::PointTable mTable;
337 pdal::PointViewPtr mView;
338 };
339#endif
340
341};
342
343
344
353class CORE_EXPORT QgsPointCloudLayerExporterTask : public QgsTask
354{
355 Q_OBJECT
356
357 public:
358
363
364 void cancel() override;
365
366 signals:
367
372
373 protected:
374
375 bool run() override;
376 void finished( bool result ) override;
377
378 private:
379 QgsPointCloudLayerExporter *mExp = nullptr;
380
381 std::unique_ptr< QgsFeedback > mOwnedFeedback;
382
383};
384#endif // QGSPOINTCLOUDLAYEREXPORTER_H
Abstract base class for all geometries.
This class represents a coordinate reference system (CRS).
Contains information about the context in which a coordinate transform is executed.
Class for doing transforms between two map coordinate systems.
QgsRange which stores a range of double values.
Definition: qgsrange.h:203
An interface for objects which accept features via addFeature(s) methods.
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition: qgsfeedback.h:45
Container of fields for a vector layer.
Definition: qgsfields.h:45
Does vector analysis using the geos library and handles import, export, exception handling*.
Definition: qgsgeos.h:99
Base class for all map layer types.
Definition: qgsmaplayer.h:73
Collection of point cloud attributes.
Represents a indexed point clouds data in octree.
QgsTask task which performs a QgsPointCloudLayerExporter layer export operation as a background task.
void exportComplete()
Emitted when exporting the layer is successfully completed.
Handles exporting point cloud layers to memory layers, OGR supported files and PDAL supported files.
void setFeedback(QgsFeedback *feedback)
Sets a QgsFeedback object to allow cancellation / progress reporting.
QgsCoordinateReferenceSystem crs() const
Gets the crs for the exported file.
ExportFormat format() const
Returns the format for the exported file or layer.
void setCrs(const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &context=QgsCoordinateTransformContext())
Sets the crs for the exported file, and the transform context that will be used for reprojection if d...
QgsRectangle filterExtent() const
Gets the filter extent for points to be exported.
QString layerName() const
Gets the name for the new layer.
void setFilterExtent(const QgsRectangle extent)
Sets a filter extent for points to be exported in the target CRS Points that fall outside the extent ...
QgsDoubleRange zRange() const
Gets the inclusive range for Z values to be exported.
void setFileName(const QString &filename)
Sets the filename for the new layer.
void setActionOnExistingFile(const QgsVectorFileWriter::ActionOnExistingFile action)
Sets whether an existing output vector file should be overwritten on appended to.
ExportFormat
Supported export formats for point clouds.
void setLayerName(const QString &name)
Sets the name for the new layer.
QString fileName() const
Gets the filename for the new layer.
void setZRange(const QgsDoubleRange zRange)
Sets an inclusive range for Z values to be exported.
void setPointsLimit(qint64 limit)
Sets the maximum number of points to be exported.
QStringList attributes() const
Gets the list of point cloud attributes that will be exported.
qint64 pointsLimit()
Gets the maximum number of points to be exported.
void setNoAttributes()
Sets that no attributes will be exported.
static QList< ExportFormat > supportedFormats()
Gets a list of the supported export formats.
QgsFeedback * feedback() const
Gets a pointer to the QgsFeedback object used for cancellation / progress reporting,...
Represents a map layer supporting display of point clouds.
A rectangle specified with double values.
Definition: qgsrectangle.h:42
Abstract base class for long running background tasks.
ActionOnExistingFile
Combination of CanAddNewLayer, CanAppendToExistingLayer, CanAddNewFieldsToExistingLayer or CanDeleteL...
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_FACTORY
Definition: qgis_sip.h:76
#define SIP_NODEFAULTCTORS
Definition: qgis_sip.h:101
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:922
const QgsCoordinateReferenceSystem & crs