{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "revised-status", "metadata": {}, "outputs": [], "source": [ "import geopandas as gpd" ] }, { "cell_type": "code", "execution_count": null, "id": "exceptional-momentum", "metadata": {}, "outputs": [], "source": [ "reg_file = 'zip://l0_regions/00_rgi70_regions.zip/00_rgi70_regions/00_rgi70_O1Regions.shp'\n", "reg_f = gpd.read_file(reg_file)\n", "# We drop the second alaska box which is useless\n", "reg_f = reg_f.drop(1)\n", "reg_f.plot(alpha=0.5, edgecolor='k');" ] }, { "cell_type": "code", "execution_count": null, "id": "exact-ridge", "metadata": {}, "outputs": [], "source": [ "reg = 14\n", "idir = 'l2_sel_reg'\n", "\n", "fp = f'{idir}/RGI{reg:02d}/RGI{reg:02d}.shp'\n", "print(fp)\n", "print('Reading...')\n", "shp = gpd.read_file(fp)" ] }, { "cell_type": "code", "execution_count": null, "id": "material-shaft", "metadata": {}, "outputs": [], "source": [ "# Get the representative points\n", "rp = shp.representative_point()\n", "\n", "# Make a dataframe out of it and add the original index to recover it later\n", "rp = rp.to_frame('geometry')\n", "rp['orig_index'] = shp.index\n", "\n", "# Make the overlay\n", "# The output are the points in rp which are intersecting with the correct region in the file\n", "intersect = gpd.overlay(rp, reg_f.loc[reg_f.RGI_CODE == f'{reg:02d}'], how='intersection')\n", " \n", "# Now select the points in the original shape which intersect\n", "odf = shp.loc[intersect['orig_index']]" ] }, { "cell_type": "code", "execution_count": null, "id": "recorded-diagnosis", "metadata": {}, "outputs": [], "source": [ "# Test: since here we are re-doing something I did already, the selected output should be the same as before\n", "assert len(odf) == len(shp)" ] }, { "cell_type": "code", "execution_count": null, "id": "central-citizenship", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.8.8" } }, "nbformat": 4, "nbformat_minor": 5 }