<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head><meta http-equiv=Content-Type content="text/html; charset=utf-8"><meta name=Generator content="Microsoft Word 15 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
span.E-mailStijl19
        {mso-style-type:personal-compose;
        font-family:"Calibri",sans-serif;
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-family:"Calibri",sans-serif;
        mso-fareast-language:EN-US;}
@page WordSection1
        {size:612.0pt 792.0pt;
        margin:70.85pt 70.85pt 70.85pt 70.85pt;}
div.WordSection1
        {page:WordSection1;}
--></style></head><body lang=NL link=blue vlink=purple style='word-wrap:break-word'><div class=WordSection1><p class=MsoNormal><span style='mso-fareast-language:EN-US'>Hi Calvin,<o:p></o:p></span></p><p class=MsoNormal><span style='mso-fareast-language:EN-US'><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-US style='mso-fareast-language:EN-US'>Thanks for sending me this from the Developers list. I am not a part of the Developer list, so I did not see the Join attribute by location tip from Andrea.<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US style='mso-fareast-language:EN-US'><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-US style='mso-fareast-language:EN-US'>Anyway, what do you mean with not getting fast result with single point lookups?<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US style='mso-fareast-language:EN-US'>When I select a single point from the 10000 point layer and run the same algorithm (check the selected features only option in the dialogue box), the result takes 0.05 seconds (12 seconds for all 10000). At least that is what is says in the log tab of the algorithm. And it sure looks quick.<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US style='mso-fareast-language:EN-US'><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-US style='mso-fareast-language:EN-US'> <o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US style='mso-fareast-language:EN-US'>Jeroen<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US style='mso-fareast-language:EN-US'><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-US style='mso-fareast-language:EN-US'><o:p> </o:p></span></p><div style='border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0cm 0cm 0cm'><p class=MsoNormal><b><span lang=EN-US>Van:</span></b><span lang=EN-US> C Hamilton <adenaculture@gmail.com> <br><b>Verzonden:</b> dinsdag 9 februari 2021 16:25<br><b>Aan:</b> Nyall Dawson <nyall.dawson@gmail.com><br><b>CC:</b> Andrea Giudiceandrea <andreaerdna@libero.it>; qgis-developer <qgis-developer@lists.osgeo.org>; Groene Bij <mail@groenebij.nl><br><b>Onderwerp:</b> Re: [QGIS-Developer] timzonefinder vs point in polygon<o:p></o:p></span></p></div><p class=MsoNormal><span lang=EN-US><o:p> </o:p></span></p><div><div><p class=MsoNormal>Nyall,<o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>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.<o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>timezonefinder: 44 seconds<o:p></o:p></p></div><div><p class=MsoNormal>Your method: 144 seconds<o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>Here is my code for each:<o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal><b>timezonefinder</b><o:p></o:p></p></div><div><p class=MsoNormal><span style='font-family:"Courier New"'>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))</span><o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal><b>Your method</b>:<o:p></o:p></p></div><div><p class=MsoNormal><span style='font-family:"Courier New"'>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())</span><br><span style='font-family:"Courier New"'>    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))</span><o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><blockquote style='border:none;border-left:solid #CCCCCC 1.0pt;padding:0cm 0cm 0cm 6.0pt;margin-left:4.8pt;margin-right:0cm'><p class=MsoNormal>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" 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" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a><br>>> > Unsubscribe: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-developer" 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" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a><br>>> Unsubscribe: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-developer" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a><o:p></o:p></p></blockquote></div></div></div></body></html>