notebooks/stats_tables/avg_area_table.ipynb (454 lines of code) (raw):
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Average H3 cell areas\n",
"\n",
"H3 covers the sphere with both hexagon and pentagon cells.\n",
"Most of the time you'll only encounter hexagon cells because there are only\n",
"12 pentagons at each resolution, and they're all positioned to be in\n",
"the ocean.\n",
"\n",
"Within a resolution, cells vary in size depending on where they are on the globe.\n",
"We'll use this notebook to compute some statistics for the sizes of the hexagon\n",
"and pentagon cells."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import h3\n",
"from tabulate import tabulate\n",
"\n",
"def num_hexagons(res):\n",
" 'Number of *hexagons* (excluding pentagons) at a resolution'\n",
" return h3.num_hexagons(res) - 12 # function name to be fixed in 4.0 release\n",
"\n",
"def earth_area(unit='km^2'):\n",
" cells = h3.get_res0_indexes()\n",
" \n",
" return sum(h3.cell_area(c, unit) for c in cells)\n",
"\n",
"def pentagon_area_total(res, unit='km^2'):\n",
" 'Area covered by all the pentagons at a resolution'\n",
" pentagons = h3.get_pentagon_indexes(res)\n",
"\n",
" return sum(h3.cell_area(c, unit) for c in pentagons)\n",
"\n",
"def pentagon_area(res, unit='km^2'):\n",
" 'All pentagons at a resolution have the same area'\n",
" return pentagon_area_total(res, unit)/12\n",
"\n",
"def hexagon_area_avg(res, unit='km^2'):\n",
" A = earth_area(unit) - pentagon_area_total(res, unit)\n",
" A = A/num_hexagons(res)\n",
" \n",
" return A\n",
"\n",
"def compute_rows(unit='km^2'):\n",
" \"\"\"\n",
" For each resolution yield:\n",
" - resolution\n",
" - average *hex* area\n",
" - *uniform* pentagon area\n",
" - ratio of pentagon/hex areas\n",
" \"\"\"\n",
" for res in range(16):\n",
" h = hexagon_area_avg(res, unit)\n",
" p = pentagon_area(res, unit)\n",
"\n",
" yield res, h, p, p/h"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[(0, ' 4,357,449.416078383', ' 2,562,182.162955496', '0.5880'),\n",
" (1, ' 609,788.441794133', ' 328,434.586246469', '0.5386'),\n",
" (2, ' 86,801.780398997', ' 44,930.898497879', '0.5176'),\n",
" (3, ' 12,393.434655088', ' 6,315.472267516', '0.5096'),\n",
" (4, ' 1,770.347654491', ' 896.582383141', '0.5064'),\n",
" (5, ' 252.903858182', ' 127.785583023', '0.5053'),\n",
" (6, ' 36.129062164', ' 18.238749548', '0.5048'),\n",
" (7, ' 5.161293360', ' 2.604669397', '0.5047'),\n",
" (8, ' 0.737327598', ' 0.372048038', '0.5046'),\n",
" (9, ' 0.105332513', ' 0.053147195', '0.5046'),\n",
" (10, ' 0.015047502', ' 0.007592318', '0.5046'),\n",
" (11, ' 0.002149643', ' 0.001084609', '0.5046'),\n",
" (12, ' 0.000307092', ' 0.000154944', '0.5046'),\n",
" (13, ' 0.000043870', ' 0.000022135', '0.5046'),\n",
" (14, ' 0.000006267', ' 0.000003162', '0.5046'),\n",
" (15, ' 0.000000895', ' 0.000000452', '0.5046')]"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"float_fmt = '{:20,.9f}'\n",
"ratio_fmt = '{:.4f}'\n",
"\n",
"def fmt_float(x):\n",
" s = float_fmt\n",
" return s.format(x)\n",
"\n",
"def fmt_ratio(x):\n",
" s = ratio_fmt\n",
" return s.format(x)\n",
"\n",
"fmt_stats = [\n",
" (a, fmt_float(b), fmt_float(c), fmt_ratio(d))\n",
" for a,b,c,d in compute_rows('km^2')\n",
"]\n",
"\n",
"fmt_stats"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"| Res | Average <ins>Hexagon</ins> Area (km<sup>2</sup>) | Pentagon Area* (km<sup>2</sup>) | Ratio (P/H) |\n",
"|------:|---------------------------------------------------:|----------------------------------:|--------------:|\n",
"| 0 | 4,357,449.416078383 | 2,562,182.162955496 | 0.5880 |\n",
"| 1 | 609,788.441794133 | 328,434.586246469 | 0.5386 |\n",
"| 2 | 86,801.780398997 | 44,930.898497879 | 0.5176 |\n",
"| 3 | 12,393.434655088 | 6,315.472267516 | 0.5096 |\n",
"| 4 | 1,770.347654491 | 896.582383141 | 0.5064 |\n",
"| 5 | 252.903858182 | 127.785583023 | 0.5053 |\n",
"| 6 | 36.129062164 | 18.238749548 | 0.5048 |\n",
"| 7 | 5.161293360 | 2.604669397 | 0.5047 |\n",
"| 8 | 0.737327598 | 0.372048038 | 0.5046 |\n",
"| 9 | 0.105332513 | 0.053147195 | 0.5046 |\n",
"| 10 | 0.015047502 | 0.007592318 | 0.5046 |\n",
"| 11 | 0.002149643 | 0.001084609 | 0.5046 |\n",
"| 12 | 0.000307092 | 0.000154944 | 0.5046 |\n",
"| 13 | 0.000043870 | 0.000022135 | 0.5046 |\n",
"| 14 | 0.000006267 | 0.000003162 | 0.5046 |\n",
"| 15 | 0.000000895 | 0.000000452 | 0.5046 |\n",
"\n",
"*: Within a given resolution, all pentagons have the same area.\n"
]
}
],
"source": [
"headers = [\n",
" 'Res',\n",
" 'Average <ins>Hexagon</ins> Area (km<sup>2</sup>)',\n",
" 'Pentagon Area* (km<sup>2</sup>)',\n",
" 'Ratio (P/H)'\n",
"]\n",
"out = tabulate(fmt_stats, headers=headers, tablefmt='pipe', stralign='right', disable_numparse=True)\n",
"\n",
"out += '\\n\\n*: Within a given resolution, all pentagons have the same area.'\n",
"print(out)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
"| Res | Average <ins>Hexagon</ins> Area (km<sup>2</sup>) | Pentagon Area* (km<sup>2</sup>) | Ratio (P/H) |\n",
"|------:|---------------------------------------------------:|----------------------------------:|--------------:|\n",
"| 0 | 4,357,449.416078383 | 2,562,182.162955496 | 0.5880 |\n",
"| 1 | 609,788.441794133 | 328,434.586246469 | 0.5386 |\n",
"| 2 | 86,801.780398997 | 44,930.898497879 | 0.5176 |\n",
"| 3 | 12,393.434655088 | 6,315.472267516 | 0.5096 |\n",
"| 4 | 1,770.347654491 | 896.582383141 | 0.5064 |\n",
"| 5 | 252.903858182 | 127.785583023 | 0.5053 |\n",
"| 6 | 36.129062164 | 18.238749548 | 0.5048 |\n",
"| 7 | 5.161293360 | 2.604669397 | 0.5047 |\n",
"| 8 | 0.737327598 | 0.372048038 | 0.5046 |\n",
"| 9 | 0.105332513 | 0.053147195 | 0.5046 |\n",
"| 10 | 0.015047502 | 0.007592318 | 0.5046 |\n",
"| 11 | 0.002149643 | 0.001084609 | 0.5046 |\n",
"| 12 | 0.000307092 | 0.000154944 | 0.5046 |\n",
"| 13 | 0.000043870 | 0.000022135 | 0.5046 |\n",
"| 14 | 0.000006267 | 0.000003162 | 0.5046 |\n",
"| 15 | 0.000000895 | 0.000000452 | 0.5046 |\n",
"\n",
"*: Within a given resolution, all pentagons have the same area."
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from IPython.display import Markdown\n",
"Markdown(out)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Now in m^2 units"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[(0, '4,357,449,416,078.392', '2,562,182,162,955.496'),\n",
" (1, ' 609,788,441,794.134', ' 328,434,586,246.469'),\n",
" (2, ' 86,801,780,398.997', ' 44,930,898,497.879'),\n",
" (3, ' 12,393,434,655.088', ' 6,315,472,267.516'),\n",
" (4, ' 1,770,347,654.491', ' 896,582,383.141'),\n",
" (5, ' 252,903,858.182', ' 127,785,583.023'),\n",
" (6, ' 36,129,062.164', ' 18,238,749.548'),\n",
" (7, ' 5,161,293.360', ' 2,604,669.397'),\n",
" (8, ' 737,327.598', ' 372,048.038'),\n",
" (9, ' 105,332.513', ' 53,147.195'),\n",
" (10, ' 15,047.502', ' 7,592.318'),\n",
" (11, ' 2,149.643', ' 1,084.609'),\n",
" (12, ' 307.092', ' 154.944'),\n",
" (13, ' 43.870', ' 22.135'),\n",
" (14, ' 6.267', ' 3.162'),\n",
" (15, ' 0.895', ' 0.452')]"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"float_fmt = '{:20,.3f}'\n",
"\n",
"def fmt_float(x):\n",
" s = float_fmt\n",
" return s.format(x)\n",
"\n",
"fmt_stats = [\n",
" (a, fmt_float(b), fmt_float(c))\n",
" for a,b,c,_ in compute_rows('m^2')\n",
"]\n",
"\n",
"fmt_stats"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"| Res | Average <ins>Hexagon</ins> Area (m<sup>2</sup>) | Pentagon Area* (m<sup>2</sup>) |\n",
"|------:|--------------------------------------------------:|---------------------------------:|\n",
"| 0 | 4,357,449,416,078.392 | 2,562,182,162,955.496 |\n",
"| 1 | 609,788,441,794.134 | 328,434,586,246.469 |\n",
"| 2 | 86,801,780,398.997 | 44,930,898,497.879 |\n",
"| 3 | 12,393,434,655.088 | 6,315,472,267.516 |\n",
"| 4 | 1,770,347,654.491 | 896,582,383.141 |\n",
"| 5 | 252,903,858.182 | 127,785,583.023 |\n",
"| 6 | 36,129,062.164 | 18,238,749.548 |\n",
"| 7 | 5,161,293.360 | 2,604,669.397 |\n",
"| 8 | 737,327.598 | 372,048.038 |\n",
"| 9 | 105,332.513 | 53,147.195 |\n",
"| 10 | 15,047.502 | 7,592.318 |\n",
"| 11 | 2,149.643 | 1,084.609 |\n",
"| 12 | 307.092 | 154.944 |\n",
"| 13 | 43.870 | 22.135 |\n",
"| 14 | 6.267 | 3.162 |\n",
"| 15 | 0.895 | 0.452 |\n",
"\n",
"*: Within a given resolution, all pentagons have the same area.\n"
]
}
],
"source": [
"headers = [\n",
" 'Res',\n",
" 'Average <ins>Hexagon</ins> Area (m<sup>2</sup>)',\n",
" 'Pentagon Area* (m<sup>2</sup>)'\n",
"]\n",
"out = tabulate(fmt_stats, headers=headers, tablefmt='pipe', stralign='right', disable_numparse=True)\n",
"\n",
"out += '\\n\\n*: Within a given resolution, all pentagons have the same area.'\n",
"print(out)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
"| Res | Average <ins>Hexagon</ins> Area (m<sup>2</sup>) | Pentagon Area* (m<sup>2</sup>) |\n",
"|------:|--------------------------------------------------:|---------------------------------:|\n",
"| 0 | 4,357,449,416,078.392 | 2,562,182,162,955.496 |\n",
"| 1 | 609,788,441,794.134 | 328,434,586,246.469 |\n",
"| 2 | 86,801,780,398.997 | 44,930,898,497.879 |\n",
"| 3 | 12,393,434,655.088 | 6,315,472,267.516 |\n",
"| 4 | 1,770,347,654.491 | 896,582,383.141 |\n",
"| 5 | 252,903,858.182 | 127,785,583.023 |\n",
"| 6 | 36,129,062.164 | 18,238,749.548 |\n",
"| 7 | 5,161,293.360 | 2,604,669.397 |\n",
"| 8 | 737,327.598 | 372,048.038 |\n",
"| 9 | 105,332.513 | 53,147.195 |\n",
"| 10 | 15,047.502 | 7,592.318 |\n",
"| 11 | 2,149.643 | 1,084.609 |\n",
"| 12 | 307.092 | 154.944 |\n",
"| 13 | 43.870 | 22.135 |\n",
"| 14 | 6.267 | 3.162 |\n",
"| 15 | 0.895 | 0.452 |\n",
"\n",
"*: Within a given resolution, all pentagons have the same area."
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Markdown(out)"
]
},
{
"cell_type": "markdown",
"metadata": {
"tags": []
},
"source": [
"# Pre-computed numerical constants for the C library\n",
"\n",
"These numbers should match [the averages which are given in the C library functions `getHexagonAreaAvgKm2` and `getHexagonAreaAvgM2`](https://github.com/uber/h3/blob/32b6e07f41353e9def5a778fc5a4a7c8cdddd93a/src/h3lib/lib/latLng.c#L262-L278)."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4.357449416078383e+06,\n",
"6.097884417941332e+05,\n",
"8.680178039899720e+04,\n",
"1.239343465508816e+04,\n",
"1.770347654491307e+03,\n",
"2.529038581819449e+02,\n",
"3.612906216441245e+01,\n",
"5.161293359717191e+00,\n",
"7.373275975944177e-01,\n",
"1.053325134272067e-01,\n",
"1.504750190766435e-02,\n",
"2.149643129451879e-03,\n",
"3.070918756316060e-04,\n",
"4.387026794728296e-05,\n",
"6.267181135324313e-06,\n",
"8.953115907605790e-07,\n"
]
}
],
"source": [
"for i in range(16):\n",
" s = '{:.15e},'.format(hexagon_area_avg(i, 'km^2'))\n",
" print(s)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4.357449416078392e+12,\n",
"6.097884417941342e+11,\n",
"8.680178039899734e+10,\n",
"1.239343465508818e+10,\n",
"1.770347654491310e+09,\n",
"2.529038581819453e+08,\n",
"3.612906216441251e+07,\n",
"5.161293359717200e+06,\n",
"7.373275975944190e+05,\n",
"1.053325134272069e+05,\n",
"1.504750190766437e+04,\n",
"2.149643129451882e+03,\n",
"3.070918756316065e+02,\n",
"4.387026794728303e+01,\n",
"6.267181135324324e+00,\n",
"8.953115907605805e-01,\n"
]
}
],
"source": [
"for i in range(16):\n",
" s = '{:.15e},'.format(hexagon_area_avg(i, 'm^2'))\n",
" print(s)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"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.7.12"
}
},
"nbformat": 4,
"nbformat_minor": 4
}