Use Menu Processing / Toolbox / Geoemtry by expression
and insert the following expression for your point layer. The expression creates lines along the points and then a buffer around it to get a polygon. Now you can use the function Count points in polygon
.
Probably some of the values should be adapted to your data. Without knowing more about the structure of your data, this is an expression that works in all cases as long as the point are located on parallel lines (tolerating a certain offset). This general functionality however means that a few values maybe need to be adapted to get an optimal result. If you provide more information, I can tell you how to adapt the expression.
Best first use a symbol layer, set to geometry generator rendering style, and paste the expression there. Then you can see in realtime how different settings affect the output. When you are satesfied, copy the expression to Geometry by expression
as mentioned above, create the polygons and count the points. Which values should be adapted is explained below. Here is the expression with some default values:
collect_geometries (
array_foreach (
array( 179,5,50,67,6,12,7,61,53,97,8,76,44,55,9,11,21),
buffer (
extend (
make_line (
geometry (get_feature_by_id (@layer, @element)) ,
project (
geometry (get_feature_by_id (@layer, @element)) ,
240000,
azimuth (
geometry ( get_feature_by_id ( @layer , 179 ) ) ,
geometry ( get_feature_by_id ( @layer , 133 ) ) )
)
),
120000,
0 ) ,
1900 )
)
)
Which settings you should change:
Let's assume you have points along a series of lines randomly distributed, as in the following screenshot. To get this solution to work, you should know from each line a unique identifier for one point - not important where this point is, at the beginning or at the end of the line, just one point per line (row). See the red points on the screenshot with their "fid" as label. If your points ar not random and you have a rule for the fid (say the fid-s of the first point on every line have a regular interval), it's even easier to create an array of the ids (one point per line). In this case you can create the array automatically. But let's assume we have random points, so we must define the array manually by selecting the fid for a point on each line: array( 179,5,50,67,6,12,7,61,53,97,8,76,44,55,9,11,21)
- that's the third line of the expression - change the values according to your data.
We use this array of fid-s to create a line through each point. Since all lines are parallel, they have the same azimuth. Thus we calculate the azimuth in one case with two arbitraty points - let's take point 179 at the bottom left and 133 on the same line (change 179 and 133 according to your data - single condition: the numbers should refer to a unique identifier of points on the same line/row):
azimuth (
geometry ( get_feature_by_id ( @layer , 179 ) ) ,
geometry ( get_feature_by_id ( @layer , 133 ) )
)
We can now draw a line from each point we selected (in the array) to another point we create by shifting this point for a certain distance in the same direction (azimuth) we calculated before, using the expression project(point,distance,azimuth)
, whereas distance is arbitrary - just long enough to cover all points. In the above expression the distance is set to 240000 - change this that if fits your points.
Because not all points lie on the same side of the arbitrary point from the array, we extend the line to the other side, using the epxression extend (geometry, start_distance, end_distance)
- start_distance
again is arbitrary, long enough to cover all points to the other side - I used 120000, change this as well until it fits your needs. end_distance
can be 0 because we already set the length for the end of the line before when we created the line.
Now we draw a buffer around the line to be sure to catch all points in case they are not positioned exactly on a line. The buffer size should be small enough to be sure not to overlap the buffer of neighboring lines. See my value for the buffer size above is set to 1900 - you can also change this. If you have quite irregular locations for the points (not exactly on a line), increase the buffer size.
We can now automatically repeat this procedure for each point we defined at the beginning in our array, using array_foreach
. Finally, we use collect_geometries
to get a MultiPolygon. We're done, the buffets show up along the line, embracing the points.