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.
feature.setGeometry(QgsGeometry.fromWkt('LineString()'))
. Anyway, I still can't manage or export a standalone DBF file.dataProvider()
, you don't need to usestartEditing()
andcommitChanges()
. You can remove those two lines.