3

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?

0

1 Answer 1

3

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.

enter image description here

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.

enter image description here

0

Your Answer

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

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