From 3e0f6afd257f89e957f6f98cfd50099fdd28f5cf Mon Sep 17 00:00:00 2001 From: Guilhem Moulin Date: Tue, 24 Oct 2023 01:14:25 +0200 Subject: list-observations: Add ability to filter by observer and/or reporter. --- list-observations | 51 ++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 40 insertions(+), 11 deletions(-) diff --git a/list-observations b/list-observations index 91aeeec..2cb7d50 100755 --- a/list-observations +++ b/list-observations @@ -22,6 +22,7 @@ import argparse from datetime import datetime import time import locale +import re from pathlib import Path from osgeo import gdal, ogr, osr gdal.UseExceptions() @@ -69,7 +70,7 @@ class redlistedAction(argparse.Action): items.extend(values) setattr(namespace, self.dest, items) -class taxonAction(argparse.Action): +class splitAndExtendAction(argparse.Action): def __init__(self, option_strings, dest, nargs=None, **kwargs): super().__init__(option_strings, dest, nargs=nargs, **kwargs) def __call__(self, parser, namespace, values, option_string=None): @@ -85,6 +86,7 @@ class taxonAction(argparse.Action): parser = argparse.ArgumentParser( description='List and filter observations from Artdatabanken.', prog = programName, + usage='''%(prog)s [--geometry=FILE] [--max-distance=N] [--redlisted[=NT,VU,…]] OBSERVATIONS_FILE''' ) parser.add_argument('--geometry', type=ePath, metavar='FILE', action=geometryAction, help='Geometry file of interest') @@ -98,10 +100,14 @@ parser.add_argument('--until', metavar='YYYY-MM-DD', help='End date for observations in ISO 8601 format') parser.add_argument('--protected', action='store_true', help='Only consider taxons that are protected by law') -parser.add_argument('--redlisted', nargs='?', metavar='[NT,…]', action=redlistedAction, +parser.add_argument('--redlisted', nargs='?', metavar='NT,…', action=redlistedAction, help='Only consider redlisted taxons') -parser.add_argument('--taxon', metavar='NAME', action=taxonAction, +parser.add_argument('--taxon', metavar='NAME', action=splitAndExtendAction, help='Only consider these taxons') +parser.add_argument('--reporter', metavar='NAME', action=splitAndExtendAction, + help='Only consider observation by these reporters') +parser.add_argument('--observer', metavar='NAME', action=splitAndExtendAction, + help='Only consider observation by these observers') parser.add_argument('observations', type=ePath, metavar='OBSERVATIONS_FILE', help='Observations file') @@ -194,6 +200,7 @@ def getFieldAsDateTime(feat, name): sum_count = 0 summary = {} +listSeparator = re.compile(r',\s*') feature = layer.GetNextFeature() while feature is not None: v = feature.GetField('IsPositiveObservation') @@ -236,6 +243,30 @@ while feature is not None: feature = layer.GetNextFeature() continue + reportedBy = getField(feature, 'ReportedBy') + if args.reporter is not None: + found = False + for r in listSeparator.split(reportedBy): + r = r.lower() + if any([sub in r for sub in args.reporter]): + found = True + break + if not found: + feature = layer.GetNextFeature() + continue + + recordedBy = getField(feature, 'RecordedBy') + if args.observer is not None: + found = False + for r in listSeparator.split(recordedBy): + r = r.lower() + if any([sub in r for sub in args.observer]): + found = True + break + if not found: + feature = layer.GetNextFeature() + continue + startDate = getFieldAsDateTime(feature, 'StartDate') if args.until is not None and (startDate is None or args.until < startDate): feature = layer.GetNextFeature() @@ -300,16 +331,14 @@ while feature is not None: if v is not None: print(f'Metod: {v}') - v = getField(feature, 'ReportedBy') - if ',' in v: - print(f'Rapportörer: {v}') + if ',' in reportedBy: + print(f'Rapportörer: {reportedBy}') else: - print(f'Rapportör: {v}') - v = getField(feature, 'RecordedBy') - if ',' in v: - print(f'Observatörer: {v}') + print(f'Rapportör: {reportedBy}') + if ',' in recordedBy: + print(f'Observatörer: {recordedBy}') else: - print(f'Observatör: {v}') + print(f'Observatör: {recordedBy}') print(f'Startdatum: {startDate.strftime("%-d %B %Y %H:%M")}') print(f'Slutdatum: {endDate.strftime("%-d %B %Y %H:%M")}') -- cgit v1.2.3