im looking for a way to get the quantity of lines from a line-layer that intersect the different polygons of a polygon-layer;

I have:

  • One line-layer with a clutter of lines, defined by ID, which represent the flight-activity of birds in a specific area. The layer is called "lines".

  • One Polygon-Layer with a specific grid. The layer is called "grid".

I need for every square-polygon from the "grid"-layer the quantity of different lines from the "lines"-layer, which intersect them. The Value is supposed to show up in a new column of the attribute-table of the "grid"-layer called "LineCount" in this example (the layers in the screenshot are just examples, the original ones have much more lines, so it is not practical to count them by hand ;-)).

The aim is to visualize the density of flight-activity in a raster-like map.

I guess there might be some solution within the refFunctions-plugin, but I'm not able to figure it out.

enter image description here


I tried to also use the refFunctions, in particular the intersecting_geom_count function but I think this only applies if the geometry from both the target and the source are the same. But we can create our own function which counts the number of lines which intersect each polygon grid cell.

  1. In your Field Calculator, click the Function Editor tab, either create a new file or edit an existing one and enter the following code:

    from qgis.core import *
    from qgis.gui import *
    @qgsfunction(args='auto', group='Custom', usesgeometry=True)
    def count_intersections(grid_layer_name, line_layer_name, feature, parent):
        grid_layer = QgsMapLayerRegistry.instance().mapLayersByName( grid_layer_name )[0]
        line_layer = QgsMapLayerRegistry.instance().mapLayersByName( line_layer_name )[0]
        count = 0
        for line_feat in line_layer.getFeatures():
            if feature.geometry().intersects(line_feat.geometry()):
                count = count + 1
        return count

    Function Editor

    Then click Load.

  1. Click the Expression tab and either create or update your LineCount field and use the following expression (where grid and line are the names of your grid and line layers respectively):

    count_intersections('grid', 'line')

    Field Calculator Expression

  1. Your LineCount field should hopefully now contain the number of lines which intersect that particular polygon grid cell. Example:



This answer comes probably too late for you, but I recently answered a very similar question: Uncertainty of a line between two coordinates with uncertainties in QGIS (I didn't see your question then).

I am copying the relevant part of my answer here:

This is a little tricky. You first have to cut your lines so that each line is split across the cells. To achieve that, use the intersection tool (Vector > Geoprocessing Tools > Intersection in both QGIS 2.18 and 3.0). Choose the line layer as "Input layer", and the grid as "Intersection layer".

To prepare the next step, we have to add a field to the layer we just obtained. Select this layer and open the field calculator; check the "create a new field option", set 'newid' as name, keep 'numeric' as type. In the "Expression" box, write $rownum, then click OK. You will get a new field with a unique id for each line segment.

Then, we'll use the mean coordinates tool (Vector > Analysis tools > Mean coordinates in QGIS 2.18, again in the toolbox for QGIS 3.0). Set the intersected layer (from the previous operation) in the "input layer" field, and 'newid' as "Unique ID field". This gives you a point layer, each point corresponding to a line segment.

Finally, use the "Count points in polygon tool" (in Vector > Analysis tools for both QGIS 2.18 and 3.0). Set the grid in the "Polygons" field and the newly created point layer in the "Points" field. This will create a new field in the grid with the number of line segments in each grid cell. You can divide this number by the total number of lines in the field calculator if you want, if you need an estimation of line probability.

Hope this helps.

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.