<div dir="ltr"><div>It is highly likely that you have a much faster machine than mine so I can only look at comparative times between timezonefinder and the PyQGIS code included below. You will notice that I take a snapshot of the time before and after I iterate through the 10,000 points using both timezonefinder as a look up and the code Nyall proposed. That is how I am getting the times. What I am really trying to determine is the fastest way to look up a point using either timezonefinder or PyQGIS code. Perhaps what I have with PyQGIS code is fast enough, but I am not satisfied if it is not close to the speed of timezonefinder.</div><div><br></div><div>Ultimately, I am trying to determine whether I should continue to use timezonefinder as the method to look up time zones in my datetimetools plugin or should I have the gpkg time zone polygon layer as a part of the plugin and do lookups using it. Right now I am just doing single point lookups and it is probably fast enough, but I don't like that it is slower than timezonefinder. I am likely to expand datetimetools plugin to have a processing algorithm to do a lookup on a whole layer and in that case the PyQGIS method is better. </div><div><br></div><div>Those are my thoughts and if there is a faster method using the gpkg time zone polygon to do the lookup, let me know.  One thing I just tried was using the shapefile from  <a href="https://github.com/evansiroky/timezone-boundary-builder">https://github.com/evansiroky/timezone-boundary-builder</a>. It appears it is not spatially indexed and was taking too long. I resaved it as a shapefile and made sure it had a spatial index, but the gpkg conversion of the shapefile is even faster to access.</div><div><br></div><div>Calvin</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Feb 9, 2021 at 11:19 AM <<a href="mailto:mail@groenebij.nl">mail@groenebij.nl</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div lang="NL" style="overflow-wrap: break-word;"><div class="gmail-m_-8756740794432147380WordSection1"><p class="MsoNormal"><span>Hi Calvin,<u></u><u></u></span></p><p class="MsoNormal"><span><u></u> <u></u></span></p><p class="MsoNormal"><span lang="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.<u></u><u></u></span></p><p class="MsoNormal"><span lang="EN-US"><u></u> <u></u></span></p><p class="MsoNormal"><span lang="EN-US">Anyway, what do you mean with not getting fast result with single point lookups?<u></u><u></u></span></p><p class="MsoNormal"><span lang="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.<u></u><u></u></span></p><p class="MsoNormal"><span lang="EN-US"><u></u> <u></u></span></p><p class="MsoNormal"><span lang="EN-US"> <u></u><u></u></span></p><p class="MsoNormal"><span lang="EN-US">Jeroen<u></u><u></u></span></p><p class="MsoNormal"><span lang="EN-US"><u></u> <u></u></span></p><p class="MsoNormal"><span lang="EN-US"><u></u> <u></u></span></p><div style="border-right:none;border-bottom:none;border-left:none;border-top:1pt solid rgb(225,225,225);padding:3pt 0cm 0cm"><p class="MsoNormal"><b><span lang="EN-US">Van:</span></b><span lang="EN-US"> C Hamilton <<a href="mailto:adenaculture@gmail.com" target="_blank">adenaculture@gmail.com</a>> <br><b>Verzonden:</b> dinsdag 9 februari 2021 16:25<br><b>Aan:</b> Nyall Dawson <<a href="mailto:nyall.dawson@gmail.com" target="_blank">nyall.dawson@gmail.com</a>><br><b>CC:</b> Andrea Giudiceandrea <<a href="mailto:andreaerdna@libero.it" target="_blank">andreaerdna@libero.it</a>>; qgis-developer <<a href="mailto:qgis-developer@lists.osgeo.org" target="_blank">qgis-developer@lists.osgeo.org</a>>; Groene Bij <<a href="mailto:mail@groenebij.nl" target="_blank">mail@groenebij.nl</a>><br><b>Onderwerp:</b> Re: [QGIS-Developer] timzonefinder vs point in polygon<u></u><u></u></span></p></div><p class="MsoNormal"><span lang="EN-US"><u></u> <u></u></span></p><div><div><p class="MsoNormal">Nyall,<u></u><u></u></p></div><div><p class="MsoNormal"><u></u> <u></u></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.<u></u><u></u></p></div><div><p class="MsoNormal"><u></u> <u></u></p></div><div><p class="MsoNormal">timezonefinder: 44 seconds<u></u><u></u></p></div><div><p class="MsoNormal">Your method: 144 seconds<u></u><u></u></p></div><div><p class="MsoNormal"><u></u> <u></u></p></div><div><p class="MsoNormal">Here is my code for each:<u></u><u></u></p></div><div><p class="MsoNormal"><u></u> <u></u></p></div><div><p class="MsoNormal"><b>timezonefinder</b><u></u><u></u></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><u></u><u></u></p></div><div><p class="MsoNormal"><u></u> <u></u></p></div><div><p class="MsoNormal"><b>Your method</b>:<u></u><u></u></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><u></u><u></u></p></div><div><p class="MsoNormal"><u></u> <u></u></p></div><div><p class="MsoNormal"><u></u> <u></u></p></div><div><blockquote style="border-top:none;border-right:none;border-bottom:none;border-left:1pt solid rgb(204,204,204);padding:0cm 0cm 0cm 6pt;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><u></u><u></u></p></blockquote></div></div></div></div></blockquote></div></div>