Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Identify if issue with same name in XML WMS capabilities
import argparse
from collections import Counter
import urllib.request
try:
from lxml import etree
except ImportError:
try:
print("Failed to import etree from lxml. Fall to default Python lib")
from xml.etree import ElementTree as etree
except ImportError:
print("Failed to import etree from native Python")
parser = argparse.ArgumentParser(description = '''%(prog)s: Check if not issues in WMS capabilities:
Can be tested for remote with something like `python3 debug_wms_capabilities_xml.py "https://ogc.geo-ide.developpement-durable.gouv.fr/wxs?map=/opt/data/carto/geoide-catalogue/1.4/org_5443264/ccf16ee8-5b97-4cf5-9257-c88102c106e2.internet.map&service=WMS&request=GetCapabilities"`
or for local path with `python3 debug_wms_capabilities_xml.py capabilities.xml`''')
parser.add_argument("path", type=str,
help="Provide WMS capabilities path (http or local)")
args = parser.parse_args()
path = args.path
if path.startswith('http'):
with urllib.request.urlopen(path) as inputcontent:
xml_content = etree.parse(inputcontent)
else:
with open(path, encoding="utf-8") as inputcontent:
xml_content = etree.parse(inputcontent)
nested_names = [layer.xpath('./*[local-name()="Name"]') for layer in xml_content.xpath('.//*[local-name()="Layer"]')]
flat_names = [item for names in nested_names for item in names if len(names) > 0]
all_names_within_layers = [name.text for name in flat_names if not name.text.startswith('inspire_common:')]
diagnostics = [k for k, v in Counter(all_names_within_layers).items() if v > 1]
if len(diagnostics) > 0:
for diagnostic in diagnostics:
print(f"You have a duplicated <Layer> with same <Name>{diagnostic}</Name>")
else:
print(f"No issues detected for duplicated <Name> in any <Layer> block")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment