Is there a way to select the cell (single) with the maximum value in a polygon layer using the intersection of a line? The first image is the grid (polygon) layer with the attribute field "VALUE" and a line.

In the second image, the line crosses over cells A, B, C, D and E.

Is there a way to use the line to select the highest "VALUE" from A through E? In some cases, the line could be shorter or longer, so an expression that only took the line and the cells it crosses would be the best scenario.

enter image description here

2 Answers 2


You can use "Select By Expression" with the following expression (replace 'Line' with the name of your line layer):

            group_by:=overlay_intersects('Line', $id),
            filter:=array_length(overlay_intersects('Line', $id)) > 0,
    ) = $id

There are quite a few QGIS expression wizards on here (I'm not one of them but I'm trying to learn) so maybe someone will offer a better expression, but I have tested this and it works well.

enter image description here

Reference: I adapted the expression from MrXsquared here.

  • I thought you will publish a PyQGIS solutions ^_^
    – Taras
    May 31 at 7:44
  • 1
    Hello @Taras! I took the opportunity to try a different solution. I'm trying to improve my knowledge of expressions. As much as I use PyQGIS, sometimes I really appreciate the beauty and power of QGIS expressions (and admire the mastery that you and others have of them).
    – Ben W
    May 31 at 7:58
  • You did it great! As well as other things, especially the PyQGIS.
    – Taras
    May 31 at 9:25
  • 1
    @Taras, thanks for the kind words :-)
    – Ben W
    May 31 at 13:29
  • 1
    I had no idea that so much could be done through QGIS expressions - thanks for sharing! Much more concise than writing it in pyQGIS. May 31 at 20:19

The can be done through pyQGIS. The script below will need to be updated with your layer names, but the bones of the functionality in there. Hope this helps

lines = QgsProject.instance().getLayersByName("Lines Layer")[0]
poly = QgsProject.instance().getLayersByName("Polygons Layer")[0]

# get all fids for lines features
fids = []
for lineFtr in lines.getFeatures():

# begin edit session
with edit(lines):
    for fid in fids:
        # set filter to only use one line feature each time
        lines.setSubString(f"$id = {fid}")

        # create virtual layer of all overlapping polys
        res = processing.run("native:extractbylocation", {
            'INPUT': poly,\
            'PREDICATE': [0],\ # 0 = extracting only intersecting polys
            'INTERSECT': line,\
        output = res["OUTPUT"]

        # get max value from overlapping polys
        polyValues = []
        for ftr in output.getFeatures():
            value = ftr["VALUE"]
        maxValue = max(polyValues)

        # save max value in current line feature
        for lineFtr in lines.getFeatures():
            lineFtr["MAX_VALUE"] = maxValue
        # update line feature
  • 1
    The script produced a few errors in my Python Console - thank you for taking the time to answer my question
    – Anthony
    May 31 at 22:32

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.