Created
November 22, 2022 00:45
-
-
Save credadun/e796a8e5bd988f6d4d97480819f77649 to your computer and use it in GitHub Desktop.
Convert_Ras_Vec_to_GPKG
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import qgis.core | |
| import os | |
| #processing.algorithmHelp("gdal:translate") # run directly in python console to get list of functions | |
| project = QgsProject.instance() | |
| lyers = iface.mapCanvas().layers() | |
| canvas = iface.mapCanvas() | |
| outPackage = QFileDialog.getSaveFileName(None, "Save File", "", "Geopackage (*.gpkg)")[0] | |
| for lyr in filter(lambda l: l.type() == QgsMapLayer.RasterLayer, lyers): | |
| inputRas = lyr.source() | |
| newTable = lyr.name() | |
| gdal_string = 'gdal_translate -of GPKG "{}" "{}" -co RASTER_TABLE={} -co APPEND_SUBDATASET=YES'.format(inputRas, outPackage,newTable) | |
| os.system(gdal_string) | |
| print(lyr.name()) | |
| for lyr in filter(lambda l: l.type() == QgsMapLayer.VectorLayer, lyers): | |
| vparams = {'LAYERS': lyr, | |
| 'OUTPUT': outPackage, | |
| 'OVERWRITE': False, # Important! | |
| 'SAVE_STYLES': True, | |
| 'SAVE_METADATA': False, | |
| 'SELECTED_FEATURES_ONLY': False} | |
| processing.run("qgis:package", vparams) | |
| print(lyr.name()) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The script converts all visible (checked in Layers tree) raster and vector layers to a single geopackage with each layer as a new table. Layers can be excluded by unchecking them in the tree.
If 'SELECTED_FEATURES_ONLY' is set to True, it will included selected vector features from server sources (WFS, ArcGIS REST etc), but will otherwise ignore servers. Vector layer metadata can be preserved (if present) by setting 'SAVE_METADATA' to True.
Supported input formats determined by GDAL (shp, gpkg, scratch, tiff etc.).