[QGIS-Developer] timzonefinder vs point in polygon

Nyall Dawson nyall.dawson at gmail.com
Mon Feb 8 18:12:05 PST 2021


On Tue, 9 Feb 2021 at 01:04, C Hamilton <adenaculture at gmail.com> wrote:
>
> I tested "Join attributes by location" and it is faster although I must have a slower machine to run on than yours.
>
> Most of the time the use case is single point matching against the time zone polygon. You mention that "Join attributes by location" is best used when the number of points >> number of polygons. What is the best method to match a single point and do that many times for uses such as during mouse motion to report the time zone of the cursor to the status bar.

I'd suggest using similar logic to what join attributes by location
does internally. In pseudocode:

search_point = ...
search_bbox = QgsRectangle( search_point.x() - some tolerance,
search_point.y() - some_tolerance, ... )
request = QgsFeatureRequest().setFilterRect(search_bbox)
zones_intersecting_bounding_box = tz_layer.getFeatures(request)
for time_zone_feature in zones_intersecting_bounding_box:
    if time_zone_feature.intersects(search_point):
        # found a hit!
        break

This will take advantage of whatever spatial index you have on the
time zone layer (e.g. the internal shapefile/gpkg/... index)

There's quite a number of extra optimisations which could be done here
if the speed isn't sufficient for mouse movements, e.g. you could
fetch all the zone features in the canvas extent and "prepare" their
geometries for ultra fast point in polygon tests, and then invalidate
this cache and rebuild on each canvas extent change. But I'd only do
that kind of thing if needed...

Nyall





>
> Calvin
>
> On Fri, Feb 5, 2021 at 6:30 PM Nyall Dawson <nyall.dawson at gmail.com> wrote:
>>
>> On Sat, 6 Feb 2021 at 07:54, Andrea Giudiceandrea <andreaerdna at libero.it> wrote:
>> >
>> > C Hamilton wrote
>> > > Using the algorithm Vector->Geoprocessing Tools->Intersection: 4 minutes,
>> > > 4
>> > > seconds
>> >
>> > Hi Calvin,
>> > maybe it could be better to use the "Join attributes by location" algorithm.
>> > It only takes few seconds to preform the join.
>>
>> Confirmed - for me it only takes ~2 seconds, and that's on an
>> non-optimised debug build! There may be a few % more performance boost
>> on the proper QGIS release builds.
>>
>> "Join attributes by location" has a bunch of extra logic to optimise
>> the method that the join is performed, which really pays off in this
>> particular situation (matching points to polygons, where number of
>> points >> number of polygons).
>>
>> Nyall
>>
>>
>>
>> >
>> > Regards.
>> >
>> > Andrea
>> >
>> >
>> >
>> > --
>> > Sent from: http://osgeo-org.1560.x6.nabble.com/QGIS-Developer-f4099106.html
>> > _______________________________________________
>> > QGIS-Developer mailing list
>> > QGIS-Developer at lists.osgeo.org
>> > List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
>> > Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
>> _______________________________________________
>> QGIS-Developer mailing list
>> QGIS-Developer at lists.osgeo.org
>> List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
>> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer


More information about the QGIS-Developer mailing list