{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "bc91d4a2-c298-4aa9-8c8f-b0253c04fc05",
   "metadata": {},
   "outputs": [],
   "source": [
    "import rasterio, os\n",
    "from rasterio.warp import calculate_default_transform, reproject, Resampling"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "85729115-fa11-4fc3-955e-5dfa447e4224",
   "metadata": {},
   "outputs": [],
   "source": [
    "#define destination crs\n",
    "dstCrs = {'init': 'EPSG:4326'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d82b50d1-f3b8-4435-ad5e-fc5a4e248c6b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['clip_RT_S2A_OPER_MSI_L1C_TL_MTI__20160506T214824_A004555_T18LTM_B02.tif',\n",
       " 'clip_RT_S2A_OPER_MSI_L1C_TL_MTI__20160506T214824_A004555_T18LTM_B03.tif',\n",
       " 'clip_RT_S2A_OPER_MSI_L1C_TL_MTI__20160506T214824_A004555_T18LTM_B04.tif',\n",
       " 'clip_RT_S2A_OPER_MSI_L1C_TL_MTI__20160506T214824_A004555_T18LTM_B08.tif']"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#source raster folder path\n",
    "srcPath = '../multipleRaster/'\n",
    "\n",
    "#get list of rasters\n",
    "lstRst = [r for r in os.listdir(srcPath) if r.endswith('tif')]\n",
    "lstRst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b44cedbb-f3ac-4604-93b2-acd5f040a4ea",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "CRS.from_epsg(32718)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#get crs of source raster\n",
    "expRst = rasterio.open(srcPath+lstRst[0])\n",
    "expRst.crs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "2e0e318e-ff54-4167-86c5-7ce777441972",
   "metadata": {},
   "outputs": [],
   "source": [
    "#destination raster folder path\n",
    "dstPath = '../outputRaster/'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "48871587-fa33-40a6-a0c3-644ba44a58ee",
   "metadata": {},
   "outputs": [],
   "source": [
    "#define raster reprojection function\n",
    "def reprojectRaster(srcRst, dstRst, dstCrs, srcPath, dstPath):\n",
    "    #open source raster\n",
    "    srcRst = rasterio.open(srcPath+srcRst)\n",
    "    \n",
    "    #calculate transform array and shape of reprojected raster\n",
    "    transform, width, height = calculate_default_transform(\n",
    "        srcRst.crs, dstCrs, srcRst.width, srcRst.height, *srcRst.bounds)\n",
    "    \n",
    "    #working of the meta for the destination raster\n",
    "    kwargs = srcRst.meta.copy()\n",
    "    kwargs.update({\n",
    "            'crs': dstCrs,\n",
    "            'transform': transform,\n",
    "            'width': width,\n",
    "            'height': height\n",
    "        })\n",
    "    \n",
    "    #open destination raster\n",
    "    dstRst = rasterio.open(dstPath+dstRst, 'w', **kwargs)\n",
    "    \n",
    "    #reproject and save raster band data\n",
    "    for i in range(1, srcRst.count + 1):\n",
    "        reproject(\n",
    "            source=rasterio.band(srcRst, i),\n",
    "            destination=rasterio.band(dstRst, i),\n",
    "            #src_transform=srcRst.transform,\n",
    "            src_crs=srcRst.crs,\n",
    "            #dst_transform=transform,\n",
    "            dst_crs=dstCrs,\n",
    "            resampling=Resampling.nearest)\n",
    "    \n",
    "    #close destination raster\n",
    "    dstRst.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "6e89445d-eb24-4917-8533-337b7562427f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Reprojection of clip_RT_S2A_OPER_MSI_L1C_TL_MTI__20160506T214824_A004555_T18LTM_B02.tif done\n",
      "Reprojection of clip_RT_S2A_OPER_MSI_L1C_TL_MTI__20160506T214824_A004555_T18LTM_B03.tif done\n",
      "Reprojection of clip_RT_S2A_OPER_MSI_L1C_TL_MTI__20160506T214824_A004555_T18LTM_B04.tif done\n",
      "Reprojection of clip_RT_S2A_OPER_MSI_L1C_TL_MTI__20160506T214824_A004555_T18LTM_B08.tif done\n"
     ]
    }
   ],
   "source": [
    "for srcRst in lstRst:\n",
    "    dstRst = srcRst[:-4]+'_reprojected'+srcRst[-4:]\n",
    "    reprojectRaster(srcRst,dstRst,dstCrs,srcPath,dstPath)\n",
    "    print('Reprojection of %s done'%srcRst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4252caee-799f-4369-bbab-9c672b01e42e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.9.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
