2

I have several raster files (raster_1, raster_2, [...], raster_n) and a point shapefile. The point shapefile (point.shp) was created using "generatepointspixelcentroidsinsidepolygons" so my points are located at the center of each raster pixel.

processing.runalg('qgis:generatepointspixelcentroidsinsidepolygons',".../raster_1.tif","input.shp", "point.shp")

My goal is to extract raster_n value to each point. This is possible using the point sampling tool plugin. However I want to use the python command on QGIS.

There must be a solution similar to:

processing.runald('qgis:pointsamplingtool',"raster_1","raster_2","raster_n","point.shp","output.shp"
3

In QGIS >= 3.4 you can use the "qgis:rastersampling" algorithm.

| improve this answer | |
  • Hi thanks a lot, however I am using QGIS 2.18.15. I found an efficient solution using R. See response. – Marcel Campion Jan 16 '19 at 9:10
  • I love the simplicity of qgis:rastersampling, and use it within the Py console. However, I'm having trouble running it in an external PyQGIS script though, since it cannot find the processing algorithm I'm trying to run: processing.run("qgis:rastersampling", params) I can run any ("native") processing algorithm easily by adding QgsNativeAlgorithms() via the following line: QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms()) But I'm not sure how to add a similar line to add any of the "qgis" processing algorithms. Any ideas? – grego Jul 30 at 18:33
0

I found a very efficient solution using R. Please find it attached below.

library(raster)
raster1 <- raster("/path/input1.tif")
raster2 <- raster("/path/input2.tif")
raster3 <- raster("/path/input3.tif")
rastern <- raster("/path/inputn.tif")
rasStack = stack(raster1, raster2, raster3, rastern)
pointCoordinates=read.csv("/path/point.csv")
coordinates(pointCoordinates)=~lat+lon
rasValue=extract(rasStack,pointCoordinates)
combinePointValue=cbind(pointCoordinates,rasValue)
write.table(combinePointValue,file="output.csv",append=FALSE, sep=",", row.names=FALSE,col.names=TRUE)

Hope that will help

| improve this answer | |

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.