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?
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.
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).
Add an empty "distance" field to the point layer. Then run the
v.distancealgorithm 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.
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.
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)