mirror of
https://github.com/qgis/QGIS.git
synced 2025-10-15 00:02:52 -04:00
MetaSearch: add logging
This commit is contained in:
parent
8f4da32b6b
commit
ee2c16c50b
@ -4,7 +4,7 @@
|
||||
# Alexander Bruy (alexander.bruy@gmail.com),
|
||||
# Maxim Dubinin (sim@gis-lab.info),
|
||||
#
|
||||
# Copyright (C) 2014 Tom Kralidis (tomkralidis@gmail.com)
|
||||
# Copyright (C) 2024 Tom Kralidis (tomkralidis@gmail.com)
|
||||
#
|
||||
# This source is free software; you can redistribute it and/or modify it under
|
||||
# the terms of the GNU General Public License as published by the Free
|
||||
@ -30,9 +30,7 @@ from qgis.PyQt.QtGui import QIcon
|
||||
|
||||
from qgis.core import QgsApplication
|
||||
from MetaSearch.dialogs.maindialog import MetaSearchDialog
|
||||
from MetaSearch.util import get_help_url, open_url, StaticContext
|
||||
|
||||
LOGGER = logging.getLogger('MetaSearch')
|
||||
from MetaSearch.util import get_help_url, log_message, open_url, StaticContext
|
||||
|
||||
|
||||
class MetaSearchPlugin:
|
||||
@ -52,6 +50,7 @@ class MetaSearchPlugin:
|
||||
"""startup"""
|
||||
|
||||
# run
|
||||
log_message('Initializing plugin', 'Info')
|
||||
run_icon = QIcon('{}/{}'.format(self.context.ppath, 'images/MetaSearch.svg'))
|
||||
self.action_run = QAction(run_icon, 'MetaSearch',
|
||||
self.iface.mainWindow())
|
||||
@ -82,6 +81,7 @@ class MetaSearchPlugin:
|
||||
def unload(self):
|
||||
"""teardown"""
|
||||
|
||||
log_message('Unloading plugin', 'Info')
|
||||
# remove the plugin menu item and icon
|
||||
self.iface.removePluginWebMenu(self.web_menu, self.action_run)
|
||||
self.iface.removePluginWebMenu(self.web_menu, self.action_help)
|
||||
@ -90,6 +90,7 @@ class MetaSearchPlugin:
|
||||
def run(self):
|
||||
"""open MetaSearch"""
|
||||
|
||||
log_message('Running plugin', 'Info')
|
||||
self.dialog.exec()
|
||||
|
||||
def help(self):
|
||||
|
@ -4,7 +4,7 @@
|
||||
# ---------------------------------------------------------
|
||||
# QGIS Catalog Service client.
|
||||
#
|
||||
# Copyright (C) 2023 Tom Kralidis (tomkralidis@gmail.com)
|
||||
# Copyright (C) 2024 Tom Kralidis (tomkralidis@gmail.com)
|
||||
#
|
||||
# This source is free software; you can redistribute it and/or modify it under
|
||||
# the terms of the GNU General Public License as published by the Free
|
||||
@ -27,6 +27,8 @@ import warnings
|
||||
import owslib
|
||||
from owslib.fes import BBox, PropertyIsLike
|
||||
|
||||
from MetaSearch.util import log_message
|
||||
|
||||
with warnings.catch_warnings():
|
||||
warnings.filterwarnings("ignore", category=ResourceWarning)
|
||||
warnings.filterwarnings("ignore", category=ImportWarning)
|
||||
@ -84,6 +86,7 @@ class CSW202Search(SearchBase):
|
||||
self.record_info_template = 'record_metadata_dc.html'
|
||||
self.constraints = []
|
||||
|
||||
log_message(f'Connecting to CSW: {self.url}', 'Info')
|
||||
self.conn = CatalogueServiceWeb(self.url, # spellok
|
||||
timeout=self.timeout,
|
||||
username=self.username,
|
||||
@ -101,6 +104,7 @@ class CSW202Search(SearchBase):
|
||||
# even for a global bbox, if a spatial filter is applied, then
|
||||
# the CSW server will skip records without a bbox
|
||||
if bbox and bbox != ['-180', '-90', '180', '90']:
|
||||
log_message(f'Setting bbox filter ({bbox})', 'Info')
|
||||
minx, miny, maxx, maxy = bbox
|
||||
self.constraints.append(BBox([miny, minx, maxy, maxx],
|
||||
crs='urn:ogc:def:crs:EPSG::4326'))
|
||||
@ -108,16 +112,20 @@ class CSW202Search(SearchBase):
|
||||
# keywords
|
||||
if keywords:
|
||||
# TODO: handle multiple word searches
|
||||
log_message(f'Setting csw:AnyText filter {keywords}', 'Info')
|
||||
self.constraints.append(PropertyIsLike('csw:AnyText', keywords))
|
||||
|
||||
if len(self.constraints) > 1: # exclusive search (a && b)
|
||||
self.constraints = [self.constraints]
|
||||
|
||||
log_message(f'Searching CSW: {self.url}', 'Info')
|
||||
self.conn.getrecords2(constraints=self.constraints, maxrecords=limit,
|
||||
startposition=offset, esn='full')
|
||||
|
||||
self.matches = self.conn.results['matches']
|
||||
self.returned = self.conn.results['returned']
|
||||
log_message(f'Matches: {self.matches}', 'Info')
|
||||
log_message(f'Returned: {self.returned}', 'Info')
|
||||
|
||||
self.request = self.conn.request
|
||||
self.response = self.conn.response
|
||||
@ -151,6 +159,7 @@ class CSW202Search(SearchBase):
|
||||
return recs
|
||||
|
||||
def get_record(self, identifier):
|
||||
log_message(f'Searching CSW for record: {identifier}', 'Info')
|
||||
self.conn.getrecordbyid([identifier])
|
||||
|
||||
return self.conn.records[identifier]
|
||||
@ -174,6 +183,7 @@ class OARecSearch(SearchBase):
|
||||
self.record_collection = None
|
||||
|
||||
if '/collections/' in self.url: # catalog is a collection
|
||||
log_message('OARec endpoint is a collection', 'Info')
|
||||
self.base_url, self.record_collection = self.url.split('/collections/') # noqa
|
||||
self.conn = Records(
|
||||
self.base_url, timeout=self.timeout, auth=self.auth)
|
||||
@ -186,6 +196,7 @@ class OARecSearch(SearchBase):
|
||||
pass
|
||||
self.request = self.conn.request
|
||||
else:
|
||||
log_message('OARec endpoint is not a collection', 'Info')
|
||||
self.conn = Records(self.url, timeout=self.timeout, auth=self.auth)
|
||||
self.request = None
|
||||
|
||||
@ -203,16 +214,22 @@ class OARecSearch(SearchBase):
|
||||
}
|
||||
|
||||
if keywords:
|
||||
log_message(f'Setting keyword search {keywords}', 'Info')
|
||||
params['q'] = keywords
|
||||
if bbox and bbox != ['-180', '-90', '180', '90']:
|
||||
log_message(f'Setting bbox search {bbox}', 'Info')
|
||||
params['bbox'] = bbox
|
||||
|
||||
log_message(f'Searching OARec: {self.url}', 'Info')
|
||||
self.response = self.conn.collection_items(**params)
|
||||
|
||||
self.matches = self.response.get('numberMatched', 0)
|
||||
self.returned = self.response.get('numberReturned', 0)
|
||||
self.request = self.conn.request
|
||||
|
||||
log_message(f'Matches: {self.matches}', 'Info')
|
||||
log_message(f'Returned: {self.returned}', 'Info')
|
||||
|
||||
def records(self):
|
||||
recs = []
|
||||
|
||||
@ -240,6 +257,7 @@ class OARecSearch(SearchBase):
|
||||
return recs
|
||||
|
||||
def get_record(self, identifier):
|
||||
log_message(f'Searching OARec endpoint for item {identifier}', 'Info')
|
||||
return self.conn.collection_item(self.record_collection, identifier)
|
||||
|
||||
def parse_link(self, link):
|
||||
@ -258,8 +276,10 @@ class OARecSearch(SearchBase):
|
||||
def get_catalog_service(url, catalog_type, timeout, username, password,
|
||||
auth=None):
|
||||
if catalog_type in [None, CATALOG_TYPES[0]]:
|
||||
log_message('CSW endpoint detected', 'Info')
|
||||
return CSW202Search(url, timeout, username, password, auth)
|
||||
elif catalog_type == CATALOG_TYPES[1]:
|
||||
log_message('OARec endpoint detected', 'Info')
|
||||
if not OWSLIB_OAREC_SUPPORTED:
|
||||
raise ValueError("OGC API - Records requires OWSLib 0.25 or above")
|
||||
return OARecSearch(url, timeout, auth)
|
||||
|
@ -4,7 +4,7 @@
|
||||
# Alexander Bruy (alexander.bruy@gmail.com),
|
||||
# Maxim Dubinin (sim@gis-lab.info)
|
||||
#
|
||||
# Copyright (C) 2014 Tom Kralidis (tomkralidis@gmail.com)
|
||||
# Copyright (C) 2024 Tom Kralidis (tomkralidis@gmail.com)
|
||||
# Copyright (C) 2014 Angelos Tzotsos (tzotsos@gmail.com)
|
||||
#
|
||||
# This source is free software; you can redistribute it and/or modify it under
|
||||
@ -35,13 +35,12 @@ with warnings.catch_warnings():
|
||||
warnings.filterwarnings("ignore", category=DeprecationWarning)
|
||||
from jinja2 import Environment, FileSystemLoader
|
||||
|
||||
from qgis.PyQt.QtCore import QUrl, QUrlQuery
|
||||
from qgis.core import Qgis, QgsMessageLog, QgsSettings
|
||||
from qgis.PyQt.QtCore import QCoreApplication, QUrl, QUrlQuery
|
||||
from qgis.PyQt.QtWidgets import QMessageBox
|
||||
from qgis.PyQt.uic import loadUiType
|
||||
|
||||
from qgis.core import Qgis, QgsSettings
|
||||
|
||||
LOGGER = logging.getLogger('MetaSearch')
|
||||
LOGGER = QgsMessageLog()
|
||||
|
||||
|
||||
class StaticContext:
|
||||
@ -167,3 +166,10 @@ def clean_ows_url(url):
|
||||
url.setQuery(query_string)
|
||||
|
||||
return url.toString()
|
||||
|
||||
|
||||
def log_message(message, level):
|
||||
"""helper function to emit logging messages"""
|
||||
|
||||
message_translated = QCoreApplication.translate('MetaSearch', message)
|
||||
LOGGER.logMessage(message_translated, 'MetaSearch', getattr(Qgis, level))
|
||||
|
Loading…
x
Reference in New Issue
Block a user