geopython / OWSLib

OWSLib is a Python package for client programming with Open Geospatial Consortium (OGC) web service (hence OWS) interface standards, and their related content models.
https://owslib.readthedocs.io
BSD 3-Clause "New" or "Revised" License
393 stars 278 forks source link

Allow for misconfigured MetadataUrl element #925

Closed jnicho02 closed 6 months ago

jnicho02 commented 6 months ago

I was trying to access this https://environment.data.gov.uk/geoservices/datasets/90abef91-d465-11e4-b63a-f0def148f590/wms?service=wms&version=1.3.0&request=GetCapabilities WMS and got a KeyError: 'type'

Looking at the xml, it includes a MetadataUrl

...
        <Layer queryable=\"1\">
            <Name>Risk_of_Flooding_from_Surface_Water_Extent_0_1_percent_annual_chance</Name>
            <Title>Risk_of_Flooding_from_Surface_Water_Extent_0_1_percent_annual_chance</Title>
            <Abstract/>
...
            <MetadataURL>
                <OnlineResource xlink:href=\"https://environment.data.gov.uk/dataset/90abef91-d465-11e4-b63a-f0def148f590\"/>
            </MetadataURL>
...
        </Layer>

Now 'type' is required for a MetadataURL and I think they should have specified it as:

    <MetadataURL type="TC211">
        <Format>text/html</Format>
        <OnlineResource xlink:type="simple" xlink:href="https://environment.data.gov.uk/dataset/90abef91-d465-11e4-b63a-f0def148f590"/>
    </MetadataURL>

...or not at all.

My request is that owslib simply ignore a badly configured MetadataURL rather than crashing out with an error.

tomkralidis commented 6 months ago

I can't reproduce your exact issue, but I get a different issue:

from owslib.wms import WebMapService

url = 'https://environment.data.gov.uk/geoservices/datasets/90abef91-d465-11e4-b63a-f0def148f590/wms'

w = WebMapService(url)

Traceback:

Traceback (most recent call last):
  File "/Users/tomkralidis/Dev/OWSLib/OWSLib/dd.py", line 6, in <module>
    w = WebMapService(url)
        ^^^^^^^^^^^^^^^^^^
  File "/Users/tomkralidis/Dev/OWSLib/OWSLib/owslib/wms.py", line 50, in WebMapService
    return wms111.WebMapService_1_1_1(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/tomkralidis/Dev/OWSLib/OWSLib/owslib/map/wms111.py", line 75, in __init__
    self._capabilities = reader.read(self.url, timeout=self.timeout)
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/tomkralidis/Dev/OWSLib/OWSLib/owslib/map/common.py", line 69, in read
    return etree.fromstring(raw_text)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "src/lxml/etree.pyx", line 3264, in lxml.etree.fromstring
  File "src/lxml/parser.pxi", line 1989, in lxml.etree._parseMemoryDocument
  File "src/lxml/parser.pxi", line 1876, in lxml.etree._parseDoc
  File "src/lxml/parser.pxi", line 1164, in lxml.etree._BaseParser._parseDoc
  File "src/lxml/parser.pxi", line 633, in lxml.etree._ParserContext._handleParseResultDoc
  File "src/lxml/parser.pxi", line 743, in lxml.etree._handleParseResult
  File "src/lxml/parser.pxi", line 672, in lxml.etree._raiseParseError
  File "<string>", line 383
lxml.etree.XMLSyntaxError: Namespace prefix xlink for type on OnlineResource is not defined, line 383, column 186

Can you post a minimal code example that emits a KeyError so that I can reproduce locally?

jnicho02 commented 6 months ago

Ah, hi Tom. I contacted the WMS provider and they updated their capabilities statement. They now have

<MetadataURL type="TC211">
    <Format>text/html</Format>
    <OnlineResource xlink:type="simple" xlink:href="https://environment.data.gov.uk/dataset/90abef91-d465-11e4-b63a-f0def148f590"/>
</MetadataURL>

...and we are connecting to it specifying the version

wms = WebMapService(url=uri, version='1.3.0')

It might need a direct test of WMSCapabilitiesReader.readString. I can make one if you wish.

My immediate problem is fixed, but it is an example of real-life. I'm accessing a lot of WMS/WFS and have had to switch to direct http calls instead of OWSLib, which is a shame. From my experience there are lots of misconfigurations and unimportant bits can block me from using the lib.

tomkralidis commented 6 months ago

Great to hear. I will close this issue for now. If you can find an example online that can be reproduced, feel free to re-open the issue and we can tackle at that point.