Skip to content

Commit ad25c24

Browse files
authoredMay 6, 2020
Merge pull request #36202 from alexbruy/geometryless-shapefile
allow creating geometryless DBF tables from the New Shapefile dialog (fix #15654)
2 parents 6ccf90f + a162cc2 commit ad25c24

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed
 

‎src/core/providers/ogr/qgsogrprovider.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3492,9 +3492,10 @@ bool QgsOgrProviderUtils::createEmptyDataSource( const QString &uri,
34923492
if ( driverName == QLatin1String( "ESRI Shapefile" ) )
34933493
{
34943494
if ( !uri.endsWith( QLatin1String( ".shp" ), Qt::CaseInsensitive ) &&
3495-
!uri.endsWith( QLatin1String( ".shz" ), Qt::CaseInsensitive ) )
3495+
!uri.endsWith( QLatin1String( ".shz" ), Qt::CaseInsensitive ) &&
3496+
!uri.endsWith( QLatin1String( ".dbf" ), Qt::CaseInsensitive ) )
34963497
{
3497-
errorMessage = QObject::tr( "URI %1 doesn't end with .shp" ).arg( uri );
3498+
errorMessage = QObject::tr( "URI %1 doesn't end with .shp or .dbf" ).arg( uri );
34983499
QgsDebugMsg( errorMessage );
34993500
return false;
35003501
}

‎src/gui/qgsnewgeopackagelayerdialog.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ QgsNewGeoPackageLayerDialog::QgsNewGeoPackageLayerDialog( QWidget *parent, Qt::W
6767
mAddAttributeButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mActionNewAttribute.svg" ) ) );
6868
mRemoveAttributeButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mActionDeleteAttribute.svg" ) ) );
6969

70-
mGeometryTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconTableLayer.svg" ) ), tr( "No geometry" ), wkbNone );
70+
mGeometryTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconTableLayer.svg" ) ), tr( "No Geometry" ), wkbNone );
7171
mGeometryTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconPointLayer.svg" ) ), tr( "Point" ), wkbPoint );
7272
mGeometryTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconLineLayer.svg" ) ), tr( "Line" ), wkbLineString );
7373
mGeometryTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconPolygonLayer.svg" ) ), tr( "Polygon" ), wkbPolygon );

‎src/gui/qgsnewvectorlayerdialog.cpp

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ QgsNewVectorLayerDialog::QgsNewVectorLayerDialog( QWidget *parent, Qt::WindowFla
5656
mWidth->setValidator( new QIntValidator( 1, 255, this ) );
5757
mPrecision->setValidator( new QIntValidator( 0, 15, this ) );
5858

59+
mGeometryTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconTableLayer.svg" ) ), tr( "No Geometry" ), QgsWkbTypes::NoGeometry );
5960
mGeometryTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconPointLayer.svg" ) ), tr( "Point" ), QgsWkbTypes::Point );
6061
mGeometryTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconPointLayer.svg" ) ), tr( "MultiPoint" ), QgsWkbTypes::MultiPoint );
6162
mGeometryTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconLineLayer.svg" ) ), tr( "Line" ), QgsWkbTypes::LineString );
@@ -99,7 +100,23 @@ QgsNewVectorLayerDialog::QgsNewVectorLayerDialog( QWidget *parent, Qt::WindowFla
99100
mAttributeView->addTopLevelItem( new QTreeWidgetItem( QStringList() << QStringLiteral( "id" ) << QStringLiteral( "Integer" ) << QStringLiteral( "10" ) << QString() ) );
100101
connect( mNameEdit, &QLineEdit::textChanged, this, &QgsNewVectorLayerDialog::nameChanged );
101102
connect( mAttributeView, &QTreeWidget::itemSelectionChanged, this, &QgsNewVectorLayerDialog::selectionChanged );
102-
connect( mGeometryTypeBox, static_cast<void( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsNewVectorLayerDialog::checkOk );
103+
connect( mGeometryTypeBox, static_cast<void( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, [ = ]( int index )
104+
{
105+
QString fileName = mFileName->filePath();
106+
if ( !fileName.isEmpty() )
107+
{
108+
if ( index == 0 )
109+
{
110+
fileName = fileName.replace( fileName.lastIndexOf( QLatin1String( ".shp" ), -1, Qt::CaseInsensitive ), 4, QLatin1String( ".dbf" ) );
111+
}
112+
else
113+
{
114+
fileName = fileName.replace( fileName.lastIndexOf( QLatin1String( ".dbf" ), -1, Qt::CaseInsensitive ), 4, QLatin1String( ".shp" ) );
115+
}
116+
mFileName->setFilePath( fileName );
117+
}
118+
checkOk();
119+
} );
103120

104121
mAddAttributeButton->setEnabled( false );
105122
mRemoveAttributeButton->setEnabled( false );
@@ -290,8 +307,15 @@ QString QgsNewVectorLayerDialog::execAndCreateLayer( QString &errorMessage, QWid
290307

291308
QgsSettings settings;
292309
QString fileName = geomDialog.filename();
293-
if ( fileformat == QLatin1String( "ESRI Shapefile" ) && !fileName.endsWith( QLatin1String( ".shp" ), Qt::CaseInsensitive ) )
310+
if ( fileformat == QLatin1String( "ESRI Shapefile" ) && ( geometrytype != QgsWkbTypes::NoGeometry && !fileName.endsWith( QLatin1String( ".shp" ), Qt::CaseInsensitive ) ) )
294311
fileName += QLatin1String( ".shp" );
312+
else if ( fileformat == QLatin1String( "ESRI Shapefile" ) && ( geometrytype == QgsWkbTypes::NoGeometry && !fileName.endsWith( QLatin1String( ".dbf" ), Qt::CaseInsensitive ) ) )
313+
{
314+
if ( fileName.endsWith( QLatin1String( ".shp" ), Qt::CaseInsensitive ) )
315+
fileName = fileName.replace( fileName.lastIndexOf( QLatin1String( ".shp" ), -1, Qt::CaseInsensitive ), 4, QLatin1String( ".dbf" ) );
316+
else
317+
fileName += QLatin1String( ".dbf" );
318+
}
295319

296320
settings.setValue( QStringLiteral( "UI/lastVectorFileFilterDir" ), QFileInfo( fileName ).absolutePath() );
297321
settings.setValue( QStringLiteral( "UI/encoding" ), enc );

0 commit comments

Comments
 (0)
Please sign in to comment.