Skip to content

Commit

Permalink
[FEATURE] Delete fields in shapefiles (finally!)
Browse files Browse the repository at this point in the history
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 500c538
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 8 deletions.
41 changes: 34 additions & 7 deletions src/providers/ogr/qgsogrprovider.cpp
Expand Up @@ -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() )
Expand Down Expand Up @@ -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 )
Expand Down
14 changes: 13 additions & 1 deletion src/providers/ogr/qgsogrprovider.h
Expand Up @@ -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 );

Expand Down

0 comments on commit 500c538

Please sign in to comment.