diff --git a/felt/core/layer_exporter.py b/felt/core/layer_exporter.py index bdd0f82..5274649 100644 --- a/felt/core/layer_exporter.py +++ b/felt/core/layer_exporter.py @@ -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] @@ -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 diff --git a/felt/test/test_layer_exporter.py b/felt/test/test_layer_exporter.py index ff609e5..6886d2a 100644 --- a/felt/test/test_layer_exporter.py +++ b/felt/test/test_layer_exporter.py @@ -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): """ @@ -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): """ @@ -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): """