Skip to content

Instantly share code, notes, and snippets.

@slarosa
Last active November 25, 2020 19:36
Show Gist options
  • Save slarosa/653e6d759cf0d82c2a24dcc499b094e0 to your computer and use it in GitHub Desktop.
Save slarosa/653e6d759cf0d82c2a24dcc499b094e0 to your computer and use it in GitHub Desktop.
'''
QGIS macro to enable selection on referenced geometry layer from
referencing geometryless table in a Project with Relations.
How it works:
1. Enable macros for the QGIS Project
2. Paste this code in Project->Macro
3. Save the Project.
Note: The QGIS project must have relationship between tables.
See https://goo.gl/w4NMZJ on how to create one-to-many relations.
'''
from qgis.PyQt.QtCore import QSettings
from qgis.utils import iface, reloadProjectMacros
from qgis.core import QgsFeatureRequest, QgsProject
from functools import partial
def openProject():
def selectionChanged(rl, fts):
referencingLayer = rl.referencingLayer()
referencedLayer = rl.referencedLayer()
ids = []
request = QgsFeatureRequest().setFilterFids(fts)
for f in referencingLayer.getFeatures(request):
ids.append(rl.getReferencedFeature(f).id())
referencedLayer.selectByIds(ids)
iface.setActiveLayer(referencedLayer)
rM = QgsProject.instance().relationManager()
rls = rM.relations()
for rlid, rl in rls.iteritems():
referencingLayer = rl.referencingLayer()
referencingLayer.selectionChanged.connect(partial(selectionChanged, rl))
def saveProject():
s = QSettings()
s.setValue("qgis/enableMacros", 3)
reloadProjectMacros()
def closeProject():
pass
@aborruso
Copy link

I will a row in the bullet points notes: "create the layers relation in the Project".

Thank you very much

@aborruso
Copy link

No, you have just written it, sorry!!!

@slarosa
Copy link
Author

slarosa commented Apr 15, 2017

@aborruso Yeah, it could be unclear, I added that the project must have relationship between tables. thanks!

Copy link

ghost commented Apr 15, 2017

Hi Salvo,
I tested your macro on Ubuntu 16.04 (QGIS 2.18), but it does'nt work.
This is the log:

An error occurred during execution of following code:
qgis.utils.reloadProjectMacros()
traceback.print_exception() failed
Python version:
2.7.12 (default, Nov 19 2016, 06:48:10)
[GCC 5.4.0 20160609]
QGIS version:
2.18.6 'Las Palmas', 5f369b0

@slarosa
Copy link
Author

slarosa commented Apr 16, 2017

@nformica wrote:

I tested your macro on Ubuntu 16.04 (QGIS 2.18), but it does'nt work.
This is the log:
An error occurred during execution of following code:
qgis.utils.reloadProjectMacros()
traceback.print_exception() failed

when it is occurring? saving the project or anywhere you try to select a row?

Copy link

ghost commented Apr 16, 2017

Just when I open the project !
Then, when I select a row nothing happens: no error alert and no features selection.

@slarosa
Copy link
Author

slarosa commented Apr 16, 2017

@nformica that is odd. That method should be called on saving the project. You just try to replace reloadProjectMacros() with pass. That code actually is not working as expected.

Copy link

ghost commented Apr 17, 2017

I tried "to replace reloadProjectMacros() with pass", but problem is the same.
This is message on Log panel (Python):
evalString()) error!
Command:
str(sys.path)
Error:
SystemError: NULL object passed to Py_BuildValue

Peraphs qgis-python module is'nt good !? ... Should I try to reinstall it?

@slarosa
Copy link
Author

slarosa commented Apr 18, 2017

@nformica, I managed to test the macro on Ubuntu-16.04 (64bit) QGIS 2.18.6 and has worked fine for me. So, the error you are getting could be a local problem.
Also, you could test if the following code work fine for you in python console:

from qgis.utils import reloadProjectMacros
reloadProjectMacros()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment