Utils

night_light.util_duckdb.abs_path(relative_path: str) str

Define an absolute path for file

Parameters:

relative_path (str) – a string of a file name

Return type:

str

Returns:

A string that is a full file path in the user’s directory

night_light.util_duckdb.connect_to_duckdb(db_path: str) DuckDBPyConnection

Establish a connection to a DuckDB database and load the spatial extension.

Parameters:

db_path (str) – Path to the DuckDB database file.

Returns:

Connection to the DuckDB database.

Return type:

duckdb.DuckDBPyConnection

night_light.util_duckdb.load_data_to_table(con: DuckDBPyConnection, data_source: str | GeoDataFrame, table_name: str) None

Load GeoJSON or GeoDataFrame into DuckDB.

Parameters:
  • con (duckdb.DuckDBPyConnection) – Connection to the DuckDB database.

  • data_source (Union[str, GeoDataFrame]) – Path to a GeoJSON file or a GeoDataFrame.

  • table_name (str) – Name of the target table.

Return type:

None

night_light.util_duckdb.load_multiple_datasets(con: DuckDBPyConnection, datasets: List[Tuple[str | GeoDataFrame, str]]) None

Load multiple GeoJSON files or GeoDataFrames into DuckDB.

Parameters:
  • con (duckdb.DuckDBPyConnection) – Connection to the DuckDB database.

  • datasets (List[Tuple[Union[str, GeoDataFrame], str]]) – List of tuples where each tuple contains: - data_source (Union[str, GeoDataFrame]): Path to a GeoJSON file or a GeoDataFrame. - table_name (str): Name of the target table.

Return type:

None

night_light.util_duckdb.query_table_to_gdf(con: DuckDBPyConnection, table_name: str, query: str = None) GeoDataFrame

Query a DuckDB table and return the results as a GeoPandas DataFrame.

Parameters:
  • con (duckdb.DuckDBPyConnection) – Connection to the DuckDB database.

  • table_name (str) – Name of the table to query.

  • query (Optional[str]) – SQL query to execute. Default is to fetch the first 10 rows.

Returns:

Results of the query.

Return type:

GeoDataFrame

night_light.util_duckdb.save_table_to_csv(con: DuckDBPyConnection, table_name: str, filename: str) None

Save a DuckDB table to a csv file.

Parameters:
  • con (duckdb.DuckDBPyConnection) – Connection to the DuckDB database.

  • table_name (str) – Name of the table to save.

  • filename (str) – Path to the output csv file.

Return type:

None

night_light.util_duckdb.save_table_to_geojson(con: DuckDBPyConnection, table_name: str, filename: str) None

Save a DuckDB table to a GeoJSON file.

Parameters:
  • con (duckdb.DuckDBPyConnection) – Connection to the DuckDB database.

  • table_name (str) – Name of the table to save.

  • filename (str) – Path to the output GeoJSON file.

Return type:

None

night_light.util_duckdb.save_table_to_parquet(con: DuckDBPyConnection, table_name: str, filename: str) None

Save a DuckDB table to a parquet file.

Parameters:
  • con (duckdb.DuckDBPyConnection) – Connection to the DuckDB database.

  • table_name (str) – Name of the table to save.

  • filename (str) – Path to the output parquet file.

Return type:

None