qgis / QGIS

QGIS is a free, open source, cross platform (lin/win/mac) geographical information system (GIS)
https://qgis.org
GNU General Public License v2.0
10.43k stars 2.98k forks source link

Join attributes by location (summary) - Wrong result with most recent versions (3.26 & 3.28) #51324

Open DorotheeCle opened 1 year ago

DorotheeCle commented 1 year ago

What is the bug or the crash?

Hi, I have a pb with the algorithm Join attributes by location (summary). I need to summarize datas of lines in each area of a grid. With an old version of Qgis (3.14), this algorithm worked very well. But with the most recents versions (3.26 & 3.28) the result is wrong: some segments are not taken into account during the join.

In this example (see image), the result of the algorithm Join attributes by location (summary), for the sum of the parameter « duree2 » of the segments in this square is 8,291 But when I select the segments in this square and when I check the stats the result is different. 1 segment (arrow) is not include by the algo.

ex  pb

Steps to reproduce the issue

data.zip The Resulting layer of the algorithm Join attributes by location file is my result. To get it :

Toolbox > Vector general > Join attributes by location (summary) and :

image

To verify the result I put the grid's labels with the result "duree2_sum", and I check with the Statistical Summary Panel the sum of the lines selected for the column "duree2". For some squares the results is ok, but for some other the result is wrong.

image

Versions

3.26 & 3.28

Supported QGIS version

New profile

Additional context

No response

DorotheeCle commented 12 months ago

Hi ! Just for news, I've tried recently with the most recent version 3.32, still doesn't work. I hope someone gonna find a solution !

freeshrugsxd commented 5 months ago

Hey @DorotheeCle

I had this exact same issue today but the problem is not the Join attributes by location algorithm -- at least not the math part. The problem is with how QGIS determines what line lies within your grid feature polygon.

I can see from your screenshots that you and I tried to do the same thing:

  1. create polygon grid
  2. create line grid using the same parameters
  3. split some linestring layer via Split with lines and the created line grid
  4. Join attribute by location (summary) to write sum of some attribute into the polygon grid layer

The problem is that some lines are not assigned to the correct grid cell because their vertices lie just outside the correct polygon. Let's say your cell's western edge is at x=100, but the first vertex of your line segment, which was created by Split with lines, is at x=99.99999999999876552... then it is not selected as within your grid cell.

You can verify what features are selected by using Vector > Research Tools > Select by Location… as I assume this uses the same logic as Join attributes by location. It gets really wild when you use intersect as predicate instead of within/contains.

As a workaround, I ended up creating centroids of the line segments and use those as input layer for Join attribute by location (summary) as the centroid is 100% certain to lie within your square grid cell.

I hope that helps and I would love to see some fuzzy coordinate matching in QGIS because for every real world GIS application I can think of 99.99999999999876552 is 100.

Greetings ✌

DorotheeCle commented 5 months ago

So simple but so effective ✨! Thank you so much @freeshrugsxd !