I am trying to convert a shapefile to SQLite file in QGIS 3.26 using Python.
Here is my code:
def convert_to_sqlite(self):
input_shp = QgsVectorLayer("C:\shapeFile\BMR_Zones_Corrected_519.shp","polygon","ogr")
print(input_shp.isValid())
QgsVectorFileWriter.writeAsVectorFormat(input_shp,"C:\shapeFile\BMR_Zones_Corrected_519.sqlite","UTF-8",input_shp.crs(),"SQLite")
This is the error I am facing:
warning:C:\Users/MTTUSER/AppData/Roaming/QGIS/QGIS3\profiles\default/python/plugins\Network_Processing\ABTDModel.py:477: DeprecationWarning: QgsVectorFileWriter.writeAsVectorFormat() is deprecated
QgsVectorFileWriter.writeAsVectorFormat(input_shp,"C:\shapeFile\BMR_Zones_Corrected_519.gpkg","UTF-8",input_shp.crs(),"GPKG")
traceback: File "C:\Users/MTTUSER/AppData/Roaming/QGIS/QGIS3\profiles\default/python/plugins\Network_Processing\ABTDModel.py", line 602, in run
result = self.dlg.exec_()
File "C:\Users/MTTUSER/AppData/Roaming/QGIS/QGIS3\profiles\default/python/plugins\Network_Processing\ABTDModel.py", line 477, in convert_to_sqlite
QgsVectorFileWriter.writeAsVectorFormat(input_shp,"C:\shapeFile\BMR_Zones_Corrected_519.gpkg","UTF-8",input_shp.crs(),"GPKG")
nmtoken
13.6k5 gold badges39 silver badges91 bronze badges
asked Nov 9, 2022 at 10:59
-
Why pyqgis? You can use geopandas. See gis.stackexchange.com/questions/141818/… and giacomodebidda.com/posts/export-a-geodataframe-to-spatialiteFardin Esmaeili– Fardin Esmaeili2022年11月10日 14:19:11 +00:00Commented Nov 10, 2022 at 14:19
1 Answer 1
Try this:
shape = r'/home/bera/GIS/Data/testdata/subway.shp'
output = r'/home/bera/GIS/Data/testdata/subway.sqlite'
vl = QgsVectorLayer(shape, 'temp')
options = QgsVectorFileWriter.SaveVectorOptions()
options.driverName = "SQLite"
QgsVectorFileWriter.writeAsVectorFormatV2(vl, output, QgsCoordinateTransformContext(), options)
answered Nov 10, 2022 at 18:13
-
1Thank you, this worked for me.BenStone– BenStone2022年12月01日 06:54:27 +00:00Commented Dec 1, 2022 at 6:54
default