1

I have 2 layers, layer A ( point data) and layer B (polygon data). Within layer A I want a column which will indicate Yes or No if a polygon is within 500m. I have created a 500m buffer for layer b and have currently used the geomtouchs function to select those polygons. I can't get it to fill the column with Yes or No though. Has anyone any other methods they would use?

  • Use join attributes by location. Then, if you have new entries from the buffer, you can put in yes, else no. – Erik Feb 11 at 14:01
2

To use the geomtouches() function, make sure the RefFunctions plugin is installed.

Use the Field Calculator on Layer A to add a text field, at least 3 characters long, with this expression:

if(geomtouches('buffer layer','$geometry') is not null, 'yes', 'no')

If you want to skip creating the buffer layer, here are two different methods.

  1. This method relies on the geomnearest() function, which is currently broken. It gives an Eval error, "error: targetFieldName not present" or "error: targetLayer not present," even when the layer and field name are correct. I'm leaving this method in my answer, because it will be the best method once this error gets fixed.

    Skip creating the buffer layer and simply use this expression:

    if(geomtouches(buffer($geometry,500),geomnearest('polygonlayer',$geometry)) is not null, 'yes', 'no')
    
    • buffer($geometry,500) calculates a 500 unit buffer around the current point feature. This buffer exists only for the calculation, so you never see it on the map. Note that the buffer units are the same units as the point layer. So if the point layer CRS uses meters, it will be a 500 meter buffer; if the point layer CRS uses decimal degrees it will be a 500 degree buffer (ie, larger than the entire world).
  2. Add an empty "distance" field to the point layer. Then run the v.distance algorithm from the Processing Toolbox. Algorithm parameters: from your point layer to your polygon layer; max distance = 500; 'upload' value choose "dist"; column name choose the "distance" field you just created; under Advanced Parameters, 'from' feature type choose "point", 'to' feature type choose "boundary". You only need to export the Nearest output, but I recommend also exporting the "Distance" output. It creates a line layer that shows which point is connected to which polygon. You can double-check the algorithm results by measuring the lines and comparing them to the "distance" value for the corresponding point.

enter image description here

This will create a copy of the point layer layer with nearest-neighbor distance values in the distance column. Be sure to save the algorithm output before closing the project.

enter image description here

To get the yes/no field, use the Field Calculator to add a text field with this expression:

if("distance" is not null, 'yes', 'no)
  • Thanks for your help. Not creating a buffer layer would save me loads of time. But struggling to get the expression to work. Am I supposed to replace part of the expression with the polygon layer name? It just says the expression is invalid. – Woody Feb 12 at 12:23
  • You're right, there should be a reference to the polygon layer in the second expression. Once I fixed that mistake, I discovered the geomnearest() function is broken. I added a different method that you can use until that bug is fixed. – csk Feb 12 at 18:02

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.