<jupyter_start><jupyter_text>This code calculates characterization factors for geo units (large countries are divided into the largest administrative units within the country and countries including islands are divided into mainland and islands). The raster-values within a unit are averaged using the rasterstats package and its zonal_stats function.<jupyter_code>import os
from osgeo import gdal, ogr, osr
from rasterstats import zonal_stats
import pandas as pd
import geopandas as gpd
data_dir = os.path.join('..', 'data')
cf_dir = os.path.join('..', 'output/characterization_factors')
out_dir = os.path.join('..', 'output/characterization_factors')
shape = os.path.join(data_dir, 'shapefiles/geo_units.shp')
raster = os.path.join(cf_dir, 'ts_100yrs.tif')
stats = zonal_stats(shape, raster, stats='mean')
ts_100yrs = [i['mean'] for i in stats]
raster = os.path.join(cf_dir, 'ms_100yrs.tif')
stats = zonal_stats(shape, raster, stats='mean')
ms_100yrs = [i['mean'] for i in stats]
raster = os.path.join(cf_dir, 'bs_100yrs.tif')
stats = zonal_stats(shape, raster, stats='mean')
bs_100yrs = [i['mean'] for i in stats]<jupyter_output><empty_output><jupyter_text>Create a geopandas dataframe and add the characterization factors to it:<jupyter_code>gdf = gpd.read_file(shape)
gdf['ts_100yrs'] = ts_100yrs
gdf['ms_100yrs'] = ms_100yrs
gdf['bs_100yrs'] = bs_100yrs
out_filename = 'geo_units_cf.shp'
gdf.to_file(os.path.join(out_dir, out_filename))
a = pd.DataFrame(gdf[['id_name', 'country', 'ts_100yrs', 'ms_100yrs', 'bs_100yrs']])
out_filename = 'geo_units_cf.xlsx'
a.to_excel(os.path.join(out_dir, out_filename))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12865853658536586, 'fraction_numerical': 0.027439024390243903, 'mean_word_length': 6.966019417475728, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '142044', 'license': 'MIT', 'n_tokens_mistral': 646, 'n_tokens_neox': 585, 'n_words': 146, 'path': 'python/6Bb_characterization_factors_100yrs_geo_units.ipynb', 'repo_name': 'ethz-esd/compaction_stoessel_2018', 'size': '1640'}