{ "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", " | x | \n", "y | \n", "h | \n", "
|---|---|---|---|
| 0 | \n", "431 | \n", "6 | \n", "-1.000000 | \n", "
| 1 | \n", "432 | \n", "6 | \n", "0.000000 | \n", "
| 2 | \n", "433 | \n", "6 | \n", "114.518776 | \n", "
| 3 | \n", "435 | \n", "6 | \n", "180.143860 | \n", "
| 4 | \n", "436 | \n", "6 | \n", "0.000000 | \n", "