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.
Answer
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.
No comments:
Post a Comment