I have 3 layers in one geopackage, all with the same name but different geometry: Point, Line, Polygon (originally coming from a MapInfo .TAB, which supports multi-geometry in one file). I already have loaded them in my project. Now i want to run native:centroids over the polygon-layer to get the centroids and save the result back into the Geopackage (and overwrite the existing point layer + delete if possible the line and polygon layer).
My script so far:
from qgis.utils import iface
from qgis.core import *
import os
layers = qgis.core.QgsProject.instance().layerTreeRoot().layerOrder()
for lyr in layers:
if lyr.type() == 0 and lyr.name() == 'lightPosts':
basename = lyr.name()
geom = lyr.wkbType()
layer_path = lyr.source()
layer_path = layer_path.replace("Polygon","Point")
input_layer = 'lightPosts'
if geom == 3:
params = {'ALL_PARTS' : False, 'INPUT' : input_layer, 'OUTPUT' : layer_path}
processing.runAndLoadResults("native:centroids",params)
Following error message it prints:
_core.QgsProcessingException: Could not create layer H:/Test_Data.gpkg|layername=lightPosts|geometrytype=Point.gpkg: Creation of data source failed (OGR error: sqlite3_open(H:/Test_Data.gpkg|layername=lightPosts|geometrytype=Point.gpkg) failed: unable to open database file)
It seems it can not acces or overwrite the layer already contained in the geopackage. How can i circumvent this problem?