From 3a8c982a3bbd5167a6b4cd5873f3b25c4f641843 Mon Sep 17 00:00:00 2001 From: SofiaNordenving <107103836+SofiaNordenving@users.noreply.github.com> Date: Thu, 16 Nov 2023 18:08:01 +0100 Subject: [PATCH 1/2] Bbox for ME BBox for ME --- BBoxWriter.py | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 BBoxWriter.py diff --git a/BBoxWriter.py b/BBoxWriter.py new file mode 100644 index 0000000..19668ac --- /dev/null +++ b/BBoxWriter.py @@ -0,0 +1,79 @@ +""" +Writes or references some easy BBox for use +""" + +""" +Definition of Africa and Africa + Middle East bbox'es based on Remco procedure +""" +import geopandas as gpd +import sqlalchemy as sa +from ingester3.config import source_db_path + +def bbox_from_cid(country_id): + """ + This is a function to find the country polygons based on country_id + Bounding boxes also available for africa and africa+middle east regions + :param country_id: 'africa', 'ame' or a country_id as number + :return: bounding box rounded to 1 decimal point + """ + GLOBE = [-180, 210, -64, 84] + BBOX_AFRICA = [-18.5, 52.0, -35.5, 38.0] + BBOX_AME = [-18.5, 64.0, -35.5, 43.0] + BBOX_ME= [19.0, 64.0, 11.0, 43.0] + + raise ValueError("This code is actually in use") + + bbox = None + country_id = country_id.lower() + + if country_id == 'africa': + bbox = BBOX_AFRICA + if country_id == 'globe': + bbox = GLOBE + if country_id == "ame": + bbox = BBOX_AME + if country_id == "me": + bbox = BBOX_ME + if bbox == None: + engine = sa.create_engine(source_db_path) + gdf_master = gpd.GeoDataFrame.from_postgis("SELECT id as country_id, geom FROM prod.country", engine, + geom_col='geom') + gdf_master = gdf_master.to_crs(4326) + multipolygon = gdf_master.loc[gdf_master['country_id'] == country_id].geom + bbox1 = multipolygon.total_bounds + bbox = [round((bbox1[0] - 0.1), 2), round((bbox1[2] + 0.1), 2), round((bbox1[1] - 0.1), 2), + round((bbox1[3] + 0.1), 2)] + + return bbox + +def bbox_from_cid_region(country_id): + """ + This is a function to find the larger bbox based on country_id + Bounding boxes also available for africa and africa+middle east regions + :param country_id: 'africa', 'ame' or a country_id as number + :return: bounding box rounded to 1 decimal point + """ + GLOBE = [-180, 210, -64, 84] + BBOX_AFRICA = [-18.5, 52.0, -35.5, 38.0] + BBOX_AME = [-18.5, 64.0, -35.5, 43.0] + BBOX_ME= [19.0, 64.0, 11.0, 43.0] + + country_id = country_id.lower() + + if country_id == 'africa': + bbox = BBOX_AFRICA + elif country_id == 'globe': + bbox = GLOBE + elif country_id == "ame": + bbox = BBOX_AME + elif country_id == "me": + bbox = BBOX_ME + else: + engine = sa.create_engine(source_db_path) + gdf_master = gpd.GeoDataFrame.from_postgis("SELECT id as country_id, geom FROM prod.country", engine, + geom_col='geom') + gdf_master = gdf_master.to_crs(4326) + multipolygon = gdf_master.loc[gdf_master['country_id'] == country_id].geom + bbox1 = multipolygon.total_bounds + bbox = [round((bbox1[0]-3), 1), round((bbox1[2]+3), 1), round((bbox1[1]-3), 1), round((bbox1[3]+3), 1)] + return bbox \ No newline at end of file From 288042b096a068a74f3fbfdebd50e3e1e8e10174 Mon Sep 17 00:00:00 2001 From: SofiaNordenving <107103836+SofiaNordenving@users.noreply.github.com> Date: Thu, 16 Nov 2023 22:02:34 +0100 Subject: [PATCH 2/2] Update BBoxWriter.py --- BBoxWriter.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/BBoxWriter.py b/BBoxWriter.py index 19668ac..0df3012 100644 --- a/BBoxWriter.py +++ b/BBoxWriter.py @@ -21,10 +21,8 @@ def bbox_from_cid(country_id): BBOX_AME = [-18.5, 64.0, -35.5, 43.0] BBOX_ME= [19.0, 64.0, 11.0, 43.0] - raise ValueError("This code is actually in use") - bbox = None - country_id = country_id.lower() +# country_id = country_id.lower() if country_id == 'africa': bbox = BBOX_AFRICA @@ -58,7 +56,7 @@ def bbox_from_cid_region(country_id): BBOX_AME = [-18.5, 64.0, -35.5, 43.0] BBOX_ME= [19.0, 64.0, 11.0, 43.0] - country_id = country_id.lower() +# country_id = country_id.lower() if country_id == 'africa': bbox = BBOX_AFRICA @@ -76,4 +74,4 @@ def bbox_from_cid_region(country_id): multipolygon = gdf_master.loc[gdf_master['country_id'] == country_id].geom bbox1 = multipolygon.total_bounds bbox = [round((bbox1[0]-3), 1), round((bbox1[2]+3), 1), round((bbox1[1]-3), 1), round((bbox1[3]+3), 1)] - return bbox \ No newline at end of file + return bbox