0

I need to figure out how many points, and which ones (with labels) fall outside the boundary of a particular polygon in QGIS.

0

1 Answer 1

2

Since you need to know wich points, with labels, fall outside the boundary of a particular polygon, It seems to me that your question is not a duplicate of Count points out of polygon using QGIS?. Equally that question has a good answer that I recommend you read, since you could achieve your goal also starting from the selection approach.


You can take advantage of the distance( geometry a, geometry b) function, to set a value to a new column in the point layer, from which you can then identify, count, and label the points that are outside a particular polygon. When a point is outside a polygon, the distance function returns a value greater than zero.

Assuming you want to establish if each point is outside the single polygon feature that has the value 1 in the id field of the polygons layer.

Assuming also that you have created a column of string type to set the value outside if the point is outside that polygon, or an empty string if not.

You could populate that attribute with the following expression:

if( distance( $geometry, geometry( get_feature( 'polygons', 'id', '1'))) > 0, 'outside', '')  

You can play a bit with this expression to get results more suited to your specific need.

17
  • I've created the string field but the command does not seem to work. I am new to this, would you please let me know how to write the command if the name of my polygon layer in "VDC" and the name of the point layer is "Households". I have created an empty string column named "string" in the polygon layer.
    – Sarah
    Jan 28, 2019 at 6:31
  • Hi @Sara, you can add an attribute to the points layer, wich tells if each point is outside the polygon or not. Also, you need to identify to which polygon of the VCD layer you want to compare the points. That polygon needs to have an "id" field for example. And you need to know to wich polygon id you want to compare the points. But the expression is for the points layer. Jan 28, 2019 at 7:05
  • Then, you can do more analysis, like count all the points with the attribute 'outside', or label them. First try to achieve that attribute in the point layer. Jan 28, 2019 at 7:09
  • I created the string attribute in the point layer and named it "outside". The VDC polygon layer only has 1 polygon. I merged them all because I need to know which households fall outside the VDC/Municipality boundary. So this polygon would have only one "id" which is named "VCODE" which I can see in the attribute table of the polygon.
    – Sarah
    Jan 28, 2019 at 7:18
  • Here is the expression which is not working: if( distance( $geometry, geometry( get_feature( 'VDC', 'VCODE', '47'))) > 0, 'outside', ''). The VCODE /id is 47 for the polygon. I selected the point layer from layers column, opened the python console and entered the expression in there.
    – Sarah
    Jan 28, 2019 at 7:27

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