diff --git a/src/app.py b/src/app.py index 1a119ecd..b44caddd 100644 --- a/src/app.py +++ b/src/app.py @@ -55,7 +55,6 @@ EXPORT_MAX_AREA_SQKM, ) from src.config import EXPORT_PATH as export_path -from src.config import HDX_MAINTAINER, HDX_OWNER_ORG, HDX_URL_PREFIX from src.config import INDEX_THRESHOLD as index_threshold from src.config import POLYGON_STATISTICS_API_URL from src.config import USE_CONNECTION_POOLING as use_connection_pooling @@ -89,6 +88,9 @@ import duckdb from hdx.data.dataset import Dataset + from src.config import HDX_MAINTAINER, HDX_OWNER_ORG, HDX_URL_PREFIX + + global LOCAL_CON_POOL # getting the pool instance which was fireup when API is started @@ -1104,6 +1106,13 @@ def get_summary_stats(self): class DuckDB: + """ + Constructor for the DuckDB class. + + Parameters: + - db_path (str): The path to the DuckDB database file. + """ + def __init__(self, db_path): dbdict = get_db_connection_params() self.db_con_str = convert_dict_to_conn_str(db_dict=dbdict) @@ -1118,6 +1127,14 @@ def __init__(self, db_path): con.load_extension("json") def run_query(self, query, attach_pgsql=False, load_spatial=False): + """ + Executes a query on the DuckDB database. + + Parameters: + - query (str): The SQL query to execute. + - attach_pgsql (bool): Flag to indicate whether to attach a PostgreSQL database. + - load_spatial (bool): Flag to indicate whether to load the spatial extension. + """ with duckdb.connect(self.db_path) as con: if attach_pgsql: con.execute( @@ -1131,6 +1148,13 @@ def run_query(self, query, attach_pgsql=False, load_spatial=False): class HDX: + """ + Constructor for the HDX class. + + Parameters: + - params (DynamicCategoriesModel): An instance of DynamicCategoriesModel containing configuration settings. + """ + def __init__(self, params): self.params = params self.iso3 = self.params.iso3 @@ -1180,6 +1204,15 @@ def __init__(self, params): self.duck_db_instance = DuckDB(self.duck_db_db_path) def types_to_tables(self, type_list: list): + """ + Maps feature types to corresponding database tables. + + Parameters: + - type_list (List[str]): List of feature types. + + Returns: + - List of database tables associated with the given feature types. + """ mapping = { "points": ["nodes"], "lines": ["ways_line", "relations"], @@ -1195,6 +1228,15 @@ def types_to_tables(self, type_list: list): return list(table_set) def format_where_clause(self, where_clause): + """ + Formats the where_clause by replacing certain patterns. + + Parameters: + - where_clause (str): SQL-like condition to filter features. + + Returns: + - Formatted where_clause. + """ pattern = r"tags\['([^']+)'\]" match = re.search(pattern, where_clause)