I have used QGIS process 'Join Attributes by Location' in one of the models I have built and I have now been asked to explain in detail exactly how the model is determining the results.

I am using it to fill in spaces in the data by joining by location using the touching option with 0.1 precision and taking attributes of the first located feature.

I have tried making mock squares and applying the same principles to determine what way QGIS picks the first located feature but I have been unsuccessful in determining how exactly it is doing it.

I have considered N-S, E-W and also the order of which the Data being transferring has been created but none of them seems to explain how it chooses the feature.

I have looked for answers to this online and in the QGIS website but have found no explanation

enter image description here

Above is a sample of what results when trying this technique. The light blue are the original polygons with their creation order attached in the attribute table and the purple is the number that joins when testing join by location to assign a number to the blank purple polygons from the light blue ones.

Does anyone know exactly how QGIS selects this number to assign?

QGIS Version 2.18.28

  • 1
    I don't know, but my guess would be either the row number or feature id. You do a similar comparison by adding these to fields in the Field Calculator with the @row_number variable and the $id variable.
    – csk
    Apr 5, 2019 at 16:08
  • 1
    The numbers in the Light blue squares are the ID no's. This is also the order in which the squares where created in the layer. I thought it could possibly have been this way you expected also as 1 and 2 behaved in this way, but when looking at the last 2 purple poly's then by that reasoning no's 4 and 6 should have filled the purples poly's, but this doesnt appear to have happened. Apr 8, 2019 at 11:21
  • 1
    For Spatialite : gaia-gis.it/gaia-sins/spatialite-cookbook/html/rtree.html . If your layer have spatial indexes, spatial operations uses them and, I think, this affects the order of the result. Database layers or even shapefiles can have spatial indexes. May 2, 2019 at 8:52
  • 3
    Each Polygons node location has been considered as a factor from each of the four corners already, along with the ID number (which is also the order in which the polygons were created). It still doesn't appear to follow any specific pattern we have come up with, which it must as someone has programmed the process so it must follow some type of rules. It surely cant just be a random grab of one touching feature can it, as I believe computers are incapable of being truly random by design May 2, 2019 at 11:12
  • 1
    I imagine that as in a RDMS an SQL statement can perform the operation in an order completely unknown for the operator. My guess is that we have the same here, so the real operation can change even on the same data each time it is performed. Maybe it is not really random, but it is in practical terms
    – Marco
    May 2, 2019 at 18:59

2 Answers 2


When using one-to-many join type, 'join attributes by location' will always select the tile that was created first out of the available tiles.

For example for the first dark blue tile (shown below), the available touching tiles are 1, 2, 8, 11 and 12. Out of these tiles, 1 was selected since it was made first.

enter image description here

In the case of the third dark blue tile (shown below), the available touching tiles are 4, 5, 6, 9, 10, 14 and 15. Out of these tiles, 10 was chosen since it was the first attribute created.

enter image description here

The order the shapes were created can be checked by using $id in the attribute table's formula bar and sorting lowest to highest.

(However, if you have your attribute table open while creating new shapes, the $ids for the new shapes can show up as negative. To fix this and get the actual $id simply close and reopen the attribute table and re-run $id.)

Hope this helped!

  1. I would change the attribute of 7 to another number, let's say 99 and rerun the tool. (Did the purple square inherit the value 99?)

  2. Another test that I would do is:

    • Back up you existing data in a new folder
    • Start editing the light blue color layer
    • Select the square with label 7
    • Cut the square (as Ctrl-X), the square will disappear
    • Stop editing the layer and save the changes
    • Start editing the layer again
    • Paste the square (it will reappear in the exact same place) but change position in the attribute table
    • Stop editing and save the edits
    • Re-run the tool

Is the value in the big purple polygon still 7? If yes, then it will be based on location, if no then it will take the first record available in the attribute table.

In my point of view, the tool takes the first record and adds it to the target attribute table when the rule applies, it will repeat the same operation until it will take the value of the last record in the original table that would comply with the specified rule.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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