9

I have a polygon layer and a point layer (with coordinates for each point). The polygon layer got created using the MMQGIS plugin (Hexagonal Polygons). I want to select all the points inside each polygon.

I worked with ArcGIS before but I want to change to open source. Furthermore, I want to approach that through a Python script since I want to analyse the extracted data. I have QGIS 2.8 installed. I am a bit overwhelmed by all the GIS libraries there are for Python.

Can I approach that sort of selection with a GIS library that is already installed with QGIS? If so, could anyone pin point me into the right direction?

BritishSteel
6,7174 gold badges41 silver badges66 bronze badges
asked Apr 2, 2015 at 12:19
1

2 Answers 2

12

Assuming you want to run your script within QGIS (from a script file or Python console), you can use the following:

import processing
processing.runalg("qgis:selectbylocation", INPUT, INTERSECT, METHOD, OUTPUT)

Here is the help description provided by the Python console which defines each parameter:

processing.alghelp("qgis:selectbylocation")
ALGORITHM: Select by location
 INPUT <ParameterVector>
 INTERSECT <ParameterVector>
 METHOD <ParameterSelection>
 OUTPUT <OutputVector>
METHOD(Modify current selection by)
 0 - creating new selection
 1 - adding to current selection
 2 - removing from current selection

To create a script entirely from scratch which doesn't call on QGIS functions, the following links might help in developing similar functionality:


EDIT:

There are a couple of posts which might be of some use with writing scripts outside QGIS for Mac:

answered Apr 2, 2015 at 12:50
5
  • Hello Joseph. Thanks for your reply. I want to use QGis functions! But I think I do not want to make that in the QGis console but call a script from the console of my computer (OS X 10.10). Which library are you using there? Or which library is QGis providing and how can I access that API? Is there anything to read on that? Commented Apr 2, 2015 at 13:23
  • No problem Stophface! I've edited my post to include links which describe how to use scripts outside QGIS for Mac. I use Windows :) In terms of scripting, I don't think there's much difference, the biggest being setting the PATHS I think (I'm also a beginner!). Commented Apr 2, 2015 at 13:38
  • Yes, that path setting is something which wont work over here at all. But once thats fixed, it will save a lot of work later on :) Is there a documentation on the library QGis is using? That way I do not have to come back here but just look things there up. Is it PyQGis? Commented Apr 2, 2015 at 13:40
  • Yes, it uses PyQGIS :) Commented Apr 2, 2015 at 13:49
  • Most welcome buddy! Commented Apr 2, 2015 at 14:13
5

Joseph's answer is correct, although there needs to be another parameter included: "PREDICATE".

ie:

processing.run("qgis:selectbylocation", {
 "INPUT":lyr_input,\
 "PREDICATE":0,\
 "INTERSECT":lyr_intersect,\
 "METHOD":0,\
 "OUTPUT":path}
)

From processing.algorithmHelp("qgis:selecybylocation"):

PREDICATE: Where the features (geometric predicate)
 Parameter type: QgsProcessingParameterEnum
 Available values:
 - 0: intersect
 - 1: contain
 - 2: disjoint
 - 3: equal
 - 4: touch
 - 5: overlap
 - 6: are within
 - 7: cross
 Accepted data types:
 - int
 - str: as string representation of int, e.g. '1'
 - QgsProperty
answered Jan 19, 2022 at 18:13

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.