I am working with OWSLib in Python and I am trying to read a WMS of the Corine land cover in Ireland for the year 2000. Below you can see the request of the WMS.
from owslib.wms import WebMapService
ireland_wms = WebMapService('https://gis-test.epa.ie/geoserver/ows?SERVICE=WMS&')
Then I print some general info about the layer of interest.
def layer_report(service, layer_name):
print(f"WMS general info:\n")
print(f"WMS version: {service.identification.version}")
print(f"WMS title: {service.identification.title}")
print(f"WMS abstract: {service.identification.abstract}")
print(f"Provider name: {service.provider.name}")
print(f"Provider address: {service.provider.contact.address}")
print(f"\nLayer info:\n")
print('- Title: ', service.contents[layer_name].title)
print('- BoundingBox: ', service.contents[layer_name].boundingBox)
print('- BoundingBoxWGS84: ', service.contents[layer_name].boundingBoxWGS84)
print('- CrsOptions: ', service[layer_name].crsOptions)
print('- Format Options: ', service.getOperationByName('GetMap').formatOptions)
print('- Layer styles: ', service[layer_name].styles)
print('- Operations ', [op.name for op in service.operations])
layer_report(ireland_wms, 'EPA:LAND_CLC00Rev')
and I get the following output
Now I have the legend that corresponds to type of Land cover, If I click on link shown in the above picture I get the following legend.
Now I would like to know if there is any way to gain more information about the legend. In particular, I would like to get, if it is possible, the color codes of the legend in order to use them for plotting. Is there any way to do this using OWSLib or any other library which is compatible with Python?
Best Answer
I found a rather nasty solution 😅
... with the official EEA DiscoMap WMS there's the possibility of getting the legend as a json-dict!
While the colors are still not directly available in the dictionary, there are base64 strings of the legend color-images... from which the actual rgba values can be extracted as follows (all imports are native python3!):
(I really don't get why they don't just provide a rgba tuple for the colors directly... but this is at least a working (python) method to extract the colors from the legend images)
... and here's the proof of concept that it actually works:
update
Since it was mentioned in the comments that this does not work with the actual WMS service... here's how you could "decode" the given legend image to get the color-codes:
Since for your WMS only a png of the full legend is available, the only possibility I see is to chop the legend into the individual entries and then do the same procedure as above, e.g.:
which then gives you something like: