I'm trying to reassign crs when writing snippets to do as described, combining with pre-existing Python code. I couldn't find the way to append to gpkg without saving the file onto local storage and then reading again.
What I'm describing here is something like:
import glob
import os
group = QgsProject.instance().layerTreeRoot().findGroup('nameOfTheGroup')
DIR = 'Users/name/Desktop/tmp/'
for child in group.children():
lyr = child.layer()
# I can only export the layer onto local
QgsVectorFileWriter.writeAsVectorFormat(
lyr, DIR+lyr.name(), "utf-8",
QgsCoordinateReferenceSystem(4326), # Reassigning CRS here
"ESRI Shapefile"
)
os.chdir(DIR)
reload from local
lyr_dir = glob.glob('*.shp')
lyrs = QgsVectorlayer(lyr, os.path.basename(), 'ogr')
and save it to gpkg
params = {'LAYERS': lyrs,
'OUTPUT': 'output.gpkg',
'OVERWRITE': False, # Important!
'SAVE_STYLES': False,
'SAVE_METADATA': False,
'SELECTED_FEATURES_ONLY': False}
processing.run("native:package", params)
Well technically it does work, but it has so much redundant part!
I know it could go around with ogr2ogr with only 2 lines of code:
>>> ogr2ogr -f GPKG assignedCRS.gpkg -t_srs "EPSG:4326" input.gpkg
>>> ogrmerge.py -f GPKG -o output.gpkg assignedCRS.gpkg
BUT this has to be done in terminal.
How do I do this with PyQGIS only?
I might need to add extra functions other than assigning crs and to fully done without Python console has more readability and in one place.
ogrmerge.py -f GPKG -o output.gpkg assignedCRS.gpkg. Is something missing from the command? As it stands now it only creates a new copy from assignedCRS.gpkg. – user30184 Oct 22 '22 at 10:57