11

I have a line shapefile of roads and a point shapefile of car collisions and am trying to count the number of collisions per road. The points do not exactly intersect with the lines. I'm hoping to end up with a new column in the 'roads' attribute table with the number of collisions.

I've found a post on how to do that in ArcGIS here: How to produce a 'count' of points intersecting lines?, but haven't been able to figure it out in QGIS.

I've found suggestions to create a buffer of the roads in order to create a polygon and use the Points in Polygon analysis, but the roads data is quite large, and creating a buffer was taking ages for me, so I'm hoping there's another option.

I've tried using the "Join attributes by location" function, with 'roads' as the target layer and 'collisions' as the join layer, but I don't know how to set the Precision input so that all of the collision points are counted.

In ArcGIS "Join based on spatial location" has the "closest" option when joining points to lines, and I'm hoping that QGIS offers something comparable.

I'm using QGIS 2.18.2

2
  • Welcome to GIS SE! As a new user please take the tour to learn about our focused Question and Answer format. What have you tried? Did the post for ArcGIS give any suggestions that look like they might work with QGIS? Please edit your question to include any extra information.
    – Midavalo
    Commented Jan 20, 2017 at 0:37
  • Please, do not forget about "What should I do when someone answers my question?"
    – Taras
    Commented Nov 5, 2022 at 8:30

2 Answers 2

12

You can use Join by location to join polyline with point shapefiles. However, the point should be exactly snapped to the line in order to be joined, otherwise, it is better to create a buffer of polygon shapefile around the road lines of distance that you can decide after several tests, then use the buffered polygon to join the points by location. To find the tool, you can go to Vector -> Data management tools -> Join by location, as you can see below:

enter image description here

The output polygon will create a count field in the attribute table of the number of accident points:

enter image description here

I used QGIS 2.14.8 for the above process.

4
  • Precision I'm assuming relates to distance between features, note the dialog is quite different as of QGIS 2.18, setting required are: geometric predicate = intersect, precision = max distance collisions are from road line, statistics for summary = sum and joined table = only keep matching records Commented Jan 20, 2017 at 10:33
  • Thanks for this - I edited to clarify that the points are not exactly snapped to the lines. Maybe I just need to experiment with using the precision setting to capture all of the collisions.
    – Logan
    Commented Jan 20, 2017 at 21:15
  • In the more recent versions (I'm using 2.18.13) you have to specify precision - just set this to 1 as would be used in this example. I created a small buffer around the point and this worked - similar type of data -> collisions / segment of road. Commented Oct 28, 2018 at 5:59
  • In QGIS 3 you need to go to the processing toolbar to open the Join attributes by location (summary) tool. Commented Mar 6, 2019 at 17:37
1

You can use a Virtual Layer. Replace "Random points" with the name of your point layer, "road" with the name of your road layer, and id with the name of some unique id field in your road layer:

select  r.*, --Select all columns from road table
        count(p.geometry) as pointcount --Count the points within 150 m for...
from "road" r
left join "Random points" p
on PtDistWithin(r.geometry, p.geometry, 150)
group by r.id --... each road id

enter image description here

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.