Commit e35e8483 authored by Cecilia Nievas's avatar Cecilia Nievas
Browse files

Added function to retrieve GHS built-up area from OBM Tiles database

parent 8b416d20
......@@ -27,6 +27,7 @@ Tools used by the GDE code to access/query/write to the OBM extract and cells da
"""
import psycopg2 # Documentation: http://initd.org/psycopg/docs/
import getpass
import numpy as np
import pandas as pd
from copy import deepcopy
......@@ -617,6 +618,49 @@ def get_obm_buildings_by_broad_occupancy(db_name, db_username, sch_name, table_n
return out_broad_occus, out_num_bdgs, gem_occus, num_entries
def get_ghs_built_up_area_from_obm_tiles(grid_cell_id, db_hostname, db_name,
db_username, db_table, source_id=1):
"""This function retrieves the GHS built-up area from the OBM tiles database for a specified
zoom level 18 quadkey.
Note that a password is required to access the database.
Args:
grid_cell_id (str): Quadkey of the zoom level 18 target tile.
db_hostname (str): Name of the server where the database is located.
db_name (str): Name of the database.
db_username (str): Username for access to the database.
db_table (str): Name of the table where this information is stored.
source_id (int): Integer indicating the source ID of the GHS built-up area. Deafault: 1.
Returns:
ghs_built_up (float): Built-up area in the target tile according to the Global Human
Settlement GHSL-BUILT multitemporal dataset available in 30-m
resolution at https://ghsl.jrc.ec.europa.eu/download.php.
"""
# Connect to the database:
connection = psycopg2.connect('host=%s dbname=%s user=%s password=%s'
% (db_hostname, db_name, db_username, getpass.getpass()))
connection.set_session(autocommit=True)
# Open a cursor to perform database operations
cursor = connection.cursor()
# Query the database:
sql_query = "SELECT built_area_size FROM %s WHERE (quadkey='%s' AND source_id=%s);" \
% (db_table, grid_cell_id, source_id)
cursor.execute(sql_query)
exec_result = cursor.fetchall()
connection.close()
if len(exec_result) == 0: # If the quadkey is not found the built-up area is zero
ghs_built_up = 0.0
elif len(exec_result) == 1:
ghs_built_up = exec_result[0][0]
else: # More than one entries found, this is an error
print("ERROR IN get_ghs_built_up_area_from_obm_tiles: MORE THAN ONE ENTRIES FOUND FOR CELL ID %s"
% (grid_cell_id))
ghs_built_up = -999.9
return ghs_built_up
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment