{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "cd404f9d",
   "metadata": {},
   "source": [
    "### Part 1 : Voronoi mesh generation"
   ]
  },
  {
   "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.  387.5 275.  162.5  50. ] m.\n",
      "/--------------------------------------------/\n",
      "\n",
      "\n",
      "/----Sumary of points for voronoi meshing----/\n",
      "Distributed points from layers: 3396\n",
      "Points from layer buffers: 10317\n",
      "Points from max refinement areas: 437\n",
      "Points from min refinement areas: 0\n",
      "Total points inside the limit: 14091\n",
      "/--------------------------------------------/\n",
      "\n",
      "Time required for point generation: 19.07 seconds \n",
      "\n",
      "The output folder ../outputData/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=5)\n",
    "\n",
    "#Open limit layers and refinement definition layers\n",
    "vorMesh.addLimit('basin','../inputData/shp/Angascancha_Basin_Extension.shp')\n",
    "vorMesh.addLayer('facilities','../inputData/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 = '../outputData/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": "16b5c2d3-32e6-46c8-8ff9-c5741a0181e5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Show the resulting voronoi mesh\n",
    "\n",
    "#open the mesh file\n",
    "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, 758432 characters]\n",
      "text/plain": [
       "<Figure size 2520x1800 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "#plot the mesh\n",
    "mesh.plot(figsize=(35,25))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "102a2746-bed8-4c22-a6b8-9866104eb03b",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a32e70a9-67ed-470c-ae1b-b2fed5eb6dc0",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
