{ "cells": [ { "cell_type": "markdown", "id": "355e0c61", "metadata": {}, "source": [ "# New Zealand DEM remake\n", "\n", "See the original notebook [here](https://nbviewer.org/github/royalosyin/Work-with-DEM-data-using-Python-from-Simple-to-Complicated/blob/master/Sup03-Ridgelines%20Map%20of%20DEM.ipynb)." ] }, { "cell_type": "code", "execution_count": 1, "id": "9d10728d", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "\n", "from lets_plot import *" ] }, { "cell_type": "code", "execution_count": 2, "id": "4d778c93", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 3, "id": "7faa0eb9", "metadata": {}, "outputs": [], "source": [ "def dataset_array_to_dataframe(dataset_array):\n", " df = pd.DataFrame.from_records([\n", " (j, i, a)\n", " for i, r in enumerate(dataset_array)\n", " for j, a in enumerate(r)\n", " ], columns=[\"x\", \"y\", \"h\"])\n", " return df\n", "\n", "def process_rows(df, step=1):\n", " def add_tails_to_row(subdf, y):\n", " xmin, xmax = subdf['x'].min(), subdf['x'].max()\n", " return pd.concat([\n", " subdf,\n", " pd.DataFrame({'x': [xmin - 1, xmax + 1], 'y': [y, y], 'h': [0, 0]})\n", " ], ignore_index=True).assign(h_max=lambda d: d['h'].max())\n", " return pd.concat([\n", " add_tails_to_row(df[df['y'] == y], y) for y in range(df['y'].min(), df['y'].max() + 1, step)\n", " ]).sort_values(by=['y', 'x']).reset_index(drop=True)" ] }, { "cell_type": "code", "execution_count": 4, "id": "439267fa", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(4243, 4)\n" ] }, { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
xyhh_max
043260.000000180.143860
14336114.518776180.143860
24356180.143860180.143860
343660.000000180.143860
442080.000000124.377769
\n", "
" ], "text/plain": [ " x y h h_max\n", "0 432 6 0.000000 180.143860\n", "1 433 6 114.518776 180.143860\n", "2 435 6 180.143860 180.143860\n", "3 436 6 0.000000 180.143860\n", "4 420 8 0.000000 124.377769" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "raw_data_array = pd.read_csv(\"https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/new_zealand.csv\", header=None).to_numpy()\n", "df = dataset_array_to_dataframe(raw_data_array)\n", "df = process_rows(df[df[\"h\"] > 0], 2)\n", "bbox = dict(xmin=df['x'].min(), ymin=df['y'].min(), xmax=df['x'].max(), ymax=df['y'].max())\n", "print(df.shape)\n", "df.head()" ] }, { "cell_type": "code", "execution_count": 5, "id": "4e517dcc", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(df) + \\\n", " geom_area_ridges(aes(\"x\", \"y\", height=\"h\"), \\\n", " stat='identity', scale=.005, \\\n", " color=\"#08519c\", fill=\"#bdd7e7\", \\\n", " sampling=sampling_pick(df.shape[0]), \\\n", " tooltips=layer_tooltips().line(\"height|@h\").format(\"@h\", 'd'), \\\n", " show_legend=False) + \\\n", " geom_text(x=bbox['xmin'] + .7 * (bbox['xmax'] - bbox['xmin']), \\\n", " y=bbox['ymin'] + .9 * (bbox['ymax'] - bbox['ymin']), \\\n", " label=\"New Zealand\", size=25, family=\"Cinzel\") + \\\n", " scale_y_continuous(trans='reverse') + \\\n", " ggsize(600, 600) + \\\n", " theme_minimal() + \\\n", " theme(axis='blank', panel_grid='blank', \\\n", " plot_background=element_rect(color='black', fill='#e6e6e6', size=1))" ] } ], "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.10.13" } }, "nbformat": 4, "nbformat_minor": 5 }