[QGIS-Developer] Event to register adding layers in QGIS
Bo Victor Thomsen
bo.victor.thomsen at gmail.com
Fri Aug 26 02:01:00 PDT 2022
Hi list -
I have a small "startup.py" project - The goal is to register every time
a user opens af layer in QGIS in a organization with lots of QGIS users.
The end goal is to find data sources that are maintained but actually
not used.
To that purpose I created the following script as a "proof of concept":
*from qgis.core import QgsProject, QgsMessageLog, Qgis, QgsLayerTreeLayer
from datetime import datetime
import os
def onAddedChildren(node, indexFrom, indexTo):
for i in range(indexFrom, indexTo+1):
child = node.children()[i]
if isinstance(child, QgsLayerTreeLayer):
layer = child.layer()
name = ''
uri = ''
if (layer):
name = layer.name()
datap = layer.dataProvider()
if datap:
uri = datap.dataSourceUri()
strx =
'"{}";"{}";"{}";"{}";"{}"'.format(datetime.today().strftime('%Y-%m-%d
%H:%M:%S'),os.getlogin(),child.name(),name,uri)
with open("d:/tmp/loglag.csv", "a") as g:
g.write(strx+'\n')
root = QgsProject.instance().layerTreeRoot()
root.addedChildren.connect(onAddedChildren)
*
(I know - I might into trouble with users appending data to the same
file at the same time, but that is solveable)
The above script works like a charm, when a user add a single layer
using the data source manager. But it partially fails when opening a
project. The layers are registred, but it doesn't register the uri for
the individual layers. I assume the event "addedChildren" is fired
before the layers is fully loaded.
So is there some other event I can use to get my full layer info ?
--
Med venlig hilsen / Kind regards
Bo Victor Thomsen
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20220826/4dfc53ac/attachment-0001.htm>
More information about the QGIS-Developer
mailing list