QGIS WMS to TIFF – Save WMS Layer to TIFF Raster in QGIS

qgisrrasterwms

In QGIS, I have a WMS layer. I am saving it to my hard drive by right click > "Save As…". Just like in HDunn's answer here. This is about my understanding of the topic, already this code is strange and not really understandable for me.

However, "Save As…" is resampling the original data by averageing it, which is not OK for me since the original raster is a categorical one. I do not know the resolution of the original WMS file (it is not included in it's description).

How could I save it with either the original resolution (and CRS) and/or with other resampling method than averaging? Natural Neighbour would be fair enough although Most Frequent Value would be most accurate. Is that possible without Python? I also understand of R, if that helps.

The WMS URL is: https://maps.rissac.hu:6443/arcgis/services/dosoremi_web_mercator/MapServer/WMSServer?LAYERS=4&PROJECTION=EPSG%3A3857&TRANSPARENT=TRUE&VERSION=1.1.1

Best Answer

For a reference of the GeoServer WMS services:

https://docs.geoserver.org/stable/en/user/services/wms/reference.html

Get server capabilities:

https://maps.rissac.hu:6443/arcgis/services/dosoremi_web_mercator/MapServer/WMSServer?service=WMS&version=1.1.1&request=GetCapabilities 

The server returns an XML file, open it with a web browser.
Assuming you want to download layer 4, these are the parts that interest you:

...
<GetMap>
<Format>image/bmp</Format>
<Format>image/jpeg</Format>
<Format>image/tiff</Format>
<Format>image/png</Format>
<Format>image/png8</Format>
<Format>image/png24</Format>
<Format>image/png32</Format>
<Format>image/gif</Format>
<Format>image/svg+xml</Format>
<DCPType>
    <HTTP>
        <Get>
            <OnlineResource xlink:type="simple" xlink:href="https://maps.rissac.hu:6443/arcgis/services/dosoremi_web_mercator/MapServer/WmsServer?"/>
       </Get>
    </HTTP>
</DCPType>
</GetMap>
...
<Layer queryable="1">
<Name>4</Name>
<Title>Agyagtartalom (%, 0-30 cm)</Title>
<Abstract></Abstract>
<SRS>EPSG:4326</SRS>
<SRS>EPSG:3857</SRS>
<!-- alias 3857 -->
<SRS>EPSG:102100</SRS>
<LatLonBoundingBox minx="16.015936" miny="45.676268" maxx="22.941947" maxy="48.610464"/>
<BoundingBox SRS="EPSG:4326" minx="16.015936" miny="45.676268" maxx="22.941947" maxy="48.610464"/>
<BoundingBox SRS="EPSG:3857" minx="1782885.857099" miny="5728621.889700" maxx="2553885.857099" maxy="6209021.889700"/>
<BoundingBox SRS="EPSG:102100" minx="1782885.857099" miny="5728621.889700" maxx="2553885.857099" maxy="6209021.889700"/>
...  

There you can find the available image formats, the reference systems and the regions (expressed in those reference systems), which you can request.

Assuming you want to obtain a TIFF file in EPSG:4326 system, of the region between latitudes 20.3 and 20.4 degrees, and longitudes between 48.0 and 48.1 degrees, with a resolution of 0.0001 degree (a grid of 1000 rows and 1000 columns).
The region is small enough to be able to download quickly and the resolution is good enough, probably the maximum that the server can provide (observe the pixels in QGIS, in the WMS layer, and its size in WGS84 coordinates).
Then, when looking at wider regions, keep in mind that you should not exceed the limits set by the server, or return an error.


The GetMap way

You can request that map through a web browser, typing the URL of the GetMap query:

https://maps.rissac.hu:6443/arcgis/services/dosoremi_web_mercator/MapServer/WMSServer?request=GetMap&service=WMS&version=1.1.1&layers=4&styles=&srs=EPSG%3A4326&bbox=20.3,48.0,20.4,48.1&width=1000&height=1000&format=image%2Ftiff  

The server will return a TIFF file with the requested map, but without referencing.
When you add it as a raster layer to QGIS, it will ask you to specify the reference system and project the image onto the canvas.
The result is that QGIS will place the image in latitude 0 and longitude 0, and the image will measure 1000 degrees wide by 1000 degrees long.
You must georeference it.


The GRASS GIS way

Assuming you already have your Location and Mapset determined to work on EPSG:4326, the first step is to establish the region:

g.region n=48.1 s=48 e=20.4 w=20.3 rows=1000 cols=1000  

I'm going to copy the commands already formed, but if you run just g.region a GUI is open that can help you in the process of setting the parameters.

You can verify that the region was created and loaded as the default region with the command g.region -p.

Then, download the image from the server:

r.in.wms --overwrite url=https://maps.rissac.hu:6443/arcgis/services/dosoremi_web_mercator/MapServer/WMSServer? output=anytestname layers=4 format=tiff  

GRASS GIS will connect to the server, download the map and load it to the canvas.
It is a good idea to check the information on the map:

r.info map=anytestname@themapsetname  

According to the documentation of g.out.gdal, the data type for the output of raster values ​​between 0 and 65535 can be UInt16, therefore the output command can be:

r.out.gdal --overwrite input=anytestname@themapsetname output=C:\somefile.tiff format=GTiff type=UInt16  

And that's it, when you load the image as a raster layer in QGIS, you will not notice any difference with the WMS layer.

I recommend that you do not intend to download very large regions with this resolution. Note that the WMS layer requests different resolutions depending on the zoom level, and when the zoom level is high the regions are small.