[GIS] 1-n relationship by location in QGIS

qgisrelation

I have a polygon layer and a point layer. According to the QGIS documentation, the childlayer has to have a foreign key. Unfortunately, my child layer has none. Is there a way to form the relation by geography only? So that even without having a proper foreign key, clicking on a polygon with the identify tool shows me all the points that are within this selected polygon in the attribute widget as child features?

I am aware I could create a new column in my child layer and assign that foreign key with the processing tools QGIS provides. As I have many layers to connect, this would be a rather time consuming process to do for all relations I want to establish!

Best Answer

Joining - also by geography - is easy with Virtual Layers in QGIS:

enter image description here

select poly.polygontext, pnt.pointtext, poly.geometry 
from mypolygon poly
inner join mypoint pnt on st_within(pnt.geometry, poly.geometry)

Result:

enter image description here

Virtuel Layers is available from within the Data Source Manager

Related Question