31#include <QRegularExpression>
33#include "moc_qgspointcloudlayersaveasdialog.cpp"
36 : QDialog( parent, fl )
41 mSelectedCrs = layer->
crs();
42 mLayerExtent = layer->
extent();
47void QgsPointCloudLayerSaveAsDialog::setup()
52 connect( mFormatComboBox,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsPointCloudLayerSaveAsDialog::mFormatComboBox_currentIndexChanged );
54 connect( mSelectAllAttributes, &QPushButton::clicked,
this, &QgsPointCloudLayerSaveAsDialog::mSelectAllAttributes_clicked );
55 connect( mDeselectAllAttributes, &QPushButton::clicked,
this, &QgsPointCloudLayerSaveAsDialog::mDeselectAllAttributes_clicked );
57 connect( mFilterGeometryGroupBox, &QgsCollapsibleGroupBox::toggled,
this, &QgsPointCloudLayerSaveAsDialog::mFilterGeometryGroupBoxCheckToggled );
58 connect( mMinimumZSpinBox,
static_cast<void (
QgsDoubleSpinBox::* )(
double )
>( &QgsDoubleSpinBox::valueChanged ),
this, &QgsPointCloudLayerSaveAsDialog::mMinimumZSpinBoxValueChanged );
59 connect( mMaximumZSpinBox,
static_cast<void (
QgsDoubleSpinBox::* )(
double )
>( &QgsDoubleSpinBox::valueChanged ),
this, &QgsPointCloudLayerSaveAsDialog::mMaximumZSpinBoxValueChanged );
62 mHelpButtonBox->setVisible(
false );
63 mButtonBox->addButton( QDialogButtonBox::Help );
64 connect( mButtonBox, &QDialogButtonBox::helpRequested,
this, &QgsPointCloudLayerSaveAsDialog::showHelp );
66 connect( mHelpButtonBox, &QDialogButtonBox::helpRequested,
this, &QgsPointCloudLayerSaveAsDialog::showHelp );
68 connect( mButtonBox, &QDialogButtonBox::accepted,
this, &QgsPointCloudLayerSaveAsDialog::accept );
69 connect( mButtonBox, &QDialogButtonBox::rejected,
this, &QgsPointCloudLayerSaveAsDialog::reject );
71 mFormatComboBox->blockSignals(
true );
73 for (
const auto &format : supportedFormats )
74 mFormatComboBox->addItem( getTranslatedNameForFormat( format ),
static_cast<int>( format ) );
77 const int defaultFormat = settings.
value( QStringLiteral(
"UI/lastPointCloudFormat" ), 0 ).toInt();
78 mFormatComboBox->setCurrentIndex( mFormatComboBox->findData( defaultFormat ) );
79 mFormatComboBox->blockSignals(
false );
80 mFormatComboBox_currentIndexChanged( 0 );
82 mCrsSelector->setCrs( mSelectedCrs );
83 mCrsSelector->setLayerCrs( mSelectedCrs );
84 mCrsSelector->setMessage( tr(
"Select the coordinate reference system for the vector file. "
85 "The data points will be transformed from the layer coordinate reference system." ) );
92 QStringList availableAttributes;
95 const QString attribute =
attributes.at( i ).name();
96 if ( attribute.compare( QLatin1String(
"X" ), Qt::CaseInsensitive ) && attribute.compare( QLatin1String(
"Y" ), Qt::CaseInsensitive ) && attribute.compare( QLatin1String(
"Z" ), Qt::CaseInsensitive ) )
98 availableAttributes.append( attribute );
102 mAttributeTable->setRowCount( availableAttributes.count() );
103 QStringList horizontalHeaders = QStringList() << tr(
"Attribute" );
104 mAttributeTable->setColumnCount( horizontalHeaders.size() );
105 mAttributeTable->setHorizontalHeaderLabels( horizontalHeaders );
107 for (
int i = 0; i < availableAttributes.count(); ++i )
109 QTableWidgetItem *item =
new QTableWidgetItem( availableAttributes.at( i ) );
110 item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsUserCheckable );
111 item->setCheckState( Qt::Checked );
112 mAttributeTable->setItem( i, 0, item );
114 mAttributeTable->resizeColumnsToContents();
118 mExtentGroupBox->setOutputCrs( mSelectedCrs );
119 mExtentGroupBox->setOriginalExtent( mLayerExtent, mSelectedCrs );
120 mExtentGroupBox->setOutputExtentFromOriginal();
121 mExtentGroupBox->setCheckable(
true );
122 mExtentGroupBox->setChecked(
false );
123 mExtentGroupBox->setCollapsed(
true );
129 mMinimumZSpinBox->setRange( std::numeric_limits<double>::lowest(), std::numeric_limits<double>::max() );
130 mMaximumZSpinBox->setRange( std::numeric_limits<double>::lowest(), std::numeric_limits<double>::max() );
133 mMinimumZSpinBox->setValue( mLayer->statistics().minimum( QStringLiteral(
"Z" ) ) );
134 mMinimumZSpinBox->setClearValue( mMinimumZSpinBox->value() );
135 mMaximumZSpinBox->setValue( mLayer->statistics().maximum( QStringLiteral(
"Z" ) ) );
136 mMaximumZSpinBox->setClearValue( mMaximumZSpinBox->value() );
140 mPointsLimitSpinBox->setMinimum( 1 );
141 mPointsLimitSpinBox->setMaximum( std::numeric_limits<int>::max() );
142 mPointsLimitSpinBox->setValue( 1e6 );
143 mPointsLimitSpinBox->setClearValue( 1e6 );
146 mFilename->setDialogTitle( tr(
"Save Layer As" ) );
147 mFilename->setDefaultRoot( settings.
value( QStringLiteral(
"UI/lastPointCloudFileFilterDir" ), QDir::homePath() ).toString() );
148 mFilename->setConfirmOverwrite(
false );
150 QgsSettings settings;
151 if ( !filePath.isEmpty() )
152 mLastUsedFilename = filePath;
154 const QFileInfo fileInfo( filePath );
155 settings.
setValue( QStringLiteral(
"UI/lastPointCloudFileFilterDir" ), fileInfo.absolutePath() );
157 if ( mDefaultOutputLayerNameFromInputLayerName.isEmpty() )
159 leLayername->setDefaultValue( suggestedLayerName );
163 if ( leLayername->text().isEmpty() && !filePath.isEmpty() && leLayername->isEnabled() )
165 leLayername->setText( suggestedLayerName );
167 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled( !filePath.isEmpty() );
172 const QgsDatumEnsemble ensemble = mSelectedCrs.datumEnsemble();
175 mCrsSelector->setSourceEnsemble( ensemble.
name() );
178 catch ( QgsNotSupportedException & )
182 mCrsSelector->setShowAccuracyWarnings(
true );
189 leLayername->setDefaultValue( mDefaultOutputLayerNameFromInputLayerName );
191 if ( leLayername->isEnabled() )
192 leLayername->setText( mDefaultOutputLayerNameFromInputLayerName );
198void QgsPointCloudLayerSaveAsDialog::accept()
205 msgBox.setIcon( QMessageBox::Question );
206 msgBox.setWindowTitle( tr(
"Save Point Cloud Layer As" ) );
207 QPushButton *overwriteFileButton = msgBox.addButton( tr(
"Overwrite File" ), QMessageBox::ActionRole );
208 QPushButton *overwriteLayerButton = msgBox.addButton( tr(
"Overwrite Layer" ), QMessageBox::ActionRole );
209 QPushButton *appendToLayerButton = msgBox.addButton( tr(
"Append to Layer" ), QMessageBox::ActionRole );
210 msgBox.setStandardButtons( QMessageBox::Cancel );
211 msgBox.setDefaultButton( QMessageBox::Cancel );
212 overwriteFileButton->hide();
213 overwriteLayerButton->hide();
214 appendToLayerButton->hide();
219 msgBox.setText( tr(
"The layer already exists. Do you want to overwrite the whole file or overwrite the layer?" ) );
220 overwriteFileButton->setVisible(
true );
221 overwriteLayerButton->setVisible(
true );
225 msgBox.setText( tr(
"The file already exists. Do you want to overwrite it?" ) );
226 overwriteFileButton->setVisible(
true );
230 msgBox.setText( tr(
"The layer already exists. Do you want to overwrite the whole file, overwrite the layer or append features to the layer?" ) );
231 appendToLayerButton->setVisible(
true );
232 overwriteFileButton->setVisible(
true );
233 overwriteLayerButton->setVisible(
true );
237 msgBox.setText( tr(
"The layer already exists. Do you want to overwrite the whole file or append features to the layer?" ) );
238 appendToLayerButton->setVisible(
true );
239 overwriteFileButton->setVisible(
true );
242 int ret = msgBox.exec();
243 if ( ret == QMessageBox::Cancel )
245 if ( msgBox.clickedButton() == overwriteFileButton )
247 else if ( msgBox.clickedButton() == overwriteLayerButton )
249 else if ( msgBox.clickedButton() == appendToLayerButton )
261 if ( QMessageBox::question(
this, tr(
"Save Point Cloud Layer As" ), tr(
"The file already exists. Do you want to overwrite it?" ) ) == QMessageBox::NoButton )
272 QStringList layerList;
273 layerList.reserve( sublayers.size() );
274 for (
const QgsProviderSublayerDetails &sublayer : sublayers )
276 layerList.append( sublayer.name() );
278 if ( layerList.length() > 1 )
280 layerList.sort( Qt::CaseInsensitive );
282 msgBox.setIcon( QMessageBox::Warning );
283 msgBox.setWindowTitle( tr(
"Overwrite File" ) );
284 msgBox.setText( tr(
"This file contains %1 layers that will be lost!\n" ).arg( QLocale().toString( layerList.length() ) ) );
285 msgBox.setDetailedText( tr(
"The following layers will be permanently lost:\n\n%1" ).arg( layerList.join(
"\n" ) ) );
286 msgBox.setStandardButtons( QMessageBox::Ok | QMessageBox::Cancel );
287 if ( msgBox.exec() == QMessageBox::Cancel )
292 QgsSettings settings;
293 settings.
setValue( QStringLiteral(
"UI/lastPointCloudFileFilterDir" ), QFileInfo(
filename() ).absolutePath() );
294 settings.
setValue( QStringLiteral(
"UI/lastPointCloudFormat" ),
static_cast<int>(
exportFormat() ) );
298void QgsPointCloudLayerSaveAsDialog::mFormatComboBox_currentIndexChanged(
int idx )
310 mAttributesSelection->setEnabled(
true );
315 mAttributesSelection->setEnabled(
false );
323 leLayername->setEnabled(
true );
330 leLayername->setEnabled(
false );
337 mWasAddToCanvasForced = !mAddToCanvas->isChecked();
338 mAddToCanvas->setEnabled(
false );
339 mAddToCanvas->setChecked(
true );
340 mFilename->setEnabled(
false );
348 mAddToCanvas->setEnabled(
true );
349 if ( mWasAddToCanvasForced )
351 mAddToCanvas->setChecked( !mAddToCanvas->isChecked() );
352 mWasAddToCanvasForced =
false;
354 mFilename->setEnabled(
true );
358 if ( mFilename->isEnabled() )
360 mFilename->setFilter( getFilterForFormat( format ) );
364 if ( !mLastUsedFilename.isEmpty() )
366 const thread_local QRegularExpression rx(
"\\.(.*?)[\\s]" );
368 ext = rx.match( getFilterForFormat( format ) ).captured( 1 );
369 if ( !ext.isEmpty() )
371 QFileInfo fi( mLastUsedFilename );
372 mFilename->setFilePath( QStringLiteral(
"%1/%2.%3" ).arg( fi.path(), fi.baseName(), ext ) );
377 if ( !mFilename->isEnabled() )
378 mFilename->setFilePath( QString() );
380 if ( !leLayername->isEnabled() )
382 leLayername->setText( QString() );
384 else if ( leLayername->text().isEmpty() )
386 QString layerName = mDefaultOutputLayerNameFromInputLayerName;
387 if ( layerName.isEmpty() && !mFilename->filePath().isEmpty() )
389 layerName = QFileInfo( mFilename->filePath() ).baseName();
390 leLayername->setDefaultValue( layerName );
392 if ( layerName.isEmpty() )
394 layerName = tr(
"new_layer" );
396 leLayername->setText( layerName );
402void QgsPointCloudLayerSaveAsDialog::mFilterGeometryGroupBoxCheckToggled(
bool checked )
405 mFilterGeometryLayerChanged( mFilterGeometryLayerComboBox->currentLayer() );
408void QgsPointCloudLayerSaveAsDialog::mFilterGeometryLayerChanged(
QgsMapLayer *layer )
410 QgsVectorLayer *vlayer =
dynamic_cast<QgsVectorLayer *
>( layer );
411 mSelectedFeaturesCheckBox->setChecked(
false );
415void QgsPointCloudLayerSaveAsDialog::mMinimumZSpinBoxValueChanged(
const double value )
417 mMaximumZSpinBox->setMinimum( value );
420void QgsPointCloudLayerSaveAsDialog::mMaximumZSpinBoxValueChanged(
const double value )
422 mMinimumZSpinBox->setMaximum( value );
428 mExtentGroupBox->setOutputCrs( mSelectedCrs );
433 return mFilename->filePath();
438 return leLayername->text();
455 for (
int i = 0; i < mAttributeTable->rowCount(); i++ )
457 if ( mAttributeTable->item( i, 0 )->checkState() == Qt::Checked )
459 attributes.append( mAttributeTable->item( i, 0 )->text() );
468 return mAddToCanvas->isChecked();
473 mAddToCanvas->setChecked( enabled );
484 return mExtentGroupBox->isChecked();
489 return mExtentGroupBox->outputExtent();
494 return mFilterGeometryGroupBox->isChecked() && mFilterGeometryLayerComboBox->count() > 0;
499 return mFilterGeometryLayerComboBox->currentLayer();
504 return hasFilterLayer() && mSelectedFeaturesCheckBox->isChecked();
509 return mAttributesSelection->isChecked() && mAttributesSelection->isEnabled();
514 return mZRangeGroupBox->isChecked();
519 return QgsDoubleRange( mMinimumZSpinBox->value(), mMaximumZSpinBox->value() );
524 return mPointsLimitGroupBox->isChecked();
529 return mPointsLimitSpinBox->value();
534 return mActionOnExistingFile;
537void QgsPointCloudLayerSaveAsDialog::mSelectAllAttributes_clicked()
539 for (
int i = 0; i < mAttributeTable->rowCount(); i++ )
541 mAttributeTable->item( i, 0 )->setCheckState( Qt::Checked );
545void QgsPointCloudLayerSaveAsDialog::mDeselectAllAttributes_clicked()
548 for (
int i = 0; i < mAttributeTable->rowCount(); i++ )
550 mAttributeTable->item( i, 0 )->setCheckState( Qt::Unchecked );
555void QgsPointCloudLayerSaveAsDialog::showHelp()
557 QgsHelp::openHelp( QStringLiteral(
"managing_data_source/create_layers.html#creating-new-layers-from-an-existing-layer" ) );
565 return QStringLiteral(
"LAZ point cloud (*.laz *.LAZ);;LAS point cloud (*.las *.LAS)" );
567 return QStringLiteral(
"GeoPackage (*.gpkg *.GPKG)" );
569 return QStringLiteral(
"AutoCAD DXF (*.dxf *.dxf)" );
571 return QStringLiteral(
"ESRI Shapefile (*.shp *.SHP)" );
573 return QStringLiteral(
"Comma separated values (*.csv *.CSV)" );
585 return tr(
"Temporary Scratch Layer" );
587 return tr(
"GeoPackage" );
589 return tr(
"AutoCAD DXF" );
591 return tr(
"ESRI Shapefile" );
593 return tr(
"LAS/LAZ point cloud" );
595 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.