I have a point Shapefile layer and a land cover raster layer.
For each point, I want to get the value (land cover) of the pixel it falls into and also of the 8 surrounding pixel.
Is there a tool in QGIS that can do this?
By using 'identify' method of QgsRasterDataProvider (PyQGIS), it is not very difficult to do that. I tested my approach with point and raster layers of next image. For simplicity, I only have considered the first feature of point layer; but the procedure can be easily extended for all features.
The code is:
mapcanvas = iface.mapCanvas()
layers = mapcanvas.layers()
#point layer
feat = layers[0].getFeatures().next()
#raster layer
rprovider = layers[1].dataProvider()
xsize = layers[1].rasterUnitsPerPixelX()
ysize = layers[1].rasterUnitsPerPixelY()
point = feat.geometry().asPoint()
#pixel and its 8 surrounding pixels from points
points = [ QgsPoint(point[0] - xsize, point[1] + ysize ),
QgsPoint(point[0], point[1] + ysize ),
QgsPoint(point[0] + xsize, point[1] + ysize ),
QgsPoint(point[0] - xsize, point[1]),
point,
QgsPoint(point[0] + xsize, point[1]),
QgsPoint(point[0] - xsize, point[1] - ysize ),
QgsPoint(point[0], point[1] - ysize ),
QgsPoint(point[0] + xsize, point[1] - ysize) ]
for point in points:
value = rprovider.identify(point, QgsRaster.IdentifyFormatValue).results()[1]
print value
After running the code, printed values for the point and its 8 neighbors can be observed at the Python Console of QGIS. Each value was corroborated with Value Tool plugin and they were all correct.