diff --git a/dataCatalog.py b/dataCatalog.py index 1a9fd6e..0145b1c 100644 --- a/dataCatalog.py +++ b/dataCatalog.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -from qgis.PyQt.QtCore import (Qt, QSettings, QTranslator, QCoreApplication, QUrl, +from qgis.PyQt.QtCore import (Qt, QCoreApplication, QUrl, QSortFilterProxyModel, QRegExp, QStringListModel) from qgis.PyQt.QtGui import QCursor, QStandardItemModel, QStandardItem from qgis.PyQt.QtWidgets import (QApplication, QDialog, QSizePolicy, QCompleter, QInputDialog, @@ -21,15 +21,6 @@ def __init__(self, iface): QDialog.__init__(self, None) self.setWindowFlags( self.windowFlags() & ~Qt.WindowContextHelpButtonHint ) self.iface = iface - - # initialize locale - locale = QSettings().value("locale/userLocale", "nl") - locale = locale[0:2] - localePath = os.path.join(os.path.dirname(__file__), 'i18n', '{}.qm'.format(locale)) - if os.path.exists(localePath): - self.translator = QTranslator() - self.translator.load(localePath) - self.initGui() def initGui(self): @@ -58,7 +49,6 @@ def initGui(self): #datamodel self.model = QStandardItemModel(self) self.proxyModel = QSortFilterProxyModel(self) - self.proxyModel.setSourceModel(self.model) self.ui.resultView.setModel(self.proxyModel ) #completer @@ -101,6 +91,8 @@ def setModel(self, records): wcsName = QStandardItem( rec['wcs'][0] ) #10 wmtsName = QStandardItem( rec['wmts'][0] ) #11 self.model.appendRow([title,wms,downloadLinks,id,abstract,wfs,wcs,wmts,wmsName,wfsName,wcsName,wmtsName]) + + self.proxyModel.setSourceModel(self.model) #overwrite def show(self): @@ -165,7 +157,6 @@ def resultViewClicked(self): def onZoekClicked(self): """Called when user clicked zoekBtn""" self.zoek = self.ui.zoekTxt.currentText() - self.model.clear() self.search() def modelFilterCbxIndexChanged(self): @@ -203,18 +194,18 @@ def search(self): else: dataType='' inspiretheme= self.ui.INSPIREthemaCbx.currentText() inspireServiceType= self.ui.INSPIREserviceCbx.currentText() - searchResult = metadata.MDdata( - self.md.searchAll( self.zoek, orgName, dataType, inspiretheme, inspireServiceType) ) + searchResult = self.md.searchAll( self.zoek, orgName, dataType, inspiretheme, inspireServiceType) else: - searchResult = metadata.MDdata( self.md.searchAll( self.zoek ) ) + searchResult = self.md.searchAll( self.zoek ) QApplication.restoreOverrideCursor() - + self.ui.countLbl.setText( "Aantal gevonden: %s" % searchResult.count ) self.ui.descriptionText.setText('') self.setModel(searchResult.records) if searchResult.count == 0: self.bar.pushMessage( QCoreApplication.translate("datacatalog", "Waarschuwing "), - QCoreApplication.translate("datacatalog", "Er zijn geen resultaten gevonden voor deze zoekopdracht"), duration=10) + QCoreApplication.translate("datacatalog", + "Er zijn geen resultaten gevonden voor deze zoekopdracht"), duration=10) def addWMS(self): """Add WMS from current record to map""" diff --git a/metadataParser.py b/metadataParser.py index 0affb0d..5252e6a 100644 --- a/metadataParser.py +++ b/metadataParser.py @@ -1,15 +1,13 @@ import json, sys, json -from urllib.parse import urlencode, unquote +from urllib.parse import urlencode, unquote, urlparse, parse_qsl import xml.etree.ElementTree as ET from io import StringIO -from .webUtil import getUrlData +from .webUtil import getUrlData, metaError -CSW_URL = "http://www.nationaalgeoregister.nl/geonetwork/srv/dut/inspire" +CSW_URL = "https://www.nationaalgeoregister.nl/geonetwork/srv/dut/inspire" class MDdata(object): - count = 0 - records = [] def __init__(self, metadataXML): """Parse a CSW-metadataXML @@ -17,6 +15,8 @@ def __init__(self, metadataXML): """ if not metadataXML: return self.count = int( metadataXML.attrib["numberOfRecordsMatched"] ) + self.records = [] + self.count = 0 mds = metadataXML.findall("{http://www.opengis.net/cat/csw/2.0.2}Record") for md in mds: @@ -47,23 +47,28 @@ def __init__(self, metadataXML): record['wms'] = self._findWXS( md, "OGC:WMS" ) record['wfs'] = self._findWXS( md, "OGC:WFS" ) record['wcs'] = self._findWXS( md, "OGC:WCS" ) - record['wmts'] = self._findWXS( md , "OGC:WMTS") + record['wmts'] = self._findWXS( md, "OGC:WMTS") record['download'] = self._findDownloads( md ) self.records.append(record) - def _findWXS(self , node, protocol= None ): - links = [n for n in node.findall("{http://purl.org/dc/elements/1.1/}URI") + def _findWXS(self, node, protocol= None ): + links = [n for n in node.findall("{http://purl.org/dc/elements/1.1/}URI") if "protocol" in n.attrib and n.attrib["protocol"] == protocol] + links += [ n for n in node.findall("{http://purl.org/dc/elements/1.1/}URI") + if n.text + and 'SERVICE' in dict( parse_qsl( urlparse( n.text.upper() ).query )) + and dict(parse_qsl(urlparse( n.text.upper() ).query))['SERVICE'] in protocol ] + if len(links) > 0: name = links[0].attrib["name"] if "name" in links[0].attrib else links[0].text return (name, links[0].text) return ("","") - def _findDownloads(self , node): + def _findDownloads(self, node): links = [n for n in node.findall("{http://purl.org/dc/elements/1.1/}URI") if "protocol" in n.attrib and "DOWNLOAD" in n.attrib["protocol"].upper() ] atoms = [n.text for n in node.findall("{http://purl.org/dc/elements/1.1/}URI") - if "protocol" in n.attrib and "atom" in n.attrib["protocol"].lower() ] + if "protocol" in n.attrib and "ATOM" in n.attrib["protocol"].upper() ] if len(links) == 0 and len(atoms) == 0: return [] @@ -76,11 +81,15 @@ def _findDownloads(self , node): try: resp= getUrlData(atom) - except: + results = [] + root = ET.fromstring(resp) + except ET.ParseError: + print( "WARNING: Geen correcte xml: {} ".format(atom) ) + return [] + except metaError as me: + print( "WARNING: http-fout {} -> geeft {}".format(atom, me.message) ) return [] - results = [] - root = ET.fromstring(resp) #parse(response).getroot() # entries = root.findall( ".//{http://www.w3.org/2005/Atom}entry" ) for entry in entries: dl = entry.find( "{http://www.w3.org/2005/Atom}link") @@ -182,7 +191,7 @@ def list_organisations(self): organisations.sort() return organisations - def search(self, q="", start=1, step=100, orgName='', dataType='', inspiretheme='', inspireServiceType=''): + def _search(self, q="", start=1, step=100, orgName='', dataType='', inspiretheme='', inspireServiceType=''): """Search the csw with the following parameters: :param q: free text to seach for @@ -209,23 +218,22 @@ def searchAll(self, q="", orgName='', dataType='', inspiretheme='', inspireServi :param inspireServiceType: filter on inspire serviceType :return: a composite XMLdocument with the results """ - start= 1 step= 100 - result = self.search(q, start, step, orgName, dataType, inspiretheme, inspireServiceType) + result = self._search(q, start, step, orgName, dataType, inspiretheme, inspireServiceType) searchResult = result.find(".//{http://www.opengis.net/cat/csw/2.0.2}SearchResults") if not searchResult: return count = int( searchResult.attrib["numberOfRecordsMatched"] ) start += step while (start) <= count: - result = self.search(q, start, step, orgName, dataType,inspiretheme, inspireServiceType) + result = self._search(q, start, step, orgName, dataType,inspiretheme, inspireServiceType) mds= result.findall(".//{http://www.opengis.net/cat/csw/2.0.2}Record") for md in mds: searchResult.append( md ) start += step - - return searchResult + mdata = MDdata( searchResult ) + return mdata def getWmsLayerNames(url): diff --git a/webUtil.py b/webUtil.py index 91da5f9..9362a5b 100644 --- a/webUtil.py +++ b/webUtil.py @@ -12,7 +12,6 @@ def getUrlData(url, data=None, returnBytes=False): :return: the response as a string """ bnr = QgsBlockingNetworkRequest() - print(url) if not data: respcode = bnr.get(QNetworkRequest( QUrl(url) ) ) else: