{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "import glob\n",
    "import os\n",
    "import pkg_resources\n",
    "import numpy as np\n",
    "import whitebox\n",
    "from pathlib import Path\n",
    "import shutil\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WhiteboxTools v2.1.0 by Dr. John B. Lindsay (c) 2017-2021\n",
      "\n",
      "WhiteboxTools is an advanced geospatial data analysis platform developed at\n",
      "the University of Guelph's Geomorphometry and Hydrogeomatics Research \n",
      "Group (GHRG). See www.whiteboxgeo.com for more details.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "wbt = whitebox.WhiteboxTools()\n",
    "print(wbt.version())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['IslandsDEMv1_2m_isn2016_07_slope_perc.tif', 'IslandsDEMv1_2m_isn2016_08_slope_perc.tif', 'reclass']\n",
      "Working directory: d:\\aProjects\\Coastline\\analysis\\raster\\\n"
     ]
    }
   ],
   "source": [
    "data_dir = \"d:\\\\aProjects\\\\Coastline\\\\analysis\\\\raster\\\\\"\n",
    "print(os.listdir(data_dir))\n",
    "\n",
    "# set whitebox working directory\n",
    "wbt.set_working_dir(data_dir)\n",
    "wbt.verbose = False\n",
    "wbt.set_compress_rasters(True) \n",
    "\n",
    "\n",
    "print(\"Working directory: {}\".format(data_dir))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Flatten Lakes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#raw_folder = \"D:\\\\aProjects\\\\Coastline\\\\experiment\\\\raw\"\n",
    "raw_folder = \"D:\\\\aDataLMI\\\\IslandsDEMv1\\\\res_2m\\\\Smth\"\n",
    "\n",
    "\n",
    "for filename in glob.iglob(f'{raw_folder }/*'):\n",
    "    #print(filename)\n",
    "    #Flatten Lakes\n",
    "    wbt.flatten_lakes(\n",
    "        dem = filename,  \n",
    "        lakes = 'islandsdemv1_lakes_cln_v_aug21.shp', #v data_dir\n",
    "        output = filename+'_fl.tif'\n",
    "        #callback=default_callback\n",
    "        )\n",
    "\n",
    "#flatten_lake_folder = r'D:\\\\aProjects\\\\Coastline\\\\experiment\\\\fl\\\\'\n",
    "flatten_lake_folder = r'D:\\\\aDataLMI\\\\IslandsDEMv1\\\\res_2m\\SmthFL\\\\'\n",
    "\n",
    "for file in glob.iglob(f'{raw_folder }/*_fl.tif'):\n",
    "     destination = flatten_lake_folder \n",
    "     shutil.move(file, destination)\n",
    "     print('Moved:', file)\n",
    "#processing time ca 65 min\n",
    "\n",
    "# print('Raw Directory: ',(os.listdir(data_dir+'/raw')))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Slope"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "metadata": {},
   "outputs": [],
   "source": [
    "raw_folder = \"D:\\\\aDataLMI\\\\IslandsDEMv1\\\\ISN2016_EPSG8088\\SmthFL_EPSG8088\"\n",
    "\n",
    "#Slope in degree\n",
    "for filename in glob.iglob(f'{raw_folder }/*'):\n",
    "    wbt.slope(\n",
    "        dem = filename,  \n",
    "        output = filename+'_slope_degr.tif',\n",
    "        zfactor = None,\n",
    "        units = \"degrees\"\n",
    "        #callback=default_callback\n",
    "        )\n",
    "\n",
    "\n",
    "# moving files from raw_folder to one I want. \n",
    "slope_folder = r'D:\\\\aDataLMI\\\\IslandsDEMv1\\\\ISN2016_EPSG8088\\\\Slope_Degr\\\\'\n",
    "\n",
    "for file in glob.iglob(f'{raw_folder }/*_slope_degr.tif'):\n",
    "     destination = slope_folder \n",
    "     shutil.move(file, destination)\n",
    "     #print('Moved:', file)\n",
    "\n",
    "#processing time ca 70:36 min\n",
    "\n",
    "\n",
    "#Slope in percent\n",
    "for filename in glob.iglob(f'{raw_folder }/*'):\n",
    "    wbt.slope(\n",
    "        dem = filename,  \n",
    "        output = filename+'_slope_perc.tif',\n",
    "        zfactor = None,\n",
    "        units = \"percent\"\n",
    "        #callback=default_callback\n",
    "        )\n",
    "\n",
    "\n",
    "# moving files from raw_folder to one I want. \n",
    "slope_folder = r'D:\\\\aDataLMI\\\\IslandsDEMv1\\\\ISN2016_EPSG8088\\\\Slope_Perc\\\\'\n",
    "\n",
    "for file in glob.iglob(f'{raw_folder }/*_slope_perc.tif'):\n",
    "     destination = slope_folder \n",
    "     shutil.move(file, destination)\n",
    "     #print('Moved:', file)\n",
    "\n",
    "#processing time ca 70:36 min"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Slope Reclassification and filtrening"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "raw_folder = \"D:\\\\aDataLMI\\\\IslandsDEMv1\\\\res_2m\\\\Slope\\\\Slope_Perc\\\\\"\n",
    "#raw_folder = \"d:\\\\aProjects\\\\Coastline\\\\analysis\\\\raster\\\\\"\n",
    "\n",
    "#Reclass slope\n",
    "for filename in glob.iglob(f'{raw_folder }/*.tif'):\n",
    "    wbt.reclass(\n",
    "        i = filename,  \n",
    "        output = filename+'_recl.tif',\n",
    "        reclass_vals = '0;min;16;1;16;max',\n",
    "        assign_mode= False\n",
    "        )\n",
    "\n",
    "\n",
    "# moving files from raw_folder to one I want. \n",
    "destination_folder = r'D:\\\\aProjects\\\\Coastline\\\\analysis\\\\raster\\\\reclass\\\\'\n",
    "\n",
    "for file in glob.iglob(f'{raw_folder }/*_recl.tif'):\n",
    "     destination = destination_folder \n",
    "     shutil.move(file, destination)\n",
    "     #print('Moved:', file)\n",
    "\n",
    "#processing time ca 30 min"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hotova prvni cast\n",
      "Hotovo, prestehovano :)\n"
     ]
    }
   ],
   "source": [
    "raw_folder = 'D:\\\\aProjects\\\\Coastline\\\\analysis\\\\raster\\\\reclass\\\\'\n",
    "#raw_folder = \"d:\\\\aProjects\\\\Coastline\\\\analysis\\\\raster\\\\\"\n",
    "\n",
    "#filter recl. slope\n",
    "for filename in glob.iglob(f'{raw_folder }/*.tif'):\n",
    "    wbt.generalize_classified_raster(\n",
    "        i = filename,  \n",
    "        output = filename+'_filt.tif',\n",
    "        min_size = 100,\n",
    "        method = 'nearest' # method: 'longest' (default), 'largest', and 'nearest'\n",
    "        )\n",
    "\n",
    "print(\"Hotova prvni cast\")\n",
    "\n",
    "# moving files from raw_folder to one I want. \n",
    "destination_folder = r'D:\\\\aProjects\\\\Coastline\\\\analysis\\\\raster\\\\filter\\\\'\n",
    "\n",
    "for file in glob.iglob(f'{raw_folder }/*_filt.tif'):\n",
    "     destination = destination_folder \n",
    "     shutil.move(file, destination)\n",
    "     #print('Moved:', file)\n",
    "print(\"Hotovo, prestehovano :)\")\n",
    "\n",
    "#processing time ca 60 min"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Other.... \n",
    "experiments and so on......."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "wbt.reclass(\n",
    "        i=\"test82_aspect.tif\",\n",
    "        output=\"test82_asp_recl.tif\",\n",
    "        reclass_vals='-1;-1;0;1;0;45;2;45;135;3;135;225;4;225;315;1;315;max',\n",
    "        assign_mode=False,\n",
    ")\n",
    "# reclass value: triplet - new value; from; less than;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "wbt.generalize_classified_raster(\n",
    "    i = 'test82_asp_recl.tif', \n",
    "    output= 'test82_asp_recl_genNEAREST100.tif', \n",
    "    min_size=100, \n",
    "    method='nearest', \n",
    ")\n",
    "# method: 'longest' (default), 'largest', and 'nearest'\n",
    "print(\"Hotovo :)\")"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "2cab75ea35dde60ee6a6a3a82a16c073b975d676ff24ab2af6fa6ed183aa8308"
  },
  "kernelspec": {
   "display_name": "Python 3.9.7 ('wbt')",
   "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"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
