How would one save a WMS view to a local raster from the python command line?

I've seen the answer to Download WMS to harddrive and that works for me but I'd like to know how to replicate that process exactly from the console.

ie How to progamatically set the layer, the extent to save, and then save that extent for that layer to a tif?

All of the searches I did threw up lots of results of people suggesting that downloading via wfs was the best solution. Or the linked solution via the gui. But none pointed me to the OWSLib solution.

Here is the working code I made on the back of the answer from @nmtoken I got.

from owslib.wms import WebMapService
from osgeo import gdal, osr
import processing

folder = '~/output'
wms = WebMapService(url, version='1.1.1')
wms_layers= list(wms.contents)

for layerno in range(0,len(wms_layers)):
    #Note that available epsgs can be determined with:
    print wms[wms_layers[0]].crsOptions
    #Note output options can be determined with:
    print wms.getOperationByName('GetMap').formatOptions
    img = wms.getmap( layers=[wms_layers[layerno]], styles=['default'], srs=srs_string, bbox=( lonmin,latmin , lonmax,latmax), size=(Xpix, Ypix), format='image/tiff' )
    out = open(raw_tiff, 'wb')
    out = None

    src_ds = gdal.Open(raw_tiff)
    format = "GTiff"
    driver = gdal.GetDriverByName(format)
    dst_ds = driver.CreateCopy(georeferenced_tiff, src_ds, 0)
    gt = [lonmin, (lonmax-lonmin)/Xpix, 0, latmax, 0, -(latmax-latmin)/Ypix]
    srs = osr.SpatialReference()
    dest_wkt = srs.ExportToWkt()
    dst_ds = None
    src_ds = None
  • Take a look at OWSLib, also note that whether you can save to TIFF directly will depend on whether the WMS service supports that output format – nmtoken Apr 10 '18 at 6:07
  • 1
    Thanks @nmtoken That did it. I hadn't heard of OWSLib but it completely solves the problem. Lucky you pointed me to it as a solution to my problem before this question was closed! Of course I'd be happy to rewrite the question to make it "on topic" but I've no idea specifically how it fails to be a "clear problem statement" as described in the first para or how the second para isn't "the desired behaviour" or what "short code" to put in when I had no clue where to start given that I hadn't previously heard of OWSLib. – Mr Purple Apr 10 '18 at 22:39
  • Glad, it has worked for you. If you've got something working to demonstrate using owslib and PyQGIS, it would be great if you could post as an answer, that way knowledge is spread :) – nmtoken Apr 11 '18 at 3:14

You can use OWSLib to work with WMS and other OGC services.

OWSLib is a Python package for client programming with Open Geospatial Consortium (OGC) web service (hence OWS) interface standards, and their related content models.

ref: OsGeo projects pages

There are a number of posts on this site related to working with OWSlib so hopefully enough to get you started.

and then save that extent for that layer to a tif?

You will only be able to do this directly if the WMS service itself supports TIFF as an output format (as advertised in the GetCapabilities response); though many WMS do, so probably won't be an issue.

  • 1
    I've dropped my working code into my question but you're welcome to emend it to your answer since your answer gave me the chance to solve my problem! – Mr Purple Apr 11 '18 at 9:46

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.