How to check whether points started in red and ended in a blue polygon (and vice versa) while sharing the same attribute?


To check if the first location of the unique ID is in one polygon and last in another.

Points sample:



You can achieve this using a virtual layer.

It would identify the 1st and last point for each category, then intersects these points with the polygons. It further ensure the polygons have the desired set of property (red, blue in your question) and at last that the 2 polygons do not have the same property. This last step is important should you have more than 2 possible values.

go to the menu Layer > Add Layer > Add/Edit Virtual Layer... and enter the following query

select * from
(  select id_easy, 
     min(ordinal) minordinal, 
     max(ordinal) maxordinal  
   from test group by id_easy
 ) as minmax
 JOIN test t1 
    on (minmax.id_easy= t1.id_easyand minmax.minordinal = t1.ordinal)
 JOIN poly p1 
    on st_intersects(t1.geometry, p1.geometry) 
      AND p1.field_a in ('blue','red')
 JOIN test t2 
    on (minmax.id_easy=t2.id_easyand minmax.maxordinal = t2.ordinal)
 JOIN poly p2 
    on st_intersects(t2.geometry, p2.geometry) 
      AND p2.field_a in ('blue','red')
 WHERE p1.field_a != p2.field_a
  • I am fresh user of QGIS, si I have a question: How to deal with the red and blue? I guess the code can not understand the colors – Mamed Mamedov Oct 21 '19 at 13:56
  • You would likely use the coded value (only you knows it...) that you have used to set the color (ex: building_category = 1) – JGH Oct 21 '19 at 15:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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