Skip to content

Commit 544acff

Browse files
refactor: use asyncpg instead of psycopg
Move sql query file next to indicator module where it is used. PR: #876 Closes #746
1 parent 7d8ba26 commit 544acff

File tree

4 files changed

+18
-42
lines changed

4 files changed

+18
-42
lines changed

ohsome_quality_api/indicators/building_comparison/indicator.py

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
import logging
22
import os
3+
from pathlib import Path
34
from string import Template
45

56
import geojson
67
import plotly.graph_objects as pgo
7-
import psycopg
88
import yaml
9-
from async_lru import alru_cache
109
from dateutil import parser
1110
from geojson import Feature
1211
from numpy import mean
1312

14-
from ohsome_quality_api.config import get_config_value
1513
from ohsome_quality_api.definitions import Color, get_attribution
1614
from ohsome_quality_api.geodatabase import client as db_client
1715
from ohsome_quality_api.indicators.base import BaseIndicator
@@ -277,27 +275,18 @@ def format_sources(self):
277275

278276

279277
# alru needs hashable type, therefore, use string instead of Feature
280-
@alru_cache
278+
# @alru_cache
281279
async def get_reference_building_area(feature_str: str, table_name: str) -> float:
282-
"""Get the building area for a AoI from the EUBUCCO dataset."""
283-
# TODO: https://github.com/GIScience/ohsome-quality-api/issues/746
284-
file_path = os.path.join(db_client.WORKING_DIR, "select_building_area.sql")
285-
with open(file_path, "r") as file:
280+
path = Path(__file__).parent / "query.sql"
281+
with open(path, "r") as file:
286282
query = file.read()
287-
dns = "postgres://{user}:{password}@{host}:{port}/{database}".format(
288-
host=get_config_value("postgres_host"),
289-
port=get_config_value("postgres_port"),
290-
database=get_config_value("postgres_db"),
291-
user=get_config_value("postgres_user"),
292-
password=get_config_value("postgres_password"),
293-
)
294283
feature = geojson.loads(feature_str)
295284
geom = geojson.dumps(feature.geometry)
296-
async with await psycopg.AsyncConnection.connect(dns) as con:
297-
async with con.cursor() as cur:
298-
await cur.execute(query.format(table_name=table_name), (geom,))
299-
res = await cur.fetchone()
300-
return res[0] or 0.0
285+
results = await db_client.fetch(
286+
query.format(table_name=table_name),
287+
geom,
288+
)
289+
return results[0][0] or 0.0
301290

302291

303292
def load_datasets_metadata() -> dict:

ohsome_quality_api/geodatabase/select_building_area.sql renamed to ohsome_quality_api/indicators/building_comparison/query.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
WITH bpoly AS (
22
SELECT
33
-- split mutlipolygon into list of polygons for more efficient processing
4-
(ST_DUMP (ST_Setsrid (ST_GeomFromGeoJSON (%s), 4326))).geom AS geom
4+
(ST_DUMP (ST_Setsrid (ST_GeomFromGeoJSON ($1), 4326))).geom AS geom
55
)
66
SELECT
77
SUM({table_name}.area) as area

ohsome_quality_api/indicators/road_comparison/indicator.py

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
import logging
22
import os
3+
from pathlib import Path
34

45
import geojson
56
import plotly.graph_objects as pgo
6-
import psycopg
77
import yaml
88
from async_lru import alru_cache
99
from geojson import Feature
1010
from numpy import mean
1111

12-
from ohsome_quality_api.config import get_config_value
1312
from ohsome_quality_api.definitions import Color, get_attribution
1413
from ohsome_quality_api.geodatabase import client as db_client
1514
from ohsome_quality_api.indicators.base import BaseIndicator
@@ -275,29 +274,17 @@ async def get_matched_roadlengths(
275274
feature_str: str,
276275
table_name: str,
277276
) -> tuple[float, float]:
278-
file_path = os.path.join(db_client.WORKING_DIR, "get_matched_roads.sql")
277+
file_path = Path(__file__).parent / "query.sql"
279278
with open(file_path, "r") as file:
280279
query = file.read()
281-
dns = "postgres://{user}:{password}@{host}:{port}/{database}".format(
282-
host=get_config_value("postgres_host"),
283-
port=get_config_value("postgres_port"),
284-
database=get_config_value("postgres_db"),
285-
user=get_config_value("postgres_user"),
286-
password=get_config_value("postgres_password"),
287-
)
288280
feature = geojson.loads(feature_str)
289281
table_name = table_name.replace(" ", "_")
290282
geom = geojson.dumps(feature.geometry)
291-
async with await psycopg.AsyncConnection.connect(dns) as con:
292-
async with con.cursor() as cur:
293-
await cur.execute(
294-
query.format(
295-
table_name=table_name,
296-
),
297-
(geom,),
298-
)
299-
res = await cur.fetchone()
300-
return res[0], res[1]
283+
results = await db_client.fetch(
284+
query.format(table_name=table_name),
285+
geom,
286+
)
287+
return results[0][0], results[0][1]
301288

302289

303290
def load_datasets_metadata() -> dict:

ohsome_quality_api/geodatabase/get_matched_roads.sql renamed to ohsome_quality_api/indicators/road_comparison/query.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
WITH bpoly AS (
22
SELECT
33
-- split mutlipolygon into list of polygons for more efficient processing
4-
(ST_DUMP (ST_Setsrid (ST_GeomFromGeoJSON (%s), 4326))).geom AS geom
4+
(ST_DUMP (ST_Setsrid (ST_GeomFromGeoJSON ($1), 4326))).geom AS geom
55
)
66
SELECT
77
SUM(cr.covered),

0 commit comments

Comments
 (0)