TransWikia.com

Open raster referenced in attribute column of vector layer

Geographic Information Systems Asked on August 23, 2021

I have a polygon layer. One of the columns holds an absolute path to a (georeferenced) raster. Is there a way to (programmatically) open this raster in QGIS and add it to the map canvas?

One Answer

This will add the first raster (in my example the only one) from the vector layers attribute table:

import os 
vector_layer = QgsProject.instance().mapLayersByName('vl')[0] #Change
field_with_path = 'abspath' #Change

rasterpath = [f[field_with_path] for f in vector_layer.getFeatures()][0] #[0] for the first feature
rlayer = iface.addRasterLayer(rasterpath,os.path.basename(rasterpath)[:-4],"gdal")

enter image description here

Correct answer by BERA on August 23, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP