{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "db8930e0-c3c0-470b-b771-686bc5a8e551",
   "metadata": {},
   "outputs": [],
   "source": [
    "import rasterio\n",
    "from rasterio.warp import calculate_default_transform, reproject, Resampling"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "804599f6-65ba-4432-b7a1-7c6fc7678a71",
   "metadata": {},
   "outputs": [],
   "source": [
    "#open source raster\n",
    "srcRst = rasterio.open('../singleRaster/landsatImage.tif')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "af4a7bb5-8178-4930-ba31-93767f89edfe",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "source raster crs:\n",
      "EPSG:32614\n",
      "destination raster crs\n",
      "{'init': 'EPSG:4326'}\n"
     ]
    }
   ],
   "source": [
    "print(\"source raster crs:\")\n",
    "print(srcRst.crs)\n",
    "\n",
    "dstCrs = {'init': 'EPSG:4326'}\n",
    "print(\"destination raster crs:\")\n",
    "print(dstCrs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "047d3176-c82d-42eb-9c79-9a68da98a337",
   "metadata": {},
   "outputs": [],
   "source": [
    "#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)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "33bacbed-42ae-4c06-a1c4-1f6a7a7c5b62",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "transform array of source raster\n",
      "| 10.00, 0.00, 567810.00|\n",
      "| 0.00,-10.00, 2127440.00|\n",
      "| 0.00, 0.00, 1.00|\n",
      "transform array of destination raster\n",
      "| 0.00, 0.00,-98.36|\n",
      "| 0.00,-0.00, 19.24|\n",
      "| 0.00, 0.00, 1.00|\n"
     ]
    }
   ],
   "source": [
    "print(\"transform array of source raster\")\n",
    "print(srcRst.transform)\n",
    "\n",
    "print(\"transform array of destination raster\")\n",
    "print(transform)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "1446813c-8f4c-41b5-b3fe-15a263b70c7e",
   "metadata": {},
   "outputs": [],
   "source": [
    "#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",
    "    })"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "d1b330da-c1f7-4d31-b27b-34819cfacba9",
   "metadata": {},
   "outputs": [],
   "source": [
    "#open destination raster\n",
    "dstRst = rasterio.open('../outputRaster/landsatImageWgs84.tif', 'w', **kwargs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "c0ae1d92-e2b0-447b-8b05-7eb970234336",
   "metadata": {},
   "outputs": [],
   "source": [
    "#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)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "c02bc2bb-fba2-41b4-9373-5e7632fb2ac8",
   "metadata": {},
   "outputs": [],
   "source": [
    "#close destination raster\n",
    "dstRst.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "67042eaa-8d7b-4de7-9ba0-4678523c3b76",
   "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
}
