Skip to content

Commit 500c538

Browse files
committed
[FEATURE] Delete fields in shapefiles (finally!)
This is currently supported only by development versions of GDAL/OGR. GDAL 1.8.x does _not_ support this feature.
1 parent d956a64 commit 500c538

File tree

2 files changed

+47
-8
lines changed

2 files changed

+47
-8
lines changed

src/providers/ogr/qgsogrprovider.cpp

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,6 +1059,30 @@ bool QgsOgrProvider::addAttributes( const QList<QgsField> &attributes )
10591059
return returnvalue;
10601060
}
10611061

1062+
bool QgsOgrProvider::deleteAttributes( const QgsAttributeIds &attributes )
1063+
{
1064+
#if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 1900
1065+
bool res = true;
1066+
QList<int> attrsLst = attributes.toList();
1067+
// sort in descending order
1068+
qSort( attrsLst.begin(), attrsLst.end(), qGreater<int>() );
1069+
foreach( int attr, attrsLst )
1070+
{
1071+
if ( OGR_L_DeleteField( ogrLayer, attr ) != OGRERR_NONE )
1072+
{
1073+
QgsDebugMsg( "Failed to delete attribute " + QString::number( attr ) );
1074+
res = false;
1075+
}
1076+
}
1077+
loadFields();
1078+
return res;
1079+
#else
1080+
QgsDebugMsg( "Deleting fields is supported only from GDAL >= 1.9.0" );
1081+
return false;
1082+
#endif
1083+
}
1084+
1085+
10621086
bool QgsOgrProvider::changeAttributeValues( const QgsChangedAttributesMap & attr_map )
10631087
{
10641088
if ( attr_map.isEmpty() )
@@ -1362,17 +1386,20 @@ int QgsOgrProvider::capabilities() const
13621386
}
13631387
#endif
13641388

1389+
if ( OGR_L_TestCapability( ogrLayer, "CreateField" ) )
1390+
{
1391+
ability |= AddAttributes;
1392+
}
1393+
1394+
if ( OGR_L_TestCapability( ogrLayer, "DeleteField" ) )
1395+
{
1396+
ability |= DeleteAttributes;
1397+
}
1398+
13651399
// OGR doesn't handle shapefiles without attributes, ie. missing DBFs well, fixes #803
13661400
if ( ogrDriverName == "ESRI Shapefile" )
13671401
{
1368-
#if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 1260
1369-
// test for Shapefile type and GDAL >= 1.2.6
13701402
ability |= CreateSpatialIndex;
1371-
#endif
1372-
#if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 1600
1373-
// adding attributes was added in GDAL 1.6
1374-
ability |= AddAttributes;
1375-
#endif
13761403
ability |= CreateAttributeIndex;
13771404

13781405
if ( mAttributeFields.size() == 0 )

src/providers/ogr/qgsogrprovider.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,21 @@ class QgsOgrProvider : public QgsVectorDataProvider
165165
/**Deletes a feature*/
166166
virtual bool deleteFeatures( const QgsFeatureIds & id );
167167

168-
/**Adds new attributess. Unfortunately not supported for layers with features in it*/
168+
/**
169+
* Adds new attributes
170+
* @param attributes list of new attributes
171+
* @return true in case of success and false in case of failure
172+
* @note added in 1.2
173+
*/
169174
virtual bool addAttributes( const QList<QgsField> &attributes );
170175

176+
/**
177+
* Deletes existing attributes
178+
* @param attributes a set containing names of attributes
179+
* @return true in case of success and false in case of failure
180+
*/
181+
virtual bool deleteAttributes( const QgsAttributeIds &attributes );
182+
171183
/**Changes attribute values of existing features */
172184
virtual bool changeAttributeValues( const QgsChangedAttributesMap & attr_map );
173185

0 commit comments

Comments
 (0)