{ "cells": [ { "cell_type": "code", "execution_count": null, "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" ] }, { "cell_type": "code", "execution_count": 26, "id": "89daafa1-b304-4c86-a820-6a8ae2527f33", "metadata": {}, "outputs": [], "source": [ "city = 'Luanda'" ] }, { "cell_type": "code", "execution_count": 18, "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": 3, "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": 19, "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)\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": 20, "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": 27, "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)" ] } ], "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 }