{ "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": { "execution": { "iopub.execute_input": "2026-01-27T17:12:28.631904Z", "iopub.status.busy": "2026-01-27T17:12:28.631798Z", "iopub.status.idle": "2026-01-27T17:12:28.634950Z", "shell.execute_reply": "2026-01-27T17:12:28.634564Z" } }, "outputs": [], "source": [ "import pandas as pd\n", "\n", "from lets_plot import *" ] }, { "cell_type": "code", "execution_count": 2, "id": "4d778c93", "metadata": { "execution": { "iopub.execute_input": "2026-01-27T17:12:28.636093Z", "iopub.status.busy": "2026-01-27T17:12:28.636017Z", "iopub.status.idle": "2026-01-27T17:12:28.637947Z", "shell.execute_reply": "2026-01-27T17:12:28.637614Z" } }, "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": { "execution": { "iopub.execute_input": "2026-01-27T17:12:28.638798Z", "iopub.status.busy": "2026-01-27T17:12:28.638727Z", "iopub.status.idle": "2026-01-27T17:12:28.642436Z", "shell.execute_reply": "2026-01-27T17:12:28.642116Z" } }, "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, *, dist=1, step_y=1):\n", " def add_tails_to_row(subdf, y):\n", " subdf = subdf.sort_values(by='x').copy()\n", " x_to_h = lambda x: subdf[subdf['x'] == x].iloc[0]['h'] if x in subdf['x'].values else 0\n", " series = []\n", " s = []\n", " last_pick = subdf['x'].min()\n", " for x in range(subdf['x'].min(), subdf['x'].max() + 1):\n", " h = x_to_h(x)\n", " if h > 0:\n", " s.append(x)\n", " last_pick = x\n", " elif x - last_pick >= dist and len(s) > 0:\n", " series.append(s)\n", " s = []\n", " if len(s) > 0:\n", " series.append(s)\n", " return pd.concat([\n", " pd.concat([\n", " pd.DataFrame({'x': s, 'y': [y] * len(s), 'h': [x_to_h(x) for x in s]}),\n", " pd.DataFrame({'x': [min(s) - 2, min(s) - 1, max(s) + 1, max(s) + 2], 'y': [y] * 4, 'h': [-1, 0, 0, -1]})\n", " ])\n", " for s in series\n", " ]).sort_values(by=['x', 'h'], ascending=[True, False])\\\n", " .drop_duplicates(subset=['x'], keep='first')\\\n", " .reset_index(drop=True)\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_y)\n", " ]).sort_values(by=['y', 'x']).reset_index(drop=True)" ] }, { "cell_type": "code", "execution_count": 4, "id": "2d881f0c-fe00-4b3c-9bb5-c0bc3f6c9d34", "metadata": { "execution": { "iopub.execute_input": "2026-01-27T17:12:28.643143Z", "iopub.status.busy": "2026-01-27T17:12:28.643070Z", "iopub.status.idle": "2026-01-27T17:12:30.149531Z", "shell.execute_reply": "2026-01-27T17:12:30.149071Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(4769, 3)\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", "
xyh
04316-1.000000
143260.000000
24336114.518776
34356180.143860
443660.000000
\n", "
" ], "text/plain": [ " x y h\n", "0 431 6 -1.000000\n", "1 432 6 0.000000\n", "2 433 6 114.518776\n", "3 435 6 180.143860\n", "4 436 6 0.000000" ] }, "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", "min_h = df[df['h'] > 0].describe()['h']['min']\n", "df = process_rows(df[df[\"h\"] > 0], dist=25, step_y=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": { "execution": { "iopub.execute_input": "2026-01-27T17:12:30.150871Z", "iopub.status.busy": "2026-01-27T17:12:30.150791Z", "iopub.status.idle": "2026-01-27T17:12:30.199953Z", "shell.execute_reply": "2026-01-27T17:12:30.199486Z" } }, "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', min_height=0, scale=.0025, \\\n", " color=\"#08519c\", fill=\"#bdd7e7\", \\\n", " sampling=sampling_pick(df.shape[0]), \\\n", " tooltips=layer_tooltips().line(\"height|@h\").format(\"@h\", ',.1~f'), \\\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 }