# How to count points in percentage that are within an extension defined by two pairs of coordinates

Starting from a points geometry layer, my goal is to count points in percentage that are within an extension defined by two pairs of coordinates corresponding to the corners:

The initial situation is as shown in the following screen shot:

The goal would be to get to labeling the percentage of points that are within the extension and the percentage of points that are outside the extent. The idea behind this issue need is to use a labeling expression that avoids using a physical layer of polygons. I add a recreation of what I am looking for:

Solution implementation:

I have tried the eurojam user solution and the correction from Xeppit user.

As you can see I have a layer of 10 points. 5 points are within the rectangle extension and 5 points are outside rectangle. Therefore the result should be 50%.

The rectangle has the following coordinates:

'lowerleft': '-0.00,-4.49'

'upperright': '5.40,4.45'

And the expression I use is:

``````round(with_variable(
'lowerleft',
'-0.00,-4.49',
with_variable(
'upperright',
'5.40,4.45',
with_variable('rect',
make_rectangle_3points(
make_point(to_real(string_to_array(@lowerleft,',')[0]),to_real(string_to_array(@lowerleft,',')[1])),
make_point(to_real(string_to_array(@upperright,',')[0]),to_real(string_to_array(@lowerleft,',')[1])),
make_point(to_real(string_to_array(@upperright,',')[1]),to_real(string_to_array(@upperright,',')[1]))),
aggregate(layer:='points',
aggregate:='count',
expression:=\$id,
filter:=intersects(\$geometry, @rect)))))
/ count(\$id) * 100,2) || ' %'
``````

• and where are the 2 pairs of coordinates come from? is it a layer, annotation, a user variable? Feb 7 at 8:19
• They are a user variable. The coordinates, in this case, define a rectangle. They are as follows: Lower left side: 133.423,2.8251 - 146.688,2.8251 Upper left side: 133.423,21.2611 - 146.688,26.2611 Feb 7 at 8:52
• I replaced '-' as delimiter with ',' in the make_point expression. because your lowerleft is '133.423,2.8251' instead of '133.423 - 2.8251' like in your first post... Feb 7 at 12:29
• Ok, now it is clear. However, although the expression now works, the result is not as expected. I add a screenshot. I would like if you could give me some hints as to where I am doing wrong Feb 7 at 13:45
• the next step is to override the position of the label: put it on the centroid of your rectangle. at the moment every point is labeled. you can use the definition of the rectangle which you use for the percent calaculation... Feb 7 at 16:31

There is a mistake in make_rectangle_3points geometry:

It should be

`````` make_rectangle_3points(
make_point(to_real(string_to_array(@lowerleft,',')[0]),to_real(string_to_array(@lowerleft,',')[1])),
make_point(to_real(string_to_array(@upperright,',')[0]),to_real(string_to_array(@lowerleft,',')[1])),
make_point(to_real(string_to_array(@upperright,',')[1]),to_real(string_to_array(@upperright,',')[1])
``````

--> Last line !

Check your rect geometry with geometry generator in symbology of a seperate layer that you can see the selecting rectangle and adapt it to your needs.

you can build an expression like the following one for a layer `points` and two user variables called `@lowerleft` and `@upperright`:

`````` with_variable('rect',
make_rectangle_3points(
make_point(to_real(string_to_array(@lowerleft,'-')[0]),to_real(string_to_array(@lowerleft,'-')[1])),
make_point(to_real(string_to_array(@upperright,'-')[0]),to_real(string_to_array(@upperright,'-')[1])),
make_point(to_real(string_to_array(@lowerleft,'-')[0]),to_real(string_to_array(@upperright,'-')[1]))),
aggregate(layer:='points',
aggregate:='count',
expression:=\$id,
filter:=intersects(\$geometry, @rect)))
/ count(\$id) * 100
``````

According to user Xeppit answer, a correction should be made so that the expression would look like this:

``````with_variable(
'lowerleft',
'-0.00,-4.49',
with_variable(
'upperright',
'5.40,4.45',
with_variable('rect',
make_rectangle_3points(
make_point(to_real(string_to_array(@lowerleft,',')[0]),to_real(string_to_array(@lowerleft,',')[1])),
make_point(to_real(string_to_array(@upperright,',')[0]),to_real(string_to_array(@lowerleft,',')[1])),
make_point(to_real(string_to_array(@upperright,',')[1]),to_real(string_to_array(@upperright,',')[1]))),
aggregate(layer:='points',
aggregate:='count',
expression:=\$id,
filter:=intersects(\$geometry, @rect))))
/ count(\$id) * 100) || ' %'
``````
• Thanks a lot for this proposed solution. I am trying to implement it but I think I have some syntax problem with the expression. I add more information to my question and ask for some more help to understand how I can make the expression work. Feb 7 at 11:42