I would like to load a virtual raster as a layer into my map. I have tried changing the datasourcedata source attribute to 1 (relative), as described here: Loading virtual vector layer in QGIS
What do I need to change for the raster? I have tried both QgsRasterLayerQgsRasterLayer
and QgsVectorLayerQgsVectorLayer
.
from osgeo import gdal
import os
vrt_options = gdal.BuildVRTOptions(resampleAlg='nearestNeighbor', addAlpha=False)
gdal.BuildVRT(r'D:\QGIS\ni25.vrt', [r'D:\QGIS\dtk25.tif'], options=vrt_options)
vrt_path = r'D:\QGIS\ni25.vrt'
Layer = QgsRasterLayer(vrt_path, "ni25_vrt", "ogr")
if not Layer.isValid():
print("Layer failed to load!")
QgsProject.instance().addMapLayer(Layer)
import os
from osgeo import gdal
vrt_options = gdal.BuildVRTOptions(resampleAlg='nearestNeighbor', addAlpha=False)
gdal.BuildVRT(r'D:\QGIS\ni25.vrt', [r'D:\QGIS\dtk25.tif'], options=vrt_options)
vrt_path = r'D:\QGIS\ni25.vrt'
Layer = QgsRasterLayer(vrt_path, "ni25_vrt", "ogr")
if not Layer.isValid():
print("Layer failed to load!")
QgsProject.instance().addMapLayer(Layer)
The layer is added but nothing shows up inon the map and there is a warning saying that the laerlayer data source could not be found. When I drag the vrt into the layers window, it works.