2

I have 1.000 point layers (shapefiles).

How can I find points intersected to a line without defining the points layer?

Because defining thousand layer takes time. And I dont wanna merge those point layers because I need it for visual purpose.

I make a custome line which has branch. I already have a solution to 'sum' each value in points that intersected in line from previous question but not finished. This is the function :

aggregate(layer:='point1',aggregate:='sum',expression:="number",filter:=intersects($geometry, geometry(@parent))) + aggregate(layer:='point2',aggregate:='sum',expression:="number",filter:=intersects($geometry, geometry(@parent)))

1st problem : i got wrong sum result after the first line i draw ( i want duplicate points only count once)

2nd probelm : imagine i have 1000 layers of points, it is so hard to type them all. So i need generalization for N points layer or any solution which doesnt require to define points layer.

UPDATE

I was thinking of another option to get correct sum number.

The picture above is the attribute table for drawn line to get intersecting point and get the point attribute.

Maybe we can merge these two column, so the yellow-highlighted line one group into one.

  • Do your point layers share a pattern in their names? – Taras Apr 20 '20 at 6:48
  • yes every layers shares the same attribute field name, says, "number" @Taras – sauki mufti Apr 20 '20 at 6:53
  • Hold on, do you have one shapefile with 1.000 points in it, or 1.000 shapefiles with a certain number of point in each? – Taras Apr 20 '20 at 6:55
  • It is just example, in my current work, i just simulate three point layers and one line layer and i found solution already using aggregate function (we have to define every point layers target). I need to generalize the solution for N point layers. If i have thousand layers will be so difficult to type it all. Because i am working on power system flow. – sauki mufti Apr 20 '20 at 6:56
  • 1000 shapefiles point layers which has attribute field name "number" @Taras – sauki mufti Apr 20 '20 at 6:57
2

You will have to loop through all your project's layers and pass them in the aggregate function with this expression:

with_variable('point_layers', array_filter(
    array_foreach(@map_layers, layer_property( @element, 'name')),  --< get a list of all layers names
@element <> layer_property(@layer, 'name') ), --< filter current layer out of the list

array_count( --< function to add all numbers in an array (you have to make it)
    array_filter( array_foreach( @point_layers ,  --< loop through all layers names
         aggregate(layer:=@element ,
         aggregate:='sum', 
         expression:="att",  --< the attribute
         filter:=intersects($geometry, geometry(@parent)))),
    @element is not NULL)--< filter all NULL values
    ))

I haven't found a function to add all numbers in an array in the qgis expression builder, so you'll have to create one by copying this in the function editor (next to the expression tab):

from qgis.core import *
from qgis.gui import *

@qgsfunction(args='auto', group='Custom')
def array_count(array, feature, parent):
    """
    Calculates Sum of numbers in an array.
    <h2>Example usage:</h2>
    <ul>
      <li>array_count(array(1, 2, 3)) -> 6</li>
    </ul>
    """
    arrayCount = sum(array)
    return arrayCount

It might be a bit slow for 1000 point layers. edit: you have to filter current layer out of the list for it to work

  • i have write down your code into expression builder and changing the attribute part into field "number" as i put the field name "number". but i get same result as the picture above. anything else did i miss ? sorry i am just newbie to QGIS – sauki mufti Apr 21 '20 at 1:34
  • i tried to understand your function and realize what you trying to build is so cool bro for generalizing the point_layer, but i still cant figure out how to solve 1st problem, or we can do sth else i have update the question – sauki mufti Apr 21 '20 at 3:35
  • I have no solutions for duplicate points, you'd have to clean your points layers first. Any reason why they are duplicated in the first place? – LeoC Apr 21 '20 at 5:54
  • because aggregate function counts starting point to finish point, when we have branch we should draw the line from starting point that we already use before, that's why it is counted for the 2nd intersecting array point so we get the sum we dont want. am i correct ? but since i am new, i dont know how to solve it :(( – sauki mufti Apr 22 '20 at 1:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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