Skip to content

Commit 72107f5

Browse files
feat(currentness): optionally query ohsomedb in preprocess function
instead of ohsome API. This is implemented as feature flag.
1 parent a7f2e4c commit 72107f5

18 files changed

+6296
-12
lines changed

ohsome_quality_api/indicators/currentness/indicator.py

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,23 @@
88
th: Threshold
99
"""
1010

11+
import json
1112
import locale
1213
import logging
1314
import os
1415
from dataclasses import dataclass
16+
from pathlib import Path
1517
from string import Template
1618

1719
import plotly.graph_objects as pgo
1820
import yaml
1921
from dateutil.parser import isoparse
2022
from geojson import Feature
23+
from ohsome_filter_to_sql.main import ohsome_filter_to_sql
2124
from plotly.subplots import make_subplots
2225

2326
from ohsome_quality_api.definitions import Color
27+
from ohsome_quality_api.geodatabase import client
2428
from ohsome_quality_api.indicators.base import BaseIndicator
2529
from ohsome_quality_api.ohsome import client as ohsome_client
2630
from ohsome_quality_api.topics.models import BaseTopic as Topic
@@ -29,7 +33,7 @@
2933
try:
3034
locale.setlocale(locale.LC_ALL, ["en_US", locale.getencoding()])
3135
except locale.Error:
32-
logging.warn(
36+
logging.warning(
3337
"Could not set locale to en_US. Output may be different than expected."
3438
)
3539

@@ -69,7 +73,13 @@ def __init__(
6973
self.bin_in_between: Bin
7074
self.bin_out_of_date: Bin
7175

72-
async def preprocess(self):
76+
async def preprocess(self, ohsomedb: bool = False):
77+
if ohsomedb:
78+
await self.preprocess_ohsomedb()
79+
else:
80+
await self.preprocess_ohsomeapi()
81+
82+
async def preprocess_ohsomeapi(self):
7383
"""Fetch all latest contributions in monthly buckets since 2008
7484
7585
Beside the creation, latest contribution includes also the change to the
@@ -114,6 +124,46 @@ async def preprocess(self):
114124
self.contrib_sum = contrib_sum
115125
self.result.timestamp_osm = self.bin_total.to_timestamps[0]
116126

127+
async def preprocess_ohsomedb(self):
128+
where = ohsome_filter_to_sql(self.topic.filter)
129+
with open(Path(__file__).parent / "query.sql", "r") as file:
130+
template = file.read()
131+
query = Template(template).substitute(
132+
{
133+
"aoi": json.dumps(self.feature["geometry"]),
134+
"filter": where,
135+
}
136+
)
137+
results = await client.fetch(query)
138+
if len(results) == 0:
139+
# no data
140+
self.contrib_sum = 0
141+
return
142+
to_timestamps = []
143+
from_timestamps = []
144+
timestamps = []
145+
contrib_abs = []
146+
contrib_sum = 0
147+
for r in reversed(results): # latest contributions first
148+
to_timestamps.append(r[0])
149+
from_timestamps.append(r[0])
150+
timestamps.append(r[0])
151+
contrib_abs.append(r[1])
152+
contrib_sum += r[1]
153+
if contrib_sum == 0:
154+
contrib_rel = [0 for _ in contrib_abs]
155+
else:
156+
contrib_rel = [c / contrib_sum for c in contrib_abs]
157+
self.bin_total = Bin(
158+
contrib_abs,
159+
contrib_rel,
160+
to_timestamps,
161+
from_timestamps,
162+
timestamps,
163+
)
164+
self.contrib_sum = contrib_sum
165+
self.result.timestamp_osm = self.bin_total.to_timestamps[0]
166+
117167
def calculate(self):
118168
"""Determine up-to-date, in-between and out-of-date contributions.
119169
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
WITH bpoly AS (
2+
SELECT
3+
ST_Setsrid (ST_GeomFromGeoJSON ('$aoi'), 4326) AS geom
4+
)
5+
SELECT
6+
Date_trunc('month', valid_from) AS month,
7+
Count(*)
8+
FROM
9+
contributions c,
10+
bpoly b
11+
WHERE
12+
ST_Intersects (c.geom, b.geom)
13+
AND (status_geom_type).status = 'latest'
14+
-- TODO
15+
-- AND contrib_type in ("CREATION", "GEOMETRY", "TAG")
16+
AND ($filter)
17+
GROUP BY
18+
month
19+
ORDER BY
20+
month;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
In the area of interest 71% of the 6879 features were edited (created or modified) for the last time in the period between 01 Aug 2022 and 01 Aug 2025.
2+
Most features are up-to-date.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Please note that in the area of interest less than 25 features of the selected topic are present today. In the area of interest 71% of the 20 features were edited (created or modified) for the last time in the period between 01 Aug 2022 and 01 Aug 2025.
2+
Most features are up-to-date.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Please note that there was no mapping activity for 13 months in this region. In the area of interest 71% of the 30 features were edited (created or modified) for the last time in the period between 01 Aug 2022 and 01 Aug 2025.
2+
Most features are up-to-date.

0 commit comments

Comments
 (0)