Giter Site home page Giter Site logo

courses's Issues

Use spatial index to check fewer features in QGIS Actions Buffer example

You can dramatically improve the performance of the https://courses.spatialthoughts.com/qgis-actions.html#select-features-in-a-buffer-zone example by using the option to pass a rectangle to QgsVectorLayer.getFeatures():

line_layer = QgsProject.instance().mapLayer('[% @layer_id %]')
polygon_layer_name = 'buildings'
distance = 20
fid = [% $id %]
line_feature = line_layer.getFeature(fid)
line_buffer = line_feature.geometry().buffer(distance, 5)
polygon_layer = QgsProject.instance().mapLayersByName(polygon_layer_name)[0]
nearby_features = [
    p.id()
    for p in polygon_layer.getFeatures(line_buffer.boundingBox()) 
    if p.geometry().intersects(line_buffer)
]
polygon_layer.selectByIds(nearby_features)

This allows the request to use a spatial index (if exist) to quickly limit the number of features to just the candidates that intersect the rectangle. So the if ... intersects only has to check those remaining buildings. Not much of a noticeable difference in the example data but with bigger datasets it will be a huge speed improvement.

Shout if you'd like it, then I'd make a pull request. Would you like to include both versions for the users to experience and learn?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.