2

I have a layer with some geometries and a data layer that defines an M:N relation. Is there any way I could select (on the map) objects that are in relation with specific id? Basically I would like to select by expression like

object_id in (select object_id from relations where relative_id = 12345)

I can do that from the db manager select * from objects where object_id in..., but that means creating new layer each time, which is extra work and needs to be deleted or there would soon be hundreds of them. And the qgis joins don't seem to support M:N relations. Is there a way to select (in qgis, not sql sense) by such query?

The layers happen to be in spatialite database. They look something like

create table objects (object_id integer primary key, geometry, attributes...);
create table relations (object_id integer references objects (object_id),
                        relative_id integer references something);
2
  • What would you like to do with this data? I.e. can you be more precise what you mean by "select in qgis sense". Commented Apr 1, 2014 at 12:27
  • @MatthiasKuhn: The kind of select that highlights them yellow (by default) on the map. Is there any other word for that that could not be ambiguous?
    – Jan Hudec
    Commented Apr 1, 2014 at 16:45

3 Answers 3

3

Why not put your complex query in the database as a view?

Technically your question is about M:N relations in QGIS, and it sounds like between your and GrantD71's comments you've concluded that it is not possible.

So for your case, and as an answer to your actual question, I'm not sure that this is a better solution, it's just what I'd do.

I would either create a view, or load the following query as a layer. Personally, I like views better because I'm usually in PostGIS and I can re-use the same view I use in QGIS in other systems (like web maps and GeoServer).

Assuming tables:

objects
--------
object_id, geometry, attribute1, attribute2,...

relations
--------
object_from_id,object_to_id

Create a view that includes ALL the references (or use this query to define the layer in QGIS).

view
----
SELECT 
    from.*,
    to.object_id AS relative_id
FROM 
    relations,
    object from,
    object to
WHERE
    relations.object_from_id = from.object_id AND
    relations.object_to_id = to.object_id

Create a layer from the view in QGIS (if you're using a view)

Filter based on the relative_id field in QGIS

Filter query: relative_id = 12345

3
  • How is that supposed to help with qgis?
    – Jan Hudec
    Commented Apr 1, 2014 at 4:58
  • You're using a spatialite database as your data source, so if you wanted to you could create a database view. You would then use that database view as your data view in QGis.
    – stuporglue
    Commented Apr 2, 2014 at 5:41
  • I know I could create view. I also don't need to create a view, because I can load query result as a layer and explicitly mention it as not good solution to the problem. So how would a view be a better solution?
    – Jan Hudec
    Commented Apr 2, 2014 at 6:26
1

It is possible to do a join and then select by attribute purely in QGIS (without accessing the DB Manager).

Add both of the tables you want to join to QGIS. Right click on the layer with geometry and then go to Properties -> Joins -> click the plus sign to bring up the "Add vector join" dialog -> select the layer and field you want to join on.

Add vector join dialog

You should have an option to keep the join cached in memory, so you can avoid clutter.

With the join created you should be able to open the layer with geometry's attribute table and do selections based on expression.

2
  • I know about that feature. It does not work with M:N relations. It only shows one related row id, but I need all related rows.
    – Jan Hudec
    Commented Apr 1, 2014 at 12:13
  • Ah, I guess I misunderstood your original question. Unfortunately I don't think you will be able to do this solely within QGIS. You will probably have to do something messy on the DBM side before adding the layer if you want use the select by expression tool.
    – GrantD71
    Commented Apr 1, 2014 at 22:58
0

there is an extension "select by relationship" that is very helpful when i use complex relationships. Maybe in your case it could help too...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.