After I managed to store SVG symbols in Geopackage BLOBs (Read (and write) SVG from Geopackage with QGIS) another idea was growing in my mind: Maybe we can store simple GDAL VRT sources in GPKG BLOBs or *.qgz files, to bind these datasources to a QGIS project?

Here's a minimal VRT which shows the city of Vienna clipped from a public WMTS (https://basemap.at):

<VRTDataset rasterXSize="293885" rasterYSize="227105" subClass="VRTWarpedDataset">
  <SRS dataAxisToSRSAxisMapping="1,2">EPSG:3857</SRS>
  <GeoTransform>  1.8013609905169420e+06,  1.4929107087160037e-01,  0.0000000000000000e+00,  6.1606686197213698e+06,  0.0000000000000000e+00, -1.4929107087160004e-01</GeoTransform>
  <VRTRasterBand dataType="Byte" band="1" subClass="VRTWarpedRasterBand">
  <VRTRasterBand dataType="Byte" band="2" subClass="VRTWarpedRasterBand">
  <VRTRasterBand dataType="Byte" band="3" subClass="VRTWarpedRasterBand">
  <VRTRasterBand dataType="Byte" band="4" subClass="VRTWarpedRasterBand">
    <SourceDataset relativeToVRT="0">WMTS:https://www.basemap.at/wmts/1.0.0/WMTSCapabilities.xml,layer=bmaporthofoto30cm</SourceDataset>
      <BandMapping src="1" dst="1" />
      <BandMapping src="2" dst="2" />
      <BandMapping src="3" dst="3" />
    <Cutline>MULTIPOLYGON (((5501234.2650394 1052508.13225313,5501234.2650394 791668.70254492,5834964.1589386 791668.70254492,5834964.1589386 1052508.13225313,5501234.2650394 1052508.13225313)))</Cutline>

enter image description here

We could store the VRT file in a QGIS *.QGZ project file and retrieve it via /VSIZIP/, but unfortunately it will be deleted after a project save. (Maybe the core devs could define a QGZ folder where we can store some custom data which won't get lost?)

Geopackage BLOBs are no option either, because I couldn't find a way to open a data stream (i.e. base64 encoded) as VRT data source. It's possible to retrieve data from BLOBs using Python and open it in QGIS from a temp file, but I would prefer a more direct solution.

GDAL has so many Virtual File System handlers, maybe we could use VSIMEM or VSISTDIN to read the data from a Geopackage BLOB.

Does anyone have a brilliant idea to solve this tricky task without using too much Python code?

(@Even Rouault ... something for you?)


As so often the answer is so near!

We can easily enter/copy the content of the VRT file instead of selecting it (QGIS 3.16.3. Windows 10). All double quotes need to be replaced by single quotes.

QGIS is and remains a mystery to me with all its hidden features!

enter image description here

If we use the wonderful changeDataSource Plugin, we don't even need to replace all double quotes and can leave the formatting as it is: enter image description here

And, best of all, it's not limited to WMTS URIs. We can use GDAL XML description files as well (i.e. TMS sources):

gdalwarp -r Lanczos -s_srs EPSG:3857 -t_srs EPSG:31256 -te -10867 330137 17813 350209 -of VRT "<GDAL_WMS><Service name=\"TMS\"><ServerUrl>https://tile.openstreetmap.org/${z}/${x}/${y}.png</ServerUrl></Service><DataWindow><UpperLeftX>-20037508.34</UpperLeftX><UpperLeftY>20037508.34</UpperLeftY><LowerRightX>20037508.34</LowerRightX><LowerRightY>-20037508.34</LowerRightY><TileLevel>18</TileLevel><TileCountX>1</TileCountX><TileCountY>1</TileCountY><YOrigin>top</YOrigin></DataWindow><Projection>EPSG:3857</Projection><BlockSizeX>256</BlockSizeX><BlockSizeY>256</BlockSizeY><BandsCount>3</BandsCount><UserAgent>QGIS 3.16.3</UserAgent><Cache /></GDAL_WMS>" vienna_osm.vrt

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.