What I am trying to accomplish is to find the number of points that intersect a created buffer. I.E. I have a layer called "Buildings", for each building I have fixed distance buffers (<400m). I also a layer called "bus_stops" that includes points. Now I want to calculated how many bus_stops are within each buffer and have a "bus_stop_count" feature in attribute table. How do I accomplish this using QGIS?

What I have tried:

I tried joining attributes by location, using the "bus_stops" layer and the buffer layer. But it doesn't work.


3 Answers 3


Another option is to use SQL Spatial and skip the buffer creation altogether.

This can be done using the DB Manager SQL editor tool (note I have had some issues on my machine but things should work fine on your machine with the SQL below - I have used PostGIS to write the sql, but the syntax in DB Manager is identical)

The logic below uses the ST_DWithin function, which is a more efficient use of finding points within a buffer - it just returns the points within the distance specified, without the need to create the buffer itself.

Usually you would use these functions in PostGIS, Spatialite or another spatial database, but the QGIS DB Manager lets you perform these functions on shapefiles, which I'm guessing you're using.

So the sql would look something like this (where I'm counting the number of bus stops from a set of schools defined by their schnum)


, count(*) as "BusStopCount"

from "schools" as sch
, "rtd_bus_stops" as b

where sch.schnum in ('455', '447')

and ST_DWithin(ST_Transform(b.geom, 2877), ST_Transform(sch.geom, 2877), 1500)

group by sch.schnum

And the output looks like this:

enter image description here

Note a few other things happening in there: I'm projecting the geometry columns of both tables to CO State Plane (2877) which is in FEET, so I can calculate the distance the ST_DWithin function uses as 500 'feet' (my data is all stored in WGS84/4326

And also note the count and grouping functions used in standard SQL count queries.

Lastly: If you DID want to use buffers, because you had different buffer sizes depending on different variables within each type of building or in my case school, you could use CASE statements in the SQL to generate the buffers based on a data value.


If you need to use the Join attributes by location algorithm (maybe for performing more statistics), you can do your task by setting this parameters in the dialog:

enter image description here

The joined output layer will have an additional field, called "count", containing the number of points which intersect the buffer.


In Processing toolbox there is function for this - Count points in polygon. This function creates a new polygon layer, which is same as original input polygon layer with addition of point counts attribute.

enter image description here

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.