2

For Selecting features within polygon from another layer using QGIS? the solution was to:

use the "Vector->Research tools->Select by Location"

However, I would like to know how to do the same thing with either an R or Python script.

3

R's Spatial classes have select/extract idioms driven by the "[" operator - for extraction by index, number, name, logical map, or spatial overlay.

I think this is close to what you mean.

library(maptools)
data(wrld_simpl)
## generate some random points
n <- 5e4
d <- data.frame(lon = runif(n, -180, 180), lat = runif(n, -90, 90), x = seq(n))
## points as Spatial DataFrame
pts <- d
coordinates(pts) <- ~lon+lat
proj4string(pts) <- CRS(proj4string(wrld_simpl))

## plot the points
plot(pts, pch = ".")

## use one polygon to mark overlaying points 
sel <- pts[subset(wrld_simpl, NAME == "Australia"), ]
points(sel, col = "yellow", pch = 16, cex = 0.3)

See ?over for more general cases.

2

For a Python script, you could call tools such as Select by location from the Python console:

import processing

processing.runalg('qgis:selectbylocation', input_layer, intersect_layer, u'intersects', 0, output_layer)

To see what parameters are required, you can call processing.alghelp("qgis:selectbylocation"):

>>> processing.alghelp("qgis:selectbylocation")

ALGORITHM: Select by location
INPUT <ParameterVector>
INTERSECT <ParameterVector>
PREDICATE <ParameterGeometryPredicate>
METHOD <ParameterSelection>
OUTPUT <OutputVector>


METHOD(Modify current selection by)
    0 - creating new selection
    1 - adding to current selection
    2 - removing from current selection

There are plenty of other selection tools which you can see using:

processing.alglist("select")

This lists all available tools with "select" in the name. You can then use them by checking which parameters are needed.

Source: QGIS 2.8 Documentation: Calling algorithms from the Python console.

Hope this helps!

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