<div dir="ltr"><div>Nyall,</div><div><br></div><div>Using "Join attributes by location" on a large set of points is fast, but if you are doing single point lookups it bothers me that I am not getting that fast of results in comparison to timezonefinder. The 10,000 points is intended to test the speed of many single point lookups. If I were going to actually do 10,000 point lookups I would use the Join attributes by location, but here are my results of single point lookups using timezonefinder vs. your method. Note that the assumption is that they can be anywhere in the EPSG:4326 bounding box. I am not trying to constrain them to the QGIS canvas.</div><div><br></div><div>timezonefinder: 44 seconds</div><div>Your method: 144 seconds</div><div><br></div><div>Here is my code for each:</div><div><br></div><div><b>timezonefinder</b></div><div><font face="monospace">from timezonefinder import TimezoneFinder<br>import time<br>tf = TimezoneFinder()<br><br>l = iface.activeLayer()<br>features = l.getFeatures()<br><br>start_time = time.time()<br>for f in features:<br>    pt = f.geometry().asPoint()<br>    name = tf.certain_timezone_at(lng=pt.x(), lat=pt.y())<br>print('Time {}'.format(time.time() - start_time))<br></font></div><div><br></div><div><b>Your method</b>:</div><div><font face="monospace">import time<br>tzlayer = QgsVectorLayer("W:\\My Documents\\GitHub\\timezone_speed_lookup_test\\timezones.gpkg", "timezones", "ogr")<br><br>l = iface.activeLayer()<br>features = l.getFeatures()<br><br>start_time = time.time()<br>for f in features:<br>    pt = f.geometry().asPoint()<br>    rect = QgsRectangle( pt.x()-0.0000000001, pt.y()-0.000000001, pt.x(),pt.y())</font><br><font face="monospace">    request = QgsFeatureRequest().setFilterRect(rect)<br>    zones_intersecting_bounding_box = tzlayer.getFeatures(request)<br>    for tz_feature in zones_intersecting_bounding_box:<br>        if tz_feature.geometry().intersects(rect):<br>            name = tz_feature[1]<br>            break<br><br>print('Time {}'.format(time.time() - start_time))</font><br></div><div><font face="monospace"><br></font></div><div><font face="monospace"><br></font></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">I'd suggest using similar logic to what join attributes by location<br>
does internally. In pseudocode:<br>
<br>
search_point = ...<br>
search_bbox = QgsRectangle( search_point.x() - some tolerance,<br>
search_point.y() - some_tolerance, ... )<br>
request = QgsFeatureRequest().setFilterRect(search_bbox)<br>
zones_intersecting_bounding_box = tz_layer.getFeatures(request)<br>
for time_zone_feature in zones_intersecting_bounding_box:<br>
    if time_zone_feature.intersects(search_point):<br>
        # found a hit!<br>
        break<br>
<br>
This will take advantage of whatever spatial index you have on the<br>
time zone layer (e.g. the internal shapefile/gpkg/... index)<br>
<br>
There's quite a number of extra optimisations which could be done here<br>
if the speed isn't sufficient for mouse movements, e.g. you could<br>
fetch all the zone features in the canvas extent and "prepare" their<br>
geometries for ultra fast point in polygon tests, and then invalidate<br>
this cache and rebuild on each canvas extent change. But I'd only do<br>
that kind of thing if needed...<br>
<br>
Nyall<br>
<br>
<br>
<br>
<br>
<br>
><br>
> Calvin<br>
><br>
> On Fri, Feb 5, 2021 at 6:30 PM Nyall Dawson <<a href="mailto:nyall.dawson@gmail.com" target="_blank">nyall.dawson@gmail.com</a>> wrote:<br>
>><br>
>> On Sat, 6 Feb 2021 at 07:54, Andrea Giudiceandrea <<a href="mailto:andreaerdna@libero.it" target="_blank">andreaerdna@libero.it</a>> wrote:<br>
>> ><br>
>> > C Hamilton wrote<br>
>> > > Using the algorithm Vector->Geoprocessing Tools->Intersection: 4 minutes,<br>
>> > > 4<br>
>> > > seconds<br>
>> ><br>
>> > Hi Calvin,<br>
>> > maybe it could be better to use the "Join attributes by location" algorithm.<br>
>> > It only takes few seconds to preform the join.<br>
>><br>
>> Confirmed - for me it only takes ~2 seconds, and that's on an<br>
>> non-optimised debug build! There may be a few % more performance boost<br>
>> on the proper QGIS release builds.<br>
>><br>
>> "Join attributes by location" has a bunch of extra logic to optimise<br>
>> the method that the join is performed, which really pays off in this<br>
>> particular situation (matching points to polygons, where number of<br>
>> points >> number of polygons).<br>
>><br>
>> Nyall<br>
>><br>
>><br>
>><br>
>> ><br>
>> > Regards.<br>
>> ><br>
>> > Andrea<br>
>> ><br>
>> ><br>
>> ><br>
>> > --<br>
>> > Sent from: <a href="http://osgeo-org.1560.x6.nabble.com/QGIS-Developer-f4099106.html" rel="noreferrer" target="_blank">http://osgeo-org.1560.x6.nabble.com/QGIS-Developer-f4099106.html</a><br>
>> > _______________________________________________<br>
>> > QGIS-Developer mailing list<br>
>> > <a href="mailto:QGIS-Developer@lists.osgeo.org" target="_blank">QGIS-Developer@lists.osgeo.org</a><br>
>> > List info: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-developer" rel="noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a><br>
>> > Unsubscribe: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-developer" rel="noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a><br>
>> _______________________________________________<br>
>> QGIS-Developer mailing list<br>
>> <a href="mailto:QGIS-Developer@lists.osgeo.org" target="_blank">QGIS-Developer@lists.osgeo.org</a><br>
>> List info: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-developer" rel="noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a><br>
>> Unsubscribe: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-developer" rel="noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a><br>
</blockquote></div></div>