Revisiting point & polygon joins
Joining polygon attributes to points based on their location is a very common GIS task. In QGIS 2, QGIS’ own implementation of “Join attributes by location” was much slower than SAGA’s “Add polygon attributes to points”. Thus, installations without SAGA were out of good options.
Luckily this issue (and many more) has been fixed by the rewrite of many geoprocessing algorithms for QGIS 3! Let’s revisit the comparison:
I’m using publicly available datasets from Naturalearth: The small scale populated places (243 points) and the large scale countries (255 polygons with many nodes). Turns out that QGIS 3’s built-in tool takes a little less than two seconds while the SAGA Processing tool requires a litte less than six seconds:
Like in the previous comparison, times were measured using the Python Console:
In both tools, only the countries’ SOVEREIGNT attribute is joined to the point attribute table:
import processing import datetime t0 = datetime.datetime.now() print("QGIS Join attributes by location ...") processing.runAndLoadResults( "qgis:joinattributesbylocation", {'INPUT':'E:/Geodata/NaturalEarth/vector_v4/natural_earth_vector/110m_cultural/ne_110m_populated_places.shp', 'JOIN':'E:/Geodata/NaturalEarth/vector_v4/natural_earth_vector/10m_cultural/ne_10m_admin_0_countries.shp', 'PREDICATE':[5],'JOIN_FIELDS':['SOVEREIGNT'], 'METHOD':0,'DISCARD_NONMATCHING':False,'OUTPUT':'memory:'}) t1 = datetime.datetime.now() print("Runtime: "+str(t1-t0)) print("SAGA Add polygon attributers to points ...") processing.runAndLoadResults("saga:addpolygonattributestopoints", {'INPUT':'E:/Geodata/NaturalEarth/vector_v4/natural_earth_vector/110m_cultural/ne_110m_populated_places.shp', 'POLYGONS':'E:/Geodata/NaturalEarth/vector_v4/natural_earth_vector/10m_cultural/ne_10m_admin_0_countries.shp', 'FIELDS':'SOVEREIGNT','OUTPUT':'C:/Users/anita/AppData/Local/Temp/processing_8b1bbde78de5490285dd530e115cca52/099660d88bf14c54a853cc230e388e55/OUTPUT.shp'}) t2 = datetime.datetime.now() print("Runtime: "+str(t2-t1))
It is worth noting that it takes longer if more attributes are to be joined to the point layer attribute table. For example, if the JOIN_FIELDS parameter is empty:
'JOIN_FIELDS':[]
instead of
'JOIN_FIELDS':['SOVEREIGNT']
then the the Join attributes by location takes almost 16 seconds. (The country layer contains 71 attributes after all.)
(The SAGA tool currently allows only joining one attribute at a time.)