{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "266d01e3-75cf-44c0-b648-a5551f3e7c6e", "metadata": {}, "outputs": [], "source": [ "import geopandas as gpd\n", "from pathlib import Path\n", "import pandas as pd\n", "import matplotlib.pyplot as plt\n", "import contextily as ctx\n", "import folium" ] }, { "cell_type": "markdown", "id": "a41d8453-f21e-45c6-b9ba-81b728a7200c", "metadata": {}, "source": [ "## Data processing" ] }, { "cell_type": "code", "execution_count": 2, "id": "a24db3f2-c414-4664-ab23-c30a77547be2", "metadata": {}, "outputs": [], "source": [ "city = 'Luanda'" ] }, { "cell_type": "code", "execution_count": 3, "id": "53e8bce0-a7c8-406e-bccb-98451d20c9b8", "metadata": {}, "outputs": [], "source": [ "# read catchment AOI\n", "aoi = gpd.read_file('AOI/luanda_catchment_level4.shp').to_crs(epsg = 4326)" ] }, { "cell_type": "code", "execution_count": 5, "id": "bdb98278-1faa-4e1e-9ec5-7efd08a327d1", "metadata": {}, "outputs": [], "source": [ "output_folder = Path('output')\n", "html_folder = Path('html')" ] }, { "cell_type": "code", "execution_count": 11, "id": "60841d8d-81c3-4b8e-ab29-dd4b867e3084", "metadata": {}, "outputs": [], "source": [ "dam_data = gpd.read_file(r\"C:\\Users\\Owner\\OneDrive\\Documents\\Career\\World Bank\\CRP\\data\\GOODD\\GOOD2_dams.shp\")\n", "reservoir_data = gpd.read_file(r\"D:\\World Bank\\CRP\\data\\GRanD\\GRanD_reservoirs_v1_3.shp\")" ] }, { "cell_type": "code", "execution_count": 12, "id": "abd0f218-975d-4f6f-ba0d-93dac4b17cb2", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Index(['DAM_ID', 'Count_ID', 'Latitud', 'Longitud', 'geometry'], dtype='object')" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dam_data.columns" ] }, { "cell_type": "code", "execution_count": 13, "id": "ddecea9a-2ec4-4aba-8366-20819bb46e28", "metadata": {}, "outputs": [], "source": [ "# filter for dams in AOI\n", "dams_list = []\n", "\n", "for i in range(len(aoi)):\n", " dams_list.append(dam_data.loc[dam_data.within(aoi.loc[i, 'geometry'])])" ] }, { "cell_type": "code", "execution_count": 14, "id": "0cb6272c-b05c-4d50-a638-f029a91bc862", "metadata": {}, "outputs": [], "source": [ "dams = pd.concat(dams_list)" ] }, { "cell_type": "code", "execution_count": 8, "id": "4ec36a66-1490-482e-9fcb-6ba2fb49306f", "metadata": {}, "outputs": [], "source": [ "dams.to_file(output_folder / f'{city.lower()}_dams.shp')" ] }, { "cell_type": "code", "execution_count": 15, "id": "c3c3b70a-6b7c-412a-b703-f57a232298aa", "metadata": {}, "outputs": [], "source": [ "# filter for reservoirs in AOI\n", "reservoirs_list = []\n", "\n", "for i in range(len(aoi)):\n", " reservoirs_list.append(reservoir_data.loc[reservoir_data.within(aoi.loc[i, 'geometry'])])" ] }, { "cell_type": "code", "execution_count": 16, "id": "aefc583c-6804-4304-b890-90dd1af2ad5b", "metadata": {}, "outputs": [], "source": [ "reservoirs = pd.concat(reservoirs_list)" ] }, { "cell_type": "code", "execution_count": 11, "id": "5058abf7-994f-44b2-b1e3-c3effc32dde4", "metadata": {}, "outputs": [], "source": [ "reservoirs.to_file(output_folder / f'{city.lower()}_reservoirs.shp')" ] }, { "cell_type": "markdown", "id": "20a24566-6612-4cd8-8410-11bd49bf772e", "metadata": {}, "source": [ "## Visualization" ] }, { "cell_type": "code", "execution_count": 17, "id": "f0ecf7d0-ee19-4c8b-9413-1bcf3f21b817", "metadata": {}, "outputs": [], "source": [ "dams['DAM_ID'] = dams['DAM_ID'].astype('int').astype('string')" ] }, { "cell_type": "code", "execution_count": 18, "id": "842705c0-3b92-4a01-894a-1fe544873d69", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
GRAND_IDRES_NAMEDAM_NAMEALT_NAMERIVERALT_RIVERMAIN_BASINSUB_BASINNEAR_CITYALT_CITY...MULTI_DAMSTIMELINECOMMENTSURLQUALITYEDITORLONG_DDLAT_DDPOLY_SRCgeometry
36723676NaNQuiminhaNaNBengoNaNSouth West CoastBengoCateteNaN...NaNNaNNaNNaN3: FairMcGill-BL13.790871-8.963743SWBDPOLYGON ((14.04531 -8.89675, 14.04585 -8.89781...
\n", "

1 rows × 59 columns

\n", "
" ], "text/plain": [ " GRAND_ID RES_NAME DAM_NAME ALT_NAME RIVER ALT_RIVER MAIN_BASIN \\\n", "3672 3676 NaN Quiminha NaN Bengo NaN South West Coast \n", "\n", " SUB_BASIN NEAR_CITY ALT_CITY ... MULTI_DAMS TIMELINE COMMENTS URL \\\n", "3672 Bengo Catete NaN ... NaN NaN NaN NaN \n", "\n", " QUALITY EDITOR LONG_DD LAT_DD POLY_SRC \\\n", "3672 3: Fair McGill-BL 13.790871 -8.963743 SWBD \n", "\n", " geometry \n", "3672 POLYGON ((14.04531 -8.89675, 14.04585 -8.89781... \n", "\n", "[1 rows x 59 columns]" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "reservoirs" ] }, { "cell_type": "code", "execution_count": 19, "id": "cfd5a4b0-4768-44d8-b651-7e2cb1c27325", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Create a folium map centered on the AOI\n", "mymap = folium.Map(control_scale = True)\n", "bounds = aoi.total_bounds.tolist()\n", "mymap.fit_bounds([bounds[:2][::-1], bounds[2:][::-1]])\n", "\n", "folium.GeoJson(\n", " aoi,\n", " style_function=lambda feature: {\n", " 'fillColor': 'transparent',\n", " 'color': 'gray',\n", " 'weight': 3\n", " },).add_to(mymap)\n", "\n", "res_tooltip = folium.GeoJsonTooltip(fields=['RES_NAME'], aliases=['Reservoir Name'], localize=True, style=('background-color: white; color: #333333;'))\n", "dam_tooltip = folium.GeoJsonTooltip(fields=['DAM_ID'], aliases=['Dam ID'], localize=True, style=('background-color: white; color: #333333;'))\n", "\n", "# Add blue polygons for reservoirs\n", "folium.GeoJson(\n", " reservoirs,\n", " style_function=lambda feature: {\n", " 'fillColor': 'blue',\n", " 'color': 'blue',\n", " 'weight': 1,\n", " },\n", " highlight_function=lambda x: {\"fillOpacity\": 0.7},\n", " tooltip=res_tooltip,\n", " zoom_on_click=True\n", ").add_to(mymap)\n", "\n", "# Add CircleMarkers for dams\n", "folium.GeoJson(\n", " dams,\n", " name=\"Dams\",\n", " marker=folium.CircleMarker(radius=8, fill_color=\"brown\", fill_opacity=0.4, color=\"brown\", weight=1),\n", " tooltip = dam_tooltip,\n", " highlight_function=lambda x: {\"fillOpacity\": 0.7},\n", " zoom_on_click=True,\n", ").add_to(mymap)\n", "\n", "# Add legend manually\n", "legend_html = '''\n", "
  Legend
\n", "   Reservoirs   \n", " \n", " \n", "
\n", "   Dams   \n", " \n", " \n", " \n", "
\n", " '''\n", "\n", "mymap.get_root().html.add_child(folium.Element(legend_html))\n", "\n", "mymap" ] }, { "cell_type": "code", "execution_count": 20, "id": "28add97a-a56e-4f90-9e27-bcf84b1e5ea6", "metadata": {}, "outputs": [], "source": [ "mymap.save(html_folder / \"dam_reservoir.html\")" ] } ], "metadata": { "kernelspec": { "display_name": "crp", "language": "python", "name": "crp" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.12" } }, "nbformat": 4, "nbformat_minor": 5 }