I am looking for a way to run select by location, specific to the features of the 'INTERSECT' layer. I have got bus locations and bus stops and want to figure out when the bus has entered and left individual stops.

enter image description here

Here the orange dots are the bus stops (with varying radius) and the purple ones are the GPS locations of one bus.

With the standard logic, select by location select the GPS locations in all stops and I cannot discriminate when the bus has visited individual stops.

I am using this command for the selection:

processing.run("native:selectbylocation", {'INPUT':Course,'PREDICATE':[0,6],'INTERSECT':Stops,'METHOD':0})
  • So you want to select one stop at a time even though the buffered bus polygon intersects multiple stop features?
    – artwork21
    Dec 3, 2019 at 13:52
  • Sorry I didn't make it clear. The orange dots are the stops (with varying radius) and the purple ones are the GPS locations of one bus. Otherwise, I want to make selection for each stop individually
    – FunTimes
    Dec 3, 2019 at 13:54

2 Answers 2


SQL queries are really optimal but I offer you an alternative that will create a layer of gps points for each stop.

My starting layers enter image description here

# Identification of gps layer
gps = QgsProject.instance().mapLayersByName("gps")[0] 
# Identification of stops layer
stops = QgsProject.instance().mapLayersByName("stops")[0] 

# List : Field with unique identifiers in your stop layer (FID for me)
index = [feature["FID"] for feature in stops.getFeatures()] 

# loop on the list
for i in index:
    # remove selection of gps layer
    # remove selection of stops layer  

    # Apply selection 
    stops.selectByExpression('"FID" ='+str(i),QgsVectorLayer.SetSelection) 
    # New layer with my selection
    selection_stops =    stops.materialize(QgsFeatureRequest().setFilterFids(stops.selectedFeatureIds())) 

    # Selectbylocation
    processing.run("native:selectbylocation", {'INPUT':gps,'PREDICATE':[0],'INTERSECT':selection_stops,'METHOD':0}) 

    # New gps layer with the result
    gps_stop = gps.materialize(QgsFeatureRequest().setFilterFids(gps.selectedFeatureIds())) 
    # add the layer to qgis project
    # rename the layer with the name of stop intersection 
    gps_stop.setName('gps in stop '+str(i)) 

# remove selection of gps layer
# remove selection of stops layer 


enter image description here

  • Thank you very much for your solution. I ran the code and it does exactly what you described. However, I would like to avoid the creation of too many files as I am planning to execute it many many times.
    – FunTimes
    Dec 4, 2019 at 14:14
  • With pleasure! There are algorithms to merge layers but this is another subject and it would be necessary to complete the code. Good luck ! Dec 4, 2019 at 14:21
  • 1
    Another option using this method would be to process the selection layer and keep it in-memory and not add it to the project, see gis.stackexchange.com/questions/76594/…
    – artwork21
    Dec 4, 2019 at 17:17

From DB Manager > Virtual Layers > SQL Window, you can run the following query (replace the tables and fields names with real ones) :

  bus_stops.id, -- change here the field name id with identifier bus stop field
  MIN(gps.time) AS bus_entered, -- change here the field name time for the gps field with time
  MAX(gps.time) AS bus_left -- same here
  my_table_bus_stop AS bus_stop, -- change here my_table_bus_stop
  my_table_gps AS gps -- change here my_table_gps
  ST_CONTAINS(bus_stop.geometry, gps.geometry)
  bus_stops.id -- change here too

You can see the result in DB Manager or load it as a Virtual Layer without geometry.

  • Thank you very much for your suggestion. I had to change it only slightly and it worked perfectly. Added a Distinct in the beginning: SELECT Distinct(bus_stops.id) Could not make it work otherwise.
    – FunTimes
    Dec 4, 2019 at 13:47
  • There is no need of a DISTINCT with a GROUP BY. Dec 4, 2019 at 13:49
  • Yes, I just ran it without the distinct and it was successful. Many cheers for your help!
    – FunTimes
    Dec 4, 2019 at 13:53

Your Answer

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

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