aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuilhem Moulin <guilhem@fripost.org>2023-10-29 15:59:27 +0100
committerGuilhem Moulin <guilhem@fripost.org>2023-10-29 23:16:56 +0100
commit88749d8cf1e7df54930b4d7f9b1b7a5cd2782975 (patch)
treebd7787b25ff8cc7992dbc595b20d2ea3abb83e71
parent3e0f6afd257f89e957f6f98cfd50099fdd28f5cf (diff)
list-observations: Refactor filtering/printing logic.
-rwxr-xr-xlist-observations142
1 files changed, 71 insertions, 71 deletions
diff --git a/list-observations b/list-observations
index 2cb7d50..869e307 100755
--- a/list-observations
+++ b/list-observations
@@ -185,65 +185,66 @@ if layer is None:
source_srs = layer.GetSpatialRef()
transform_srs = osr.CoordinateTransformation(source_srs, target_srs)
-def getField(feat, name):
- idx = feature.GetFieldIndex(name)
- if idx is not None and idx >= 0:
- return feature.GetField(idx)
- return None
-def getFieldAsDateTime(feat, name):
- idx = feature.GetFieldIndex(name)
- if idx is not None and idx >= 0:
- v = feature.GetFieldAsDateTime(idx)
- t = (v[0], v[1], v[2], v[3], v[4], int(v[5]), v[6], -1, -1)
- return datetime.fromtimestamp(time.mktime(t))
- return None
-
+layerDefn = layer.GetLayerDefn()
+fieldCount = layerDefn.GetFieldCount()
sum_count = 0
summary = {}
listSeparator = re.compile(r',\s*')
-feature = layer.GetNextFeature()
-while feature is not None:
- v = feature.GetField('IsPositiveObservation')
+def printObservation(feature):
+ global sum_count
+ properties = {}
+ for i in range(fieldCount):
+ if not feature.IsFieldSet(i):
+ continue
+ fieldDefn = layerDefn.GetFieldDefn(i)
+ if feature.IsFieldNull(i):
+ val = None
+ elif fieldDefn.type == ogr.OFTInteger: # booleans are there too
+ val = feature.GetFieldAsInteger(i)
+ elif fieldDefn.type == ogr.OFTString:
+ val = feature.GetFieldAsString(i)
+ elif fieldDefn.type == ogr.OFTDateTime:
+ st = feature.GetFieldAsDateTime(i)
+ t = (st[0], st[1], st[2], st[3], st[4], int(st[5]), st[6], -1, -1)
+ val = datetime.fromtimestamp(time.mktime(t))
+ else:
+ raise Exception(f'{fieldDefn.name}: Unknown field type {fieldDefn.type}')
+ properties[fieldDefn.name] = val
+
+ v = properties.get('IsPositiveObservation')
if v is not None and not v:
- feature = layer.GetNextFeature()
- continue
- v = feature.GetField('IsNaturalOccurrence')
+ return
+ v = properties.get('IsNaturalOccurrence')
if v is not None and not v:
- feature = layer.GetNextFeature()
- continue
+ return
- uncertainty = feature.GetField('CoordinateUncertaintyInMeters')
+ uncertainty = properties.get('CoordinateUncertaintyInMeters')
if uncertainty is not None and uncertainty > args.max_uncertainty:
- feature = layer.GetNextFeature()
- continue
+ return
- vernacularName = getField(feature, 'VernacularName')
+ vernacularName = properties.get('VernacularName')
if vernacularName is None:
raise Exception('Missing name')
if args.taxon is not None:
name = vernacularName.lower()
if not any([sub in name for sub in args.taxon]):
- feature = layer.GetNextFeature()
- continue
+ return
- protectedByLaw = feature.GetField('ProtectedByLaw')
+ protectedByLaw = properties.get('ProtectedByLaw')
if args.protected and (protectedByLaw is None or not protectedByLaw):
- feature = layer.GetNextFeature()
- continue
+ return
- taxonIsRedlisted = feature.GetField('TaxonIsRedlisted')
- redlistCategory = getField(feature, 'RedlistCategory')
+ taxonIsRedlisted = properties.get('TaxonIsRedlisted')
+ redlistCategory = properties.get('RedlistCategory')
if args.redlisted is not None:
if args.redlisted == 'TaxonIsRedlisted':
if taxonIsRedlisted is None or not taxonIsRedlisted:
- feature = layer.GetNextFeature()
- continue
+ return
elif type(args.redlisted) == list:
if redlistCategory is None or redlistCategory not in args.redlisted:
- feature = layer.GetNextFeature()
- continue
+ return
- reportedBy = getField(feature, 'ReportedBy')
+ reportedBy = properties.get('ReportedBy')
if args.reporter is not None:
found = False
for r in listSeparator.split(reportedBy):
@@ -252,10 +253,9 @@ while feature is not None:
found = True
break
if not found:
- feature = layer.GetNextFeature()
- continue
+ return
- recordedBy = getField(feature, 'RecordedBy')
+ recordedBy = properties.get('RecordedBy')
if args.observer is not None:
found = False
for r in listSeparator.split(recordedBy):
@@ -264,18 +264,15 @@ while feature is not None:
found = True
break
if not found:
- feature = layer.GetNextFeature()
- continue
+ return
- startDate = getFieldAsDateTime(feature, 'StartDate')
+ startDate = properties.get('StartDate')
if args.until is not None and (startDate is None or args.until < startDate):
- feature = layer.GetNextFeature()
- continue
+ return
- endDate = getFieldAsDateTime(feature, 'EndDate')
+ endDate = properties.get('EndDate')
if args.since is not None and (endDate is None or endDate < args.since):
- feature = layer.GetNextFeature()
- continue
+ return
geom = feature.GetGeometryRef()
geom = geom.Clone()
@@ -291,8 +288,7 @@ while feature is not None:
if uncertainty is not None:
max_distance += uncertainty
if max_distance < distance:
- feature = layer.GetNextFeature()
- continue
+ return
if sum_count > 0:
print('')
@@ -305,29 +301,29 @@ while feature is not None:
n = summary.get(name, 0)
summary[name] = n + 1
- v = getField(feature, 'OrganismQuantityInt')
+ v = properties.get('OrganismQuantityInt')
if v is None:
v = 'Noterad'
else:
v = str(v)
- unit = getField(feature, 'OrganismQuantityUnit')
+ unit = properties.get('OrganismQuantityUnit')
if unit is not None and unit != '':
v += ' ' + unit
print(f'Antal & enhet: {v}')
- v = getField(feature, 'Sex')
+ v = properties.get('Sex')
if v is not None:
print(f'Kön: {v}')
- v = getField(feature, 'LifeStage')
+ v = properties.get('LifeStage')
if v is not None:
print(f'Ålder/stadium: {v}')
- v = getField(feature, 'Activity')
+ v = properties.get('Activity')
if v is not None:
print(f'Aktivitet: {v}')
- v = getField(feature, 'DiscoveryMethod')
+ v = properties.get('DiscoveryMethod')
if v is not None:
print(f'Metod: {v}')
@@ -343,25 +339,25 @@ while feature is not None:
print(f'Startdatum: {startDate.strftime("%-d %B %Y %H:%M")}')
print(f'Slutdatum: {endDate.strftime("%-d %B %Y %H:%M")}')
- v = getField(feature, 'Projects')
+ v = properties.get('Projects')
if v is not None:
print(f'Projects: {v}')
- v = getField(feature, 'OccurrenceRemarks')
+ v = properties.get('OccurrenceRemarks')
if v is not None:
print(f'Kommentar: {v}')
- v = getField(feature, 'DatasetName')
+ v = properties.get('DatasetName')
if v is not None:
print(f'Källa: {v}')
- v = getField(feature, 'Url')
+ v = properties.get('Url')
if v is not None:
print(f'URL: {v}')
if geom.GetGeometryType() != ogr.wkbPoint:
raise Exception('Not a point')
- x, y = geom.GetPoint_2D(i)
- v = getField(feature, 'Locality')
+ x, y = geom.GetPoint_2D(0)
+ v = properties.get('Locality')
if v is not None:
print(f'Lokalnamn: {v}')
coords = f'Koordinater: N{y:.0f}, Ö{x:.0f} (±{uncertainty:.0f}m) {target_srs.GetName()}'
@@ -405,26 +401,26 @@ while feature is not None:
v = []
if protectedByLaw is not None and protectedByLaw:
v.append('nationellt fridlyst')
- v2 = getField(feature, 'TaxonIsInvasiveInSweden')
+ v2 = properties.get('TaxonIsInvasiveInSweden')
if v2 is not None and v2:
v.append('främmande i Sverige')
- v2 = getField(feature, 'TaxonIsInvasiveEuRegulation')
+ v2 = properties.get('TaxonIsInvasiveEuRegulation')
if v2 is not None and v2:
v.append('invasiv enligt EU-förordning 1143/2014')
- v2 = getField(feature, 'Natura2000HabitatsDirective')
+ v2 = properties.get('Natura2000HabitatsDirective')
if v2 is not None and v2:
v2 = []
- v3 = getField(feature, 'Natura2000HabitatsDirectiveArticle2')
+ v3 = properties.get('Natura2000HabitatsDirectiveArticle2')
if v3 is not None and v3:
- v3 = getField(feature, 'Natura2000HabitatsDirectiveArticle2PrioritySpecie')
+ v3 = properties.get('Natura2000HabitatsDirectiveArticle2PrioritySpecie')
if v3 is not None and v3:
v2.append('II (prioriterad art)')
else:
v2.append('II')
- v3 = getField(feature, 'Natura2000HabitatsDirectiveArticle4')
+ v3 = properties.get('Natura2000HabitatsDirectiveArticle4')
if v3 is not None and v3:
v2.append('IV')
- v3 = getField(feature, 'Natura2000HabitatsDirectiveArticle5')
+ v3 = properties.get('Natura2000HabitatsDirectiveArticle5')
if v3 is not None and v3:
v2.append('V')
if len(v2) == 0:
@@ -433,10 +429,10 @@ while feature is not None:
v.append('habitatdirektivets bilaga ' + v2[0])
else:
v.append('habitatdirektivets bilagor ' + ', '.join(v2[0:-1]) + ' & ' + v2[-1])
- v2 = getField(feature, 'TaxonIsSignalSpecie')
+ v2 = properties.get('TaxonIsSignalSpecie')
if v2 is not None and v2:
v.append('signalart enligt Skogsstyrelsen')
- v2 = getField(feature, 'BirdDirective')
+ v2 = properties.get('BirdDirective')
if v2 is not None and v2:
v.append('fågeldirektivet')
if len(v) > 0:
@@ -447,6 +443,10 @@ while feature is not None:
print(f'Naturvård: {v}')
sum_count += 1
+
+feature = layer.GetNextFeature()
+while feature is not None:
+ printObservation(feature)
feature = layer.GetNextFeature()
if sum_count == 0: