1

I can't make sense of the difference between:

  1. QGIS Select by location tool (Vector->Research tools->Select by Location") which gives the correct result :7 records match the extent:

    OOTB tool result of 7 intersected records

  2. Creating a custmised pyqgis tool that uses the extent of the canvas to create a QgsRectangle and uses the layer it gives 66 matching records...why? United Arab Emirates is nowhere close!

    Grab the extent from the input parameter (canvas extent) and force them to float, then:

    vlayer.selectByRect(QgsRectangle(xmin,ymin,xmax,ymax))
    features = [f for f in vlayer.getSelectedFeatures()]
    
    model_feedback.pushInfo("\nMatched selected features from extent intersection: {}\n".format(len(features)))
    

The countries layer is in WGS84, the project is in WGS84, the returned extent minx,miny,maxx,maxy is correct for the area I have around Denmark

result from comparing canvas rectangle extent with countries

Any ideas what's wrong or is there a bug at play?

1

I think you might have made a mistake with the extent coordinates. This is what you used:

xmin = 6.550986341429791
xmax = 52.089222964702195
ymin = 14.6224323720
ymax = 58.35776867917401

Using the extents you mentioned, this is what it looks like on the map:

Current map

(where the pale pink shows the extent, the yellow country being Denmark).

But if we swap xmax with ymin:

xmin = 6.550986341429791
xmax = 14.6224323720
ymin = 52.089222964702195
ymax = 58.35776867917401

This is what it looks like on the map:

![Updated map

(where the smaller pink box is the updated extent).

This should now match the countries you had when you used the Select by Location tool.


For the record, I use Lurpak butter all the time.

| improve this answer | |
  • 1
    Well spotted! thanks - that sorted it. The returned extent of the canvas was in a different order to the QgsRectangle specified order - or I accidentally swapped them. PS I also use Lurpak all the time. – GIS_py Nov 1 '19 at 17:28
  • @GIS_py - Most welcome, glad it helped :) – Joseph Nov 4 '19 at 9:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.