1

I wrote the following script to save only the selected features after have a selection by location done. But I have got an error when the saving line is executed. I don't want to load these layers in the map canvas.

How could I do it right?

I'm using QGIS 2.18.17 and Linux Mint 19.

#
import processing

# Load vector layers
rio = QgsVectorLayer('/home/tiago/Documentos/PRIM_Mineracao/rio/mata/rio_doce.shp', 'rio_doce','ogr')
drenagem = QgsVectorLayer('/home/tiago/Documentos/PRIM_Mineracao/rio/mata/rio_mata_disol.shp','rio_mata_disol','ogr')

# Selection by Location
sel = processing.runalg("qgis:selectbylocation", drenagem, rio,['intersects'],0,0)

# Save selected features
crs = QgsCoordinateReferenceSystem("EPSG:4674")
nome1 = '/home/tiago/Documentos/PRIM_Mineracao/rio/mata/'
nome2 = 'rio_doce_P'
caminho = nome1+nome2+'.shp'
QgsVectorFileWriter.writeAsVectorFormat(sel, caminho, crs, "ESRI Shapefile", True)

That's the error message thar I've got:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: arguments did not match any overloaded call:
  writeAsVectorFormat(QgsVectorLayer, str, str, QgsCoordinateReferenceSystem, driverName: str = '', onlySelected: bool = False, errorMessage: str = '', datasourceOptions: Sequence[str] = [], layerOptions: Sequence[str] = [], skipAttributeCreation: bool = False, newFilename: str = '', symbologyExport: QgsVectorFileWriter.SymbologyExport = QgsVectorFileWriter.NoSymbology, symbologyScale: float = 1, filterExtent: QgsRectangle = None, overrideGeometryType: QgsWKBTypes.Type = QgsWKBTypes.Unknown, forceMulti: bool = False, includeZ: bool = False, attributes: Sequence[int] = [], fieldValueConverter: QgsVectorFileWriter.FieldValueConverter = nullptr): argument 1 has unexpected type 'dict'
  writeAsVectorFormat(QgsVectorLayer, str, str, QgsCoordinateTransform, driverName: str = '', onlySelected: bool = False, errorMessage: str = '', datasourceOptions: Sequence[str] = [], layerOptions: Sequence[str] = [], skipAttributeCreation: bool = False, newFilename: str = '', symbologyExport: QgsVectorFileWriter.SymbologyExport = QgsVectorFileWriter.NoSymbology, symbologyScale: float = 1, filterExtent: QgsRectangle = None, overrideGeometryType: QgsWKBTypes.Type = QgsWKBTypes.Unknown, forceMulti: bool = False, includeZ: bool = False, attributes: Sequence[int] = [], fieldValueConverter: QgsVectorFileWriter.FieldValueConverter = nullptr): argument 1 has unexpected type 'dict'
  writeAsVectorFormat(QgsVectorLayer, str, QgsVectorFileWriter.SaveVectorOptions, newFilename: str = '', errorMessage: str = ''): argument 1 has unexpected type 'dict'
2
  • 1
    Try this: QgsVectorFileWriter.writeAsVectorFormat(rio, caminho, "utf-8", crs, "ESRI Shapefile", onlySelected=True)
    – Joseph
    May 31, 2019 at 9:41
  • Thank you @Joseph, I've tried what you suggested but it returned an empty shapefile, no geometry nor attributes.
    – tiago
    May 31, 2019 at 14:14

1 Answer 1

1

I achieved the desired output using a different tool. Instead of QgsVectorFileWriter.writeAsVectorFormat it worked with qgis:saveselectedfeatures. So the code got like this:

#
import processing

# Load vector layers
rio = QgsVectorLayer('/home/tiago/Documentos/PRIM_Mineracao/rio/mata/rio_doce.shp', 'rio_doce','ogr')
drenagem = QgsVectorLayer('/home/tiago/Documentos/PRIM_Mineracao/rio/mata/rio_mata_disol.shp','rio_mata_disol','ogr')

# Selection by Location
processing.runalg("qgis:selectbylocation", drenagem, rio,['intersects'],0,0)

# Save selected features
crs = QgsCoordinateReferenceSystem("EPSG:4674")
nome1 = '/home/tiago/Documentos/PRIM_Mineracao/rio/mata/'
nome2 = 'rio_doce_P'
caminho = nome1+nome2+'.shp'
processing.runalg('qgis:saveselectedfeatures', drenagem, caminho)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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