1

I need to create a standalone DBF file with some features. I know how to add the fields but I'm not able to add features and to create only a standalone DBF, without any other files such as .shp or .prj. I've tried creating a LineString in memory and then writing it with QgsVectorFileWriter, using ESRI Shapefile as driver, but it creates a simple shapefile with no features. The GDAL docs say that it can manage standalone DBF but I'm struggling with it and I can't find the way to do it.

This is the code I tried to create it and the export it.

self.work_line_table = QgsVectorLayer('LineString', 'Line_table', 'memory')

[Code for adding fields]

# Add features using the attribute from another layer. It's not working
self.work_line_table.startEditing()
for line in self.work_line_layer.getFeatures():
    line_id = line['IdLinia']
    feature = QgsFeature()
    feature.setAttributes([line_id, self.municipi_codi_ine])
    self.work_line_table.dataProvider().addFeatures([feature])

self.work_line_table.commitChanges()

# Export the table
QgsVectorFileWriter.writeAsVectorFormat(self.work_line_table,
                                        os.path.join(GENERADOR_WORK_DIR, 'MM_LiniesTaula.dbf'),
                                        'utf-8', self.crs, 'ESRI Shapefile')

I've thought in creating it as shapefile and then removing all the other files associated to it except the DBF file, but anyway I can't add any feature to the DBF.

2
  • I've fixed the issue that I could not add features to the shapefile: it was because I wasn't adding any type of geometry to each feature. All that I had to do is to add an empty geometry such as feature.setGeometry(QgsGeometry.fromWkt('LineString()')). Anyway, I still can't manage or export a standalone DBF file. Apr 27 at 10:44
  • 1
    When adding features to the layer using dataProvider() , you don't need to use startEditing() and commitChanges(). You can remove those two lines. Apr 27 at 11:02
1

Not sure why you expect a standalone DBF. By default, if you export to a DBF file, you will lose the geometry along the transformation. You may transform the geometry as an "ordinary" attribute e.g like a WKT geometry column to keep the info (in your case line.geometry().asWkt() is a way to get it)

You can find below a standalone recipe to do the job to export to DBF from an existing layer. It will create only a DBF with a CPG file (useful to know the encoding but not used by tools like LibreOffice or Excel, only by tools using OGR/GDAL behind the scene). Adapt it to your case.

layer = iface.activeLayer()
project = QgsProject.instance()
contextProject = project.transformContext()

optionsDbf = QgsVectorFileWriter.SaveVectorOptions()
optionsDbf.driverName = 'DBF file'
QgsVectorFileWriter.writeAsVectorFormatV2 (
    layer,
    '/tmp/myfile.dbf',
    contextProject,
    optionsDbf
)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.