I am relatively new to PyQGIS and am attempting to save a SpatialLite layer into a new layer (preferably in the same database) with a different CRS (I had it working in shapefile but have had to move the data to SpatialLite which I have not used before). I keep on getting errors about the types of my variables, but am unable to figure out how else to do it and I can't find an example online which says what each of the variables are when exporting to SQLite.
layers = QgsProject.instance().mapLayersByName('SiteCard_AMG')
layer = QgsVectorLayer(layers[0].dataProvider().dataSourceUri(), '', 'ogr')
crs=QgsCoordinateReferenceSystem("epsg:4326")
error = QgsVectorFileWriter.writeAsVectorFormat(layer, 'C:\\Users\\Megan\\Documents\\new.sqlite', 'utf-8', crs, 'SQLite', False, None, ['SPATIALITE=YES'])
error as follows;
Traceback (most recent call last):
File "C:\PROGRA~1\QGIS3~1.10\apps\Python37\lib\code.py", line 90, in runcode
exec(code, self.locals)
File "<input>", line 1, in <module>
File "<string>", line 47, in <module>
TypeError: QgsVectorFileWriter.writeAsVectorFormat(): arguments did not match any overloaded call:
overload 1: argument 7 has unexpected type 'NoneType'
overload 2: argument 4 has unexpected type 'QgsCoordinateReferenceSystem'
overload 3: argument 3 has unexpected type 'str'
1 Answer 1
the following code works in my case to export a vector layer to an sqlite database:
layer = iface.activeLayer()
crs = QgsCoordinateReferenceSystem("epsg:4326")
s_out_file = r"%PATH_TO_OUTPUT_FILE.sqlite"
err = QgsVectorFileWriter.writeAsVectorFormat(layer, s_out_file, "utf-8", crs, driverName='SQLite', onlySelected=False, datasourceOptions=['SPATIALITE=YES'])
The call to writeAsVectorFormat()
is very similar to yours, but without the None
parameter (7th parameter in your case), which is in fact not needed.
2 side notes based on your example code:
- Could you not use directly
layers[0]
as the vector layer to be saved in the output sqlite database (i.e., without instantiating thelayer
variable)? writeAsVectorFormat
was deprecated in QGIS 3.10 in favor ofwriteAsVectorFormatV2
. So, depending on the version you are using, you might want to adapt the code. A possible code snippet that useswriteAsVectorFormatV2
and changes the CRS of the output layer is the following:
layer = iface.activeLayer()
s_out_file = r"%PATH_TO_OUTPUT_FILE_V2.sqlite"
ctc = QgsProject.instance().transformContext()
o_save_options = QgsVectorFileWriter.SaveVectorOptions()
o_save_options.layerName = layer.name()
o_save_options.driverName = 'SQLite'
o_save_options.fileEncoding = 'utf-8'
o_save_options.onlySelectedFeatures = False
o_save_options.layerOptions = ['SPATIALITE=YES']
o_save_options.ct = QgsCoordinateTransform(QgsCoordinateReferenceSystem("EPSG:2056"), QgsCoordinateReferenceSystem("EPSG:4326"), QgsProject.instance())
err = QgsVectorFileWriter.writeAsVectorFormatV2(layer, s_out_file, ctc, o_save_options)
I hope this helps.
-
my issue with using writeAsVectorFormatV2 is I haven't been able to successfully set a new crs. If you could please post me an example I would like to shift to use V2, thanks.mallen– mallen2020年06月25日 02:15:09 +00:00Commented Jun 25, 2020 at 2:15
-
Hi @mallen, I have edited my original answer to provide an example using
writeAsVectorFormatV2
. I hope it will work. Cheers.fastest– fastest2020年06月25日 17:02:45 +00:00Commented Jun 25, 2020 at 17:02 -
Hi @mallen, thanks for letting me know. Can you then accept the answer as the correct one? Thank you.fastest– fastest2020年06月26日 10:03:30 +00:00Commented Jun 26, 2020 at 10:03
-
Hi @mallen, thanks for that. You should be able also to flag the answer as the one that solved your problem. Then other people will also notice that and the solution will be more easily shared. Thanks.fastest– fastest2020年06月26日 11:45:00 +00:00Commented Jun 26, 2020 at 11:45