{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Data Preperation - Difference Quarries Hoher Meisner 21-19\n",
    "\n",
    "* calculate the areas of quarries -> Mask\n",
    "* mask difference file with Mask-file\n",
    "* calculate some reclassified rasters for better visuality\n",
    "\n",
    "#### Import\n",
    "* import modules used in the script"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import arcpy\n",
    "import rasterio\n",
    "import os\n",
    "\n",
    "from arcpy import env \n",
    "from arcpy.sa import *\n",
    "from arcpy.ia import *\n",
    "from arcpy.da import *\n",
    "from rasterio.plot import show_hist, show\n",
    "from matplotlib import pyplot\n",
    "\n",
    "base_path = os.getcwd()",
    "%matplotlib inline\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Input & Output files\n",
    "* define all input filepaths\n",
    "* define all output files (filepath + filename)\n",
    "* set the distance parameter used for calculating the buffer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "buffer_quarry = os.path.join(base_path, r\"newData\\HoherMeisner\\hesse_shp_hohermeisner\\buffer_quarry_10m.shp\")\n",
    "\n",
    "buffer_complete_dis = os.path.join(base_path, r\"newData\\HoherMeisner\\hesse_shp_hohermeisner\\hesse_shp_quarry_hm_buffer_10m.shp\")\n",
    "\n",
    "diff_21_19 = os.path.join(base_path, r\"newData\\HoherMeisner\\diff_ref_21_19_hohermeisner\\diff21_19_HoherMeisner.tif\")\n",
    "\n",
    "diff_21_19_mask =  os.path.join(base_path, r\"newData\\HoherMeisner\\diff_ref_21_19_hohermeisner\\diff21_19_HoherMeisner_quarry_mask.tif\")\n",
    "\n",
    "distance = \"10 Meters\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Create the Mask to delete areas from the difference raster\n",
    "* seperated categories in the shapefile are dissolved into one"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "arcpy.management.Dissolve(buffer_quarry, buffer_complete_dis)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* raster-data is extract by mask -> that means all raster data, which is not covered by Mask-shapefile is deleted"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "outExtractByMask = arcpy.sa.ExtractByMask(diff_21_19, buffer_complete_dis)\n",
    "outExtractByMask.save(diff_21_19_mask)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Reclassify Masked-Raster \n",
    "* calculate statistics of masked-raster-file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "stats_min = outExtractByMask.minimum\n",
    "stats_max = outExtractByMask.maximum\n",
    "stats_mean = outExtractByMask.mean\n",
    "stats_std = outExtractByMask.standardDeviation\n",
    "print(\"Minimum: {0}, Maximum: {1}, Mean: {2}, Standard Deviation: {3}\".format(stats_min, stats_max, stats_mean, stats_std))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* define ranges that are used to reclassify the raster data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "range_class01 = RemapRange([[stats_min, -0.4,\"NODATA\"],[-0.4,-0.3,-4],[-0.3,-0.2,-3],[-0.2,-0.1,-2],[-0.1,0,-1],[0,0.1,1],[0.1,0.2,2],[0.2,0.3,3],[0.3,0.4,4],[0.4, stats_max,\"NODATA\"]])\n",
    "range_class02 = RemapRange([[stats_min, -1,\"NODATA\"],[-1,-0.75,-4],[-0.75,-0.5,-3],[-0.5,-0.25,-2],[-0.25,0,-1],[0,0.25,1],[0.25,0.5,2],[0.5,0.75,3],[0.75,1,4],[1, stats_max,\"NODATA\"]])\n",
    "\n",
    "range_class03 = RemapRange([[stats_min, -0.4,-5],[-0.4,-0.3,-4],[-0.3,-0.2,-3],[-0.2,-0.1,-2],[-0.1,0,-1],[0,0.1,1],[0.1,0.2,2],[0.2,0.3,3],[0.3,0.4,4],[0.4, stats_max,5]])\n",
    "range_class04 = RemapRange([[stats_min, -1,-5],[-1,-0.75,-4],[-0.75,-0.5,-3],[-0.5,-0.25,-2],[-0.25,0,-1],[0,0.25,1],[0.25,0.5,2],[0.5,0.75,3],[0.75,1,4],[1, stats_max,5]])\n",
    "\n",
    "\n",
    "range_class05 = RemapRange([[stats_min, -1.75,\"NODATA\"],[-1.75,-1.25,-3],[-1.25,-0.75,-2],[-0.75,-0.25,-1],[-0.25,0.25,0],[0.25,0.75,1],[0.75,1.25,2],[1.25,1.75,3],[1.75, stats_max,\"NODATA\"]])\n",
    "range_class06 = RemapRange([[stats_min, -0.7,\"NODATA\"],[-0.7,-0.5,-3],[-0.5,-0.3,-2],[-0.3,-0.1,-1],[-0.1,0.1,0],[0.1,0.3,1],[0.3,0.5,2],[0.5,0.7,3],[0.7, stats_max,\"NODATA\"]])\n",
    "range_class07 = RemapRange([[stats_min, -0.35,\"NODATA\"],[-0.35,-0.25,-3],[-0.25,-0.15,-2],[-0.15,-0.05,-1],[-0.05,0.05,0],[0.05,0.15,1],[0.15,0.25,2],[0.25,0.35,3],[0.35, stats_max,\"NODATA\"]])\n",
    "\n",
    "range_class08 = RemapRange([[stats_min, -1.75,-4],[-1.75,-1.25,-3],[-1.25,-0.75,-2],[-0.75,-0.25,-1],[-0.25,0.25,\"NODATA\"],[0.25,0.75,1],[0.75,1.25,2],[1.25,1.75,3],[1.75, stats_max,4]])\n",
    "range_class09 = RemapRange([[stats_min, -0.7,-4],[-0.7,-0.5,-3],[-0.5,-0.3,-2],[-0.3,-0.1,-1],[-0.1,0.1,\"NODATA\"],[0.1,0.3,1],[0.3,0.5,2],[0.5,0.7,3],[0.7, stats_max,4]])\n",
    "range_class10 = RemapRange([[stats_min, -0.35,-4],[-0.35,-0.25,-3],[-0.25,-0.15,-2],[-0.15,-0.05,-1],[-0.05,0.05,\"NODATA\"],[0.05,0.15,1],[0.15,0.25,2],[0.25,0.35,3],[0.35, stats_max,4]])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* reclassify masked-raster & save the reclassified rasters to new files"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "outReclass1 = Reclassify(diff_21_19_mask, \"Value\", range_class01)\n",
    "outReclass1.save(os.path.join(base_path, r\"newData\\HoherMeisner\\diff_ref_21_19_hohermeisner\\diff21_19_quarryHoherM_r01.tif\"))\n",
    "\n",
    "outReclass2 = Reclassify(diff_21_19_mask, \"Value\", range_class02)\n",
    "outReclass2.save(os.path.join(base_path, r\"newData\\HoherMeisner\\diff_ref_21_19_hohermeisner\\diff21_19_quarryHoherM_r02.tif\"))\n",
    "\n",
    "outReclass3 = Reclassify(diff_21_19_mask, \"Value\", range_class03)\n",
    "outReclass3.save(os.path.join(base_path, r\"newData\\HoherMeisner\\diff_ref_21_19_hohermeisner\\diff21_19_quarryHoherM_r03.tif\"))\n",
    "\n",
    "outReclass4 = Reclassify(diff_21_19_mask, \"Value\", range_class04)\n",
    "outReclass4.save(os.path.join(base_path, r\"newData\\HoherMeisner\\diff_ref_21_19_hohermeisner\\diff21_19_quarryHoherM_r04.tif\"))\n",
    "\n",
    "outReclass5 = Reclassify(diff_21_19_mask, \"Value\", range_class05)\n",
    "outReclass5.save(os.path.join(base_path, r\"newData\\HoherMeisner\\diff_ref_21_19_hohermeisner\\diff21_19_quarryHoherM_r05.tif\"))\n",
    "\n",
    "outReclass6 = Reclassify(diff_21_19_mask, \"Value\", range_class06)\n",
    "outReclass6.save(os.path.join(base_path, r\"newData\\HoherMeisner\\diff_ref_21_19_hohermeisner\\diff21_19_quarryHoherM_r06.tif\"))\n",
    "\n",
    "outReclass7 = Reclassify(diff_21_19_mask, \"Value\", range_class07)\n",
    "outReclass7.save(os.path.join(base_path, r\"newData\\HoherMeisner\\diff_ref_21_19_hohermeisner\\diff21_19_quarryHoherM_r07.tif\"))\n",
    "\n",
    "outReclass8 = Reclassify(diff_21_19_mask, \"Value\", range_class08)\n",
    "outReclass8.save(os.path.join(base_path, r\"newData\\HoherMeisner\\diff_ref_21_19_hohermeisner\\diff21_19_quarryHoherM_r08.tif\"))\n",
    "\n",
    "outReclass9 = Reclassify(diff_21_19_mask, \"Value\", range_class09)\n",
    "outReclass9.save(os.path.join(base_path, r\"newData\\HoherMeisner\\diff_ref_21_19_hohermeisner\\diff21_19_quarryHoherM_r09.tif\"))\n",
    "\n",
    "outReclass10 = Reclassify(diff_21_19_mask, \"Value\", range_class10)\n",
    "outReclass10.save(os.path.join(base_path, r\"newData\\HoherMeisner\\diff_ref_21_19_hohermeisner\\diff21_19_quarryHoherM_r10.tif\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "umwelt4",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "file_extension": ".py",
   "name": "python",
   "version": "3.10.8 (tags/v3.10.8:aaaf517, Oct 11 2022, 16:50:30) [MSC v.1933 64 bit (AMD64)]"
  },
  "vscode": {
   "interpreter": {
    "hash": "36b39eb320444ff0f57387d3dd94682afe60c45a261c005cd9efdc8c90386cca"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}