{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "cd404f9d",
   "metadata": {},
   "source": [
    "# Ejercicio parte 1 : Creación de una malla no estructurada"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1aebdc9a",
   "metadata": {},
   "source": [
    "En este ejemplo vamos a generar una malla irregular Voronoi para la cuenca de Angascancha. Debido a que las condiciones de borde de un drenaje generan cambios más localizados en el gradiente hidráulico se requiere que la malla se más refinada en los alrededores de la red hídrica.\n",
    "\n",
    "El aporte de este tutorial es de crear un área de estudio delimitadora, para este caso la cuenca de Angascancha, y el refinamiento de mallas Voronoi alrededor de la red hídrica de la cuenca.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "472dc38b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "/--------Sumary of cell discretization-------/\n",
      "Maximun refinement: 500.00 m.\n",
      "Minimum refinement: 50.00 m.\n",
      "Cell size list: [500.   481.25 462.5  443.75 425.   406.25 387.5  368.75 350.   331.25\n",
      " 312.5  293.75 275.   256.25 237.5  218.75 200.   181.25 162.5  143.75\n",
      " 125.   106.25  87.5   68.75  50.  ] m.\n",
      "/--------------------------------------------/\n",
      "\n",
      "\n",
      "/----Sumary of points for voronoi meshing----/\n",
      "Distributed points from layers: 3396\n",
      "Points from layer buffers: 38699\n",
      "Points from max refinement areas: 437\n",
      "Points from min refinement areas: 0\n",
      "Total points inside the limit: 42240\n",
      "/--------------------------------------------/\n",
      "\n",
      "Time required for point generation: 48.92 seconds \n",
      "\n",
      "The output folder ../examples/out/angascancha exists\n"
     ]
    }
   ],
   "source": [
    "import warnings\n",
    "warnings.filterwarnings('ignore')\n",
    "\n",
    "import os\n",
    "import sys\n",
    "#adding path to source directory\n",
    "sys.path.insert(0, '../src')\n",
    "import geopandas as gpd\n",
    "from geoVoronoi import createVoronoi\n",
    "\n",
    "#Create mesh object\n",
    "vorMesh = createVoronoi()\n",
    "\n",
    "#Define base refinement and refinement levels\n",
    "vorMesh.defineParameters(maxRef = 500, minRef=50, stages=25)\n",
    "\n",
    "#Open limit layers and refinement definition layers\n",
    "vorMesh.addLimit('basin','../examples/In/shp/Angascancha_Basin_Extension.shp')\n",
    "vorMesh.addLayer('facilities','../examples/In/shp/rios.shp')\n",
    "\n",
    "#Generate point pair array\n",
    "vorMesh.extractOrgVertices()\n",
    "\n",
    "#Generate the point cloud and voronoi\n",
    "vorMesh.createPointCloud()\n",
    "vorMesh.generateVoronoi()\n",
    "\n",
    "#check or create an output folder\n",
    "outPath = '../examples/out/angascancha'\n",
    "if os.path.isdir(outPath):\n",
    "    print('The output folder %s exists'%outPath)\n",
    "else:\n",
    "    os.mkdir(outPath)\n",
    "    print('The output folder %s has been generated.'%outPath)\n",
    "\n",
    "#Export point data and voronoi polygons\n",
    "#Points\n",
    "vorMesh.getPointsAsShp('vertexOrg',outPath+'/vertexOrg.shp')\n",
    "vorMesh.getPointsAsShp('vertexDist',outPath+'/vertexDist.shp')\n",
    "vorMesh.getPointsAsShp('vertexBuffer',outPath+'/vertexBuffer.shp')\n",
    "vorMesh.getPointsAsShp('vertexMaxRef',outPath+'/vertexMaxRef.shp')\n",
    "vorMesh.getPointsAsShp('vertexMinRef',outPath+'/vertexMinRef.shp')\n",
    "vorMesh.getPointsAsShp('vertexTotal',outPath+'/vertexTotal.shp')\n",
    "#Polygons\n",
    "\n",
    "vorMesh.getPolyAsShp('voronoiRegions',outPath+'/voronoiRegions.shp')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "821cd57f",
   "metadata": {},
   "outputs": [],
   "source": [
    "mesh=gpd.read_file(outPath+'/voronoiRegions.shp')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "a4877cb4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<AxesSubplot:>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 1112336 characters]\n",
      "text/plain": [
       "<Figure size 2520x1800 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "mesh.plot(figsize=(35,25))"
   ]
  }
 ],
 "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.8.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
