# processes Gridded Livestock of the World data
import os
import sys
import math
import warnings
import yaml
import pandas as pd
import geopandas as gpd
from osgeo import gdal
from pathlib import Path
import glob
import numpy as np
import rasterio.mask
import rasterio
import fiona
from fiona.crs import from_epsg
from pathlib import Path
city = 'Luanda'
data_folder = Path(r'D:\World Bank\CRP\data\Gridded Livestock of the World')
output_folder = Path('output')
livestocks = {'Bf': 'buffaloes',
'Ct': 'cattle',
'Ch': 'chickens',
'Dk': 'ducks',
'Gt': 'goats',
'Ho': 'horses',
'Pg': 'pigs',
'Sh': 'sheep'}
clipped_ls = {}
# read AOI
aoi = gpd.read_file('AOI/luanda.shp').to_crs(epsg = 4326)
for ls in livestocks:
input_raster = data_folder / f'{livestocks[ls]}' / f'5_{ls}_2010_Da.tif'
with rasterio.open(input_raster) as src:
clipped_ls[ls], out_transform = rasterio.mask.mask(
src, aoi.geometry, crop = True)
out_meta = src.meta.copy()
out_meta.update({"driver": "GTiff",
"height": clipped_ls[ls].shape[1],
"width": clipped_ls[ls].shape[2],
"transform": out_transform})
for ls in clipped_ls:
clipped_ls[ls][clipped_ls[ls] == -1.7e+308] = 0
out_image1 = sum(clipped_ls.values())
with rasterio.open(output_folder / f'{city.lower()}_livestock.tif', 'w', **out_meta) as dest:
dest.write(out_image1)