4

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">
    <ColorInterp>Red</ColorInterp>
  </VRTRasterBand>
  <VRTRasterBand dataType="Byte" band="2" subClass="VRTWarpedRasterBand">
    <ColorInterp>Green</ColorInterp>
  </VRTRasterBand>
  <VRTRasterBand dataType="Byte" band="3" subClass="VRTWarpedRasterBand">
    <ColorInterp>Blue</ColorInterp>
  </VRTRasterBand>
  <VRTRasterBand dataType="Byte" band="4" subClass="VRTWarpedRasterBand">
    <ColorInterp>Alpha</ColorInterp>
  </VRTRasterBand>
  <BlockXSize>512</BlockXSize>
  <BlockYSize>128</BlockYSize>
  <GDALWarpOptions>
    <SourceDataset relativeToVRT="0">WMTS:https://www.basemap.at/wmts/1.0.0/WMTSCapabilities.xml,layer=bmaporthofoto30cm</SourceDataset>
    <Transformer>
      <ApproxTransformer>
        <MaxError>0.125</MaxError>
        <BaseTransformer>
          <GenImgProjTransformer>
            <SrcGeoTransform>977649.895640023053,0.149291070871599985,0,6281289.98263385892,0,-0.149291070871599985</SrcGeoTransform>
            <SrcInvGeoTransform>-6548616.00182950962,6.69832424780491387,0,42074116.9981704801,0,-6.69832424780491387</SrcInvGeoTransform>
            <DstGeoTransform>1801360.99051694199,0.149291070871600373,0,6160668.61972136982,0,-0.14929107087160004</DstGeoTransform>
            <DstInvGeoTransform>-12066100.0018294789,6.6983242478048961,0,41266155.9981704652,0,-6.6983242478049112</DstInvGeoTransform>
          </GenImgProjTransformer>
        </BaseTransformer>
      </ApproxTransformer>
    </Transformer>
    <BandList>
      <BandMapping src="1" dst="1" />
      <BandMapping src="2" dst="2" />
      <BandMapping src="3" dst="3" />
    </BandList>
    <SrcAlphaBand>4</SrcAlphaBand>
    <DstAlphaBand>4</DstAlphaBand>
    <Cutline>MULTIPOLYGON (((5501234.2650394 1052508.13225313,5501234.2650394 791668.70254492,5834964.1589386 791668.70254492,5834964.1589386 1052508.13225313,5501234.2650394 1052508.13225313)))</Cutline>
  </GDALWarpOptions>
</VRTDataset>

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?)

2

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.