Skip to content

Commit

Permalink
Fix logic for older QGIS
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jun 5, 2024
1 parent 80c7ae7 commit 2af4187
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 19 deletions.
19 changes: 8 additions & 11 deletions felt/core/layer_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,21 +370,18 @@ def export_vector_layer(
writer_options.attributes = fields.allAttributesList()
if fid_index >= 0:
fid_type = fields.field(fid_index).type()
if (Qgis.QGIS_VERSION_INT < 32400 and
fid_type not in (
QVariant.Int,
QVariant.UInt,
QVariant.LongLong,
QVariant.ULongLong)):
needs_rewrite = force_rewrite_fid or fid_type not in (
QVariant.Int,
QVariant.UInt,
QVariant.LongLong,
QVariant.ULongLong)
if Qgis.QGIS_VERSION_INT < 32400 and needs_rewrite:
# older QGIS, can't rename attributes during export, so
# drop FID
writer_options.attributes = [a for a in
writer_options.attributes if
a != fid_index]
elif force_rewrite_fid or fid_type not in (QVariant.Int,
QVariant.UInt,
QVariant.LongLong,
QVariant.ULongLong):
elif needs_rewrite:
writer_options.attributesExportNames = [
f.name() if f.name().lower() != 'fid' else 'old_fid'
for f in fields]
Expand All @@ -399,7 +396,7 @@ def export_vector_layer(
)
# pylint: enable=unused-variable

if (Qgis.QGIS_VERSION_INT >= 32400 and not force_rewrite_fid and
if (not force_rewrite_fid and
res == QgsVectorFileWriter.WriterError.ErrFeatureWriteFailed):
# could not write attributes -- possibly eg due to duplicate
# FIDs. Let's try with renaming FID
Expand Down
38 changes: 30 additions & 8 deletions felt/test/test_layer_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,12 @@ def test_gml_conversion(self):
self.assertTrue(out_layer.isValid())
self.assertEqual(out_layer.featureCount(), layer.featureCount())
self.assertEqual(out_layer.wkbType(), QgsWkbTypes.MultiPolygon)
self.assertEqual([f.name() for f in out_layer.fields()],
if Qgis.QGIS_VERSION_INT >= 32400:
self.assertEqual([f.name() for f in out_layer.fields()],
['fid', 'old_fid', 'name', 'intval', 'floatval'])
else:
self.assertEqual([f.name() for f in out_layer.fields()],
['fid', 'name', 'intval', 'floatval'])

def test_layer_conversion_string_fid(self):
"""
Expand Down Expand Up @@ -262,12 +266,21 @@ def test_layer_conversion_string_fid(self):
self.assertTrue(out_layer.isValid())
self.assertEqual(out_layer.featureCount(), layer.featureCount())
self.assertEqual(out_layer.wkbType(), QgsWkbTypes.Point)
self.assertEqual([f.name() for f in out_layer.fields()],
if Qgis.QGIS_VERSION_INT >= 32400:
self.assertEqual([f.name() for f in out_layer.fields()],
['fid', 'old_fid', 'label'])
else:
self.assertEqual([f.name() for f in out_layer.fields()],
['fid', 'label'])
features = list(out_layer.getFeatures())
self.assertEqual([feature.attributes() for feature in features],
[[1, 'abc', 'def'],
[2, '15', 'ghi']])
if Qgis.QGIS_VERSION_INT >= 32400:
self.assertEqual([feature.attributes() for feature in features],
[[1, 'abc', 'def'],
[2, '15', 'ghi']])
else:
self.assertEqual([feature.attributes() for feature in features],
[[1, 'def'],
[2, 'ghi']])

def test_layer_conversion_duplicate_fid(self):
"""
Expand Down Expand Up @@ -305,11 +318,20 @@ def test_layer_conversion_duplicate_fid(self):
self.assertTrue(out_layer.isValid())
self.assertEqual(out_layer.featureCount(), layer.featureCount())
self.assertEqual(out_layer.wkbType(), QgsWkbTypes.Point)
self.assertEqual([f.name() for f in out_layer.fields()],
if Qgis.QGIS_VERSION_INT >= 32400:
self.assertEqual([f.name() for f in out_layer.fields()],
['fid', 'old_fid', 'label'])
else:
self.assertEqual([f.name() for f in out_layer.fields()],
['fid', 'label'])

features = list(out_layer.getFeatures())
self.assertEqual([feature.attributes() for feature in features],
[[1, 15, 'abc'], [2, 15, 'def']])
if Qgis.QGIS_VERSION_INT >= 32400:
self.assertEqual([feature.attributes() for feature in features],
[[1, 15, 'abc'], [2, 15, 'def']])
else:
self.assertEqual([feature.attributes() for feature in features],
[[1, 'abc'], [2, 'def']])

def test_raster_conversion_raw(self):
"""
Expand Down

0 comments on commit 2af4187

Please sign in to comment.