{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "87459711-2cb9-41b8-861d-3fd8477dddea", "metadata": {}, "outputs": [], "source": [ "# processes Gridded Livestock of the World data" ] }, { "cell_type": "code", "execution_count": 1, "id": "4d7af637-4a9e-4155-a627-2bc9262c317d", "metadata": {}, "outputs": [], "source": [ "import os\n", "import sys\n", "import math\n", "import warnings\n", "import yaml\n", "import pandas as pd\n", "import geopandas as gpd\n", "from osgeo import gdal\n", "from pathlib import Path\n", "import glob\n", "import numpy as np\n", "import rasterio.mask\n", "import rasterio\n", "import fiona\n", "from fiona.crs import from_epsg\n", "from pathlib import Path\n", "import folium\n", "# from folium import plugins\n", "# from matplotlib import cm\n", "# from rasterio import warp\n", "# from rasterio.enums import Resampling\n", "from rasterio.features import shapes\n", "from branca.colormap import linear" ] }, { "cell_type": "code", "execution_count": 2, "id": "89daafa1-b304-4c86-a820-6a8ae2527f33", "metadata": {}, "outputs": [], "source": [ "city = 'Luanda'" ] }, { "cell_type": "markdown", "id": "f52418e9-b7ca-48a9-9f29-fd634bd4efb2", "metadata": { "tags": [] }, "source": [ "### Data processing" ] }, { "cell_type": "code", "execution_count": 3, "id": "3c69fdbe-378f-49b5-bd0b-ff7c5cefd2ac", "metadata": {}, "outputs": [], "source": [ "data_folder = Path(r'D:\\World Bank\\CRP\\data\\Gridded Livestock of the World')\n", "output_folder = Path('output')\n", "livestocks = {'Bf': 'buffaloes',\n", " 'Ct': 'cattle',\n", " 'Ch': 'chickens',\n", " 'Dk': 'ducks',\n", " 'Gt': 'goats',\n", " 'Ho': 'horses',\n", " 'Pg': 'pigs',\n", " 'Sh': 'sheep'}\n", "clipped_ls = {}" ] }, { "cell_type": "code", "execution_count": 4, "id": "1183ffb6-9018-423d-ac0f-df029e3ce14e", "metadata": {}, "outputs": [], "source": [ "# read AOI\n", "aoi = gpd.read_file('AOI/luanda.shp').to_crs(epsg = 4326)" ] }, { "cell_type": "code", "execution_count": 49, "id": "ede72db8-8191-49e9-b912-cd80c277d68d", "metadata": {}, "outputs": [], "source": [ "for ls in livestocks:\n", " input_raster = data_folder / f'{livestocks[ls]}' / f'5_{ls}_2010_Da.tif'\n", " with rasterio.open(input_raster) as src:\n", " clipped_ls[ls], out_transform = rasterio.mask.mask(\n", " src, aoi.geometry, crop = True, all_touched = True)\n", " out_meta = src.meta.copy()\n", "\n", " out_meta.update({\"driver\": \"GTiff\",\n", " \"height\": clipped_ls[ls].shape[1],\n", " \"width\": clipped_ls[ls].shape[2],\n", " \"transform\": out_transform})" ] }, { "cell_type": "code", "execution_count": 50, "id": "b2e15539-e813-499c-8ee9-0201de20c4a0", "metadata": {}, "outputs": [], "source": [ "for ls in clipped_ls:\n", " clipped_ls[ls][clipped_ls[ls] == -1.7e+308] = 0\n", "\n", "out_image1 = sum(clipped_ls.values())" ] }, { "cell_type": "code", "execution_count": 51, "id": "6c4d9aee-2460-4bbb-96d6-527cc0e3f25a", "metadata": {}, "outputs": [], "source": [ "with rasterio.open(output_folder / f'{city.lower()}_livestock.tif', 'w', **out_meta) as dest:\n", " dest.write(out_image1)" ] }, { "cell_type": "markdown", "id": "920e8803-4faf-47f8-acaa-8e67628fd85a", "metadata": {}, "source": [ "### Data conversion to JSON" ] }, { "cell_type": "code", "execution_count": 37, "id": "1945f9af-38d7-4bea-a47b-022d19288971", "metadata": {}, "outputs": [], "source": [ "with rasterio.open(output_folder / f'{city.lower()}_livestock.tif') as src:\n", " image = np.float32(src.read(1))\n", " results = ({'properties': {'raster_val': np.float32(v)}, 'geometry': s} for i, (s, v) in enumerate(shapes(image, mask = None, transform = src.transform)))\n", " geoms = list(results)\n", " gpd_polygonized_raster = gpd.GeoDataFrame.from_features(geoms)\n", " gpd_polygonized_raster = gpd_polygonized_raster[gpd_polygonized_raster.raster_val != 0]\n", "\n", "gpd_polygonized_raster.to_file(output_folder / f'{city.lower()}_livestock.geojson', driver = 'GeoJSON', crs = 'EPSG:4326')\n", "# gpd_polygonized_raster = gpd_polygonized_raster.to_crs(epsg = 4326)" ] }, { "cell_type": "markdown", "id": "bc1dff34-3ca3-4a17-9f8b-9263449c90d1", "metadata": {}, "source": [ "### Map with JSON" ] }, { "cell_type": "code", "execution_count": 5, "id": "bec67645-7f4b-41a6-8440-ba86c99dd4b0", "metadata": {}, "outputs": [], "source": [ "glw = gpd.read_file(output_folder / f'{city.lower()}_livestock.geojson').to_crs('epsg:4326')" ] }, { "cell_type": "code", "execution_count": 6, "id": "79d56909-7bdf-4336-94c6-3116ede5fbbf", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Create a folium map centered on the AOI\n", "mymap = folium.Map(control_scale = True)\n", "aoi_bounds = aoi.total_bounds.tolist()\n", "mymap.fit_bounds([aoi_bounds[:2][::-1], aoi_bounds[2:][::-1]])\n", "\n", "folium.GeoJson(\n", " aoi,\n", " style_function=lambda feature: {\n", " 'fillColor': 'transparent',\n", " 'color': 'gray',\n", " 'weight': 3\n", " },\n", " control = False).add_to(mymap)" ] }, { "cell_type": "code", "execution_count": 7, "id": "cb26a4dc-0bc8-4e00-8291-56b3bf830aad", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Create a colormap based on the 'YlOrRd' scale and the 'raster_val' column\n", "colormap = linear.YlOrRd_09.scale(glw['raster_val'].min(), glw['raster_val'].max())\n", "\n", "# Add GeoJson layer with choropleth style\n", "folium.GeoJson(\n", " glw,\n", " name='Number of livestocks',\n", " style_function=lambda feature: {\n", " 'fillColor': colormap(feature['properties']['raster_val']),\n", " 'fillOpacity': 0.7,\n", " 'weight': 0,\n", " },\n", " highlight_function=lambda x: {'fillOpacity': 0.9},\n", " tooltip=folium.GeoJsonTooltip(fields=['raster_val'], aliases = ['Number of livestocks'], labels=True, sticky=False),\n", ").add_to(mymap)\n", "\n", "# Add Layer Control to toggle the choropleth layer\n", "folium.LayerControl().add_to(mymap)" ] }, { "cell_type": "code", "execution_count": 8, "id": "5f821788-2a4c-41ef-a9ca-85b71be94eab", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mymap" ] }, { "cell_type": "code", "execution_count": 90, "id": "32f5a23b-5999-467d-9988-2fb801433503", "metadata": {}, "outputs": [], "source": [ "mymap.save(html_folder / \"glw.html\")" ] }, { "cell_type": "markdown", "id": "f55128af-a23c-47e1-ba21-27671e3d070c", "metadata": { "jp-MarkdownHeadingCollapsed": true, "tags": [] }, "source": [ "### Data conversion to PNG (deprecated)" ] }, { "cell_type": "markdown", "id": "720250cb-514f-4557-91bf-b98ebdf7629c", "metadata": {}, "source": [ "Convert to PNG to enable folium visualization" ] }, { "cell_type": "code", "execution_count": 28, "id": "0eedc613-9378-4c5d-ba3f-da16af494a13", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'driver': 'PNG', 'dtype': 'uint8', 'nodata': -1.7e+308, 'width': 9, 'height': 10, 'count': 1, 'crs': CRS.from_epsg(4326), 'transform': Affine(0.08333333333333333, 0.0, 12.916666666666657,\n", " 0.0, -0.08333333333333333, -8.583333333333329)}\n", "[[ 0 0 0 0 0 0 38 2 0]\n", " [ 0 0 0 0 0 0 63 4 0]\n", " [ 0 0 0 0 172 255 84 24 7]\n", " [ 0 0 0 167 167 239 53 59 2]\n", " [ 0 0 99 165 115 207 71 2 0]\n", " [ 0 0 4 29 34 44 35 0 0]\n", " [ 0 3 3 3 4 4 34 0 0]\n", " [ 0 0 5 3 0 0 0 0 0]\n", " [ 0 0 4 0 0 0 0 0 0]\n", " [ 0 0 0 0 0 0 0 0 0]]\n", "0\n", "255\n" ] } ], "source": [ "with rasterio.open(output_folder / f'{city.lower()}_livestock.tif') as src:\n", " Z = src.read(1)\n", " min_val = np.min(Z)\n", " max_val = np.max(Z)\n", " scaled_array = 255 * (Z - min_val) / (max_val - min_val)\n", " uint8_array = scaled_array.round().astype(np.uint8)\n", " \n", " meta = src.meta\n", " meta['driver'] = 'PNG'\n", " meta['dtype'] = 'uint8'\n", " \n", " bounds = src.bounds # used in folium visualization later\n", "\n", "with rasterio.open(output_folder / f'{city.lower()}_livestock.png', 'w', **meta) as dst:\n", " dst.write(uint8_array, 1)\n", " # dst.write_colormap(\n", " # 1, {\n", " # np.min(uint8_array): (255, 245, 235, 255),\n", " # np.max(uint8_array): (127,39,4, 255) })" ] }, { "cell_type": "markdown", "id": "5b4b349c-7da2-425b-80de-bfa2f3d1492b", "metadata": { "jp-MarkdownHeadingCollapsed": true, "tags": [] }, "source": [ "### Map with PNG (deprecated)" ] }, { "cell_type": "code", "execution_count": 93, "id": "d9a8ae33-426b-43c0-9e50-430e54921180", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 93, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Create a folium map centered on the AOI\n", "mymap = folium.Map(control_scale = True)\n", "aoi_bounds = aoi.total_bounds.tolist()\n", "mymap.fit_bounds([aoi_bounds[:2][::-1], aoi_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)" ] }, { "cell_type": "code", "execution_count": 94, "id": "7936f8a7-1afa-49f8-bc6e-e4da248dddd7", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 94, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Add the raster image as an overlay\n", "folium.raster_layers.ImageOverlay(\n", " image=uint8_array,\n", " bounds=[(bounds[1], bounds[0]), (bounds[3], bounds[2])],\n", " colormap=lambda x: (255,0,0,x),\n", " # colormap=cm.viridis,\n", " opacity=0.8,\n", ").add_to(mymap)" ] }, { "cell_type": "code", "execution_count": 95, "id": "c3284ce0-3ad3-4c64-8bf8-df56fcd72ddd", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 95, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Add legend manually\n", "legend_html = f\"\"\"\n", "
\n", "

Livestock Counts

\n", "
\n", "
\n", " 0\n", " {\"{:,.0f}\".format(max_val)}\n", "
\n", "
\n", "\"\"\"\n", "\n", "mymap.get_root().html.add_child(folium.Element(legend_html))" ] }, { "cell_type": "code", "execution_count": 96, "id": "bfeb34d2-c991-430e-ba85-6f78a9f7f9cd", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 96, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mymap" ] } ], "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 }