31#include <QRegularExpression>
34#include "moc_qgspointcloudlayersaveasdialog.cpp"
36using namespace Qt::StringLiterals;
39 : QDialog( parent, fl )
44 mSelectedCrs = layer->
crs();
45 mLayerExtent = layer->
extent();
50void QgsPointCloudLayerSaveAsDialog::setup()
55 connect( mFormatComboBox,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsPointCloudLayerSaveAsDialog::mFormatComboBox_currentIndexChanged );
57 connect( mSelectAllAttributes, &QPushButton::clicked,
this, &QgsPointCloudLayerSaveAsDialog::mSelectAllAttributes_clicked );
58 connect( mDeselectAllAttributes, &QPushButton::clicked,
this, &QgsPointCloudLayerSaveAsDialog::mDeselectAllAttributes_clicked );
60 connect( mFilterGeometryGroupBox, &QgsCollapsibleGroupBox::toggled,
this, &QgsPointCloudLayerSaveAsDialog::mFilterGeometryGroupBoxCheckToggled );
61 connect( mMinimumZSpinBox,
static_cast<void (
QgsDoubleSpinBox::* )(
double )
>( &QgsDoubleSpinBox::valueChanged ),
this, &QgsPointCloudLayerSaveAsDialog::mMinimumZSpinBoxValueChanged );
62 connect( mMaximumZSpinBox,
static_cast<void (
QgsDoubleSpinBox::* )(
double )
>( &QgsDoubleSpinBox::valueChanged ),
this, &QgsPointCloudLayerSaveAsDialog::mMaximumZSpinBoxValueChanged );
65 mHelpButtonBox->setVisible(
false );
66 mButtonBox->addButton( QDialogButtonBox::Help );
67 connect( mButtonBox, &QDialogButtonBox::helpRequested,
this, &QgsPointCloudLayerSaveAsDialog::showHelp );
69 connect( mHelpButtonBox, &QDialogButtonBox::helpRequested,
this, &QgsPointCloudLayerSaveAsDialog::showHelp );
71 connect( mButtonBox, &QDialogButtonBox::accepted,
this, &QgsPointCloudLayerSaveAsDialog::accept );
72 connect( mButtonBox, &QDialogButtonBox::rejected,
this, &QgsPointCloudLayerSaveAsDialog::reject );
74 mFormatComboBox->blockSignals(
true );
76 for (
const auto &format : supportedFormats )
77 mFormatComboBox->addItem( getTranslatedNameForFormat( format ),
static_cast<int>( format ) );
80 const int defaultFormat = settings.
value( u
"UI/lastPointCloudFormat"_s, 0 ).toInt();
81 mFormatComboBox->setCurrentIndex( mFormatComboBox->findData( defaultFormat ) );
82 mFormatComboBox->blockSignals(
false );
83 mFormatComboBox_currentIndexChanged( 0 );
85 mCrsSelector->setCrs( mSelectedCrs );
86 mCrsSelector->setLayerCrs( mSelectedCrs );
87 mCrsSelector->setMessage( tr(
"Select the coordinate reference system for the vector file. "
88 "The data points will be transformed from the layer coordinate reference system." ) );
95 QStringList availableAttributes;
98 const QString attribute =
attributes.at( i ).name();
99 if ( attribute.compare(
'X'_L1, Qt::CaseInsensitive ) && attribute.compare(
'Y'_L1, Qt::CaseInsensitive ) && attribute.compare(
'Z'_L1, Qt::CaseInsensitive ) )
101 availableAttributes.append( attribute );
105 mAttributeTable->setRowCount( availableAttributes.count() );
106 QStringList horizontalHeaders = QStringList() << tr(
"Attribute" );
107 mAttributeTable->setColumnCount( horizontalHeaders.size() );
108 mAttributeTable->setHorizontalHeaderLabels( horizontalHeaders );
110 for (
int i = 0; i < availableAttributes.count(); ++i )
112 QTableWidgetItem *item =
new QTableWidgetItem( availableAttributes.at( i ) );
113 item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsUserCheckable );
114 item->setCheckState( Qt::Checked );
115 mAttributeTable->setItem( i, 0, item );
117 mAttributeTable->resizeColumnsToContents();
121 mExtentGroupBox->setOutputCrs( mSelectedCrs );
122 mExtentGroupBox->setOriginalExtent( mLayerExtent, mSelectedCrs );
123 mExtentGroupBox->setOutputExtentFromOriginal();
124 mExtentGroupBox->setCheckable(
true );
125 mExtentGroupBox->setChecked(
false );
126 mExtentGroupBox->setCollapsed(
true );
132 mMinimumZSpinBox->setRange( std::numeric_limits<double>::lowest(), std::numeric_limits<double>::max() );
133 mMaximumZSpinBox->setRange( std::numeric_limits<double>::lowest(), std::numeric_limits<double>::max() );
136 mMinimumZSpinBox->setValue( mLayer->statistics().minimum( u
"Z"_s ) );
137 mMinimumZSpinBox->setClearValue( mMinimumZSpinBox->value() );
138 mMaximumZSpinBox->setValue( mLayer->statistics().maximum( u
"Z"_s ) );
139 mMaximumZSpinBox->setClearValue( mMaximumZSpinBox->value() );
143 mPointsLimitSpinBox->setMinimum( 1 );
144 mPointsLimitSpinBox->setMaximum( std::numeric_limits<int>::max() );
145 mPointsLimitSpinBox->setValue( 1e6 );
146 mPointsLimitSpinBox->setClearValue( 1e6 );
149 mFilename->setDialogTitle( tr(
"Save Layer As" ) );
150 mFilename->setDefaultRoot( settings.
value( u
"UI/lastPointCloudFileFilterDir"_s, QDir::homePath() ).toString() );
151 mFilename->setConfirmOverwrite(
false );
153 QgsSettings settings;
154 if ( !filePath.isEmpty() )
155 mLastUsedFilename = filePath;
157 const QFileInfo fileInfo( filePath );
158 settings.
setValue( u
"UI/lastPointCloudFileFilterDir"_s, fileInfo.absolutePath() );
160 if ( mDefaultOutputLayerNameFromInputLayerName.isEmpty() )
162 leLayername->setDefaultValue( suggestedLayerName );
166 if ( leLayername->text().isEmpty() && !filePath.isEmpty() && leLayername->isEnabled() )
168 leLayername->setText( suggestedLayerName );
170 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled( !filePath.isEmpty() );
175 const QgsDatumEnsemble ensemble = mSelectedCrs.datumEnsemble();
178 mCrsSelector->setSourceEnsemble( ensemble.
name() );
181 catch ( QgsNotSupportedException & )
185 mCrsSelector->setShowAccuracyWarnings(
true );
192 leLayername->setDefaultValue( mDefaultOutputLayerNameFromInputLayerName );
194 if ( leLayername->isEnabled() )
195 leLayername->setText( mDefaultOutputLayerNameFromInputLayerName );
201void QgsPointCloudLayerSaveAsDialog::accept()
208 msgBox.setIcon( QMessageBox::Question );
209 msgBox.setWindowTitle( tr(
"Save Point Cloud Layer As" ) );
210 QPushButton *overwriteFileButton = msgBox.addButton( tr(
"Overwrite File" ), QMessageBox::ActionRole );
211 QPushButton *overwriteLayerButton = msgBox.addButton( tr(
"Overwrite Layer" ), QMessageBox::ActionRole );
212 QPushButton *appendToLayerButton = msgBox.addButton( tr(
"Append to Layer" ), QMessageBox::ActionRole );
213 msgBox.setStandardButtons( QMessageBox::Cancel );
214 msgBox.setDefaultButton( QMessageBox::Cancel );
215 overwriteFileButton->hide();
216 overwriteLayerButton->hide();
217 appendToLayerButton->hide();
222 msgBox.setText( tr(
"The layer already exists. Do you want to overwrite the whole file or overwrite the layer?" ) );
223 overwriteFileButton->setVisible(
true );
224 overwriteLayerButton->setVisible(
true );
228 msgBox.setText( tr(
"The file already exists. Do you want to overwrite it?" ) );
229 overwriteFileButton->setVisible(
true );
233 msgBox.setText( tr(
"The layer already exists. Do you want to overwrite the whole file, overwrite the layer or append features to the layer?" ) );
234 appendToLayerButton->setVisible(
true );
235 overwriteFileButton->setVisible(
true );
236 overwriteLayerButton->setVisible(
true );
240 msgBox.setText( tr(
"The layer already exists. Do you want to overwrite the whole file or append features to the layer?" ) );
241 appendToLayerButton->setVisible(
true );
242 overwriteFileButton->setVisible(
true );
245 int ret = msgBox.exec();
246 if ( ret == QMessageBox::Cancel )
248 if ( msgBox.clickedButton() == overwriteFileButton )
250 else if ( msgBox.clickedButton() == overwriteLayerButton )
252 else if ( msgBox.clickedButton() == appendToLayerButton )
264 if ( QMessageBox::question(
this, tr(
"Save Point Cloud Layer As" ), tr(
"The file already exists. Do you want to overwrite it?" ) ) == QMessageBox::NoButton )
275 QStringList layerList;
276 layerList.reserve( sublayers.size() );
277 for (
const QgsProviderSublayerDetails &sublayer : sublayers )
279 layerList.append( sublayer.name() );
281 if ( layerList.length() > 1 )
283 layerList.sort( Qt::CaseInsensitive );
285 msgBox.setIcon( QMessageBox::Warning );
286 msgBox.setWindowTitle( tr(
"Overwrite File" ) );
287 msgBox.setText( tr(
"This file contains %1 layers that will be lost!\n" ).arg( QLocale().toString( layerList.length() ) ) );
288 msgBox.setDetailedText( tr(
"The following layers will be permanently lost:\n\n%1" ).arg( layerList.join(
"\n" ) ) );
289 msgBox.setStandardButtons( QMessageBox::Ok | QMessageBox::Cancel );
290 if ( msgBox.exec() == QMessageBox::Cancel )
295 QgsSettings settings;
296 settings.
setValue( u
"UI/lastPointCloudFileFilterDir"_s, QFileInfo(
filename() ).absolutePath() );
301void QgsPointCloudLayerSaveAsDialog::mFormatComboBox_currentIndexChanged(
int idx )
313 mAttributesSelection->setEnabled(
true );
318 mAttributesSelection->setEnabled(
false );
326 leLayername->setEnabled(
true );
333 leLayername->setEnabled(
false );
340 mWasAddToCanvasForced = !mAddToCanvas->isChecked();
341 mAddToCanvas->setEnabled(
false );
342 mAddToCanvas->setChecked(
true );
343 mFilename->setEnabled(
false );
351 mAddToCanvas->setEnabled(
true );
352 if ( mWasAddToCanvasForced )
354 mAddToCanvas->setChecked( !mAddToCanvas->isChecked() );
355 mWasAddToCanvasForced =
false;
357 mFilename->setEnabled(
true );
361 if ( mFilename->isEnabled() )
363 mFilename->setFilter( getFilterForFormat( format ) );
367 if ( !mLastUsedFilename.isEmpty() )
369 const thread_local QRegularExpression rx(
"\\.(.*?)[\\s]" );
371 ext = rx.match( getFilterForFormat( format ) ).captured( 1 );
372 if ( !ext.isEmpty() )
374 QFileInfo fi( mLastUsedFilename );
375 mFilename->setFilePath( u
"%1/%2.%3"_s.arg( fi.path(), fi.baseName(), ext ) );
380 if ( !mFilename->isEnabled() )
381 mFilename->setFilePath( QString() );
383 if ( !leLayername->isEnabled() )
385 leLayername->setText( QString() );
387 else if ( leLayername->text().isEmpty() )
389 QString layerName = mDefaultOutputLayerNameFromInputLayerName;
390 if ( layerName.isEmpty() && !mFilename->filePath().isEmpty() )
392 layerName = QFileInfo( mFilename->filePath() ).baseName();
393 leLayername->setDefaultValue( layerName );
395 if ( layerName.isEmpty() )
397 layerName = tr(
"new_layer" );
399 leLayername->setText( layerName );
405void QgsPointCloudLayerSaveAsDialog::mFilterGeometryGroupBoxCheckToggled(
bool checked )
408 mFilterGeometryLayerChanged( mFilterGeometryLayerComboBox->currentLayer() );
411void QgsPointCloudLayerSaveAsDialog::mFilterGeometryLayerChanged(
QgsMapLayer *layer )
413 QgsVectorLayer *vlayer =
dynamic_cast<QgsVectorLayer *
>( layer );
414 mSelectedFeaturesCheckBox->setChecked(
false );
418void QgsPointCloudLayerSaveAsDialog::mMinimumZSpinBoxValueChanged(
const double value )
420 mMaximumZSpinBox->setMinimum( value );
423void QgsPointCloudLayerSaveAsDialog::mMaximumZSpinBoxValueChanged(
const double value )
425 mMinimumZSpinBox->setMaximum( value );
431 mExtentGroupBox->setOutputCrs( mSelectedCrs );
436 return mFilename->filePath();
441 return leLayername->text();
458 for (
int i = 0; i < mAttributeTable->rowCount(); i++ )
460 if ( mAttributeTable->item( i, 0 )->checkState() == Qt::Checked )
462 attributes.append( mAttributeTable->item( i, 0 )->text() );
471 return mAddToCanvas->isChecked();
476 mAddToCanvas->setChecked( enabled );
487 return mExtentGroupBox->isChecked();
492 return mExtentGroupBox->outputExtent();
497 return mFilterGeometryGroupBox->isChecked() && mFilterGeometryLayerComboBox->count() > 0;
502 return mFilterGeometryLayerComboBox->currentLayer();
507 return hasFilterLayer() && mSelectedFeaturesCheckBox->isChecked();
512 return mAttributesSelection->isChecked() && mAttributesSelection->isEnabled();
517 return mZRangeGroupBox->isChecked();
522 return QgsDoubleRange( mMinimumZSpinBox->value(), mMaximumZSpinBox->value() );
527 return mPointsLimitGroupBox->isChecked();
532 return mPointsLimitSpinBox->value();
537 return mActionOnExistingFile;
540void QgsPointCloudLayerSaveAsDialog::mSelectAllAttributes_clicked()
542 for (
int i = 0; i < mAttributeTable->rowCount(); i++ )
544 mAttributeTable->item( i, 0 )->setCheckState( Qt::Checked );
548void QgsPointCloudLayerSaveAsDialog::mDeselectAllAttributes_clicked()
551 for (
int i = 0; i < mAttributeTable->rowCount(); i++ )
553 mAttributeTable->item( i, 0 )->setCheckState( Qt::Unchecked );
558void QgsPointCloudLayerSaveAsDialog::showHelp()
560 QgsHelp::openHelp( u
"managing_data_source/create_layers.html#creating-new-layers-from-an-existing-layer"_s );
568 return u
"LAZ point cloud (*.laz *.LAZ);;LAS point cloud (*.las *.LAS)"_s;
570 return u
"GeoPackage (*.gpkg *.GPKG)"_s;
572 return u
"AutoCAD DXF (*.dxf *.dxf)"_s;
574 return u
"ESRI Shapefile (*.shp *.SHP)"_s;
576 return u
"Comma separated values (*.csv *.CSV)"_s;
588 return tr(
"Temporary Scratch Layer" );
590 return tr(
"GeoPackage" );
592 return tr(
"AutoCAD DXF" );
594 return tr(
"ESRI Shapefile" );
596 return tr(
"LAS/LAZ point cloud" );
598 return tr(
"Comma separated values" );
Represents a coordinate reference system (CRS).
bool isValid() const
Returns true if the datum ensemble is a valid object, or false if it is a null/invalid object.
QString name() const
Display name of datum ensemble.
QgsRange which stores a range of double values.
The QgsSpinBox is a spin box with a clear button that will set the value to the defined clear value.
@ ClearToDefault
Reset value to default value (see defaultValue() ).
static void enableAutoGeometryRestore(QWidget *widget, const QString &key=QString())
Register the widget to allow its position to be automatically saved and restored when open and closed...
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Map canvas is a class for displaying all GIS data types on a canvas.
const QgsMapSettings & mapSettings() const
Gets access to properties used for map rendering.
void layerChanged(QgsMapLayer *layer)
Emitted whenever the currently selected layer changes.
static QString launderLayerName(const QString &name)
Launders a layer's name, converting it into a format which is general suitable for file names or data...
Base class for all map layer types.
QgsCoordinateReferenceSystem crs
QgsRectangle visibleExtent() const
Returns the actual extent derived from requested extent that takes output image size into account.
QgsCoordinateReferenceSystem destinationCrs() const
Returns the destination coordinate reference system for the map render.
ExportFormat
Supported export formats for point clouds.
@ Csv
Comma separated values.
@ Las
LAS/LAZ point cloud.
static QList< ExportFormat > supportedFormats()
Gets a list of the supported export formats.
void setMapCanvas(QgsMapCanvas *canvas)
Sets a map canvas to associate with the dialog.
QgsPointCloudLayerSaveAsDialog(QgsPointCloudLayer *layer, QWidget *parent=nullptr, Qt::WindowFlags fl=Qt::WindowFlags())
Construct a new QgsPointCloudLayerSaveAsDialog.
QString filename() const
Returns the target filename.
bool hasPointsLimit() const
Determines if limiting the number of exported points is enabled.
bool hasFilterLayer() const
Determines if points will be spatially filtered by a layer's features.
QgsRectangle filterExtent() const
Determines the extent to be exported.
bool hasZRange() const
Determines if filtering by Z values is activated.
QgsCoordinateReferenceSystem crsObject() const
Returns the CRS chosen for export.
bool hasAttributes() const
Determines if attributes will be exported as fields.
QgsDoubleRange zRange() const
Determines the Z range of points to be exported.
QgsPointCloudLayerExporter::ExportFormat exportFormat() const
The format in which the export should be written.
bool hasFilterExtent() const
Determines if filtering the export by an extent is activated.
int pointsLimit() const
Determines the limit to the total number of points.
bool filterLayerSelectedOnly() const
Determines if only the selected features from the filterLayer will be used for spatial filtering.
QString layername() const
Returns the target layer name.
void setAddToCanvas(bool checked)
Sets whether the "add to canvas" checkbox should be checked.
bool addToCanvas() const
Returns true if the "add to canvas" checkbox is checked.
QgsMapLayer * filterLayer() const
Returns the layer responsible for spatially filtering points.
QStringList attributes() const
Returns a list of attributes which are selected for saving.
QgsVectorFileWriter::ActionOnExistingFile creationActionOnExistingFile() const
Returns creation action.
Represents a map layer supporting display of point clouds.
QgsRectangle extent() const override
Returns the extent of the layer.
QgsPointCloudAttributeCollection attributes() const
Returns the attributes available from the layer.
QList< QgsProviderSublayerDetails > querySublayers(const QString &uri, Qgis::SublayerQueryFlags flags=Qgis::SublayerQueryFlags(), QgsFeedback *feedback=nullptr) const
Queries the specified uri and returns a list of any valid sublayers found in the dataset which can be...
static QgsProviderRegistry * instance(const QString &pluginPath=QString())
Means of accessing canonical single instance.
A rectangle specified with double values.
Stores settings for use within QGIS.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
@ CanAppendToExistingLayer
Flag to indicate that new features can be added to an existing layer.
@ CanAddNewLayer
Flag to indicate that a new layer can be added to the dataset.
@ CanDeleteLayer
Flag to indicate that an existing layer can be deleted.
static QgsVectorFileWriter::EditionCapabilities editionCapabilities(const QString &datasetName)
Returns edition capabilities for an existing dataset name.
QFlags< EditionCapability > EditionCapabilities
Combination of CanAddNewLayer, CanAppendToExistingLayer, CanAddNewFieldsToExistingLayer or CanDeleteL...
static bool targetLayerExists(const QString &datasetName, const QString &layerName)
Returns whether the target layer already exists.
ActionOnExistingFile
Enumeration to describe how to handle existing files.
@ CreateOrOverwriteLayer
Create or overwrite layer.
@ CreateOrOverwriteFile
Create or overwrite file.
@ AppendToLayerNoNewFields
Append features to existing layer, but do not create new fields.
int selectedFeatureCount() const
Returns the number of features that are selected in this layer.