2

I want to select points 2 and 3 with a 'selection with expression' in QGIS 3.4. I want to do this by using the buffer function in the expression builder.

I already managed to create a separate buffer and then selecting points that are overlapped by the buffers.

I expect that the expression will select points 2 & 3. The expression buffer( $geometry, 5000) is not giving the result i expect.

Buffered selection

  • $geometry uses the current feature/layer as base. – Erik Jun 21 at 12:58
  • I selected the church layer, clicked select by expression and filled in the expression. – BearHunt Jun 21 at 12:59
  • The question is unclear. What are you creating buffers around? what do you hope to capture with the buffers? if you simply need to select points 2 and 3 you can do so without a buffer. – Marshall Jun 21 at 13:02
  • My goal is to select all parking lots within 250m of churches. In order to do this, I do not want to create a separate layer with buffers. I know that the Overpass API of OSM has the ability to select features around another feature using the 'around(x)' parameter. – BearHunt Jun 21 at 13:05
  • You need to tell QGIS, which layer is the base for the buffer, or from which layer the selection should happen. Otherwise all selections will happen on only one layer. – Erik Jun 21 at 13:08
1

Unlike the Buffer tool, the buffer function in the expression builder can only accept a single feature as input. It can't buffer an entire layer. So you'll have to merge all of your church points into a single multipoint feature before you can do this process using the select by expression tool.

  1. Use the Dissolve tool (processing toolbox) to merge the church points into a new layer with a single multipoint feature.

  2. Assuming that layer is called 'church_points_dissolved', it has an attribute field 'ID', and the only feature in that layer has 'ID' = 0, you can use this expression in the Select by Expression tool to select parking lot points that fall within a specified buffer distance of a church point:

    intersects($geometry, geometry(get_feature('church_points_dissolved','ID',0)))
    

Note: This method is at least as much work as simply creating a separate buffer layer of the church points, and using the Select by Location tool to select parking lot points that intersect the buffer layer.

  • Ok, thank you for clarification. I thought that a 'simple' selection query was possible. I stick to creating a layer. – BearHunt Jun 21 at 21:12
  • actually I just found a method using the select by expression tool with a function from the RefFunctions plugin. will post as a separate answer. – csk Jun 21 at 21:16
  • 2
    I don't have Q handy to try out right now but couldn't you collect all the point features into one geometry by using collect() in the expression builder in Q 3.x? – she_weeds Jun 23 at 7:17
  • 1
    @she_weeds Thank you! I've been stumped by the lack of a merge or dissolve function for combining geometries in the expression builder. It didn't occur to me to look at the aggregate functions. You're right, the collect function is what I've been looking for. – csk Jun 25 at 15:24
5

Thanks to the suggestion by @she_weeds, I found a much better solution. This method can be performed entirely in the Select by Expression tool, without creating any additional layers.

Use the collect function to combine the church points into a multipoint geometry. Use this multipoint geometry in the intersects function. Because we're using collect on a different layer, we have to put it inside the aggregate() function, like so:

intersects($geometry, buffer(aggregate('churches','collect',$geometry),5000))
2

After posting my previous answer, I found a way to do this selection without creating any additional layers.

  1. Install the RefFunctions plugin. This adds several new functions to the expression builder, including geomdistance().

  2. Use the geomdistance() function to select points in the parking lot layer that are within 5000 distance units of any point in the church layer. See example expression below. In the example, the church point layer is called 'churches'. It has a field called 'field', where all the values in that field are 1.

    geomdistance('churches','field',5000)=1
    

Notes:

  • You can use a field already in your church point layer, as long as every feature has a value, and you can write a true/false expression that is true for every feature. Eg, if you have a numerical field where every feature has a value greater than 0, write the expression as geomdistance('churches','yourfield',5000)>1. For some reason, it doesn't work with the criteria is not null.

  • The distance is calculated using the project settings for distance calculations.

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.