Skip to content

Instantly share code, notes, and snippets.

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
from lxml import etree
except ImportError:
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 ""`
or for local path with `python3 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)
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>")
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