I am trying to develop a plugin to export selected features from a layer as shapefile.
I have a comboBox from which I can select features. After selecting features in comboBox, I tried following code but unable to get new shapefile of these particular features.
Initially I defined a location for exported shapefile by using push button.
def select_output_file(self):
filename, _filter = QFileDialog.getSaveFileName(
self.dlg, "Select output file ","", '*.shp')
self.dlg.lineEdit.setText(filename)
And then connect to this function:-
def run(self):
if self.first_start == True:
self.first_start = False
self.dlg = GetMapDialog()
self.dlg.pushButton.clicked.connect(self.select_output_file)
And after getting features in comboBox, I tried this:
# show the dialog
self.dlg.show()
# Run the dialog event loop
result = self.dlg.exec_()
# See if OK was pressed
if result:
filename = self.dlg.lineEdit.text()
with open(filename, 'w') as output_file:
layer = selectedlayer ##U already add a main layer as selectedLayer
selectedFeature = self.dlg.comboBox.currentText
layer.select(selectedFeature)
writer = QgsVectorFileWriter.writeAsVectorFormat(layer, output_file, 'utf-8', layer.crs(),'ESRI Shapefile', onlySelected=True)
selected_layer = iface.addVectorLayer(output_file, '', 'ogr')
del(writer)
After this, I got following error
Traceback (most recent call last): File "C:\Users/DELL/AppData/Roaming/QGIS/QGIS3\profiles\default/python/plugins\get_map\get_map.py", line 226, in run writer = QgsVectorFileWriter.writeAsVectorFormat(layer, output_file, 'utf-8', layer.crs(),'ESRI Shapefile', onlySelected=True) TypeError: QgsVectorFileWriter.writeAsVectorFormat(): arguments did not match any overloaded call: overload 1: argument 2 has unexpected type '_io.TextIOWrapper' overload 2: argument 2 has unexpected type '_io.TextIOWrapper' overload 3: argument 2 has unexpected type '_io.TextIOWrapper'
How can I overcome this error by which I can export a shapefile of a particular feature?