36 #include <QPushButton>
38 #include <QMessageBox>
39 #include <QFileDialog>
43 #include <ogr_srs_api.h>
44 #include <gdal_version.h>
45 #include <cpl_error.h>
46 #include <cpl_string.h>
48 #define DEFAULT_OGR_FID_COLUMN_TITLE "fid" // default value from OGR
51 : QDialog( parent, fl )
56 connect( mAddAttributeButton, &QToolButton::clicked,
this, &QgsNewGeoPackageLayerDialog::mAddAttributeButton_clicked );
57 connect( mRemoveAttributeButton, &QToolButton::clicked,
this, &QgsNewGeoPackageLayerDialog::mRemoveAttributeButton_clicked );
58 connect( mFieldTypeBox,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsNewGeoPackageLayerDialog::mFieldTypeBox_currentIndexChanged );
59 connect( mGeometryTypeBox,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsNewGeoPackageLayerDialog::mGeometryTypeBox_currentIndexChanged );
60 connect( mTableNameEdit, &QLineEdit::textChanged,
this, &QgsNewGeoPackageLayerDialog::mTableNameEdit_textChanged );
61 connect( mTableNameEdit, &QLineEdit::textEdited,
this, &QgsNewGeoPackageLayerDialog::mTableNameEdit_textEdited );
62 connect( mLayerIdentifierEdit, &QLineEdit::textEdited,
this, &QgsNewGeoPackageLayerDialog::mLayerIdentifierEdit_textEdited );
63 connect( buttonBox, &QDialogButtonBox::accepted,
this, &QgsNewGeoPackageLayerDialog::buttonBox_accepted );
64 connect( buttonBox, &QDialogButtonBox::rejected,
this, &QgsNewGeoPackageLayerDialog::buttonBox_rejected );
65 connect( buttonBox, &QDialogButtonBox::helpRequested,
this, &QgsNewGeoPackageLayerDialog::showHelp );
74 mGeometryTypeBox->addItem(
QgsApplication::getThemeIcon( QStringLiteral(
"/mIconPointLayer.svg" ) ), tr(
"MultiPoint" ), wkbMultiPoint );
75 mGeometryTypeBox->addItem(
QgsApplication::getThemeIcon( QStringLiteral(
"/mIconLineLayer.svg" ) ), tr(
"MultiLine" ), wkbMultiLineString );
76 mGeometryTypeBox->addItem(
QgsApplication::getThemeIcon( QStringLiteral(
"/mIconPolygonLayer.svg" ) ), tr(
"MultiPolygon" ), wkbMultiPolygon );
80 mGeometryTypeBox->addItem(
QgsApplication::getThemeIcon( QStringLiteral(
"/mIconLineLayer.svg" ) ), tr(
"CircularString" ), wkbCircularString );
82 mGeometryTypeBox->addItem(
QgsApplication::getThemeIcon( QStringLiteral(
"/mIconLineLayer.svg" ) ), tr(
"CompoundCurve" ), wkbCompoundCurve );
83 mGeometryTypeBox->addItem(
QgsApplication::getThemeIcon( QStringLiteral(
"/mIconPolygonLayer.svg" ) ), tr(
"CurvePolygon" ), wkbCurvePolygon );
85 mGeometryTypeBox->addItem(
QgsApplication::getThemeIcon( QStringLiteral(
"/mIconPolygonLayer.svg" ) ), tr(
"MultiSurface" ), wkbMultiSurface );
86 mGeometryTypeBox->setCurrentIndex( -1 );
88 mGeometryWithZCheckBox->setEnabled(
false );
89 mGeometryWithMCheckBox->setEnabled(
false );
90 mGeometryColumnEdit->setEnabled(
false );
91 mGeometryColumnEdit->setText( QStringLiteral(
"geometry" ) );
93 mCheckBoxCreateSpatialIndex->setEnabled(
false );
94 mCrsSelector->setEnabled(
false );
97 mFieldTypeBox->addItem(
QgsApplication::getThemeIcon( QStringLiteral(
"/mIconFieldInteger.svg" ) ), tr(
"Whole Number (integer)" ),
"integer" );
98 mFieldTypeBox->addItem(
QgsApplication::getThemeIcon( QStringLiteral(
"/mIconFieldInteger.svg" ) ), tr(
"Whole Number (integer 64 bit)" ),
"integer64" );
99 mFieldTypeBox->addItem(
QgsApplication::getThemeIcon( QStringLiteral(
"/mIconFieldFloat.svg" ) ), tr(
"Decimal Number (real)" ),
"real" );
101 mFieldTypeBox->addItem(
QgsApplication::getThemeIcon( QStringLiteral(
"/mIconFieldDateTime.svg" ) ), tr(
"Date and Time" ),
"datetime" );
105 mOkButton = buttonBox->button( QDialogButtonBox::Ok );
106 mOkButton->setEnabled(
false );
108 connect( mFieldNameEdit, &QLineEdit::textChanged,
this, &QgsNewGeoPackageLayerDialog::fieldNameChanged );
109 connect( mAttributeView, &QTreeWidget::itemSelectionChanged,
this, &QgsNewGeoPackageLayerDialog::selectionChanged );
110 connect( mTableNameEdit, &QLineEdit::textChanged,
this, &QgsNewGeoPackageLayerDialog::checkOk );
111 connect( mGeometryTypeBox,
static_cast<void( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsNewGeoPackageLayerDialog::checkOk );
113 mAddAttributeButton->setEnabled(
false );
114 mRemoveAttributeButton->setEnabled(
false );
116 mCheckBoxCreateSpatialIndex->setChecked(
true );
120 mDatabase->setFilter( tr(
"GeoPackage" ) +
" (*.gpkg)" );
121 mDatabase->setDialogTitle( tr(
"Select Existing or Create a New GeoPackage Database Fileā¦" ) );
122 mDatabase->setDefaultRoot( settings.
value( QStringLiteral(
"UI/lastVectorFileFilterDir" ), QDir::homePath() ).toString() );
123 mDatabase->setConfirmOverwrite(
false );
127 QFileInfo tmplFileInfo( filePath );
128 settings.
setValue( QStringLiteral(
"UI/lastVectorFileFilterDir" ), tmplFileInfo.absolutePath() );
129 if ( !filePath.isEmpty() && !mTableNameEdited )
131 QFileInfo fileInfo( filePath );
132 mTableNameEdit->setText( fileInfo.baseName() );
139 QCompleter *completer =
new QCompleter(
this );
140 completer->setModel( ogrProviderModel );
142 completer->setCompletionMode( QCompleter::PopupCompletion );
143 completer->setFilterMode( Qt::MatchContains );
144 mDatabase->lineEdit()->setCompleter( completer );
149 mCrsSelector->setCrs(
crs );
154 mDatabase->setReadOnly(
true );
157 void QgsNewGeoPackageLayerDialog::mFieldTypeBox_currentIndexChanged(
int )
159 QString myType = mFieldTypeBox->currentData( Qt::UserRole ).toString();
160 mFieldLengthEdit->setEnabled( myType == QLatin1String(
"text" ) );
161 if ( myType != QLatin1String(
"text" ) )
162 mFieldLengthEdit->clear();
166 void QgsNewGeoPackageLayerDialog::mGeometryTypeBox_currentIndexChanged(
int )
168 OGRwkbGeometryType geomType =
static_cast<OGRwkbGeometryType
>
169 ( mGeometryTypeBox->currentData( Qt::UserRole ).toInt() );
170 bool isSpatial = geomType != wkbNone;
171 mGeometryWithZCheckBox->setEnabled( isSpatial );
172 mGeometryWithMCheckBox->setEnabled( isSpatial );
173 mGeometryColumnEdit->setEnabled( isSpatial );
174 mCheckBoxCreateSpatialIndex->setEnabled( isSpatial );
175 mCrsSelector->setEnabled( isSpatial );
178 void QgsNewGeoPackageLayerDialog::mTableNameEdit_textChanged(
const QString &text )
180 mTableNameEdited = !text.isEmpty();
181 if ( !text.isEmpty() && !mLayerIdentifierEdited )
183 mLayerIdentifierEdit->setText( text );
187 void QgsNewGeoPackageLayerDialog::mTableNameEdit_textEdited(
const QString &text )
190 mTableNameEdited = !text.isEmpty();
193 void QgsNewGeoPackageLayerDialog::mLayerIdentifierEdit_textEdited(
const QString &text )
196 mLayerIdentifierEdited = !text.isEmpty();
199 void QgsNewGeoPackageLayerDialog::checkOk()
201 bool ok = !mDatabase->filePath().isEmpty() &&
202 !mTableNameEdit->text().isEmpty() &&
203 mGeometryTypeBox->currentIndex() != -1;
205 mOkButton->setEnabled( ok );
208 void QgsNewGeoPackageLayerDialog::mAddAttributeButton_clicked()
210 if ( !mFieldNameEdit->text().isEmpty() )
212 QString myName = mFieldNameEdit->text();
213 const QString featureId = mFeatureIdColumnEdit->text().isEmpty() ? QStringLiteral(
DEFAULT_OGR_FID_COLUMN_TITLE ) : mFeatureIdColumnEdit->text();
214 if ( myName.compare( featureId, Qt::CaseInsensitive ) == 0 )
216 QMessageBox::critical(
this, tr(
"Add Field" ), tr(
"The field cannot have the same name as the feature identifier." ) );
221 QString myType = mFieldTypeBox->currentData( Qt::UserRole ).toString();
222 QString length = mFieldLengthEdit->text();
223 mAttributeView->addTopLevelItem(
new QTreeWidgetItem( QStringList() << myName << myType << length ) );
227 mFieldNameEdit->clear();
231 void QgsNewGeoPackageLayerDialog::mRemoveAttributeButton_clicked()
233 delete mAttributeView->currentItem();
238 void QgsNewGeoPackageLayerDialog::fieldNameChanged(
const QString &name )
240 mAddAttributeButton->setDisabled( name.isEmpty() || ! mAttributeView->findItems( name, Qt::MatchExactly ).isEmpty() );
243 void QgsNewGeoPackageLayerDialog::selectionChanged()
245 mRemoveAttributeButton->setDisabled( mAttributeView->selectedItems().isEmpty() );
248 void QgsNewGeoPackageLayerDialog::buttonBox_accepted()
254 void QgsNewGeoPackageLayerDialog::buttonBox_rejected()
259 bool QgsNewGeoPackageLayerDialog::apply()
261 QString fileName( mDatabase->filePath() );
262 if ( !fileName.endsWith( QLatin1String(
".gpkg" ), Qt::CaseInsensitive ) )
263 fileName += QLatin1String(
".gpkg" );
265 bool createNewDb =
false;
267 if ( QFile( fileName ).exists( fileName ) )
269 bool overwrite =
false;
276 msgBox.setIcon( QMessageBox::Question );
277 msgBox.setWindowTitle( tr(
"New GeoPackage Layer" ) );
278 msgBox.setText( tr(
"The File already exists. Do you want to overwrite the existing file with a new database or add a new layer to it?" ) );
279 QPushButton *overwriteButton = msgBox.addButton( tr(
"Overwrite" ), QMessageBox::ActionRole );
280 QPushButton *addNewLayerButton = msgBox.addButton( tr(
"Add New Layer" ), QMessageBox::ActionRole );
281 msgBox.setStandardButtons( QMessageBox::Cancel );
282 msgBox.setDefaultButton( addNewLayerButton );
284 if ( property(
"hideDialogs" ).toBool() )
286 overwrite = property(
"question_existing_db_answer_overwrite" ).toBool();
288 cancel = !property(
"question_existing_db_answer_add_new_layer" ).toBool();
292 int ret = msgBox.exec();
293 if ( ret == QMessageBox::Cancel )
295 if ( msgBox.clickedButton() == overwriteButton )
316 QFile( fileName ).remove();
325 OGRSFDriverH hGpkgDriver = OGRGetDriverByName(
"GPKG" );
328 if ( !property(
"hideDialogs" ).toBool() )
329 QMessageBox::critical(
this, tr(
"New GeoPackage Layer" ),
330 tr(
"Layer creation failed. GeoPackage driver not found." ) );
337 hDS.reset( OGR_Dr_CreateDataSource( hGpkgDriver, fileName.toUtf8().constData(),
nullptr ) );
340 QString msg( tr(
"Creation of database failed (OGR error: %1)" ).arg( QString::fromUtf8( CPLGetLastErrorMsg() ) ) );
341 if ( !property(
"hideDialogs" ).toBool() )
342 QMessageBox::critical(
this, tr(
"New GeoPackage Layer" ), msg );
348 OGRSFDriverH hDriver =
nullptr;
349 hDS.reset( OGROpen( fileName.toUtf8().constData(),
true, &hDriver ) );
352 QString msg( tr(
"Opening of database failed (OGR error: %1)" ).arg( QString::fromUtf8( CPLGetLastErrorMsg() ) ) );
353 if ( !property(
"hideDialogs" ).toBool() )
354 QMessageBox::critical(
this, tr(
"New GeoPackage Layer" ), msg );
357 if ( hDriver != hGpkgDriver )
359 QString msg( tr(
"Opening of file succeeded, but this is not a GeoPackage database." ) );
360 if ( !property(
"hideDialogs" ).toBool() )
361 QMessageBox::critical(
this, tr(
"New GeoPackage Layer" ), msg );
366 QString tableName( mTableNameEdit->text() );
368 bool overwriteTable =
false;
369 if ( OGR_DS_GetLayerByName( hDS.get(), tableName.toUtf8().constData() ) )
371 if ( property(
"hideDialogs" ).toBool() )
373 overwriteTable = property(
"question_existing_layer_answer_overwrite" ).toBool();
375 else if ( QMessageBox::question(
this, tr(
"New GeoPackage Layer" ),
376 tr(
"A table with the same name already exists. Do you want to overwrite it?" ),
377 QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) == QMessageBox::Yes )
379 overwriteTable =
true;
382 if ( !overwriteTable )
388 QString layerIdentifier( mLayerIdentifierEdit->text() );
389 QString layerDescription( mLayerDescriptionEdit->text() );
391 OGRwkbGeometryType wkbType =
static_cast<OGRwkbGeometryType
>
392 ( mGeometryTypeBox->currentData( Qt::UserRole ).toInt() );
395 if ( mGeometryWithZCheckBox->isChecked() )
396 wkbType = OGR_GT_SetZ( wkbType );
398 if ( mGeometryWithMCheckBox->isChecked() )
399 wkbType = OGR_GT_SetM( wkbType );
404 if ( wkbType != wkbNone && srs.
isValid() )
407 hSRS = OSRNewSpatialReference( srsWkt.toLocal8Bit().data() );
411 char **options =
nullptr;
413 if ( overwriteTable )
414 options = CSLSetNameValue( options,
"OVERWRITE",
"YES" );
415 if ( !layerIdentifier.isEmpty() )
416 options = CSLSetNameValue( options,
"IDENTIFIER", layerIdentifier.toUtf8().constData() );
417 if ( !layerDescription.isEmpty() )
418 options = CSLSetNameValue( options,
"DESCRIPTION", layerDescription.toUtf8().constData() );
420 QString featureId( mFeatureIdColumnEdit->text() );
421 if ( !featureId.isEmpty() )
422 options = CSLSetNameValue( options,
"FID", featureId.toUtf8().constData() );
424 QString geometryColumn( mGeometryColumnEdit->text() );
425 if ( wkbType != wkbNone && !geometryColumn.isEmpty() )
426 options = CSLSetNameValue( options,
"GEOMETRY_COLUMN", geometryColumn.toUtf8().constData() );
428 if ( wkbType != wkbNone )
429 options = CSLSetNameValue( options,
"SPATIAL_INDEX", mCheckBoxCreateSpatialIndex->isChecked() ?
"YES" :
"NO" );
431 OGRLayerH hLayer = OGR_DS_CreateLayer( hDS.get(), tableName.toUtf8().constData(), hSRS, wkbType, options );
432 CSLDestroy( options );
437 QString msg( tr(
"Creation of layer failed (OGR error: %1)" ).arg( QString::fromUtf8( CPLGetLastErrorMsg() ) ) );
438 if ( !property(
"hideDialogs" ).toBool() )
439 QMessageBox::critical(
this, tr(
"New GeoPackage Layer" ), msg );
443 QTreeWidgetItemIterator it( mAttributeView );
446 QString fieldName( ( *it )->text( 0 ) );
447 QString fieldType( ( *it )->text( 1 ) );
448 QString fieldWidth( ( *it )->text( 2 ) );
451 OGRFieldType ogrType( OFTString );
452 if ( fieldType == QLatin1String(
"text" ) )
454 else if ( fieldType == QLatin1String(
"integer" ) )
455 ogrType = OFTInteger;
456 else if ( fieldType == QLatin1String(
"integer64" ) )
457 ogrType = OFTInteger64;
458 else if ( fieldType == QLatin1String(
"real" ) )
460 else if ( fieldType == QLatin1String(
"date" ) )
462 else if ( fieldType == QLatin1String(
"datetime" ) )
463 ogrType = OFTDateTime;
464 else if ( fieldType == QLatin1String(
"bool" ) )
466 ogrType = OFTInteger;
469 else if ( fieldType == QLatin1String(
"binary" ) )
472 int ogrWidth = fieldWidth.toInt();
475 if ( ogrType != OFTBinary )
476 OGR_Fld_SetWidth( fld.get(), ogrWidth );
478 OGR_Fld_SetSubType( fld.get(), OFSTBoolean );
480 if ( OGR_L_CreateField( hLayer, fld.get(),
true ) != OGRERR_NONE )
482 if ( !property(
"hideDialogs" ).toBool() )
484 QMessageBox::critical(
this, tr(
"New GeoPackage Layer" ),
485 tr(
"Creation of field %1 failed (OGR error: %2)" )
486 .arg( fieldName, QString::fromUtf8( CPLGetLastErrorMsg() ) ) );
497 OGR_L_ResetReading( hLayer );
498 if ( CPLGetLastErrorType() != CE_None )
500 QString msg( tr(
"Creation of layer failed (OGR error: %1)" ).arg( QString::fromUtf8( CPLGetLastErrorMsg() ) ) );
501 if ( !property(
"hideDialogs" ).toBool() )
502 QMessageBox::critical(
this, tr(
"New GeoPackage Layer" ), msg );
507 QString uri( QStringLiteral(
"%1|layername=%2" ).arg( fileName, tableName ) );
508 QString userVisiblelayerName( layerIdentifier.isEmpty() ? tableName : layerIdentifier );
510 std::unique_ptr< QgsVectorLayer > layer = qgis::make_unique< QgsVectorLayer >( uri, userVisiblelayerName, QStringLiteral(
"ogr" ), layerOptions );
511 if ( layer->isValid() )
516 QList<QgsMapLayer *> myList;
517 myList << layer.release();
530 if ( !property(
"hideDialogs" ).toBool() )
531 QMessageBox::critical(
this, tr(
"New GeoPackage Layer" ), tr(
"%1 is an invalid layer and cannot be loaded." ).arg( tableName ) );
539 mBehavior = behavior;
544 mAddToProject = addToProject;
547 void QgsNewGeoPackageLayerDialog::showHelp()
549 QgsHelp::openHelp( QStringLiteral(
"managing_data_source/create_layers.html#creating-a-new-geopackage-layer" ) );