Skip to content

Commit

Permalink
Allow to edit empty shapefiles
Browse files Browse the repository at this point in the history
Fixes #38834
  • Loading branch information
elpaso authored and nyalldawson committed Jan 19, 2021
1 parent 5b2685d commit 221c3a8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
6 changes: 0 additions & 6 deletions src/core/providers/ogr/qgsogrprovider.cpp
Expand Up @@ -2982,12 +2982,6 @@ void QgsOgrProvider::computeCapabilities()
ability |= CreateSpatialIndex;
ability |= CreateAttributeIndex;

if ( mAttributeFields.size() == 0 )
{
QgsMessageLog::logMessage( tr( "Shapefiles without attribute are considered read-only." ), tr( "OGR" ) );
ability &= ~( AddFeatures | DeleteFeatures | ChangeAttributeValues | AddAttributes | DeleteAttributes );
}

if ( ( ability & ChangeAttributeValues ) == 0 )
{
// on readonly shapes OGR reports that it can delete features although it can't RandomWrite
Expand Down
34 changes: 33 additions & 1 deletion tests/src/python/test_provider_ogr.py
Expand Up @@ -19,14 +19,16 @@
import mockedwebserver

from osgeo import gdal, ogr # NOQA
from qgis.PyQt.QtCore import QVariant, QByteArray
from qgis.PyQt.QtCore import QVariant, QByteArray, QTemporaryDir
from qgis.core import (
NULL,
QgsAuthMethodConfig,
QgsApplication,
QgsCoordinateTransformContext,
QgsProject,
QgsField,
QgsFields,
QgsGeometry,
QgsRectangle,
QgsProviderRegistry,
QgsFeature,
Expand All @@ -35,6 +37,7 @@
QgsDataProvider,
QgsVectorDataProvider,
QgsVectorLayer,
QgsVectorFileWriter,
QgsWkbTypes,
QgsNetworkAccessManager
)
Expand Down Expand Up @@ -869,6 +872,35 @@ def testHTTPRequestsOverrider(self):
with mockedwebserver.install_http_handler(handler):
QgsVectorLayer("OAPIF:http://127.0.0.1:%d/collections/foo authcfg='%s'" % (port, config.id()), 'test', 'ogr')

def testShapefilesWithNoAttributes(self):
"""Test issue GH #38834"""

ml = QgsVectorLayer('Point?crs=epsg:4326', 'test', 'memory')
self.assertTrue(ml.isValid())

d = QTemporaryDir()
options = QgsVectorFileWriter.SaveVectorOptions()
options.driverName = 'ESRI Shapefile'
options.layerName = 'writetest'
err, _ = QgsVectorFileWriter.writeAsVectorFormatV2(ml, os.path.join(d.path(), 'writetest.shp'), QgsCoordinateTransformContext(), options)
self.assertEqual(err, QgsVectorFileWriter.NoError)
self.assertTrue(os.path.isfile(os.path.join(d.path(), 'writetest.shp')))

vl = QgsVectorLayer(os.path.join(d.path(), 'writetest.shp'))
self.assertTrue(bool(vl.dataProvider().capabilities() & QgsVectorDataProvider.AddFeatures))

# Let's try if we can really add features
feature = QgsFeature(vl.fields())
geom = QgsGeometry.fromWkt('POINT(9 45)')
feature.setGeometry(geom)
self.assertTrue(vl.startEditing())
self.assertTrue(vl.addFeatures([feature]))
self.assertTrue(vl.commitChanges())
del (vl)

vl = QgsVectorLayer(os.path.join(d.path(), 'writetest.shp'))
self.assertEqual(vl.featureCount(), 1)


if __name__ == '__main__':
unittest.main()

0 comments on commit 221c3a8

Please sign in to comment.