{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Reduce Regions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Setup" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Ecoscope" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ECOSCOPE_RAW = \"https://raw.githubusercontent.com/wildlife-dynamics/ecoscope/master\"\n", "\n", "!pip install 'ecoscope[analysis,mapping,plotting] @ git+https://github.com/wildlife-dynamics/ecoscope@v1.8.12' &> /dev/null" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import os\n", "import sys\n", "\n", "import geopandas as gpd\n", "import numpy as np\n", "\n", "import ecoscope\n", "\n", "ecoscope.init()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Google Drive" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "output_dir = \"Ecoscope-Outputs\"\n", "\n", "if \"google.colab\" in sys.modules:\n", " from google.colab import drive\n", "\n", " drive.mount(\"/content/drive/\", force_remount=True)\n", " output_dir = os.path.join(\"/content/drive/MyDrive/\", output_dir)\n", "\n", "os.makedirs(output_dir, exist_ok=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Load AOI" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ecoscope.io.download_file(\n", " f\"{ECOSCOPE_RAW}/tests/sample_data/vector/maec_4zones_UTM36S.gpkg\",\n", " os.path.join(output_dir, \"maec_4zones_UTM36S.gpkg\"),\n", ")\n", "\n", "AOI_FILE = os.path.join(output_dir, \"maec_4zones_UTM36S.gpkg\")\n", "\n", "regions_gdf = gpd.GeoDataFrame.from_file(AOI_FILE).to_crs(4326).set_index(\"ZONE\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Visualize AOI" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "regions_gdf.explore()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Apply Mean Reduction" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "raster_name = os.path.join(output_dir, \"mara_dem.tif\")\n", "\n", "ecoscope.io.download_file(f\"{ECOSCOPE_RAW}/tests/sample_data/raster/mara_dem.tif\", raster_name)\n", "\n", "ecoscope.io.raster.reduce_region(gdf=regions_gdf, raster_path_list=[raster_name], reduce_func=np.mean)" ] } ], "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.12" } }, "nbformat": 4, "nbformat_minor": 4 }