Skip to content
Permalink
Browse files

[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.
  • Loading branch information
wonder-sk committed Oct 21, 2011
1 parent d956a64 commit 500c538bd1f86f3df00ac27d24852039c2801ca6
Showing with 47 additions and 8 deletions.
  1. +34 −7 src/providers/ogr/qgsogrprovider.cpp
  2. +13 −1 src/providers/ogr/qgsogrprovider.h
@@ -1059,6 +1059,30 @@ bool QgsOgrProvider::addAttributes( const QList<QgsField> &attributes )
return returnvalue;
}

bool QgsOgrProvider::deleteAttributes( const QgsAttributeIds &attributes )
{
#if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 1900
bool res = true;
QList<int> attrsLst = attributes.toList();
// sort in descending order
qSort( attrsLst.begin(), attrsLst.end(), qGreater<int>() );
foreach( int attr, attrsLst )
{
if ( OGR_L_DeleteField( ogrLayer, attr ) != OGRERR_NONE )
{
QgsDebugMsg( "Failed to delete attribute " + QString::number( attr ) );
res = false;
}
}
loadFields();
return res;
#else
QgsDebugMsg( "Deleting fields is supported only from GDAL >= 1.9.0" );
return false;
#endif
}


bool QgsOgrProvider::changeAttributeValues( const QgsChangedAttributesMap & attr_map )
{
if ( attr_map.isEmpty() )
@@ -1362,17 +1386,20 @@ int QgsOgrProvider::capabilities() const
}
#endif

if ( OGR_L_TestCapability( ogrLayer, "CreateField" ) )
{
ability |= AddAttributes;
}

if ( OGR_L_TestCapability( ogrLayer, "DeleteField" ) )
{
ability |= DeleteAttributes;
}

// OGR doesn't handle shapefiles without attributes, ie. missing DBFs well, fixes #803
if ( ogrDriverName == "ESRI Shapefile" )
{
#if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 1260
// test for Shapefile type and GDAL >= 1.2.6
ability |= CreateSpatialIndex;
#endif
#if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 1600
// adding attributes was added in GDAL 1.6
ability |= AddAttributes;
#endif
ability |= CreateAttributeIndex;

if ( mAttributeFields.size() == 0 )
@@ -165,9 +165,21 @@ class QgsOgrProvider : public QgsVectorDataProvider
/**Deletes a feature*/
virtual bool deleteFeatures( const QgsFeatureIds & id );

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

/**
* Deletes existing attributes
* @param attributes a set containing names of attributes
* @return true in case of success and false in case of failure
*/
virtual bool deleteAttributes( const QgsAttributeIds &attributes );

/**Changes attribute values of existing features */
virtual bool changeAttributeValues( const QgsChangedAttributesMap & attr_map );

0 comments on commit 500c538

Please sign in to comment.
You can’t perform that action at this time.